Skip to content

Commit e623263

Browse files
authored
Merge pull request #8499 from Roasbeef/taproot-nonce-tlv-t
multi: upgrade new taproot TLVs to use tlv.OptionalRecordT
2 parents 72764b1 + cac779b commit e623263

22 files changed

+426
-291
lines changed

channeldb/channel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,7 @@ func (c *OpenChannel) ChanSyncMsg() (*lnwire.ChannelReestablish, error) {
15511551
// If this is a taproot channel, then we'll need to generate our next
15521552
// verification nonce to send to the remote party. They'll use this to
15531553
// sign the next update to our commitment transaction.
1554-
var nextTaprootNonce *lnwire.Musig2Nonce
1554+
var nextTaprootNonce lnwire.OptMusig2NonceTLV
15551555
if c.ChanType.IsTaproot() {
15561556
taprootRevProducer, err := DeriveMusig2Shachain(
15571557
c.RevocationProducer,
@@ -1569,7 +1569,7 @@ func (c *OpenChannel) ChanSyncMsg() (*lnwire.ChannelReestablish, error) {
15691569
"nonce: %w", err)
15701570
}
15711571

1572-
nextTaprootNonce = (*lnwire.Musig2Nonce)(&nextNonce.PubNonce)
1572+
nextTaprootNonce = lnwire.SomeMusig2Nonce(nextNonce.PubNonce)
15731573
}
15741574

15751575
return &lnwire.ChannelReestablish{

funding/manager.go

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ var (
4848
//
4949
// NOTE: for itest, this value is changed to 10ms.
5050
checkPeerChannelReadyInterval = 1 * time.Second
51+
52+
// errNoLocalNonce is returned when a local nonce is not found in the
53+
// expected TLV.
54+
errNoLocalNonce = fmt.Errorf("local nonce not found")
55+
56+
// errNoPartialSig is returned when a partial sig is not found in the
57+
// expected TLV.
58+
errNoPartialSig = fmt.Errorf("partial sig not found")
5159
)
5260

5361
// WriteOutpoint writes an outpoint to an io.Writer. This is not the same as
@@ -952,6 +960,28 @@ func (f *Manager) failFundingFlow(peer lnpeer.Peer, cid *chanIdentifier,
952960
}
953961
}
954962

963+
// sendWarning sends a new warning message to the target peer, targeting the
964+
// specified cid with the passed funding error.
965+
func (f *Manager) sendWarning(peer lnpeer.Peer, cid *chanIdentifier,
966+
fundingErr error) {
967+
968+
msg := fundingErr.Error()
969+
970+
errMsg := &lnwire.Warning{
971+
ChanID: cid.tempChanID,
972+
Data: lnwire.WarningData(msg),
973+
}
974+
975+
log.Debugf("Sending funding warning to peer (%x): %v",
976+
peer.IdentityKey().SerializeCompressed(),
977+
spew.Sdump(errMsg),
978+
)
979+
980+
if err := peer.SendMessage(false, errMsg); err != nil {
981+
log.Errorf("unable to send error message to peer %v", err)
982+
}
983+
}
984+
955985
// reservationCoordinator is the primary goroutine tasked with progressing the
956986
// funding workflow between the wallet, and any outside peers or local callers.
957987
//
@@ -1801,17 +1831,17 @@ func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
18011831
}
18021832

18031833
if resCtx.reservation.IsTaproot() {
1804-
if msg.LocalNonce == nil {
1805-
err := fmt.Errorf("local nonce not set for taproot " +
1806-
"chan")
1807-
log.Error(err)
1808-
f.failFundingFlow(
1809-
resCtx.peer, cid, err,
1810-
)
1834+
localNonce, err := msg.LocalNonce.UnwrapOrErrV(errNoLocalNonce)
1835+
if err != nil {
1836+
log.Error(errNoLocalNonce)
1837+
1838+
f.failFundingFlow(resCtx.peer, cid, errNoLocalNonce)
1839+
1840+
return
18111841
}
18121842

18131843
remoteContribution.LocalNonce = &musig2.Nonces{
1814-
PubNonce: *msg.LocalNonce,
1844+
PubNonce: localNonce,
18151845
}
18161846
}
18171847

@@ -1827,13 +1857,6 @@ func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
18271857
log.Debugf("Remote party accepted commitment constraints: %v",
18281858
spew.Sdump(remoteContribution.ChannelConfig.ChannelConstraints))
18291859

1830-
var localNonce *lnwire.Musig2Nonce
1831-
if commitType.IsTaproot() {
1832-
localNonce = (*lnwire.Musig2Nonce)(
1833-
&ourContribution.LocalNonce.PubNonce,
1834-
)
1835-
}
1836-
18371860
// With the initiator's contribution recorded, respond with our
18381861
// contribution in the next message of the workflow.
18391862
fundingAccept := lnwire.AcceptChannel{
@@ -1854,7 +1877,12 @@ func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
18541877
UpfrontShutdownScript: ourContribution.UpfrontShutdown,
18551878
ChannelType: chanType,
18561879
LeaseExpiry: msg.LeaseExpiry,
1857-
LocalNonce: localNonce,
1880+
}
1881+
1882+
if commitType.IsTaproot() {
1883+
fundingAccept.LocalNonce = lnwire.SomeMusig2Nonce(
1884+
ourContribution.LocalNonce.PubNonce,
1885+
)
18581886
}
18591887

18601888
if err := peer.SendMessage(true, &fundingAccept); err != nil {
@@ -2044,15 +2072,17 @@ func (f *Manager) funderProcessAcceptChannel(peer lnpeer.Peer,
20442072
}
20452073

20462074
if resCtx.reservation.IsTaproot() {
2047-
if msg.LocalNonce == nil {
2048-
err := fmt.Errorf("local nonce not set for taproot " +
2049-
"chan")
2050-
log.Error(err)
2051-
f.failFundingFlow(resCtx.peer, cid, err)
2075+
localNonce, err := msg.LocalNonce.UnwrapOrErrV(errNoLocalNonce)
2076+
if err != nil {
2077+
log.Error(errNoLocalNonce)
2078+
2079+
f.failFundingFlow(resCtx.peer, cid, errNoLocalNonce)
2080+
2081+
return
20522082
}
20532083

20542084
remoteContribution.LocalNonce = &musig2.Nonces{
2055-
PubNonce: *msg.LocalNonce,
2085+
PubNonce: localNonce,
20562086
}
20572087
}
20582088

@@ -2263,7 +2293,9 @@ func (f *Manager) continueFundingAccept(resCtx *reservationWithCtx,
22632293
return
22642294
}
22652295

2266-
fundingCreated.PartialSig = partialSig.ToWireSig()
2296+
fundingCreated.PartialSig = lnwire.MaybePartialSigWithNonce(
2297+
partialSig.ToWireSig(),
2298+
)
22672299
} else {
22682300
fundingCreated.CommitSig, err = lnwire.NewSigFromSignature(sig)
22692301
if err != nil {
@@ -2317,14 +2349,15 @@ func (f *Manager) fundeeProcessFundingCreated(peer lnpeer.Peer,
23172349
// our internal input.Signature type.
23182350
var commitSig input.Signature
23192351
if resCtx.reservation.IsTaproot() {
2320-
if msg.PartialSig == nil {
2321-
log.Errorf("partial sig not included: %v", err)
2352+
partialSig, err := msg.PartialSig.UnwrapOrErrV(errNoPartialSig)
2353+
if err != nil {
23222354
f.failFundingFlow(peer, cid, err)
2355+
23232356
return
23242357
}
23252358

23262359
commitSig = new(lnwallet.MusigPartialSig).FromWireSig(
2327-
msg.PartialSig,
2360+
&partialSig,
23282361
)
23292362
} else {
23302363
commitSig, err = msg.CommitSig.ToSignature()
@@ -2408,7 +2441,9 @@ func (f *Manager) fundeeProcessFundingCreated(peer lnpeer.Peer,
24082441
return
24092442
}
24102443

2411-
fundingSigned.PartialSig = partialSig.ToWireSig()
2444+
fundingSigned.PartialSig = lnwire.MaybePartialSigWithNonce(
2445+
partialSig.ToWireSig(),
2446+
)
24122447
} else {
24132448
fundingSigned.CommitSig, err = lnwire.NewSigFromSignature(sig)
24142449
if err != nil {
@@ -2565,14 +2600,15 @@ func (f *Manager) funderProcessFundingSigned(peer lnpeer.Peer,
25652600
// our internal input.Signature type.
25662601
var commitSig input.Signature
25672602
if resCtx.reservation.IsTaproot() {
2568-
if msg.PartialSig == nil {
2569-
log.Errorf("partial sig not included: %v", err)
2603+
partialSig, err := msg.PartialSig.UnwrapOrErrV(errNoPartialSig)
2604+
if err != nil {
25702605
f.failFundingFlow(peer, cid, err)
2606+
25712607
return
25722608
}
25732609

25742610
commitSig = new(lnwallet.MusigPartialSig).FromWireSig(
2575-
msg.PartialSig,
2611+
&partialSig,
25762612
)
25772613
} else {
25782614
commitSig, err = msg.CommitSig.ToSignature()
@@ -3153,8 +3189,8 @@ func (f *Manager) sendChannelReady(completeChan *channeldb.OpenChannel,
31533189
}
31543190
f.nonceMtx.Unlock()
31553191

3156-
channelReadyMsg.NextLocalNonce = (*lnwire.Musig2Nonce)(
3157-
&localNonce.PubNonce,
3192+
channelReadyMsg.NextLocalNonce = lnwire.SomeMusig2Nonce(
3193+
localNonce.PubNonce,
31583194
)
31593195
}
31603196

@@ -3824,11 +3860,9 @@ func (f *Manager) handleChannelReady(peer lnpeer.Peer, //nolint:funlen
38243860
channelReadyMsg.AliasScid = &alias
38253861

38263862
if firstVerNonce != nil {
3827-
wireNonce := (*lnwire.Musig2Nonce)(
3828-
&firstVerNonce.PubNonce,
3863+
channelReadyMsg.NextLocalNonce = lnwire.SomeMusig2Nonce( //nolint:lll
3864+
firstVerNonce.PubNonce,
38293865
)
3830-
3831-
channelReadyMsg.NextLocalNonce = wireNonce
38323866
}
38333867

38343868
err = peer.SendMessage(true, channelReadyMsg)
@@ -3873,16 +3907,21 @@ func (f *Manager) handleChannelReady(peer lnpeer.Peer, //nolint:funlen
38733907
log.Infof("ChanID(%v): applying local+remote musig2 nonces",
38743908
chanID)
38753909

3876-
if msg.NextLocalNonce == nil {
3877-
log.Errorf("remote nonces are nil")
3910+
remoteNonce, err := msg.NextLocalNonce.UnwrapOrErrV(
3911+
errNoLocalNonce,
3912+
)
3913+
if err != nil {
3914+
cid := newChanIdentifier(msg.ChanID)
3915+
f.sendWarning(peer, cid, err)
3916+
38783917
return
38793918
}
38803919

38813920
chanOpts = append(
38823921
chanOpts,
38833922
lnwallet.WithLocalMusigNonces(localNonce),
38843923
lnwallet.WithRemoteMusigNonces(&musig2.Nonces{
3885-
PubNonce: *msg.NextLocalNonce,
3924+
PubNonce: remoteNonce,
38863925
}),
38873926
)
38883927
}
@@ -4714,13 +4753,6 @@ func (f *Manager) handleInitFundingMsg(msg *InitFundingMsg) {
47144753
log.Infof("Starting funding workflow with %v for pending_id(%x), "+
47154754
"committype=%v", msg.Peer.Address(), chanID, commitType)
47164755

4717-
var localNonce *lnwire.Musig2Nonce
4718-
if commitType.IsTaproot() {
4719-
localNonce = (*lnwire.Musig2Nonce)(
4720-
&ourContribution.LocalNonce.PubNonce,
4721-
)
4722-
}
4723-
47244756
fundingOpen := lnwire.OpenChannel{
47254757
ChainHash: *f.cfg.Wallet.Cfg.NetParams.GenesisHash,
47264758
PendingChannelID: chanID,
@@ -4743,8 +4775,14 @@ func (f *Manager) handleInitFundingMsg(msg *InitFundingMsg) {
47434775
UpfrontShutdownScript: shutdown,
47444776
ChannelType: chanType,
47454777
LeaseExpiry: leaseExpiry,
4746-
LocalNonce: localNonce,
47474778
}
4779+
4780+
if commitType.IsTaproot() {
4781+
fundingOpen.LocalNonce = lnwire.SomeMusig2Nonce(
4782+
ourContribution.LocalNonce.PubNonce,
4783+
)
4784+
}
4785+
47484786
if err := msg.Peer.SendMessage(true, &fundingOpen); err != nil {
47494787
e := fmt.Errorf("unable to send funding request message: %v",
47504788
err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ require (
4444
github.com/lightningnetwork/lnd/kvdb v1.4.5
4545
github.com/lightningnetwork/lnd/queue v1.1.1
4646
github.com/lightningnetwork/lnd/ticker v1.1.1
47-
github.com/lightningnetwork/lnd/tlv v1.2.1
47+
github.com/lightningnetwork/lnd/tlv v1.2.3
4848
github.com/lightningnetwork/lnd/tor v1.1.2
4949
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796
5050
github.com/miekg/dns v1.1.43

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,8 @@ github.com/lightningnetwork/lnd/queue v1.1.1 h1:99ovBlpM9B0FRCGYJo6RSFDlt8/vOkQQ
438438
github.com/lightningnetwork/lnd/queue v1.1.1/go.mod h1:7A6nC1Qrm32FHuhx/mi1cieAiBZo5O6l8IBIoQxvkz4=
439439
github.com/lightningnetwork/lnd/ticker v1.1.1 h1:J/b6N2hibFtC7JLV77ULQp++QLtCwT6ijJlbdiZFbSM=
440440
github.com/lightningnetwork/lnd/ticker v1.1.1/go.mod h1:waPTRAAcwtu7Ji3+3k+u/xH5GHovTsCoSVpho0KDvdA=
441-
github.com/lightningnetwork/lnd/tlv v1.2.1 h1:zV6nBuPNKthAnDc2UR8D8RK+408w3eumRsiJFf3ukyU=
442-
github.com/lightningnetwork/lnd/tlv v1.2.1/go.mod h1:GJ1JLyA7FWJsjJ2zKJe9+urxwYi15GweC8c509mUfCs=
441+
github.com/lightningnetwork/lnd/tlv v1.2.3 h1:If5ibokA/UoCBGuCKaY6Vn2SJU0l9uAbehCnhTZjEP8=
442+
github.com/lightningnetwork/lnd/tlv v1.2.3/go.mod h1:zDkmqxOczP6LaLTvSFDQ1SJUfHcQRCMKFj93dn3eMB8=
443443
github.com/lightningnetwork/lnd/tor v1.1.2 h1:3zv9z/EivNFaMF89v3ciBjCS7kvCj4ZFG7XvD2Qq0/k=
444444
github.com/lightningnetwork/lnd/tor v1.1.2/go.mod h1:j7T9uJ2NLMaHwE7GiBGnpYLn4f7NRoTM6qj+ul6/ycA=
445445
github.com/ltcsuite/ltcd v0.0.0-20190101042124-f37f8bf35796 h1:sjOGyegMIhvgfq5oaue6Td+hxZuf3tDC8lAPrFldqFw=

0 commit comments

Comments
 (0)