Skip to content

Commit f561ea9

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 512028b commit f561ea9

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
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: 10 additions & 5 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.
@@ -160,10 +165,8 @@ func (s *SwapKit) CreateHtlcVpkt() (*tappsbt.VPacket, error) {
160165
ScriptKey: asset.NUMSScriptKey,
161166
})
162167
pkt.Outputs = append(pkt.Outputs, &tappsbt.VOutput{
163-
// todo(sputn1ck) assetversion
164-
AssetVersion: asset.Version(1),
168+
AssetVersion: asset.V1,
165169
Amount: uint64(s.Amount),
166-
Interactive: true,
167170
AnchorOutputIndex: 1,
168171
ScriptKey: asset.NewScriptKey(
169172
tapScriptKey.PubKey,
@@ -337,7 +340,9 @@ func (s *SwapKit) CreatePreimageWitness(ctx context.Context,
337340
Value: sweepBtcPacket.Inputs[1].WitnessUtxo.Value,
338341
}
339342

340-
//sweepBtcPacket.UnsignedTx.TxIn[0].Sequence = 1
343+
if s.CheckCSV {
344+
sweepBtcPacket.UnsignedTx.TxIn[0].Sequence = 1
345+
}
341346

342347
successScript, err := s.GetSuccessScript()
343348
if err != nil {

0 commit comments

Comments
 (0)