| | 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 | | { |
| 184 | 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(PubKey localDelayedPubKey, PubKey revocationPubKey, uint toSelfDelay, LightningMoney amount) |
| 180 | 20 | | : base(GenerateToLocalScript(localDelayedPubKey, revocationPubKey, toSelfDelay), amount) |
| | 21 | | { |
| 180 | 22 | | ArgumentNullException.ThrowIfNull(localDelayedPubKey); |
| 180 | 23 | | ArgumentNullException.ThrowIfNull(revocationPubKey); |
| | 24 | |
|
| 180 | 25 | | LocalDelayedPubKey = localDelayedPubKey; |
| 180 | 26 | | RevocationPubKey = revocationPubKey; |
| 180 | 27 | | ToSelfDelay = toSelfDelay; |
| 180 | 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 | |
|
| 180 | 58 | | ArgumentNullException.ThrowIfNull(localDelayedPubKey); |
| 180 | 59 | | ArgumentNullException.ThrowIfNull(revocationPubKey); |
| | 60 | |
|
| 180 | 61 | | var script = new Script( |
| 180 | 62 | | OpcodeType.OP_IF, |
| 180 | 63 | | Op.GetPushOp(revocationPubKey.ToBytes()), |
| 180 | 64 | | OpcodeType.OP_ELSE, |
| 180 | 65 | | Op.GetPushOp(toSelfDelay), |
| 180 | 66 | | OpcodeType.OP_CHECKSEQUENCEVERIFY, |
| 180 | 67 | | OpcodeType.OP_DROP, |
| 180 | 68 | | Op.GetPushOp(localDelayedPubKey.ToBytes()), |
| 180 | 69 | | OpcodeType.OP_ENDIF, |
| 180 | 70 | | OpcodeType.OP_CHECKSIG |
| 180 | 71 | | ); |
| | 72 | |
|
| | 73 | | // Check if script is correct |
| 180 | 74 | | if (script.IsUnspendable || !script.IsValid) |
| | 75 | | { |
| 0 | 76 | | throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'."); |
| | 77 | | } |
| | 78 | |
|
| 180 | 79 | | return script; |
| | 80 | | } |
| | 81 | | } |