| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Bitcoin.Outputs; |
| | 4 | |
|
| | 5 | | using Domain.Money; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Represents a to_local_anchor/to_remote_anchor output in a commitment transaction. |
| | 9 | | /// </summary> |
| | 10 | | public class ToAnchorOutput : BaseOutput |
| | 11 | | { |
| 104 | 12 | | public override ScriptType ScriptType => ScriptType.P2WSH; |
| | 13 | |
|
| 104 | 14 | | public PubKey RemoteFundingPubKey { get; set; } |
| | 15 | |
|
| | 16 | | public ToAnchorOutput(PubKey remoteFundingPubKey, LightningMoney amount) |
| 104 | 17 | | : base(GenerateAnchorScript(remoteFundingPubKey), amount) |
| | 18 | | { |
| 104 | 19 | | RemoteFundingPubKey = remoteFundingPubKey; |
| 104 | 20 | | } |
| | 21 | |
|
| | 22 | | /// <summary> |
| | 23 | | /// Creates a ToAnchorOutput object from a NBitcoin.TxOut. |
| | 24 | | /// </summary> |
| | 25 | | /// <param name="txOut">The TxOut object.</param> |
| | 26 | | /// <param name="remoteFundingPubKey">The remote funding public key.</param> |
| | 27 | | /// <returns>A ToAnchorOutput object.</returns> |
| | 28 | | public static ToAnchorOutput FromTxOut(TxOut txOut, PubKey remoteFundingPubKey) |
| | 29 | | { |
| 0 | 30 | | return new ToAnchorOutput(remoteFundingPubKey, (ulong)txOut.Value.Satoshi); |
| | 31 | | } |
| | 32 | |
|
| | 33 | | private static Script GenerateAnchorScript(PubKey pubKey) |
| | 34 | | { |
| | 35 | | /* The following script can be read as: |
| | 36 | | ** spendingPubKey = the pubkey trying to sign this spend |
| | 37 | | ** signature = the signature given by spendingPubKey |
| | 38 | | ** nSequence = Provided by the spending transaction |
| | 39 | | ** |
| | 40 | | ** if (signature is valid for local_funding_pubkey/remote_funding_pubkey) |
| | 41 | | ** { |
| | 42 | | ** return true |
| | 43 | | ** } |
| | 44 | | ** else |
| | 45 | | ** { |
| | 46 | | ** if (currentTransactionInputSequence < 16) |
| | 47 | | ** { |
| | 48 | | ** exit |
| | 49 | | ** } |
| | 50 | | ** } |
| | 51 | | ** return true |
| | 52 | | */ |
| | 53 | |
|
| 104 | 54 | | return new Script( |
| 104 | 55 | | Op.GetPushOp(pubKey.ToBytes()), |
| 104 | 56 | | OpcodeType.OP_CHECKSIG, |
| 104 | 57 | | OpcodeType.OP_IFDUP, |
| 104 | 58 | | OpcodeType.OP_NOTIF, |
| 104 | 59 | | OpcodeType.OP_16, |
| 104 | 60 | | OpcodeType.OP_CHECKSEQUENCEVERIFY, |
| 104 | 61 | | OpcodeType.OP_ENDIF |
| 104 | 62 | | ); |
| | 63 | | } |
| | 64 | | } |