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