Skip to content

Commit cd91f90

Browse files
committed
loop: use MuSig2 1.0 scheme for the new MuSig2 swaps
1 parent b47f67a commit cd91f90

File tree

4 files changed

+41
-66
lines changed

4 files changed

+41
-66
lines changed

client_test.go

Lines changed: 7 additions & 25 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"
@@ -277,30 +276,13 @@ func testLoopOutResume(t *testing.T, confs uint32, expired, preimageRevealed,
277276
// Expect client to register for our expected number of confirmations.
278277
confIntent := ctx.AssertRegisterConf(preimageRevealed, int32(confs))
279278

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

306288
signalSwapPaymentResult(nil)
@@ -319,7 +301,7 @@ func testLoopOutResume(t *testing.T, confs uint32, expired, preimageRevealed,
319301
func(r error) {},
320302
func(r error) {},
321303
preimageRevealed,
322-
confIntent, scriptVersion,
304+
confIntent, GetHtlcScriptVersion(protocolVersion),
323305
)
324306
}
325307

loopin_test.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ import (
88
"github.com/btcsuite/btcd/btcutil"
99
"github.com/btcsuite/btcd/wire"
1010
"github.com/lightninglabs/loop/loopdb"
11-
"github.com/lightninglabs/loop/swap"
1211
"github.com/lightninglabs/loop/test"
1312
"github.com/lightningnetwork/lnd/chainntnfs"
14-
"github.com/lightningnetwork/lnd/input"
1513
invpkg "github.com/lightningnetwork/lnd/invoices"
1614
"github.com/lightningnetwork/lnd/routing/route"
1715
"github.com/stretchr/testify/require"
@@ -450,37 +448,10 @@ func testLoopInResume(t *testing.T, state loopdb.SwapState, expired bool,
450448
pendSwap.Loop.Events[0].Cost = cost
451449
}
452450

453-
var (
454-
htlc *swap.Htlc
455-
err error
451+
htlc, err := GetHtlc(
452+
testPreimage.Hash(), &contract.SwapContract,
453+
cfg.lnd.ChainParams,
456454
)
457-
458-
switch GetHtlcScriptVersion(storedVersion) {
459-
case swap.HtlcV2:
460-
htlc, err = swap.NewHtlcV2(
461-
contract.CltvExpiry,
462-
contract.HtlcKeys.SenderScriptKey,
463-
contract.HtlcKeys.ReceiverScriptKey,
464-
testPreimage.Hash(),
465-
cfg.lnd.ChainParams,
466-
)
467-
468-
case swap.HtlcV3:
469-
htlc, err = swap.NewHtlcV3(
470-
input.MuSig2Version040,
471-
contract.CltvExpiry,
472-
contract.HtlcKeys.SenderInternalPubKey,
473-
contract.HtlcKeys.ReceiverInternalPubKey,
474-
contract.HtlcKeys.SenderScriptKey,
475-
contract.HtlcKeys.ReceiverScriptKey,
476-
testPreimage.Hash(),
477-
cfg.lnd.ChainParams,
478-
)
479-
480-
default:
481-
t.Fatalf("unknown HTLC script version")
482-
}
483-
484455
require.NoError(t, err)
485456

486457
err = ctx.store.CreateLoopIn(testPreimage.Hash(), contract)

loopout.go

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,22 +1355,37 @@ func (s *loopOutSwap) createMuSig2SweepTxn(
13551355
return nil, err
13561356
}
13571357

1358-
signers := [][]byte{
1359-
s.HtlcKeys.SenderInternalPubKey[1:],
1360-
s.HtlcKeys.ReceiverInternalPubKey[1:],
1358+
var (
1359+
signers [][]byte
1360+
muSig2Verion input.MuSig2Version
1361+
)
1362+
1363+
// Depending on the MuSig2 version we either pass 32 byte Schnorr
1364+
// public keys or normal 33 byte public keys.
1365+
if s.ProtocolVersion >= loopdb.ProtocolVersionMuSig2 {
1366+
muSig2Verion = input.MuSig2Version100RC2
1367+
signers = [][]byte{
1368+
s.HtlcKeys.SenderInternalPubKey[:],
1369+
s.HtlcKeys.ReceiverInternalPubKey[:],
1370+
}
1371+
} else {
1372+
muSig2Verion = input.MuSig2Version040
1373+
signers = [][]byte{
1374+
s.HtlcKeys.SenderInternalPubKey[1:],
1375+
s.HtlcKeys.ReceiverInternalPubKey[1:],
1376+
}
13611377
}
13621378

1363-
htlc, ok := s.htlc.HtlcScript.(*swap.HtlcScriptV3)
1379+
htlcScript, ok := s.htlc.HtlcScript.(*swap.HtlcScriptV3)
13641380
if !ok {
13651381
return nil, fmt.Errorf("non taproot htlc")
13661382
}
13671383

13681384
// Now we're creating a local MuSig2 session using the receiver key's
13691385
// key locator and the htlc's root hash.
13701386
musig2SessionInfo, err := s.lnd.Signer.MuSig2CreateSession(
1371-
ctx, input.MuSig2Version040,
1372-
&s.HtlcKeys.ClientScriptKeyLocator, signers,
1373-
lndclient.MuSig2TaprootTweakOpt(htlc.RootHash[:], false),
1387+
ctx, muSig2Verion, &s.HtlcKeys.ClientScriptKeyLocator, signers,
1388+
lndclient.MuSig2TaprootTweakOpt(htlcScript.RootHash[:], false),
13741389
)
13751390
if err != nil {
13761391
return nil, err
@@ -1434,7 +1449,7 @@ func (s *loopOutSwap) createMuSig2SweepTxn(
14341449
// To be sure that we're good, parse and validate that the combined
14351450
// signature is indeed valid for the sig hash and the internal pubkey.
14361451
err = s.executeConfig.verifySchnorrSig(
1437-
htlc.TaprootKey, sigHash, finalSig,
1452+
htlcScript.TaprootKey, sigHash, finalSig,
14381453
)
14391454
if err != nil {
14401455
return nil, err

swap.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,15 @@ func GetHtlc(hash lntypes.Hash, contract *loopdb.SwapContract,
8282
)
8383

8484
case swap.HtlcV3:
85+
// Swaps that implement the new MuSig2 protocol will be expected
86+
// to use the 1.0RC2 MuSig2 key derivation scheme.
87+
muSig2Version := input.MuSig2Version040
88+
if contract.ProtocolVersion >= loopdb.ProtocolVersionMuSig2 {
89+
muSig2Version = input.MuSig2Version100RC2
90+
}
91+
8592
return swap.NewHtlcV3(
86-
input.MuSig2Version040,
93+
muSig2Version,
8794
contract.CltvExpiry,
8895
contract.HtlcKeys.SenderInternalPubKey,
8996
contract.HtlcKeys.ReceiverInternalPubKey,

0 commit comments

Comments
 (0)