Skip to content
Closed
11 changes: 11 additions & 0 deletions lnwallet/reservation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet/chanfunding"
Expand Down Expand Up @@ -212,6 +213,11 @@ type ChannelReservation struct {
// commitment state.
pushMSat lnwire.MilliSatoshi

// tapscriptRoot is the root of the tapscript tree that will be used to
// create the musig2 funding output. This is only used for taproot
// channels.
tapscriptRoot fn.Option[chainhash.Hash]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused since it's already in partialState?


wallet *LightningWallet
chanFunder chanfunding.Assembler

Expand Down Expand Up @@ -412,6 +418,10 @@ func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
chanType |= channeldb.ScidAliasFeatureBit
}

if req.TapscriptRoot.IsSome() {
chanType |= channeldb.TapscriptRootBit
}

return &ChannelReservation{
ourContribution: &ChannelContribution{
FundingAmount: ourBalance.ToSatoshis(),
Expand Down Expand Up @@ -445,6 +455,7 @@ func NewChannelReservation(capacity, localFundingAmt btcutil.Amount,
InitialLocalBalance: ourBalance,
InitialRemoteBalance: theirBalance,
Memo: req.Memo,
TapscriptRoot: req.TapscriptRoot,
},
pushMSat: req.PushMSat,
pendingChanID: req.PendingChanID,
Expand Down
30 changes: 30 additions & 0 deletions lnwallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/btcsuite/btcwallet/wallet"
"github.com/davecgh/go-spew/spew"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
Expand Down Expand Up @@ -200,6 +201,11 @@ type InitFundingReserveMsg struct {
// channel that will be useful to our future selves.
Memo []byte

// TapscriptRoot is the root of the tapscript tree that will be used to
// create the funding output. This is an optional field that should
// only be set for taproot channels,
TapscriptRoot fn.Option[chainhash.Hash]

// err is a channel in which all errors will be sent across. Will be
// nil if this initial set is successful.
//
Expand Down Expand Up @@ -2083,8 +2089,14 @@ func (l *LightningWallet) verifyCommitSig(res *ChannelReservation,
// already. If we're the responder in the funding flow, we may
// not have generated it already.
if res.musigSessions == nil {
fundingOpts := fn.MapOptionZ(
res.partialState.TapscriptRoot,
TapscriptRootToOpt,
)

_, fundingOutput, err := input.GenTaprootFundingScript(
localKey, remoteKey, channelValue,
fundingOpts...,
)
if err != nil {
return err
Expand Down Expand Up @@ -2324,8 +2336,13 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
fundingTxOut *wire.TxOut
)
if chanType.IsTaproot() {
fundingOpts := fn.MapOptionZ(
pendingReservation.partialState.TapscriptRoot,
TapscriptRootToOpt,
)
fundingWitnessScript, fundingTxOut, err = input.GenTaprootFundingScript( //nolint:lll
ourKey.PubKey, theirKey.PubKey, channelValue,
fundingOpts...,
)
} else {
fundingWitnessScript, fundingTxOut, err = input.GenFundingPkScript( //nolint:lll
Expand Down Expand Up @@ -2448,6 +2465,14 @@ func TapscriptRootToOpt(root chainhash.Hash) []input.FundingScriptOpt {
return []input.FundingScriptOpt{input.WithTapscriptRoot(root)}
}

// TapscriptRootToTweak is a helper function that converts a tapscript root
// into a tweak that can be used with the musig2 API.
func TapscriptRootToTweak(root chainhash.Hash) input.MuSig2Tweaks {
return input.MuSig2Tweaks{
TaprootTweak: root[:],
}
}

// ValidateChannel will attempt to fully validate a newly mined channel, given
// its funding transaction and existing channel state. If this method returns
// an error, then the mined channel is invalid, and shouldn't be used.
Expand All @@ -2469,8 +2494,13 @@ func (l *LightningWallet) ValidateChannel(channelState *channeldb.OpenChannel,
// funding transaction, and also commitment validity.
var fundingScript []byte
if channelState.ChanType.IsTaproot() {
fundingOpts := fn.MapOptionZ(
channelState.TapscriptRoot, TapscriptRootToOpt,
)

fundingScript, _, err = input.GenTaprootFundingScript(
localKey, remoteKey, int64(channel.Capacity),
fundingOpts...,
)
if err != nil {
return err
Expand Down