|
| 1 | +package htlc |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/btcsuite/btcd/btcec/v2" |
| 5 | + "github.com/btcsuite/btcd/btcec/v2/schnorr" |
| 6 | + "github.com/btcsuite/btcd/txscript" |
| 7 | + "github.com/decred/dcrd/dcrec/secp256k1/v4" |
| 8 | + "github.com/lightninglabs/taproot-assets/asset" |
| 9 | + "github.com/lightningnetwork/lnd/input" |
| 10 | + "github.com/lightningnetwork/lnd/keychain" |
| 11 | + "github.com/lightningnetwork/lnd/lntypes" |
| 12 | +) |
| 13 | + |
| 14 | +// GenSuccessPathScript constructs an HtlcScript for the success payment path. |
| 15 | +func GenSuccessPathScript(receiverHtlcKey *btcec.PublicKey, |
| 16 | + swapHash lntypes.Hash) ([]byte, error) { |
| 17 | + |
| 18 | + builder := txscript.NewScriptBuilder() |
| 19 | + |
| 20 | + builder.AddData(schnorr.SerializePubKey(receiverHtlcKey)) |
| 21 | + builder.AddOp(txscript.OP_CHECKSIGVERIFY) |
| 22 | + builder.AddOp(txscript.OP_SIZE) |
| 23 | + builder.AddInt64(32) |
| 24 | + builder.AddOp(txscript.OP_EQUALVERIFY) |
| 25 | + builder.AddOp(txscript.OP_HASH160) |
| 26 | + builder.AddData(input.Ripemd160H(swapHash[:])) |
| 27 | + builder.AddOp(txscript.OP_EQUALVERIFY) |
| 28 | + builder.AddInt64(1) |
| 29 | + builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY) |
| 30 | + |
| 31 | + return builder.Script() |
| 32 | +} |
| 33 | + |
| 34 | +// GenTimeoutPathScript constructs an HtlcScript for the timeout payment path. |
| 35 | +func GenTimeoutPathScript(senderHtlcKey *btcec.PublicKey, csvExpiry int64) ( |
| 36 | + []byte, error) { |
| 37 | + |
| 38 | + builder := txscript.NewScriptBuilder() |
| 39 | + builder.AddData(schnorr.SerializePubKey(senderHtlcKey)) |
| 40 | + builder.AddOp(txscript.OP_CHECKSIGVERIFY) |
| 41 | + builder.AddInt64(csvExpiry) |
| 42 | + builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY) |
| 43 | + return builder.Script() |
| 44 | +} |
| 45 | + |
| 46 | +// GetOpTrueScript returns a script that always evaluates to true. |
| 47 | +func GetOpTrueScript() ([]byte, error) { |
| 48 | + return txscript.NewScriptBuilder().AddOp(txscript.OP_TRUE).Script() |
| 49 | +} |
| 50 | + |
| 51 | +// CreateOpTrueLeaf creates a taproot leaf that always evaluates to true. |
| 52 | +func CreateOpTrueLeaf() (asset.ScriptKey, txscript.TapLeaf, |
| 53 | + *txscript.IndexedTapScriptTree, *txscript.ControlBlock, error) { |
| 54 | + |
| 55 | + // Create the taproot OP_TRUE script. |
| 56 | + tapScript, err := GetOpTrueScript() |
| 57 | + if err != nil { |
| 58 | + return asset.ScriptKey{}, txscript.TapLeaf{}, nil, nil, err |
| 59 | + } |
| 60 | + |
| 61 | + tapLeaf := txscript.NewBaseTapLeaf(tapScript) |
| 62 | + tree := txscript.AssembleTaprootScriptTree(tapLeaf) |
| 63 | + rootHash := tree.RootNode.TapHash() |
| 64 | + tapKey := txscript.ComputeTaprootOutputKey(asset.NUMSPubKey, rootHash[:]) |
| 65 | + |
| 66 | + merkleRootHash := tree.RootNode.TapHash() |
| 67 | + |
| 68 | + controlBlock := &txscript.ControlBlock{ |
| 69 | + LeafVersion: txscript.BaseLeafVersion, |
| 70 | + InternalKey: asset.NUMSPubKey, |
| 71 | + } |
| 72 | + tapScriptKey := asset.ScriptKey{ |
| 73 | + PubKey: tapKey, |
| 74 | + TweakedScriptKey: &asset.TweakedScriptKey{ |
| 75 | + RawKey: keychain.KeyDescriptor{ |
| 76 | + PubKey: asset.NUMSPubKey, |
| 77 | + }, |
| 78 | + Tweak: merkleRootHash[:], |
| 79 | + }, |
| 80 | + } |
| 81 | + if tapKey.SerializeCompressed()[0] == |
| 82 | + secp256k1.PubKeyFormatCompressedOdd { |
| 83 | + |
| 84 | + controlBlock.OutputKeyYIsOdd = true |
| 85 | + } |
| 86 | + |
| 87 | + return tapScriptKey, tapLeaf, tree, controlBlock, nil |
| 88 | +} |
0 commit comments