@@ -11,9 +11,12 @@ import (
11
11
"github.com/lightningnetwork/lnd/lntypes"
12
12
)
13
13
14
- // GenSuccessPathScript constructs an HtlcScript for the success payment path.
14
+ // GenSuccessPathScript constructs a script for the success path of the HTLC
15
+ // payment. Optionally includes a CHECKSEQUENCEVERIFY (CSV) of 1 if `csv` is
16
+ // true, to prevent potential pinning attacks when the HTLC is not part of a
17
+ // package relay.
15
18
func GenSuccessPathScript (receiverHtlcKey * btcec.PublicKey ,
16
- swapHash lntypes.Hash ) ([]byte , error ) {
19
+ swapHash lntypes.Hash , csvOne bool ) ([]byte , error ) {
17
20
18
21
builder := txscript .NewScriptBuilder ()
19
22
@@ -24,9 +27,22 @@ func GenSuccessPathScript(receiverHtlcKey *btcec.PublicKey,
24
27
builder .AddOp (txscript .OP_EQUALVERIFY )
25
28
builder .AddOp (txscript .OP_HASH160 )
26
29
builder .AddData (input .Ripemd160H (swapHash [:]))
27
- builder .AddOp (txscript .OP_EQUALVERIFY )
28
- builder .AddInt64 (1 )
29
- builder .AddOp (txscript .OP_CHECKSEQUENCEVERIFY )
30
+ // OP_EQUAL will leave 0 or 1 on the stack depending on whether the hash
31
+ // matches.
32
+ // - If it matches and CSV is not used, the script will
33
+ // evaulate to true.
34
+ // - If it matches and CSV is used, we'll have 1 on the stack which is
35
+ // used to verify the CSV condition.
36
+ // - If it does not match, we'll have 0 on the stack which will cause
37
+ // the script to fail even if CSV is used.
38
+ builder .AddOp (txscript .OP_EQUAL )
39
+
40
+ if csvOne {
41
+ // If csvOne is true, we add a CHECKSEQUENCEVERIFY to ensure
42
+ // that the HTLC can only be claimed after at least one
43
+ // confirmation.
44
+ builder .AddOp (txscript .OP_CHECKSEQUENCEVERIFY )
45
+ }
30
46
31
47
return builder .Script ()
32
48
}
@@ -61,7 +77,9 @@ func CreateOpTrueLeaf() (asset.ScriptKey, txscript.TapLeaf,
61
77
tapLeaf := txscript .NewBaseTapLeaf (tapScript )
62
78
tree := txscript .AssembleTaprootScriptTree (tapLeaf )
63
79
rootHash := tree .RootNode .TapHash ()
64
- tapKey := txscript .ComputeTaprootOutputKey (asset .NUMSPubKey , rootHash [:])
80
+ tapKey := txscript .ComputeTaprootOutputKey (
81
+ asset .NUMSPubKey , rootHash [:],
82
+ )
65
83
66
84
merkleRootHash := tree .RootNode .TapHash ()
67
85
0 commit comments