diff --git a/bin/bwallet-cli b/bin/bwallet-cli index 14edc92e3..784d55e25 100755 --- a/bin/bwallet-cli +++ b/bin/bwallet-cli @@ -152,7 +152,12 @@ class CLI { async getAccount() { const acct = this.config.str([0, 'account']); const account = await this.wallet.getAccount(acct); + this.log(account); + } + async getAccountByIndex() { + const index = this.config.uint([0, 'accountIndex']); + const account = await this.wallet.getAccountIndex(index); this.log(account); } @@ -542,6 +547,11 @@ class CLI { this.argv.shift(); await this.getAccount(); break; + case 'accountIndex': + if (this.argv[0] === 'get') + this.argv.shift(); + await this.getAccountByIndex(); + break; case 'address': await this.createAddress(); break; @@ -608,6 +618,7 @@ class CLI { this.log(' $ abandon [hash]: Abandon a transaction.'); this.log(' $ account create [account-name]: Create account.'); this.log(' $ account get [account-name]: Get account details.'); + this.log(' $ accountIndex get [account-index]: Get account details by index.'); this.log(' $ account list: List account names.'); this.log(' $ address: Derive new address.'); this.log(' $ admin [command]: Admin commands'); diff --git a/lib/client/wallet.js b/lib/client/wallet.js index 812386e26..325c210c3 100644 --- a/lib/client/wallet.js +++ b/lib/client/wallet.js @@ -432,6 +432,17 @@ class WalletClient extends Client { return this.get(`/wallet/${id}/account/${account}`); } + /** + * Get wallet account. + * @param {Number} id + * @param {Number|String} accountIndex + * @returns {Promise} + */ + + getAccountIndex(id, accountIndex) { + return this.get(`/wallet/${id}/account/id/${accountIndex}`); + } + /** * Create account. * @param {Number} id @@ -901,6 +912,10 @@ class Wallet extends EventEmitter { return this.client.getAccount(this.id, account); } + getAccountIndex(account) { + return this.client.getAccountIndex(this.id, account); + } + /** * Create account. * @param {String} name diff --git a/lib/wallet/http.js b/lib/wallet/http.js index b3ec7d89a..13e9136a6 100644 --- a/lib/wallet/http.js +++ b/lib/wallet/http.js @@ -319,6 +319,7 @@ class HTTP extends Server { this.get('/wallet/:id/account/:account', async (req, res) => { const valid = Validator.fromRequest(req); const acct = valid.str('account'); + const account = await req.wallet.getAccount(acct); if (!account) { @@ -331,6 +332,28 @@ class HTTP extends Server { res.json(200, account.toJSON(balance)); }); + this.get('/wallet/:id/account/id/:accountIndex', async (req, res) => { + const valid = Validator.fromRequest(req); + let acct = valid.str('accountIndex'); + + const num = parseInt(acct); + + if (!Number.isNaN(num)) { + acct = num; + } + + const account = await req.wallet.getAccount(acct); + + if (!account) { + res.json(404); + return; + } + + const balance = await req.wallet.getBalance(acct); + + res.json(200, account.toJSON(balance)); + }); + // Create account this.put('/wallet/:id/account/:account', async (req, res) => { const valid = Validator.fromRequest(req); diff --git a/lib/wallet/rpc.js b/lib/wallet/rpc.js index 72e171377..bf54bdc80 100644 --- a/lib/wallet/rpc.js +++ b/lib/wallet/rpc.js @@ -134,6 +134,7 @@ class RPC extends RPCBase { this.add('getaccountaddress', this.getAccountAddress); this.add('getaccount', this.getAccount); this.add('getaddressesbyaccount', this.getAddressesByAccount); + this.add('getaddressesbyaccountindex', this.getAddressesByAccountIndex); this.add('getbalance', this.getBalance); this.add('getnewaddress', this.getNewAddress); this.add('getrawchangeaddress', this.getRawChangeAddress); @@ -443,15 +444,48 @@ class RPC extends RPCBase { const wallet = this.wallet; const valid = new Validator(args); - let name = valid.str(0, ''); const addrs = []; - if (name === '') - name = 'default'; + let acct = valid.get(0, 'default'); + + const num = parseInt(acct); + let accountName = ''; + + if (!Number.isNaN(num)) { + accountName = accountName + num; + acct = accountName; + } + + let paths; + try { + paths = await wallet.getPaths(acct); + } catch (e) { + if (e.message === 'Account not found.') + return []; + throw e; + } + + for (const path of paths) { + const addr = path.toAddress(); + addrs.push(addr.toString(this.network)); + } + + return addrs; + } + + async getAddressesByAccountIndex(args, help) { + if (help || args.length !== 1) + throw new RPCError(errs.MISC_ERROR, 'getaddressesbyaccountindex "index"'); + + const wallet = this.wallet; + const valid = new Validator(args); + const addrs = []; + + const acct = valid.uint(0, 'default'); let paths; try { - paths = await wallet.getPaths(name); + paths = await wallet.getPathsByIndex(acct); } catch (e) { if (e.message === 'Account not found.') return []; diff --git a/lib/wallet/wallet.js b/lib/wallet/wallet.js index 3703deaf9..a10e7f046 100644 --- a/lib/wallet/wallet.js +++ b/lib/wallet/wallet.js @@ -923,6 +923,10 @@ class Wallet extends EventEmitter { return this.wdb.getWalletPaths(this.wid); } + async getPathsByIndex(index) { + return this.getAccountPathsByIndex(index); + } + /** * Get all account paths. * @param {String|Number} acct @@ -938,7 +942,9 @@ class Wallet extends EventEmitter { const hashes = await this.getAccountHashes(index); const name = await this.getAccountName(acct); - assert(name); + if (name == null) { + throw new Error('Account not found.'); + } const result = []; @@ -956,6 +962,25 @@ class Wallet extends EventEmitter { return result; } + async getAccountPathsByIndex(index) { + if (index === -1) + throw new Error('Account not found.'); + const hashes = await this.getAccountHashes(index); + + const result = []; + + for (const hash of hashes) { + const path = await this.readPath(hash); + + assert(path); + assert(path.account === index); + + result.push(path); + } + + return result; + } + /** * Import a keyring (will not exist on derivation chain). * Rescanning must be invoked manually.