Skip to content

Commit b8cb4e6

Browse files
committed
lnwallet: update internal funding flow w/ tapscript root
This isn't hooked up yet to the funding manager, but with this commit, we can now start to write internal unit tests that handle musig2 channels with a tapscript root.
1 parent 7d3c83b commit b8cb4e6

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lnwallet/reservation.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/btcsuite/btcd/chaincfg/chainhash"
1212
"github.com/btcsuite/btcd/wire"
1313
"github.com/lightningnetwork/lnd/channeldb"
14+
"github.com/lightningnetwork/lnd/fn"
1415
"github.com/lightningnetwork/lnd/input"
1516
"github.com/lightningnetwork/lnd/keychain"
1617
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
@@ -212,6 +213,11 @@ type ChannelReservation struct {
212213
// commitment state.
213214
pushMSat lnwire.MilliSatoshi
214215

216+
// tapscriptRoot is the root of the tapscript tree that will be used to
217+
// create the musig2 funding output. This is only used for taproot
218+
// channels.
219+
tapscriptRoot fn.Option[chainhash.Hash]
220+
215221
wallet *LightningWallet
216222
chanFunder chanfunding.Assembler
217223

@@ -412,6 +418,10 @@ func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
412418
chanType |= channeldb.ScidAliasFeatureBit
413419
}
414420

421+
if req.TapscriptRoot.IsSome() {
422+
chanType |= channeldb.TapscriptRootBit
423+
}
424+
415425
return &ChannelReservation{
416426
ourContribution: &ChannelContribution{
417427
FundingAmount: ourBalance.ToSatoshis(),
@@ -445,6 +455,7 @@ func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
445455
InitialLocalBalance: ourBalance,
446456
InitialRemoteBalance: theirBalance,
447457
Memo: req.Memo,
458+
TapscriptRoot: req.TapscriptRoot,
448459
},
449460
pushMSat: req.PushMSat,
450461
pendingChanID: req.PendingChanID,

lnwallet/wallet.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/btcsuite/btcwallet/wallet"
2424
"github.com/davecgh/go-spew/spew"
2525
"github.com/lightningnetwork/lnd/channeldb"
26+
"github.com/lightningnetwork/lnd/fn"
2627
"github.com/lightningnetwork/lnd/input"
2728
"github.com/lightningnetwork/lnd/keychain"
2829
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -200,6 +201,11 @@ type InitFundingReserveMsg struct {
200201
// channel that will be useful to our future selves.
201202
Memo []byte
202203

204+
// TapscriptRoot is the root of the tapscript tree that will be used to
205+
// create the funding output. This is an optional field that should
206+
// only be set for taproot channels,
207+
TapscriptRoot fn.Option[chainhash.Hash]
208+
203209
// err is a channel in which all errors will be sent across. Will be
204210
// nil if this initial set is successful.
205211
//
@@ -2083,8 +2089,14 @@ func (l *LightningWallet) verifyCommitSig(res *ChannelReservation,
20832089
// already. If we're the responder in the funding flow, we may
20842090
// not have generated it already.
20852091
if res.musigSessions == nil {
2092+
fundingOpts := fn.MapOptionZ(
2093+
res.partialState.TapscriptRoot,
2094+
TapscriptRootToOpt,
2095+
)
2096+
20862097
_, fundingOutput, err := input.GenTaprootFundingScript(
20872098
localKey, remoteKey, channelValue,
2099+
fundingOpts...,
20882100
)
20892101
if err != nil {
20902102
return err
@@ -2324,8 +2336,13 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
23242336
fundingTxOut *wire.TxOut
23252337
)
23262338
if chanType.IsTaproot() {
2339+
fundingOpts := fn.MapOptionZ(
2340+
pendingReservation.partialState.TapscriptRoot,
2341+
TapscriptRootToOpt,
2342+
)
23272343
fundingWitnessScript, fundingTxOut, err = input.GenTaprootFundingScript( //nolint:lll
23282344
ourKey.PubKey, theirKey.PubKey, channelValue,
2345+
fundingOpts...,
23292346
)
23302347
} else {
23312348
fundingWitnessScript, fundingTxOut, err = input.GenFundingPkScript( //nolint:lll
@@ -2448,6 +2465,14 @@ func TapscriptRootToOpt(root chainhash.Hash) []input.FundingScriptOpt {
24482465
return []input.FundingScriptOpt{input.WithTapscriptRoot(root)}
24492466
}
24502467

2468+
// TapscriptRootToTweak is a helper function that converts a tapscript root
2469+
// into a tweak that can be used with the musig2 API.
2470+
func TapscriptRootToTweak(root chainhash.Hash) input.MuSig2Tweaks {
2471+
return input.MuSig2Tweaks{
2472+
TaprootTweak: root[:],
2473+
}
2474+
}
2475+
24512476
// ValidateChannel will attempt to fully validate a newly mined channel, given
24522477
// its funding transaction and existing channel state. If this method returns
24532478
// an error, then the mined channel is invalid, and shouldn't be used.
@@ -2469,8 +2494,13 @@ func (l *LightningWallet) ValidateChannel(channelState *channeldb.OpenChannel,
24692494
// funding transaction, and also commitment validity.
24702495
var fundingScript []byte
24712496
if channelState.ChanType.IsTaproot() {
2497+
fundingOpts := fn.MapOptionZ(
2498+
channelState.TapscriptRoot, TapscriptRootToOpt,
2499+
)
2500+
24722501
fundingScript, _, err = input.GenTaprootFundingScript(
24732502
localKey, remoteKey, int64(channel.Capacity),
2503+
fundingOpts...,
24742504
)
24752505
if err != nil {
24762506
return err

0 commit comments

Comments
 (0)