| | 1 | | using System.Diagnostics.CodeAnalysis; |
| | 2 | | using NBitcoin; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Bitcoin.Outputs; |
| | 5 | |
|
| | 6 | | using Crypto.Hashes; |
| | 7 | | using Domain.Crypto.Constants; |
| | 8 | | using Domain.Money; |
| | 9 | | using Exceptions; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Represents a received HTLC output in a commitment transaction. |
| | 13 | | /// </summary> |
| | 14 | | public class ReceivedHtlcOutput : BaseHtlcOutput |
| | 15 | | { |
| 196 | 16 | | public override ScriptType ScriptType => ScriptType.P2WSH; |
| | 17 | |
|
| | 18 | | [SetsRequiredMembers] |
| | 19 | | public ReceivedHtlcOutput(LightningMoney anchorAmount, PubKey revocationPubKey, PubKey remoteHtlcPubKey, |
| | 20 | | PubKey localHtlcPubKey, ReadOnlyMemory<byte> paymentHash, LightningMoney amount, |
| | 21 | | ulong cltvExpiry) |
| 196 | 22 | | : base(GenerateToLocalHtlcScript(anchorAmount, revocationPubKey, remoteHtlcPubKey, localHtlcPubKey, paymentHash, |
| 196 | 23 | | cltvExpiry), |
| 196 | 24 | | amount) |
| | 25 | | { |
| 196 | 26 | | RevocationPubKey = revocationPubKey; |
| 196 | 27 | | RemoteHtlcPubKey = remoteHtlcPubKey; |
| 196 | 28 | | LocalHtlcPubKey = localHtlcPubKey; |
| 196 | 29 | | PaymentHash = paymentHash; |
| 196 | 30 | | CltvExpiry = cltvExpiry; |
| 196 | 31 | | } |
| | 32 | |
|
| | 33 | | private static Script GenerateToLocalHtlcScript(LightningMoney anchorAmount, PubKey revocationPubKey, |
| | 34 | | PubKey remoteHtlcPubKey, PubKey localHtlcPubKey, |
| | 35 | | ReadOnlyMemory<byte> paymentHash, ulong cltvExpiry) |
| | 36 | | { |
| | 37 | | // Hash the revocationPubKey |
| 196 | 38 | | using var sha256 = new Sha256(); |
| 196 | 39 | | Span<byte> revocationPubKeySha256Hash = stackalloc byte[CryptoConstants.SHA256_HASH_LEN]; |
| 196 | 40 | | sha256.AppendData(revocationPubKey.ToBytes()); |
| 196 | 41 | | sha256.GetHashAndReset(revocationPubKeySha256Hash); |
| 196 | 42 | | var revocationPubKeyHashRipemd160 = Ripemd160.Hash(revocationPubKeySha256Hash); |
| | 43 | |
|
| | 44 | | // Hash the paymentHash |
| 196 | 45 | | var paymentHashRipemd160 = Ripemd160.Hash(paymentHash.Span); |
| | 46 | |
|
| 196 | 47 | | List<Op> ops = [ |
| 196 | 48 | | OpcodeType.OP_DUP, |
| 196 | 49 | | OpcodeType.OP_HASH160, |
| 196 | 50 | | Op.GetPushOp(revocationPubKeyHashRipemd160), |
| 196 | 51 | | OpcodeType.OP_EQUAL, |
| 196 | 52 | | OpcodeType.OP_IF, |
| 196 | 53 | | OpcodeType.OP_CHECKSIG, |
| 196 | 54 | | OpcodeType.OP_ELSE, |
| 196 | 55 | | Op.GetPushOp(remoteHtlcPubKey.ToBytes()), |
| 196 | 56 | | OpcodeType.OP_SWAP, |
| 196 | 57 | | OpcodeType.OP_SIZE, |
| 196 | 58 | | Op.GetPushOp(32), |
| 196 | 59 | | OpcodeType.OP_EQUAL, |
| 196 | 60 | | OpcodeType.OP_IF, |
| 196 | 61 | | OpcodeType.OP_HASH160, |
| 196 | 62 | | Op.GetPushOp(paymentHashRipemd160), |
| 196 | 63 | | OpcodeType.OP_EQUALVERIFY, |
| 196 | 64 | | OpcodeType.OP_2, |
| 196 | 65 | | OpcodeType.OP_SWAP, |
| 196 | 66 | | Op.GetPushOp(localHtlcPubKey.ToBytes()), |
| 196 | 67 | | OpcodeType.OP_2, |
| 196 | 68 | | OpcodeType.OP_CHECKMULTISIG, |
| 196 | 69 | | OpcodeType.OP_ELSE, |
| 196 | 70 | | OpcodeType.OP_DROP, |
| 196 | 71 | | Op.GetPushOp((long)cltvExpiry), |
| 196 | 72 | | OpcodeType.OP_CHECKLOCKTIMEVERIFY, |
| 196 | 73 | | OpcodeType.OP_DROP, |
| 196 | 74 | | OpcodeType.OP_CHECKSIG, |
| 196 | 75 | | OpcodeType.OP_ENDIF |
| 196 | 76 | | ]; |
| | 77 | |
|
| 196 | 78 | | if (!anchorAmount.IsZero) |
| | 79 | | { |
| 60 | 80 | | ops.AddRange([ |
| 60 | 81 | | OpcodeType.OP_1, |
| 60 | 82 | | OpcodeType.OP_CHECKSEQUENCEVERIFY, |
| 60 | 83 | | OpcodeType.OP_DROP |
| 60 | 84 | | ]); |
| | 85 | | } |
| | 86 | |
|
| | 87 | | // Close last IF |
| 196 | 88 | | ops.Add(OpcodeType.OP_ENDIF); |
| | 89 | |
|
| 196 | 90 | | var script = new Script(ops); |
| | 91 | |
|
| | 92 | | // Check if script is correct |
| 196 | 93 | | if (script.IsUnspendable || !script.IsValid) |
| | 94 | | { |
| 0 | 95 | | throw new InvalidScriptException("ScriptPubKey is either 'invalid' or 'unspendable'."); |
| | 96 | | } |
| | 97 | |
|
| 196 | 98 | | return script; |
| 196 | 99 | | } |
| | 100 | | } |