Skip to content

Commit b43fa11

Browse files
committed
utils: add htlc & swap related helpers
1 parent a9be69b commit b43fa11

File tree

6 files changed

+97
-83
lines changed

6 files changed

+97
-83
lines changed

client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightninglabs/loop/loopdb"
1818
"github.com/lightninglabs/loop/swap"
1919
"github.com/lightninglabs/loop/sweep"
20+
"github.com/lightninglabs/loop/utils"
2021
"github.com/lightningnetwork/lnd/lntypes"
2122
"github.com/lightningnetwork/lnd/routing/route"
2223
"google.golang.org/grpc"
@@ -232,7 +233,7 @@ func (s *Client) FetchSwaps(ctx context.Context) ([]*SwapInfo, error) {
232233
LastUpdate: swp.LastUpdateTime(),
233234
}
234235

235-
htlc, err := GetHtlc(
236+
htlc, err := utils.GetHtlc(
236237
swp.Hash, &swp.Contract.SwapContract,
237238
s.lndServices.ChainParams,
238239
)
@@ -265,7 +266,7 @@ func (s *Client) FetchSwaps(ctx context.Context) ([]*SwapInfo, error) {
265266
LastUpdate: swp.LastUpdateTime(),
266267
}
267268

268-
htlc, err := GetHtlc(
269+
htlc, err := utils.GetHtlc(
269270
swp.Hash, &swp.Contract.SwapContract,
270271
s.lndServices.ChainParams,
271272
)
@@ -540,7 +541,7 @@ func (s *Client) getLoopOutSweepFee(ctx context.Context, confTarget int32) (
540541
return 0, err
541542
}
542543

543-
scriptVersion := GetHtlcScriptVersion(
544+
scriptVersion := utils.GetHtlcScriptVersion(
544545
loopdb.CurrentProtocolVersion(),
545546
)
546547

@@ -731,7 +732,7 @@ func (s *Client) estimateFee(ctx context.Context, amt btcutil.Amount,
731732
// Generate a dummy address for fee estimation.
732733
witnessProg := [32]byte{}
733734

734-
scriptVersion := GetHtlcScriptVersion(
735+
scriptVersion := utils.GetHtlcScriptVersion(
735736
loopdb.CurrentProtocolVersion(),
736737
)
737738

loopd/view.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/lightninglabs/lndclient"
99
"github.com/lightninglabs/loop"
1010
"github.com/lightninglabs/loop/loopdb"
11+
"github.com/lightninglabs/loop/utils"
1112
)
1213

1314
// view prints all swaps currently in the database.
@@ -56,7 +57,7 @@ func viewOut(swapClient *loop.Client, chainParams *chaincfg.Params) error {
5657
for _, s := range swaps {
5758
s := s
5859

59-
htlc, err := loop.GetHtlc(
60+
htlc, err := utils.GetHtlc(
6061
s.Hash, &s.Contract.SwapContract, chainParams,
6162
)
6263
if err != nil {
@@ -107,7 +108,7 @@ func viewIn(swapClient *loop.Client, chainParams *chaincfg.Params) error {
107108
for _, s := range swaps {
108109
s := s
109110

110-
htlc, err := loop.GetHtlc(
111+
htlc, err := utils.GetHtlc(
111112
s.Hash, &s.Contract.SwapContract, chainParams,
112113
)
113114
if err != nil {

loopin.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/lightninglabs/loop/labels"
1919
"github.com/lightninglabs/loop/loopdb"
2020
"github.com/lightninglabs/loop/swap"
21+
"github.com/lightninglabs/loop/utils"
2122
"github.com/lightningnetwork/lnd/chainntnfs"
2223
invpkg "github.com/lightningnetwork/lnd/invoices"
2324
"github.com/lightningnetwork/lnd/keychain"
@@ -442,7 +443,7 @@ func validateLoopInContract(height int32, response *newLoopInResponse) error {
442443
// initHtlcs creates and updates the native and nested segwit htlcs of the
443444
// loopInSwap.
444445
func (s *loopInSwap) initHtlcs() error {
445-
htlc, err := GetHtlc(
446+
htlc, err := utils.GetHtlc(
446447
s.hash, &s.SwapContract, s.swapKit.lnd.ChainParams,
447448
)
448449
if err != nil {

loopout.go

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ import (
2222
"github.com/lightninglabs/loop/loopdb"
2323
"github.com/lightninglabs/loop/swap"
2424
"github.com/lightninglabs/loop/sweep"
25+
"github.com/lightninglabs/loop/utils"
2526
"github.com/lightningnetwork/lnd/chainntnfs"
2627
"github.com/lightningnetwork/lnd/channeldb"
2728
"github.com/lightningnetwork/lnd/input"
2829
"github.com/lightningnetwork/lnd/lnrpc"
2930
"github.com/lightningnetwork/lnd/lntypes"
30-
"github.com/lightningnetwork/lnd/zpay32"
3131
)
3232

3333
const (
@@ -207,7 +207,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
207207
swapKit.lastUpdateTime = initiationTime
208208

209209
// Create the htlc.
210-
htlc, err := GetHtlc(
210+
htlc, err := utils.GetHtlc(
211211
swapKit.hash, swapKit.contract, swapKit.lnd.ChainParams,
212212
)
213213
if err != nil {
@@ -220,7 +220,9 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
220220

221221
// Obtain the payment addr since we'll need it later for routing plugin
222222
// recommendation and possibly for cancel.
223-
paymentAddr, err := obtainSwapPaymentAddr(contract.SwapInvoice, cfg)
223+
paymentAddr, err := utils.ObtainSwapPaymentAddr(
224+
contract.SwapInvoice, cfg.lnd.ChainParams,
225+
)
224226
if err != nil {
225227
return nil, err
226228
}
@@ -263,7 +265,7 @@ func resumeLoopOutSwap(cfg *swapConfig, pend *loopdb.LoopOut,
263265
)
264266

265267
// Create the htlc.
266-
htlc, err := GetHtlc(
268+
htlc, err := utils.GetHtlc(
267269
swapKit.hash, swapKit.contract, swapKit.lnd.ChainParams,
268270
)
269271
if err != nil {
@@ -275,8 +277,8 @@ func resumeLoopOutSwap(cfg *swapConfig, pend *loopdb.LoopOut,
275277

276278
// Obtain the payment addr since we'll need it later for routing plugin
277279
// recommendation and possibly for cancel.
278-
paymentAddr, err := obtainSwapPaymentAddr(
279-
pend.Contract.SwapInvoice, cfg,
280+
paymentAddr, err := utils.ObtainSwapPaymentAddr(
281+
pend.Contract.SwapInvoice, cfg.lnd.ChainParams,
280282
)
281283
if err != nil {
282284
return nil, err
@@ -302,24 +304,6 @@ func resumeLoopOutSwap(cfg *swapConfig, pend *loopdb.LoopOut,
302304
return swap, nil
303305
}
304306

305-
// obtainSwapPaymentAddr will retrieve the payment addr from the passed invoice.
306-
func obtainSwapPaymentAddr(swapInvoice string, cfg *swapConfig) (
307-
*[32]byte, error) {
308-
309-
swapPayReq, err := zpay32.Decode(
310-
swapInvoice, cfg.lnd.ChainParams,
311-
)
312-
if err != nil {
313-
return nil, err
314-
}
315-
316-
if swapPayReq.PaymentAddr == nil {
317-
return nil, fmt.Errorf("expected payment address for invoice")
318-
}
319-
320-
return swapPayReq.PaymentAddr, nil
321-
}
322-
323307
// sendUpdate reports an update to the swap state.
324308
func (s *loopOutSwap) sendUpdate(ctx context.Context) error {
325309
info := s.swapInfo()

swap.go

Lines changed: 2 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ import (
44
"context"
55
"time"
66

7-
"github.com/btcsuite/btcd/chaincfg"
87
"github.com/lightninglabs/lndclient"
98
"github.com/lightninglabs/loop/loopdb"
109
"github.com/lightninglabs/loop/swap"
11-
"github.com/lightningnetwork/lnd/input"
10+
"github.com/lightninglabs/loop/utils"
1211
"github.com/lightningnetwork/lnd/lntypes"
1312
)
1413

@@ -50,59 +49,10 @@ func newSwapKit(hash lntypes.Hash, swapType swap.Type, cfg *swapConfig,
5049
}
5150
}
5251

53-
// GetHtlcScriptVersion returns the correct HTLC script version for the passed
54-
// protocol version.
55-
func GetHtlcScriptVersion(
56-
protocolVersion loopdb.ProtocolVersion) swap.ScriptVersion {
57-
58-
// If the swap was initiated before we had our v3 script, use v2.
59-
if protocolVersion < loopdb.ProtocolVersionHtlcV3 ||
60-
protocolVersion == loopdb.ProtocolVersionUnrecorded {
61-
62-
return swap.HtlcV2
63-
}
64-
65-
return swap.HtlcV3
66-
}
67-
6852
// IsTaproot returns true if the swap referenced by the passed swap contract
6953
// uses the v3 (taproot) htlc.
7054
func IsTaprootSwap(swapContract *loopdb.SwapContract) bool {
71-
return GetHtlcScriptVersion(swapContract.ProtocolVersion) == swap.HtlcV3
72-
}
73-
74-
// GetHtlc composes and returns the on-chain swap script.
75-
func GetHtlc(hash lntypes.Hash, contract *loopdb.SwapContract,
76-
chainParams *chaincfg.Params) (*swap.Htlc, error) {
77-
78-
switch GetHtlcScriptVersion(contract.ProtocolVersion) {
79-
case swap.HtlcV2:
80-
return swap.NewHtlcV2(
81-
contract.CltvExpiry, contract.HtlcKeys.SenderScriptKey,
82-
contract.HtlcKeys.ReceiverScriptKey, hash,
83-
chainParams,
84-
)
85-
86-
case swap.HtlcV3:
87-
// Swaps that implement the new MuSig2 protocol will be expected
88-
// to use the 1.0RC2 MuSig2 key derivation scheme.
89-
muSig2Version := input.MuSig2Version040
90-
if contract.ProtocolVersion >= loopdb.ProtocolVersionMuSig2 {
91-
muSig2Version = input.MuSig2Version100RC2
92-
}
93-
94-
return swap.NewHtlcV3(
95-
muSig2Version,
96-
contract.CltvExpiry,
97-
contract.HtlcKeys.SenderInternalPubKey,
98-
contract.HtlcKeys.ReceiverInternalPubKey,
99-
contract.HtlcKeys.SenderScriptKey,
100-
contract.HtlcKeys.ReceiverScriptKey,
101-
hash, chainParams,
102-
)
103-
}
104-
105-
return nil, swap.ErrInvalidScriptVersion
55+
return utils.GetHtlcScriptVersion(swapContract.ProtocolVersion) == swap.HtlcV3
10656
}
10757

10858
// swapInfo constructs and returns a filled SwapInfo from

utils/htlc_utils.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package utils
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/btcsuite/btcd/chaincfg"
7+
"github.com/lightninglabs/loop/loopdb"
8+
"github.com/lightninglabs/loop/swap"
9+
"github.com/lightningnetwork/lnd/input"
10+
"github.com/lightningnetwork/lnd/lntypes"
11+
"github.com/lightningnetwork/lnd/zpay32"
12+
)
13+
14+
// GetHtlc composes and returns the on-chain swap script.
15+
func GetHtlc(hash lntypes.Hash, contract *loopdb.SwapContract,
16+
chainParams *chaincfg.Params) (*swap.Htlc, error) {
17+
18+
switch GetHtlcScriptVersion(contract.ProtocolVersion) {
19+
case swap.HtlcV2:
20+
return swap.NewHtlcV2(
21+
contract.CltvExpiry, contract.HtlcKeys.SenderScriptKey,
22+
contract.HtlcKeys.ReceiverScriptKey, hash,
23+
chainParams,
24+
)
25+
26+
case swap.HtlcV3:
27+
// Swaps that implement the new MuSig2 protocol will be expected
28+
// to use the 1.0RC2 MuSig2 key derivation scheme.
29+
muSig2Version := input.MuSig2Version040
30+
if contract.ProtocolVersion >= loopdb.ProtocolVersionMuSig2 {
31+
muSig2Version = input.MuSig2Version100RC2
32+
}
33+
34+
return swap.NewHtlcV3(
35+
muSig2Version,
36+
contract.CltvExpiry,
37+
contract.HtlcKeys.SenderInternalPubKey,
38+
contract.HtlcKeys.ReceiverInternalPubKey,
39+
contract.HtlcKeys.SenderScriptKey,
40+
contract.HtlcKeys.ReceiverScriptKey,
41+
hash, chainParams,
42+
)
43+
}
44+
45+
return nil, swap.ErrInvalidScriptVersion
46+
}
47+
48+
// GetHtlcScriptVersion returns the correct HTLC script version for the passed
49+
// protocol version.
50+
func GetHtlcScriptVersion(
51+
protocolVersion loopdb.ProtocolVersion) swap.ScriptVersion {
52+
53+
// If the swap was initiated before we had our v3 script, use v2.
54+
if protocolVersion < loopdb.ProtocolVersionHtlcV3 ||
55+
protocolVersion == loopdb.ProtocolVersionUnrecorded {
56+
57+
return swap.HtlcV2
58+
}
59+
60+
return swap.HtlcV3
61+
}
62+
63+
// ObtainSwapPaymentAddr will retrieve the payment addr from the passed invoice.
64+
func ObtainSwapPaymentAddr(swapInvoice string, chainParams *chaincfg.Params) (
65+
*[32]byte, error) {
66+
67+
swapPayReq, err := zpay32.Decode(swapInvoice, chainParams)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
if swapPayReq.PaymentAddr == nil {
73+
return nil, fmt.Errorf("expected payment address for invoice")
74+
}
75+
76+
return swapPayReq.PaymentAddr, nil
77+
}

0 commit comments

Comments
 (0)