lnwallet+lnrpc: add derivation info for P2TR change addrs

In some situations (for example in Taproot Assets), we need to be able
to prove that an address is a bare BIP-0086 address that doesn't commit
to any script. We can do that by providing the BIP-0032 derivation info
and internal key.
This commit is contained in:
Oliver Gugger committed 2024-02-26 11:05:18 +01:00
1 parent 094fdbfa72
commit fb20cd598e
4 files changed
+250 -1

No files matched your search

+29 -1
View File
@@ -1758,6 +1758,34 @@ func (w *WalletKit) handleChange(packet *psbt.Packet, changeIndex int32,
return 0, fmt.Errorf("could not derive change script: %w", err)
}
// We need to add the derivation info for the change address in case it
// is a P2TR address. This is mostly to prove it's a bare BIP-0086
// address, which is required for some protocols (such as Taproot
// Assets).
pOut := psbt.POutput{}
_, isTaprootChangeAddr := changeAddr.(*btcutil.AddressTaproot)
if isTaprootChangeAddr {
changeAddrInfo, err := w.cfg.Wallet.AddressInfo(changeAddr)
if err != nil {
return 0, fmt.Errorf("could not get address info: %w",
err)
}
deriv, trDeriv, _, err := btcwallet.Bip32DerivationFromAddress(
changeAddrInfo,
)
if err != nil {
return 0, fmt.Errorf("could not get derivation info: "+
"%w", err)
}
pOut.TaprootInternalKey = trDeriv.XOnlyPubKey
pOut.Bip32Derivation = []*psbt.Bip32Derivation{deriv}
pOut.TaprootBip32Derivation = []*psbt.TaprootBip32Derivation{
trDeriv,
}
}
newChangeIndex := int32(len(packet.Outputs))
packet.UnsignedTx.TxOut = append(
packet.UnsignedTx.TxOut, &wire.TxOut{
@@ -1765,7 +1793,7 @@ func (w *WalletKit) handleChange(packet *psbt.Packet, changeIndex int32,
PkScript: changeScript,
},
)
packet.Outputs = append(packet.Outputs, psbt.POutput{})
packet.Outputs = append(packet.Outputs, pOut)
return newChangeIndex, nil
}