| | | 1 | | using NBitcoin; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Bitcoin.Outputs; |
| | | 4 | | |
| | | 5 | | using Domain.Money; |
| | | 6 | | using Exceptions; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Represents a to_local output in a commitment transaction. |
| | | 10 | | /// </summary> |
| | | 11 | | public class ToLocalOutput : BaseOutput |
| | | 12 | | { |
| | 108 | 13 | | public override ScriptType ScriptType => ScriptType.P2WSH; |
| | | 14 | | |
| | 4 | 15 | | public PubKey LocalDelayedPubKey { get; } |
| | 4 | 16 | | public PubKey RevocationPubKey { get; } |
| | 4 | 17 | | public uint ToSelfDelay { get; } |
| | | 18 | | |
| | | 19 | | public ToLocalOutput(LightningMoney amount, PubKey localDelayedPubKey, PubKey revocationPubKey, uint toSelfDelay) |
| | 104 | 20 | | : base(amount, GenerateToLocalScript(localDelayedPubKey, revocationPubKey, toSelfDelay)) |
| | | 21 | | { |
| | 104 | 22 | | ArgumentNullException.ThrowIfNull(localDelayedPubKey); |
| | 104 | 23 | | ArgumentNullException.ThrowIfNull(revocationPubKey); |
| | | 24 | | |
| | 104 | 25 | | LocalDelayedPubKey = localDelayedPubKey; |
| | 104 | 26 | | RevocationPubKey = revocationPubKey; |
| | 104 | 27 | | ToSelfDelay = toSelfDelay; |
| | 104 | 28 | | } |
| | | 29 | | |
| | | 30 | | private static Script GenerateToLocalScript(PubKey localDelayedPubKey, PubKey revocationPubKey, uint toSelfDelay) |
| | | 31 | | { |
| | | 32 | | /* The following script can be read as: |
| | | 33 | | ** spendingPubKey = the pubkey trying to sign this spend |
| | | 34 | | ** signature = the signature given by spendingPubKey |
| | | 35 | | ** nSequence = Provided by the spending transaction |
| | | 36 | | ** |
| | | 37 | | ** if (revocationPubKey = spendingPubKey) |
| | | 38 | | ** { // Revocation key path |
| | | 39 | | ** return revocationPubKey |
| | | 40 | | ** } |
| | | 41 | | ** else |
| | | 42 | | ** { // Delayed key path |
| | | 43 | | ** if (nSequence < toSelfDelay) |
| | | 44 | | ** { |
| | | 45 | | ** exit |
| | | 46 | | ** } |
| | | 47 | | ** else |
| | | 48 | | ** { |
| | | 49 | | ** return localDelayedPubKey |
| | | 50 | | ** } |
| | | 51 | | ** } |
| | | 52 | | ** if (signature is valid for spendingPubKey) |
| | | 53 | | ** { |
| | | 54 | | ** return true |
| | | 55 | | ** } |
| | | 56 | | */ |
| | | 57 | | |
| | 104 | 58 | | ArgumentNullException.ThrowIfNull(localDelayedPubKey); |
| | 104 | 59 | | ArgumentNullException.ThrowIfNull(revocationPubKey); |
| | | 60 | | |
| | 104 | 61 | | var script = new Script( |
| | 104 | 62 | | OpcodeType.OP_IF, |
| | 104 | 63 | | Op.GetPushOp(revocationPubKey.ToBytes()), |
| | 104 | 64 | | OpcodeType.OP_ELSE, |
| | 104 | 65 | | Op.GetPushOp(toSelfDelay), |
| | 104 | 66 | | OpcodeType.OP_CHECKSEQUENCEVERIFY, |
| | 104 | 67 | | OpcodeType.OP_DROP, |
| | 104 | 68 | | Op.GetPushOp(localDelayedPubKey.ToBytes()), |
| | 104 | 69 | | OpcodeType.OP_ENDIF, |
| | 104 | 70 | | OpcodeType.OP_CHECKSIG |
| | 104 | 71 | | ); |
| | | 72 | | |
| | | 73 | | // Check if the script is correct |
| | 104 | 74 | | if (script.IsUnspendable || !script.IsValid) |
| | | 75 | | { |
| | 0 | 76 | | throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'."); |
| | | 77 | | } |
| | | 78 | | |
| | 104 | 79 | | return script; |
| | | 80 | | } |
| | | 81 | | } |