| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Bitcoin.Outputs; |
| | 4 | |
|
| | 5 | | using Domain.Money; |
| | 6 | |
|
| | 7 | | /// <summary> |
| | 8 | | /// Represents a to_remote output in a commitment transaction. |
| | 9 | | /// </summary> |
| | 10 | | public class ToRemoteOutput : BaseOutput |
| | 11 | | { |
| | 12 | | private readonly bool _hasAnchorOutputs; |
| | 13 | |
|
| 184 | 14 | | public override ScriptType ScriptType => _hasAnchorOutputs |
| 184 | 15 | | ? ScriptType.P2WSH |
| 184 | 16 | | : ScriptType.P2WPKH; |
| | 17 | |
|
| 4 | 18 | | public PubKey RemotePubKey { get; } |
| | 19 | |
|
| | 20 | | public ToRemoteOutput(bool hasAnchorOutputs, PubKey remotePubKey, LightningMoney amount) |
| 176 | 21 | | : base(GenerateToRemoteScript(hasAnchorOutputs, remotePubKey), amount) |
| | 22 | | { |
| 176 | 23 | | ArgumentNullException.ThrowIfNull(remotePubKey); |
| | 24 | |
|
| 176 | 25 | | _hasAnchorOutputs = hasAnchorOutputs; |
| | 26 | |
|
| 176 | 27 | | RemotePubKey = remotePubKey; |
| 176 | 28 | | } |
| | 29 | |
|
| | 30 | | private static Script GenerateToRemoteScript(bool hasAnchorOutputs, PubKey remotePubKey) |
| | 31 | | { |
| 176 | 32 | | ArgumentNullException.ThrowIfNull(remotePubKey); |
| | 33 | |
|
| 176 | 34 | | if (hasAnchorOutputs) |
| | 35 | | { |
| | 36 | | /* The following script can be read as: |
| | 37 | | ** spendingPubKey = the pubkey trying to sign this spend |
| | 38 | | ** nSequence = Provided by the spending transaction |
| | 39 | | ** |
| | 40 | | ** if (signature is valid for spendingPubKey && nSequence >= 1) { |
| | 41 | | ** return true |
| | 42 | | ** } else { |
| | 43 | | ** return false |
| | 44 | | ** } |
| | 45 | | */ |
| 84 | 46 | | return new Script( |
| 84 | 47 | | Op.GetPushOp(remotePubKey.ToBytes()), |
| 84 | 48 | | OpcodeType.OP_CHECKSIGVERIFY, |
| 84 | 49 | | OpcodeType.OP_1, |
| 84 | 50 | | OpcodeType.OP_CHECKSEQUENCEVERIFY |
| 84 | 51 | | ); |
| | 52 | | } |
| | 53 | |
|
| | 54 | | // If we don't require anchor outputs, we'll return a P2WPKH redeemScript |
| 92 | 55 | | return remotePubKey.WitHash.ScriptPubKey; |
| | 56 | | } |
| | 57 | | } |