| | | 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 | | |
| | 72 | 14 | | public override ScriptType ScriptType => _hasAnchorOutputs |
| | 72 | 15 | | ? ScriptType.P2WSH |
| | 72 | 16 | | : ScriptType.P2WPKH; |
| | | 17 | | |
| | 0 | 18 | | public PubKey RemotePubKey { get; } |
| | | 19 | | |
| | | 20 | | public ToRemoteOutput(LightningMoney amount, bool hasAnchorOutputs, PubKey remotePubKey) |
| | 72 | 21 | | : base(amount, GenerateToRemoteScript(hasAnchorOutputs, remotePubKey)) |
| | | 22 | | { |
| | 72 | 23 | | ArgumentNullException.ThrowIfNull(remotePubKey); |
| | | 24 | | |
| | 72 | 25 | | _hasAnchorOutputs = hasAnchorOutputs; |
| | | 26 | | |
| | 72 | 27 | | RemotePubKey = remotePubKey; |
| | 72 | 28 | | } |
| | | 29 | | |
| | | 30 | | private static Script GenerateToRemoteScript(bool hasAnchorOutputs, PubKey remotePubKey) |
| | | 31 | | { |
| | 72 | 32 | | ArgumentNullException.ThrowIfNull(remotePubKey); |
| | | 33 | | |
| | 72 | 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 | | */ |
| | 4 | 46 | | return new Script( |
| | 4 | 47 | | Op.GetPushOp(remotePubKey.ToBytes()), |
| | 4 | 48 | | OpcodeType.OP_CHECKSIGVERIFY, |
| | 4 | 49 | | OpcodeType.OP_1, |
| | 4 | 50 | | OpcodeType.OP_CHECKSEQUENCEVERIFY |
| | 4 | 51 | | ); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // If we don't require anchor outputs, we'll return a P2WPKH redeemScript |
| | 68 | 55 | | return remotePubKey.WitHash.ScriptPubKey; |
| | | 56 | | } |
| | | 57 | | } |