Skip to content

Commit 7f19c43

Browse files
authored
Merge pull request #547 from bhandras/musig-keyreveal
loop: Loop In MuSig2 support
2 parents dbf6fe0 + cd91f90 commit 7f19c43

16 files changed

+1071
-570
lines changed

client_test.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/lightninglabs/loop/loopdb"
1414
"github.com/lightninglabs/loop/swap"
1515
"github.com/lightninglabs/loop/test"
16-
"github.com/lightningnetwork/lnd/input"
1716
"github.com/lightningnetwork/lnd/lnrpc"
1817
"github.com/lightningnetwork/lnd/lntypes"
1918
"github.com/stretchr/testify/require"
@@ -240,8 +239,12 @@ func testLoopOutResume(t *testing.T, confs uint32, expired, preimageRevealed,
240239
Preimage: preimage,
241240
AmountRequested: amt,
242241
CltvExpiry: 744,
243-
ReceiverKey: receiverKey,
244-
SenderKey: senderKey,
242+
HtlcKeys: loopdb.HtlcKeys{
243+
SenderScriptKey: senderKey,
244+
SenderInternalPubKey: senderKey,
245+
ReceiverScriptKey: receiverKey,
246+
ReceiverInternalPubKey: receiverKey,
247+
},
245248
MaxSwapFee: 60000,
246249
MaxMinerFee: 50000,
247250
ProtocolVersion: protocolVersion,
@@ -273,30 +276,13 @@ func testLoopOutResume(t *testing.T, confs uint32, expired, preimageRevealed,
273276
// Expect client to register for our expected number of confirmations.
274277
confIntent := ctx.AssertRegisterConf(preimageRevealed, int32(confs))
275278

276-
// Assert that the loopout htlc equals to the expected one.
277-
scriptVersion := GetHtlcScriptVersion(protocolVersion)
278-
var htlc *swap.Htlc
279-
280-
switch scriptVersion {
281-
case swap.HtlcV2:
282-
htlc, err = swap.NewHtlcV2(
283-
pendingSwap.Contract.CltvExpiry, senderKey,
284-
receiverKey, hash, &chaincfg.TestNet3Params,
285-
)
286-
287-
case swap.HtlcV3:
288-
htlc, err = swap.NewHtlcV3(
289-
input.MuSig2Version040,
290-
pendingSwap.Contract.CltvExpiry, senderKey,
291-
receiverKey, senderKey, receiverKey, hash,
292-
&chaincfg.TestNet3Params,
293-
)
294-
295-
default:
296-
t.Fatalf(swap.ErrInvalidScriptVersion.Error())
297-
}
298-
279+
htlc, err := GetHtlc(
280+
hash, &pendingSwap.Contract.SwapContract,
281+
&chaincfg.TestNet3Params,
282+
)
299283
require.NoError(t, err)
284+
285+
// Assert that the loopout htlc equals to the expected one.
300286
require.Equal(t, htlc.PkScript, confIntent.PkScript)
301287

302288
signalSwapPaymentResult(nil)
@@ -315,7 +301,7 @@ func testLoopOutResume(t *testing.T, confs uint32, expired, preimageRevealed,
315301
func(r error) {},
316302
func(r error) {},
317303
preimageRevealed,
318-
confIntent, scriptVersion,
304+
confIntent, GetHtlcScriptVersion(protocolVersion),
319305
)
320306
}
321307

loopdb/loop.go

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,31 @@ import (
1010
"github.com/lightningnetwork/lnd/lntypes"
1111
)
1212

13+
// HtlcKeys is a holder of all keys used when constructing the swap HTLC. Since
14+
// it's used for both loop in and loop out swaps it may hold partial information
15+
// about the sender or receiver depending on the swap type.
16+
type HtlcKeys struct {
17+
// SenderScriptKey is the sender's public key that is used in the HTLC,
18+
// specifically when constructing the script spend scripts.
19+
SenderScriptKey [33]byte
20+
21+
// SenderInternalPubKey is the sender's internal pubkey that is used in
22+
// taproot HTLCs as part of the aggregate internal key.
23+
SenderInternalPubKey [33]byte
24+
25+
// ReceiverScriptKey is the receiver's public key that is used in the
26+
// HTLC, specifically when constructing the script spend scripts.
27+
ReceiverScriptKey [33]byte
28+
29+
// ReceiverInternalPubKey is the sender's internal pubkey that is used
30+
// in taproot HTLCs as part of the aggregate internal key.
31+
ReceiverInternalPubKey [33]byte
32+
33+
// ClientScriptKeyLocator is the client's key locator for the key used
34+
// in the HTLC script spend scripts.
35+
ClientScriptKeyLocator keychain.KeyLocator
36+
}
37+
1338
// SwapContract contains the base data that is serialized to persistent storage
1439
// for pending swaps.
1540
type SwapContract struct {
@@ -19,18 +44,8 @@ type SwapContract struct {
1944
// AmountRequested is the total amount of the swap.
2045
AmountRequested btcutil.Amount
2146

22-
// SenderKey is the key of the sender that will be used in the on-chain
23-
// HTLC.
24-
SenderKey [33]byte
25-
26-
// ReceiverKey is the of the receiver that will be used in the on-chain
27-
// HTLC.
28-
ReceiverKey [33]byte
29-
30-
// ClientKeyLocator is the key locator (family and index) for the client
31-
// key. It is for the receiver key if this is a loop out contract, or
32-
// the sender key if this is a loop in contract.
33-
ClientKeyLocator keychain.KeyLocator
47+
// HtlcKeys holds all keys used in the swap HTLC construction.
48+
HtlcKeys HtlcKeys
3449

3550
// CltvExpiry is the total absolute CLTV expiry of the swap.
3651
CltvExpiry int32

loopdb/loopin.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,15 @@ func serializeLoopInContract(swap *LoopInContract) (
6363
return nil, err
6464
}
6565

66-
n, err := b.Write(swap.SenderKey[:])
66+
n, err := b.Write(swap.HtlcKeys.SenderScriptKey[:])
6767
if err != nil {
6868
return nil, err
6969
}
7070
if n != keyLength {
7171
return nil, fmt.Errorf("sender key has invalid length")
7272
}
7373

74-
n, err = b.Write(swap.ReceiverKey[:])
74+
n, err = b.Write(swap.HtlcKeys.ReceiverScriptKey[:])
7575
if err != nil {
7676
return nil, err
7777
}
@@ -161,15 +161,15 @@ func deserializeLoopInContract(value []byte) (*LoopInContract, error) {
161161
return nil, err
162162
}
163163

164-
n, err := r.Read(contract.SenderKey[:])
164+
n, err := r.Read(contract.HtlcKeys.SenderScriptKey[:])
165165
if err != nil {
166166
return nil, err
167167
}
168168
if n != keyLength {
169169
return nil, fmt.Errorf("sender key has invalid length")
170170
}
171171

172-
n, err = r.Read(contract.ReceiverKey[:])
172+
n, err = r.Read(contract.HtlcKeys.ReceiverScriptKey[:])
173173
if err != nil {
174174
return nil, err
175175
}

loopdb/loopout.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ func deserializeLoopOutContract(value []byte, chainParams *chaincfg.Params) (
134134
return nil, err
135135
}
136136

137-
n, err := r.Read(contract.SenderKey[:])
137+
n, err := r.Read(contract.HtlcKeys.SenderScriptKey[:])
138138
if err != nil {
139139
return nil, err
140140
}
141141
if n != keyLength {
142142
return nil, fmt.Errorf("sender key has invalid length")
143143
}
144144

145-
n, err = r.Read(contract.ReceiverKey[:])
145+
n, err = r.Read(contract.HtlcKeys.ReceiverScriptKey[:])
146146
if err != nil {
147147
return nil, err
148148
}
@@ -229,15 +229,15 @@ func serializeLoopOutContract(swap *LoopOutContract) (
229229
return nil, err
230230
}
231231

232-
n, err := b.Write(swap.SenderKey[:])
232+
n, err := b.Write(swap.HtlcKeys.SenderScriptKey[:])
233233
if err != nil {
234234
return nil, err
235235
}
236236
if n != keyLength {
237237
return nil, fmt.Errorf("sender key has invalid length")
238238
}
239239

240-
n, err = b.Write(swap.ReceiverKey[:])
240+
n, err = b.Write(swap.HtlcKeys.ReceiverScriptKey[:])
241241
if err != nil {
242242
return nil, err
243243
}

loopdb/protocol_version.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ const (
5555
// HTLC v3 (P2TR) script for swaps.
5656
ProtocolVersionHtlcV3 = 10
5757

58+
// ProtocolVersionMuSig2 will enable MuSig2 signature scheme for loops.
59+
ProtocolVersionMuSig2 ProtocolVersion = 11
60+
5861
// ProtocolVersionUnrecorded is set for swaps were created before we
5962
// started saving protocol version with swaps.
6063
ProtocolVersionUnrecorded ProtocolVersion = math.MaxUint32
@@ -65,7 +68,7 @@ const (
6568

6669
// experimentalRPCProtocolVersion defines the RPC protocol version that
6770
// includes all currently experimentally released features.
68-
experimentalRPCProtocolVersion = looprpc.ProtocolVersion_HTLC_V3
71+
experimentalRPCProtocolVersion = looprpc.ProtocolVersion_MUSIG2
6972
)
7073

7174
var (
@@ -141,6 +144,9 @@ func (p ProtocolVersion) String() string {
141144
case ProtocolVersionHtlcV3:
142145
return "HTLC V3"
143146

147+
case ProtocolVersionMuSig2:
148+
return "MuSig2"
149+
144150
default:
145151
return "Unknown"
146152
}

0 commit comments

Comments
 (0)