Skip to content

Commit 17589aa

Browse files
rociiukaralabe
authored andcommitted
accounts, internal/ethapi: use common Accounts method (#18428)
* accounts/mananger, internal/ethapi/api: Add new function AllAccounts on account manager to remove the duplication code on getting all wallets accounts * Rename to Accounts * Rename to AllAccounts
1 parent 3e993ff commit 17589aa

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

accounts/manager.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sort"
2222
"sync"
2323

24+
"github.com/ethereum/go-ethereum/common"
2425
"github.com/ethereum/go-ethereum/event"
2526
)
2627

@@ -162,6 +163,20 @@ func (am *Manager) Wallet(url string) (Wallet, error) {
162163
return nil, ErrUnknownWallet
163164
}
164165

166+
// Accounts returns all account addresses of all wallets within the account manager
167+
func (am *Manager) Accounts() []common.Address {
168+
am.lock.RLock()
169+
defer am.lock.RUnlock()
170+
171+
addresses := make([]common.Address, 0) // return [] instead of nil if empty
172+
for _, wallet := range am.wallets {
173+
for _, account := range wallet.Accounts() {
174+
addresses = append(addresses, account.Address)
175+
}
176+
}
177+
return addresses
178+
}
179+
165180
// Find attempts to locate the wallet corresponding to a specific account. Since
166181
// accounts can be dynamically added to and removed from wallets, this method has
167182
// a linear runtime in the number of wallets.

internal/ethapi/api.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,7 @@ func NewPublicAccountAPI(am *accounts.Manager) *PublicAccountAPI {
191191

192192
// Accounts returns the collection of accounts this node manages
193193
func (s *PublicAccountAPI) Accounts() []common.Address {
194-
addresses := make([]common.Address, 0) // return [] instead of nil if empty
195-
for _, wallet := range s.am.Wallets() {
196-
for _, account := range wallet.Accounts() {
197-
addresses = append(addresses, account.Address)
198-
}
199-
}
200-
return addresses
194+
return s.am.Accounts()
201195
}
202196

203197
// PrivateAccountAPI provides an API to access accounts managed by this node.
@@ -220,13 +214,7 @@ func NewPrivateAccountAPI(b Backend, nonceLock *AddrLocker) *PrivateAccountAPI {
220214

221215
// listAccounts will return a list of addresses for accounts this node manages.
222216
func (s *PrivateAccountAPI) ListAccounts() []common.Address {
223-
addresses := make([]common.Address, 0) // return [] instead of nil if empty
224-
for _, wallet := range s.am.Wallets() {
225-
for _, account := range wallet.Accounts() {
226-
addresses = append(addresses, account.Address)
227-
}
228-
}
229-
return addresses
217+
return s.am.Accounts()
230218
}
231219

232220
// rawWallet is a JSON representation of an accounts.Wallet interface, with its

0 commit comments

Comments
 (0)