Skip to content

Commit ad3094d

Browse files
committed
lnwallet: add error to aux funding controller
1 parent a232b58 commit ad3094d

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

funding/aux_funding.go

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package funding
22

33
import (
44
"github.com/btcsuite/btcd/chaincfg/chainhash"
5+
"github.com/lightningnetwork/lnd/channeldb"
56
"github.com/lightningnetwork/lnd/fn"
67
"github.com/lightningnetwork/lnd/lnwallet"
78
"github.com/lightningnetwork/lnd/protofsm"
@@ -17,16 +18,45 @@ type AuxFundingController interface {
1718
// handle custom messages specific to the funding type.
1819
protofsm.MsgEndpoint
1920

20-
// DescPendingChanID takes a pending channel ID, that may already be
21+
// DescFromPendingChanID takes a pending channel ID, that may already be
2122
// known due to prior custom channel messages, and maybe returns an aux
2223
// funding desc which can be used to modify how a channel is funded.
23-
//
24-
// TODO(roasbeef): erorr on validation if fail due to invalid root
25-
// match?
26-
DescFromPendingChanID(PendingChanID) fn.Option[lnwallet.AuxFundingDesc]
24+
DescFromPendingChanID(PendingChanID,
25+
*channeldb.OpenChannel, lnwallet.CommitmentKeyRing,
26+
bool) (fn.Option[lnwallet.AuxFundingDesc], error)
2727

2828
// DeriveTapscriptRoot takes a pending channel ID and maybe returns a
2929
// tapscript root that should be used when creating any musig2 sessions
3030
// for a channel.
31-
DeriveTapscriptRoot(PendingChanID) fn.Option[chainhash.Hash]
31+
DeriveTapscriptRoot(PendingChanID) (fn.Option[chainhash.Hash], error)
32+
}
33+
34+
// descFromPendingChanID takes a pending channel ID, that may already be
35+
// known due to prior custom channel messages, and maybe returns an aux
36+
// funding desc which can be used to modify how a channel is funded.
37+
func descFromPendingChanID(controller fn.Option[AuxFundingController],
38+
chanID PendingChanID, openChan *channeldb.OpenChannel,
39+
keyRing lnwallet.CommitmentKeyRing,
40+
initiator bool) (fn.Option[lnwallet.AuxFundingDesc], error) {
41+
42+
if controller.IsNone() {
43+
return fn.None[lnwallet.AuxFundingDesc](), nil
44+
}
45+
46+
return controller.UnsafeFromSome().DescFromPendingChanID(
47+
chanID, openChan, keyRing, initiator,
48+
)
49+
}
50+
51+
// deriveTapscriptRoot takes a pending channel ID and maybe returns a
52+
// tapscript root that should be used when creating any musig2 sessions
53+
// for a channel.
54+
func deriveTapscriptRoot(controller fn.Option[AuxFundingController],
55+
chanID PendingChanID) (fn.Option[chainhash.Hash], error) {
56+
57+
if controller.IsNone() {
58+
return fn.None[chainhash.Hash](), nil
59+
}
60+
61+
return controller.UnsafeFromSome().DeriveTapscriptRoot(chanID)
3262
}

funding/manager.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,11 +1631,14 @@ func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
16311631
// At this point, if we have an AuxFundingController active, we'll
16321632
// check to see if we have a special tapscript root to use in our
16331633
// musig2 funding output.
1634-
tapscriptRoot := fn.MapOption(
1635-
func(a AuxFundingController) fn.Option[chainhash.Hash] {
1636-
return a.DeriveTapscriptRoot(msg.PendingChannelID)
1637-
},
1638-
)(f.cfg.AuxFundingController)
1634+
tapscriptRoot, err := deriveTapscriptRoot(
1635+
f.cfg.AuxFundingController, msg.PendingChannelID,
1636+
)
1637+
if err != nil {
1638+
err = fmt.Errorf("error deriving tapscript root: %w", err)
1639+
log.Error(err)
1640+
f.failFundingFlow(peer, cid, err)
1641+
}
16391642

16401643
req := &lnwallet.InitFundingReserveMsg{
16411644
ChainHash: &msg.ChainHash,
@@ -1653,7 +1656,7 @@ func (f *Manager) fundeeProcessOpenChannel(peer lnpeer.Peer,
16531656
ZeroConf: zeroConf,
16541657
OptionScidAlias: scid,
16551658
ScidAliasFeature: scidFeatureVal,
1656-
TapscriptRoot: fn.FlattenOption(tapscriptRoot),
1659+
TapscriptRoot: tapscriptRoot,
16571660
}
16581661

16591662
reservation, err := f.cfg.Wallet.InitChannelReservation(req)
@@ -4631,11 +4634,14 @@ func (f *Manager) handleInitFundingMsg(msg *InitFundingMsg) {
46314634
// At this point, if we have an AuxFundingController active, we'll
46324635
// check to see if we have a special tapscript root to use in our
46334636
// musig2 funding output.
4634-
tapscriptRoot := fn.MapOption(
4635-
func(a AuxFundingController) fn.Option[chainhash.Hash] {
4636-
return a.DeriveTapscriptRoot(chanID)
4637-
},
4638-
)(f.cfg.AuxFundingController)
4637+
tapscriptRoot, err := deriveTapscriptRoot(
4638+
f.cfg.AuxFundingController, chanID,
4639+
)
4640+
if err != nil {
4641+
err = fmt.Errorf("error deriving tapscript root: %w", err)
4642+
msg.Err <- err
4643+
return
4644+
}
46394645

46404646
req := &lnwallet.InitFundingReserveMsg{
46414647
ChainHash: &msg.ChainHash,
@@ -4660,7 +4666,7 @@ func (f *Manager) handleInitFundingMsg(msg *InitFundingMsg) {
46604666
OptionScidAlias: scid,
46614667
ScidAliasFeature: scidFeatureVal,
46624668
Memo: msg.Memo,
4663-
TapscriptRoot: fn.FlattenOption(tapscriptRoot),
4669+
TapscriptRoot: tapscriptRoot,
46644670
CustomChannelData: msg.CustomChannelData,
46654671
}
46664672

0 commit comments

Comments
 (0)