Skip to content

Commit b0f643d

Browse files
committed
lnwallet: update GenTaprootFundingScript to also return the taproot internal key
1 parent c98aeaa commit b0f643d

File tree

8 files changed

+21
-15
lines changed

8 files changed

+21
-15
lines changed

contractcourt/chain_watcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func (c *chainWatcher) Start() error {
308308
fundingOpts := fn.MapOptionZ(
309309
chanState.TapscriptRoot, lnwallet.TapscriptRootToOpt,
310310
)
311-
c.fundingPkScript, _, err = input.GenTaprootFundingScript(
311+
c.fundingPkScript, _, _, err = input.GenTaprootFundingScript(
312312
localKey, remoteKey, 0, fundingOpts...,
313313
)
314314
if err != nil {

funding/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2881,7 +2881,7 @@ func makeFundingScript(channel *channeldb.OpenChannel) ([]byte, error) {
28812881
fundingOpts := fn.MapOptionZ(
28822882
channel.TapscriptRoot, lnwallet.TapscriptRootToOpt,
28832883
)
2884-
pkScript, _, err := input.GenTaprootFundingScript(
2884+
pkScript, _, _, err := input.GenTaprootFundingScript(
28852885
localKey, remoteKey, int64(channel.Capacity),
28862886
fundingOpts...,
28872887
)

input/script_utils.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,12 @@ func WithTapscriptRoot(root chainhash.Hash) FundingScriptOpt {
224224
}
225225

226226
// GenTaprootFundingScript constructs the taproot-native funding output that
227-
// uses musig2 to create a single aggregated key to anchor the channel.
228-
func GenTaprootFundingScript(aPub, bPub *btcec.PublicKey,
229-
amt int64, opts ...FundingScriptOpt) ([]byte, *wire.TxOut, error) {
227+
// uses musig2 to create a single aggregated key to anchor the channel. This
228+
// also returns the musig2 aggregated key to allow the callers to examine the
229+
// pre tweaked key as well as the final combined key.
230+
func GenTaprootFundingScript(aPub, bPub *btcec.PublicKey, amt int64,
231+
opts ...FundingScriptOpt) ([]byte, *wire.TxOut,
232+
*musig2.AggregateKey, error) {
230233

231234
options := defaultFundingScriptOpts()
232235
for _, optFunc := range opts {
@@ -248,22 +251,23 @@ func GenTaprootFundingScript(aPub, bPub *btcec.PublicKey,
248251
[]*btcec.PublicKey{aPub, bPub}, true, musig2Opts,
249252
)
250253
if err != nil {
251-
return nil, nil, fmt.Errorf("unable to combine keys: %w", err)
254+
return nil, nil, nil, fmt.Errorf("unable to combine "+
255+
"keys: %w", err)
252256
}
253257

254258
// Now that we have the combined key, we can create a taproot pkScript
255259
// from this, and then make the txout given the amount.
256260
pkScript, err := PayToTaprootScript(combinedKey.FinalKey)
257261
if err != nil {
258-
return nil, nil, fmt.Errorf("unable to make taproot "+
262+
return nil, nil, nil, fmt.Errorf("unable to make taproot "+
259263
"pkscript: %w", err)
260264
}
261265

262266
txOut := wire.NewTxOut(amt, pkScript)
263267

264268
// For the "witness program" we just return the raw pkScript since the
265269
// output we create can _only_ be spent with a musig2 signature.
266-
return pkScript, txOut, nil
270+
return pkScript, txOut, combinedKey, nil
267271
}
268272

269273
// SpendMultiSig generates the witness stack required to redeem the 2-of-2 p2wsh

itest/lnd_funding_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,7 @@ func deriveFundingShim(ht *lntest.HarnessTest, carol, dave *node.HarnessNode,
11631163
daveKey, err = btcec.ParsePubKey(daveFundingKey.RawKeyBytes)
11641164
require.NoError(ht, err)
11651165

1166-
_, fundingOutput, err = input.GenTaprootFundingScript(
1166+
_, fundingOutput, _, err = input.GenTaprootFundingScript(
11671167
carolKey, daveKey, int64(chanSize),
11681168
)
11691169
require.NoError(ht, err)

lnwallet/chanfunding/canned_assembler.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,12 @@ func (s *ShimIntent) FundingOutput() ([]byte, *wire.TxOut, error) {
9292

9393
// Similar to the existing p2wsh script, we'll always ensure
9494
// the keys are sorted before use.
95-
return input.GenTaprootFundingScript(
95+
pkScript, txOut, _, err := input.GenTaprootFundingScript(
9696
s.localKey.PubKey, s.remoteKey, int64(totalAmt),
9797
scriptOpts...,
9898
)
99+
100+
return pkScript, txOut, err
99101
}
100102

101103
return input.GenFundingPkScript(

lnwallet/channel.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,7 @@ func (lc *LightningChannel) createSignDesc() error {
15371537
chanState.TapscriptRoot, TapscriptRootToOpt,
15381538
)
15391539

1540-
fundingPkScript, _, err = input.GenTaprootFundingScript(
1540+
fundingPkScript, _, _, err = input.GenTaprootFundingScript(
15411541
localKey, remoteKey, int64(lc.channelState.Capacity),
15421542
fundingOpts...,
15431543
)

lnwallet/wallet.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2151,7 +2151,7 @@ func (l *LightningWallet) verifyCommitSig(res *ChannelReservation,
21512151
TapscriptRootToOpt,
21522152
)
21532153

2154-
_, fundingOutput, err := input.GenTaprootFundingScript(
2154+
_, fundingOutput, _, err := input.GenTaprootFundingScript(
21552155
localKey, remoteKey, channelValue,
21562156
fundingOpts...,
21572157
)
@@ -2398,7 +2398,7 @@ func (l *LightningWallet) handleSingleFunderSigs(req *addSingleFunderSigsMsg) {
23982398
pendingReservation.partialState.TapscriptRoot,
23992399
TapscriptRootToOpt,
24002400
)
2401-
fundingWitnessScript, fundingTxOut, err = input.GenTaprootFundingScript( //nolint:lll
2401+
fundingWitnessScript, fundingTxOut, _, err = input.GenTaprootFundingScript( //nolint:lll
24022402
ourKey.PubKey, theirKey.PubKey, channelValue,
24032403
fundingOpts...,
24042404
)
@@ -2556,7 +2556,7 @@ func (l *LightningWallet) ValidateChannel(channelState *channeldb.OpenChannel,
25562556
channelState.TapscriptRoot, TapscriptRootToOpt,
25572557
)
25582558

2559-
fundingScript, _, err = input.GenTaprootFundingScript(
2559+
fundingScript, _, _, err = input.GenTaprootFundingScript(
25602560
localKey, remoteKey, int64(channel.Capacity),
25612561
fundingOpts...,
25622562
)

routing/router.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@ func makeFundingScript(bitcoinKey1, bitcoinKey2 []byte,
15421542
return nil, err
15431543
}
15441544

1545-
fundingScript, _, err := input.GenTaprootFundingScript(
1545+
fundingScript, _, _, err := input.GenTaprootFundingScript(
15461546
pubKey1, pubKey2, 0,
15471547
)
15481548
if err != nil {

0 commit comments

Comments
 (0)