mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-26 17:26:23 +02:00
walletrpc+lnwallet: possibility to define an address type for changes in FundPsbt
By default, P2TR addresses are used for changes. However, some users might encounter some problems with this change. We add the possibility to define a custom address type in FundPsbt for default/imported accounts (only P2TR for now). If no address type is specified for these accounts, we will use P2WKH by default.
This commit is contained in:
@@ -36,7 +36,11 @@ var (
|
||||
// fund the outputs specified in the passed in packet with the specified fee
|
||||
// rate. If there is change left, a change output from the internal wallet is
|
||||
// added and the index of the change output is returned. Otherwise no additional
|
||||
// output is created and the index -1 is returned.
|
||||
// output is created and the index -1 is returned. If no custom change
|
||||
// scope is specified, the BIP0084 will be used for default accounts and single
|
||||
// imported public keys. For custom account, no key scope should be provided
|
||||
// as the coin selection key scope will always be used to generate the change
|
||||
// address.
|
||||
//
|
||||
// NOTE: If the packet doesn't contain any inputs, coin selection is performed
|
||||
// automatically. The account parameter must be non-empty as it determines which
|
||||
@@ -49,7 +53,8 @@ var (
|
||||
//
|
||||
// This is a part of the WalletController interface.
|
||||
func (b *BtcWallet) FundPsbt(packet *psbt.Packet, minConfs int32,
|
||||
feeRate chainfee.SatPerKWeight, accountName string) (int32, error) {
|
||||
feeRate chainfee.SatPerKWeight, accountName string,
|
||||
changeScope *waddrmgr.KeyScope) (int32, error) {
|
||||
|
||||
// The fee rate is passed in using units of sat/kw, so we'll convert
|
||||
// this to sat/KB as the CreateSimpleTx method requires this unit.
|
||||
@@ -59,32 +64,58 @@ func (b *BtcWallet) FundPsbt(packet *psbt.Packet, minConfs int32,
|
||||
keyScope *waddrmgr.KeyScope
|
||||
accountNum uint32
|
||||
)
|
||||
|
||||
switch accountName {
|
||||
// If the default/imported account name was specified, we'll provide a
|
||||
// nil key scope to FundPsbt, allowing it to select inputs from both key
|
||||
// scopes (NP2WKH, P2WKH).
|
||||
// For default accounts and single imported public keys, we'll provide a
|
||||
// nil key scope to FundPsbt, allowing it to select nputs from all
|
||||
// scopes (NP2WKH, P2WKH, P2TR). By default, the change key scope for
|
||||
// these accounts will be P2WKH.
|
||||
case lnwallet.DefaultAccountName:
|
||||
if changeScope == nil {
|
||||
changeScope = &waddrmgr.KeyScopeBIP0084
|
||||
}
|
||||
|
||||
accountNum = defaultAccount
|
||||
|
||||
case waddrmgr.ImportedAddrAccountName:
|
||||
if changeScope == nil {
|
||||
changeScope = &waddrmgr.KeyScopeBIP0084
|
||||
}
|
||||
|
||||
accountNum = importedAccount
|
||||
|
||||
// Otherwise, map the account name to its key scope and internal account
|
||||
// number to only select inputs from said account.
|
||||
// number to only select inputs from said account. No change key scope
|
||||
// should have been specified as a custom account should only have one
|
||||
// key scope. Providing a change key scope would break this assumption
|
||||
// and lead to non-deterministic behavior by using a different change
|
||||
// key scope than the custom account key scope. The change key scope
|
||||
// will always be the same as the coin selection.
|
||||
default:
|
||||
if changeScope != nil {
|
||||
return 0, fmt.Errorf("couldn't select a " +
|
||||
"custom change type for custom accounts")
|
||||
}
|
||||
|
||||
scope, account, err := b.wallet.LookupAccount(accountName)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
keyScope = &scope
|
||||
changeScope = keyScope
|
||||
accountNum = account
|
||||
}
|
||||
|
||||
var opts []wallet.TxCreateOption
|
||||
if changeScope != nil {
|
||||
opts = append(opts, wallet.WithCustomChangeScope(changeScope))
|
||||
}
|
||||
|
||||
// Let the wallet handle coin selection and/or fee estimation based on
|
||||
// the partial TX information in the packet.
|
||||
return b.wallet.FundPsbt(
|
||||
packet, keyScope, minConfs, accountNum, feeSatPerKB,
|
||||
b.cfg.CoinSelectionStrategy,
|
||||
b.cfg.CoinSelectionStrategy, opts...,
|
||||
)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user