diff --git a/wallet/addresses.go b/wallet/addresses.go index da622919e..7a350f223 100644 --- a/wallet/addresses.go +++ b/wallet/addresses.go @@ -504,7 +504,7 @@ func (s accountBranchChildUpdates) UpdateDB(ctx context.Context, w *Wallet, mayb // nextAddress returns the next address of an account branch. func (w *Wallet) nextAddress(ctx context.Context, op errors.Op, updates *accountBranchChildUpdates, maybeDBTX walletdb.ReadWriteTx, - accountName string, account, branch uint32, + account, branch uint32, callOpts ...NextAddressCallOption) (a stdaddr.Address, rerr error) { if updates != nil && maybeDBTX != nil { @@ -634,6 +634,18 @@ func (w *Wallet) nextAddress(ctx context.Context, op errors.Op, } else { return nil, errors.E(op, errors.Bug, "no method to update DB with addresses") } + + var accountName string + if maybeDBTX != nil { + addrmgrNs := maybeDBTX.ReadBucket(waddrmgrNamespaceKey) + accountName, err = w.manager.AccountName(addrmgrNs, account) + } else { + accountName, err = w.AccountName(ctx, account) + } + if err != nil { + return nil, errors.E(op, err) + } + alb.cursor++ addr := &xpubAddress{ AddressPubKeyHashEcdsaSecp256k1V0: apkh, @@ -753,10 +765,8 @@ func (w *Wallet) NewExternalAddress(ctx context.Context, account uint32, callOpt if err := w.notVotingAcct(ctx, op, account); err != nil { return nil, err } - - accountName, _ := w.AccountName(ctx, account) return w.nextAddress(ctx, op, nil, nil, - accountName, account, udb.ExternalBranch, callOpts...) + account, udb.ExternalBranch, callOpts...) } // NewInternalAddress returns an internal address. @@ -767,10 +777,8 @@ func (w *Wallet) NewInternalAddress(ctx context.Context, account uint32, callOpt if err := w.notVotingAcct(ctx, op, account); err != nil { return nil, err } - - accountName, _ := w.AccountName(ctx, account) return w.nextAddress(ctx, op, nil, nil, - accountName, account, udb.InternalBranch, callOpts...) + account, udb.InternalBranch, callOpts...) } // notVotingAcct errors if an account is a special voting type. This account @@ -795,7 +803,7 @@ func (w *Wallet) notVotingAcct(ctx context.Context, op errors.Op, account uint32 } func (w *Wallet) newChangeAddress(ctx context.Context, op errors.Op, updates *accountBranchChildUpdates, - maybeDBTX walletdb.ReadWriteTx, accountName string, account uint32, gap gapPolicy) (stdaddr.Address, error) { + maybeDBTX walletdb.ReadWriteTx, account uint32, gap gapPolicy) (stdaddr.Address, error) { // Imported voting accounts must not be used for change. if err := w.notVotingAcct(ctx, op, account); err != nil { @@ -809,7 +817,7 @@ func (w *Wallet) newChangeAddress(ctx context.Context, op errors.Op, updates *ac if account == udb.ImportedAddrAccount { account = udb.DefaultAccountNum } - return w.nextAddress(ctx, op, updates, maybeDBTX, accountName, account, udb.InternalBranch, withGapPolicy(gap)) + return w.nextAddress(ctx, op, updates, maybeDBTX, account, udb.InternalBranch, withGapPolicy(gap)) } // NewChangeAddress returns an internal address. This is identical to @@ -818,8 +826,7 @@ func (w *Wallet) newChangeAddress(ctx context.Context, op errors.Op, updates *ac // policy. func (w *Wallet) NewChangeAddress(ctx context.Context, account uint32) (stdaddr.Address, error) { const op errors.Op = "wallet.NewChangeAddress" - accountName, _ := w.AccountName(ctx, account) - return w.newChangeAddress(ctx, op, nil, nil, accountName, account, gapPolicyWrap) + return w.newChangeAddress(ctx, op, nil, nil, account, gapPolicyWrap) } // BIP0044BranchNextIndexes returns the next external and internal branch child @@ -951,9 +958,8 @@ type p2PKHChangeSource struct { } func (src *p2PKHChangeSource) Script() ([]byte, uint16, error) { - const accountName = "" // not returned, so can be faked. changeAddress, err := src.wallet.newChangeAddress(src.ctx, "", src.updates, - src.dbtx, accountName, src.account, src.gapPolicy) + src.dbtx, src.account, src.gapPolicy) if err != nil { return nil, 0, err } @@ -979,9 +985,8 @@ type p2PKHTreasuryChangeSource struct { // Script returns the treasury change script and is required for change source // interface. func (src *p2PKHTreasuryChangeSource) Script() ([]byte, uint16, error) { - const accountName = "" // not returned, so can be faked. changeAddress, err := src.wallet.newChangeAddress(src.ctx, "", src.updates, - src.dbtx, accountName, src.account, src.gapPolicy) + src.dbtx, src.account, src.gapPolicy) if err != nil { return nil, 0, err } diff --git a/wallet/createtx.go b/wallet/createtx.go index 995381572..6b8d15d49 100644 --- a/wallet/createtx.go +++ b/wallet/createtx.go @@ -807,9 +807,8 @@ func (w *Wallet) compressWalletInternal(ctx context.Context, op errors.Op, dbtx // Check if output address is default, and generate a new address if needed if changeAddr == nil { - const accountName = "" // not used, so can be faked. changeAddr, err = w.newChangeAddress(ctx, op, nil, dbtx, - accountName, account, gapPolicyIgnore) + account, gapPolicyIgnore) if err != nil { return nil, errors.E(op, err) } @@ -1175,8 +1174,7 @@ func (w *Wallet) purchaseTickets(ctx context.Context, op errors.Op, } stakeAddrFunc := func(op errors.Op, account, branch uint32) (stdaddr.StakeAddress, uint32, error) { - const accountName = "" // not used, so can be faked. - a, err := w.nextAddress(ctx, op, nil, nil, accountName, + a, err := w.nextAddress(ctx, op, nil, nil, account, branch, WithGapPolicyIgnore()) if err != nil { return nil, 0, err diff --git a/wallet/mixing.go b/wallet/mixing.go index aa8dc4c80..ad4c7f10c 100644 --- a/wallet/mixing.go +++ b/wallet/mixing.go @@ -56,9 +56,8 @@ func (w *Wallet) makeGen(ctx context.Context, account, branch uint32) mixclient. updates := make(accountBranchChildUpdates, 0, mcount) for i := range mcount { - const accountName = "" // not used, so can be faked. mixAddr, err := w.nextAddress(ctx, op, &updates, nil, - accountName, account, branch, WithGapPolicyIgnore()) + account, branch, WithGapPolicyIgnore()) if err != nil { return nil, err } @@ -392,9 +391,8 @@ SplitPoints: var change *wire.TxOut if changeValue > 0 { updates := make(accountBranchChildUpdates, 0, 1) - const accountName = "" // not used, so can be faked. addr, err := w.nextAddress(ctx, op, &updates, nil, - accountName, changeAccount, udb.InternalBranch, WithGapPolicyIgnore()) + changeAccount, udb.InternalBranch, WithGapPolicyIgnore()) if err != nil { return errors.E(op, err) }