Skip to content

Commit

Permalink
lnwallet/chanfunding: update assemblers to support musig2
Browse files Browse the repository at this point in the history
In this commit, we update the set of intents and assemblers to recognize
musig2. For this change, we use a new bool, `musig2`, then use that to
determine if we need to use the new taproot funding scripts or not.
  • Loading branch information
Roasbeef committed May 12, 2023
1 parent e43c6c3 commit d386888
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lnwallet/chanfunding/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ type Request struct {
// ChangeAddr is a closure that will provide the Assembler with a
// change address for the funding transaction if needed.
ChangeAddr func() (btcutil.Address, error)

// Musig2 is true, then musig2 will be used to generate teh funding
// output. By definition, this'll also use segwit v1 (taproot) for the
// funding output.
Musig2 bool
}

// Intent is returned by an Assembler and represents the base functionality the
Expand Down
29 changes: 28 additions & 1 deletion lnwallet/chanfunding/canned_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type ShimIntent struct {
// a normal channel. Until this height, it's considered frozen, so it
// can only be cooperatively closed by the responding party.
thawHeight uint32

// musig2 determines if the funding output should use musig2 to
// generate an aggregate key to use as the taproot-native multi-sig
// output.
musig2 bool
}

// FundingOutput returns the witness script, and the output that creates the
Expand All @@ -48,6 +53,19 @@ func (s *ShimIntent) FundingOutput() ([]byte, *wire.TxOut, error) {
}

totalAmt := s.localFundingAmt + s.remoteFundingAmt

// If musig2 is active, then we'll return a single aggregated key
// rather than using the "existing" funding script.
if s.musig2 {
// Similar to the existing p2wsh script, we'll always ensure
// the keys are sorted before use.
return input.GenTaprootFundingScript(
s.localKey.PubKey,
s.remoteKey,
int64(totalAmt),
)
}

return input.GenFundingPkScript(
s.localKey.PubKey.SerializeCompressed(),
s.remoteKey.SerializeCompressed(),
Expand Down Expand Up @@ -171,13 +189,20 @@ type CannedAssembler struct {
// a normal channel. Until this height, it's considered frozen, so it
// can only be cooperatively closed by the responding party.
thawHeight uint32

// musig2 determines if the funding output should use musig2 to
// generate an aggregate key to use as the taproot-native multi-sig
// output.
musig2 bool
}

// NewCannedAssembler creates a new CannedAssembler from the material required
// to construct a funding output and channel point.
//
// TODO(roasbeef): pass in chan type instead?
func NewCannedAssembler(thawHeight uint32, chanPoint wire.OutPoint,
fundingAmt btcutil.Amount, localKey *keychain.KeyDescriptor,
remoteKey *btcec.PublicKey, initiator bool) *CannedAssembler {
remoteKey *btcec.PublicKey, initiator, musig2 bool) *CannedAssembler {

return &CannedAssembler{
initiator: initiator,
Expand All @@ -186,6 +211,7 @@ func NewCannedAssembler(thawHeight uint32, chanPoint wire.OutPoint,
fundingAmt: fundingAmt,
chanPoint: chanPoint,
thawHeight: thawHeight,
musig2: musig2,
}
}

Expand All @@ -207,6 +233,7 @@ func (c *CannedAssembler) ProvisionChannel(req *Request) (Intent, error) {
remoteKey: c.remoteKey,
chanPoint: &c.chanPoint,
thawHeight: c.thawHeight,
musig2: c.musig2,
}

if c.initiator {
Expand Down
1 change: 1 addition & 0 deletions lnwallet/chanfunding/psbt_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ func (p *PsbtAssembler) ProvisionChannel(req *Request) (Intent, error) {
intent := &PsbtIntent{
ShimIntent: ShimIntent{
localFundingAmt: p.fundingAmt,
musig2: req.Musig2,
},
State: PsbtShimRegistered,
BasePsbt: p.basePsbt,
Expand Down
1 change: 1 addition & 0 deletions lnwallet/chanfunding/wallet_assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
ShimIntent: ShimIntent{
localFundingAmt: localContributionAmt,
remoteFundingAmt: r.RemoteAmt,
musig2: r.Musig2,
},
InputCoins: selectedCoins,
coinLocker: w.cfg.CoinLocker,
Expand Down
4 changes: 3 additions & 1 deletion rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1847,10 +1847,12 @@ func newFundingShimAssembler(chanPointShim *lnrpc.ChanPointShim, initiator bool,

// With all the parts assembled, we can now make the canned assembler
// to pass into the wallet.
//
// TODO(roasbeef): update to support musig2
return chanfunding.NewCannedAssembler(
chanPointShim.ThawHeight, *chanPoint,
btcutil.Amount(chanPointShim.Amt), &localKeyDesc,
remoteKey, initiator,
remoteKey, initiator, false,
), nil
}

Expand Down

0 comments on commit d386888

Please sign in to comment.