Skip to content

Commit 9fdd531

Browse files
committed
swap: add HTLC script version
1 parent e9f71e9 commit 9fdd531

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed

client.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,9 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
175175

176176
for _, swp := range loopOutSwaps {
177177
htlc, err := swap.NewHtlc(
178-
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
179-
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
180-
s.lndServices.ChainParams,
178+
swap.HtlcV1, swp.Contract.CltvExpiry,
179+
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
180+
swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
181181
)
182182
if err != nil {
183183
return nil, err
@@ -195,18 +195,18 @@ func (s *Client) FetchSwaps() ([]*SwapInfo, error) {
195195

196196
for _, swp := range loopInSwaps {
197197
htlcNP2WSH, err := swap.NewHtlc(
198-
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
199-
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcNP2WSH,
200-
s.lndServices.ChainParams,
198+
swap.HtlcV1, swp.Contract.CltvExpiry,
199+
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
200+
swp.Hash, swap.HtlcNP2WSH, s.lndServices.ChainParams,
201201
)
202202
if err != nil {
203203
return nil, err
204204
}
205205

206206
htlcP2WSH, err := swap.NewHtlc(
207-
swp.Contract.CltvExpiry, swp.Contract.SenderKey,
208-
swp.Contract.ReceiverKey, swp.Hash, swap.HtlcP2WSH,
209-
s.lndServices.ChainParams,
207+
swap.HtlcV1, swp.Contract.CltvExpiry,
208+
swp.Contract.SenderKey, swp.Contract.ReceiverKey,
209+
swp.Hash, swap.HtlcP2WSH, s.lndServices.ChainParams,
210210
)
211211
if err != nil {
212212
return nil, err

loopd/view.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
5050

5151
for _, s := range swaps {
5252
htlc, err := swap.NewHtlc(
53+
swap.HtlcV1,
5354
s.Contract.CltvExpiry,
5455
s.Contract.SenderKey,
5556
s.Contract.ReceiverKey,
@@ -101,6 +102,7 @@ func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
101102

102103
for _, s := range swaps {
103104
htlc, err := swap.NewHtlc(
105+
swap.HtlcV1,
104106
s.Contract.CltvExpiry,
105107
s.Contract.SenderKey,
106108
s.Contract.ReceiverKey,

loopin_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,9 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool) {
331331
}
332332

333333
htlc, err := swap.NewHtlc(
334-
contract.CltvExpiry, contract.SenderKey, contract.ReceiverKey,
335-
testPreimage.Hash(), swap.HtlcNP2WSH, cfg.lnd.ChainParams,
334+
swap.HtlcV1, contract.CltvExpiry, contract.SenderKey,
335+
contract.ReceiverKey, testPreimage.Hash(), swap.HtlcNP2WSH,
336+
cfg.lnd.ChainParams,
336337
)
337338
if err != nil {
338339
t.Fatal(err)

swap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
5151
// getHtlc composes and returns the on-chain swap script.
5252
func (s *swapKit) getHtlc(outputType swap.HtlcOutputType) (*swap.Htlc, error) {
5353
return swap.NewHtlc(
54-
s.contract.CltvExpiry, s.contract.SenderKey,
54+
swap.HtlcV1, s.contract.CltvExpiry, s.contract.SenderKey,
5555
s.contract.ReceiverKey, s.hash, outputType,
5656
s.swapConfig.lnd.ChainParams,
5757
)

swap/htlc.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"crypto/sha256"
66
"errors"
7+
"fmt"
78

89
"github.com/btcsuite/btcd/chaincfg"
910
"github.com/btcsuite/btcd/txscript"
@@ -25,8 +26,17 @@ const (
2526
HtlcNP2WSH
2627
)
2728

29+
// ScriptVersion defines the HTLC script version.
30+
type ScriptVersion uint8
31+
32+
const (
33+
// HtlcV1 refers to the original version of the HTLC script.
34+
HtlcV1 ScriptVersion = iota
35+
)
36+
2837
// Htlc contains relevant htlc information from the receiver perspective.
2938
type Htlc struct {
39+
Version ScriptVersion
3040
Script []byte
3141
PkScript []byte
3242
Hash lntypes.Hash
@@ -45,6 +55,7 @@ var (
4555
// the maximum value for cltv expiry to get the maximum (worst case)
4656
// script size.
4757
QuoteHtlc, _ = NewHtlc(
58+
HtlcV1,
4859
^int32(0), quoteKey, quoteKey, quoteHash, HtlcP2WSH,
4960
&chaincfg.MainNetParams,
5061
)
@@ -65,13 +76,26 @@ func (h HtlcOutputType) String() string {
6576
}
6677

6778
// NewHtlc returns a new instance.
68-
func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
79+
func NewHtlc(version ScriptVersion, cltvExpiry int32,
80+
senderKey, receiverKey [33]byte,
6981
hash lntypes.Hash, outputType HtlcOutputType,
7082
chainParams *chaincfg.Params) (*Htlc, error) {
7183

72-
script, err := swapHTLCScript(
73-
cltvExpiry, senderKey, receiverKey, hash,
84+
var (
85+
err error
86+
script []byte
7487
)
88+
89+
switch version {
90+
case HtlcV1:
91+
script, err = swapHTLCScriptV1(
92+
cltvExpiry, senderKey, receiverKey, hash,
93+
)
94+
95+
default:
96+
return nil, fmt.Errorf("unknown script version: %v", version)
97+
}
98+
7599
if err != nil {
76100
return nil, err
77101
}
@@ -135,6 +159,7 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
135159

136160
return &Htlc{
137161
Hash: hash,
162+
Version: version,
138163
Script: script,
139164
PkScript: pkScript,
140165
OutputType: outputType,
@@ -144,19 +169,19 @@ func NewHtlc(cltvExpiry int32, senderKey, receiverKey [33]byte,
144169
}, nil
145170
}
146171

147-
// SwapHTLCScript returns the on-chain HTLC witness script.
172+
// SwapHTLCScriptV1 returns the on-chain HTLC witness script.
148173
//
149174
// OP_SIZE 32 OP_EQUAL
150175
// OP_IF
151-
// OP_HASH160 <ripemd160(swap_hash)> OP_EQUALVERIFY
152-
// <recvr key>
176+
// OP_HASH160 <ripemd160(swapHash)> OP_EQUALVERIFY
177+
// <receiverHtlcKey>
153178
// OP_ELSE
154179
// OP_DROP
155180
// <cltv timeout> OP_CHECKLOCKTIMEVERIFY OP_DROP
156-
// <sender key>
181+
// <senderHtlcKey>
157182
// OP_ENDIF
158183
// OP_CHECKSIG
159-
func swapHTLCScript(cltvExpiry int32, senderHtlcKey,
184+
func swapHTLCScriptV1(cltvExpiry int32, senderHtlcKey,
160185
receiverHtlcKey [33]byte, swapHash lntypes.Hash) ([]byte, error) {
161186

162187
builder := txscript.NewScriptBuilder()

0 commit comments

Comments
 (0)