Skip to content

Commit 0e3567b

Browse files
committed
assets: add no-csv option to the asset HTLC to support package relay
This commit enables package relayed HTLCs by making the CSV check in the success path optional.
1 parent 18e850b commit 0e3567b

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

assets/htlc/script.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@ import (
1111
"github.com/lightningnetwork/lnd/lntypes"
1212
)
1313

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.
1518
func GenSuccessPathScript(receiverHtlcKey *btcec.PublicKey,
16-
swapHash lntypes.Hash) ([]byte, error) {
19+
swapHash lntypes.Hash, csv bool) ([]byte, error) {
1720

1821
builder := txscript.NewScriptBuilder()
1922

@@ -25,8 +28,11 @@ func GenSuccessPathScript(receiverHtlcKey *btcec.PublicKey,
2528
builder.AddOp(txscript.OP_HASH160)
2629
builder.AddData(input.Ripemd160H(swapHash[:]))
2730
builder.AddOp(txscript.OP_EQUALVERIFY)
28-
//builder.AddInt64(1)
29-
//builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY)
31+
32+
if csv {
33+
builder.AddInt64(1)
34+
builder.AddOp(txscript.OP_CHECKSEQUENCEVERIFY)
35+
}
3036

3137
return builder.Script()
3238
}
@@ -61,7 +67,9 @@ func CreateOpTrueLeaf() (asset.ScriptKey, txscript.TapLeaf,
6167
tapLeaf := txscript.NewBaseTapLeaf(tapScript)
6268
tree := txscript.AssembleTaprootScriptTree(tapLeaf)
6369
rootHash := tree.RootNode.TapHash()
64-
tapKey := txscript.ComputeTaprootOutputKey(asset.NUMSPubKey, rootHash[:])
70+
tapKey := txscript.ComputeTaprootOutputKey(
71+
asset.NUMSPubKey, rootHash[:],
72+
)
6573

6674
merkleRootHash := tree.RootNode.TapHash()
6775

assets/htlc/swapkit.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,16 @@ type SwapKit struct {
5050
// AddressParams is the chain parameters of the chain the deposit is
5151
// being created on.
5252
AddressParams *address.ChainParams
53+
54+
// CheckCSV indicates whether the success path script should include a
55+
// CHECKSEQUENCEVERIFY check. This is used to prevent potential pinning
56+
// attacks when the HTLC is not part of a package relay.
57+
CheckCSV bool
5358
}
5459

5560
// GetSuccessScript returns the success path script of the swap HTLC.
5661
func (s *SwapKit) GetSuccessScript() ([]byte, error) {
57-
return GenSuccessPathScript(s.ReceiverPubKey, s.SwapHash)
62+
return GenSuccessPathScript(s.ReceiverPubKey, s.SwapHash, s.CheckCSV)
5863
}
5964

6065
// GetTimeoutScript returns the timeout path script of the swap HTLC.
@@ -337,7 +342,9 @@ func (s *SwapKit) CreatePreimageWitness(ctx context.Context,
337342
Value: sweepBtcPacket.Inputs[1].WitnessUtxo.Value,
338343
}
339344

340-
//sweepBtcPacket.UnsignedTx.TxIn[0].Sequence = 1
345+
if s.CheckCSV {
346+
sweepBtcPacket.UnsignedTx.TxIn[0].Sequence = 1
347+
}
341348

342349
successScript, err := s.GetSuccessScript()
343350
if err != nil {

0 commit comments

Comments
 (0)