mirror of
https://github.com/LBRYFoundation/lbcwallet.git
synced 2025-08-23 17:47:29 +00:00
multi-account: update getaddressbyaccount
This commit is contained in:
parent
a02435bbf7
commit
6a610a8cdf
3 changed files with 41 additions and 12 deletions
|
@ -394,16 +394,25 @@ func dumpPrivKey(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
|
||||||
|
|
||||||
// getAddressesByAccount handles a getaddressesbyaccount request by returning
|
// getAddressesByAccount handles a getaddressesbyaccount request by returning
|
||||||
// all addresses for an account, or an error if the requested account does
|
// all addresses for an account, or an error if the requested account does
|
||||||
// not exist.
|
// not exist. If addresstype is also specified, only those address types are
|
||||||
func getAddressesByAccount(icmd interface{}, w *wallet.Wallet) (interface{}, error) {
|
// returned.
|
||||||
|
func getAddressesByAccount(icmd interface{}, w *wallet.Wallet) (
|
||||||
|
interface{}, error) {
|
||||||
|
|
||||||
cmd := icmd.(*btcjson.GetAddressesByAccountCmd)
|
cmd := icmd.(*btcjson.GetAddressesByAccountCmd)
|
||||||
|
|
||||||
account, err := w.AccountNumber(waddrmgr.KeyScopeBIP0044, cmd.Account)
|
account, err := w.AccountNumber(*cmd.Account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
addrs, err := w.AccountAddresses(account)
|
// Use specified scope, if provided.
|
||||||
|
scope, err := lookupKeyScope(cmd.AddressType)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
addrs, err := w.AccountAddresses(account, scope)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -412,6 +421,7 @@ func getAddressesByAccount(icmd interface{}, w *wallet.Wallet) (interface{}, err
|
||||||
for i, a := range addrs {
|
for i, a := range addrs {
|
||||||
addrStrs[i] = a.EncodeAddress()
|
addrStrs[i] = a.EncodeAddress()
|
||||||
}
|
}
|
||||||
|
|
||||||
return addrStrs, nil
|
return addrStrs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,7 +83,7 @@ func TestTxToOutputsDryRun(t *testing.T) {
|
||||||
}
|
}
|
||||||
change := dryRunTx.Tx.TxOut[dryRunTx.ChangeIndex]
|
change := dryRunTx.Tx.TxOut[dryRunTx.ChangeIndex]
|
||||||
|
|
||||||
addresses, err := w.AccountAddresses(0)
|
addresses, err := w.AccountAddresses(0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to get addresses: %v", err)
|
t.Fatalf("unable to get addresses: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,7 @@ func TestTxToOutputsDryRun(t *testing.T) {
|
||||||
}
|
}
|
||||||
change2 := dryRunTx2.Tx.TxOut[dryRunTx2.ChangeIndex]
|
change2 := dryRunTx2.Tx.TxOut[dryRunTx2.ChangeIndex]
|
||||||
|
|
||||||
addresses, err = w.AccountAddresses(0)
|
addresses, err = w.AccountAddresses(0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to get addresses: %v", err)
|
t.Fatalf("unable to get addresses: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -135,7 +135,7 @@ func TestTxToOutputsDryRun(t *testing.T) {
|
||||||
}
|
}
|
||||||
change3 := tx.Tx.TxOut[tx.ChangeIndex]
|
change3 := tx.Tx.TxOut[tx.ChangeIndex]
|
||||||
|
|
||||||
addresses, err = w.AccountAddresses(0)
|
addresses, err = w.AccountAddresses(0, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("unable to get addresses: %v", err)
|
t.Fatalf("unable to get addresses: %v", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1407,13 +1407,32 @@ func (w *Wallet) ChangePassphrases(publicOld, publicNew, privateOld,
|
||||||
|
|
||||||
// AccountAddresses returns the addresses for every created address for an
|
// AccountAddresses returns the addresses for every created address for an
|
||||||
// account.
|
// account.
|
||||||
func (w *Wallet) AccountAddresses(account uint32) (addrs []btcutil.Address, err error) {
|
func (w *Wallet) AccountAddresses(account uint32, scope *waddrmgr.KeyScope) (
|
||||||
|
addrs []btcutil.Address, err error) {
|
||||||
|
|
||||||
|
// By default, append all addresses under this account.
|
||||||
|
fn := func(maddr waddrmgr.ManagedAddress) error {
|
||||||
|
addrs = append(addrs, maddr.Address())
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// If scope is set, append only those have the address type under
|
||||||
|
// this scope.
|
||||||
|
if scope != nil {
|
||||||
|
addrSchema := waddrmgr.ScopeAddrMap[*scope]
|
||||||
|
|
||||||
|
fn = func(maddr waddrmgr.ManagedAddress) error {
|
||||||
|
if maddr.AddrType() == addrSchema.InternalAddrType ||
|
||||||
|
maddr.AddrType() == addrSchema.ExternalAddrType {
|
||||||
|
addrs = append(addrs, maddr.Address())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
|
err = walletdb.View(w.db, func(tx walletdb.ReadTx) error {
|
||||||
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
|
addrmgrNs := tx.ReadBucket(waddrmgrNamespaceKey)
|
||||||
return w.Manager.ForEachAccountAddress(addrmgrNs, account, func(maddr waddrmgr.ManagedAddress) error {
|
return w.Manager.ForEachAccountAddress(addrmgrNs, account, fn)
|
||||||
addrs = append(addrs, maddr.Address())
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue