lnwallet/chanfunding: update assemblers to support musig2

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.
This commit is contained in:
Olaoluwa Osuntokun 2023-01-18 19:33:50 -08:00
parent 9a65806c09
commit 95d627af4e
No known key found for this signature in database
GPG Key ID: 3BBD59E99B280306
6 changed files with 40 additions and 4 deletions

View File

@ -110,6 +110,11 @@ type Request struct {
// ChangeAddr is a closure that will provide the Assembler with a // ChangeAddr is a closure that will provide the Assembler with a
// change address for the funding transaction if needed. // change address for the funding transaction if needed.
ChangeAddr func() (btcutil.Address, error) ChangeAddr func() (btcutil.Address, error)
// Musig2 if true, then musig2 will be used to generate the 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 // Intent is returned by an Assembler and represents the base functionality the

View File

@ -35,6 +35,11 @@ type ShimIntent struct {
// a normal channel. Until this height, it's considered frozen, so it // a normal channel. Until this height, it's considered frozen, so it
// can only be cooperatively closed by the responding party. // can only be cooperatively closed by the responding party.
thawHeight uint32 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 // FundingOutput returns the witness script, and the output that creates the
@ -48,6 +53,19 @@ func (s *ShimIntent) FundingOutput() ([]byte, *wire.TxOut, error) {
} }
totalAmt := s.localFundingAmt + s.remoteFundingAmt 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( return input.GenFundingPkScript(
s.localKey.PubKey.SerializeCompressed(), s.localKey.PubKey.SerializeCompressed(),
s.remoteKey.SerializeCompressed(), s.remoteKey.SerializeCompressed(),
@ -171,13 +189,20 @@ type CannedAssembler struct {
// a normal channel. Until this height, it's considered frozen, so it // a normal channel. Until this height, it's considered frozen, so it
// can only be cooperatively closed by the responding party. // can only be cooperatively closed by the responding party.
thawHeight uint32 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 // NewCannedAssembler creates a new CannedAssembler from the material required
// to construct a funding output and channel point. // to construct a funding output and channel point.
//
// TODO(roasbeef): pass in chan type instead?
func NewCannedAssembler(thawHeight uint32, chanPoint wire.OutPoint, func NewCannedAssembler(thawHeight uint32, chanPoint wire.OutPoint,
fundingAmt btcutil.Amount, localKey *keychain.KeyDescriptor, fundingAmt btcutil.Amount, localKey *keychain.KeyDescriptor,
remoteKey *btcec.PublicKey, initiator bool) *CannedAssembler { remoteKey *btcec.PublicKey, initiator, musig2 bool) *CannedAssembler {
return &CannedAssembler{ return &CannedAssembler{
initiator: initiator, initiator: initiator,
@ -186,6 +211,7 @@ func NewCannedAssembler(thawHeight uint32, chanPoint wire.OutPoint,
fundingAmt: fundingAmt, fundingAmt: fundingAmt,
chanPoint: chanPoint, chanPoint: chanPoint,
thawHeight: thawHeight, thawHeight: thawHeight,
musig2: musig2,
} }
} }
@ -215,6 +241,7 @@ func (c *CannedAssembler) ProvisionChannel(req *Request) (Intent, error) {
remoteKey: c.remoteKey, remoteKey: c.remoteKey,
chanPoint: &c.chanPoint, chanPoint: &c.chanPoint,
thawHeight: c.thawHeight, thawHeight: c.thawHeight,
musig2: c.musig2,
} }
if c.initiator { if c.initiator {

View File

@ -531,6 +531,7 @@ func (p *PsbtAssembler) ProvisionChannel(req *Request) (Intent, error) {
intent := &PsbtIntent{ intent := &PsbtIntent{
ShimIntent: ShimIntent{ ShimIntent: ShimIntent{
localFundingAmt: p.fundingAmt, localFundingAmt: p.fundingAmt,
musig2: req.Musig2,
}, },
State: PsbtShimRegistered, State: PsbtShimRegistered,
BasePsbt: p.basePsbt, BasePsbt: p.basePsbt,

View File

@ -476,6 +476,7 @@ func (w *WalletAssembler) ProvisionChannel(r *Request) (Intent, error) {
ShimIntent: ShimIntent{ ShimIntent: ShimIntent{
localFundingAmt: localContributionAmt, localFundingAmt: localContributionAmt,
remoteFundingAmt: r.RemoteAmt, remoteFundingAmt: r.RemoteAmt,
musig2: r.Musig2,
}, },
InputCoins: selectedCoins, InputCoins: selectedCoins,
coinLocker: w.cfg.CoinLocker, coinLocker: w.cfg.CoinLocker,

View File

@ -2974,11 +2974,11 @@ func testSingleFunderExternalFundingTx(miner *rpctest.Harness,
thawHeight := uint32(200) thawHeight := uint32(200)
aliceExternalFunder := chanfunding.NewCannedAssembler( aliceExternalFunder := chanfunding.NewCannedAssembler(
thawHeight, *chanPoint, btcutil.Amount(chanAmt), &aliceFundingKey, thawHeight, *chanPoint, btcutil.Amount(chanAmt), &aliceFundingKey,
bobFundingKey.PubKey, true, bobFundingKey.PubKey, true, false,
) )
bobShimIntent, err := chanfunding.NewCannedAssembler( bobShimIntent, err := chanfunding.NewCannedAssembler(
thawHeight, *chanPoint, btcutil.Amount(chanAmt), &bobFundingKey, thawHeight, *chanPoint, btcutil.Amount(chanAmt), &bobFundingKey,
aliceFundingKey.PubKey, false, aliceFundingKey.PubKey, false, false,
).ProvisionChannel(&chanfunding.Request{ ).ProvisionChannel(&chanfunding.Request{
LocalAmt: btcutil.Amount(chanAmt), LocalAmt: btcutil.Amount(chanAmt),
MinConfs: 1, MinConfs: 1,

View File

@ -1819,10 +1819,12 @@ func newFundingShimAssembler(chanPointShim *lnrpc.ChanPointShim, initiator bool,
// With all the parts assembled, we can now make the canned assembler // With all the parts assembled, we can now make the canned assembler
// to pass into the wallet. // to pass into the wallet.
//
// TODO(roasbeef): update to support musig2
return chanfunding.NewCannedAssembler( return chanfunding.NewCannedAssembler(
chanPointShim.ThawHeight, *chanPoint, chanPointShim.ThawHeight, *chanPoint,
btcutil.Amount(chanPointShim.Amt), &localKeyDesc, btcutil.Amount(chanPointShim.Amt), &localKeyDesc,
remoteKey, initiator, remoteKey, initiator, false,
), nil ), nil
} }