-
Notifications
You must be signed in to change notification settings - Fork 808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Get account by index instead of name #1135
base: master
Are you sure you want to change the base?
Changes from all commits
07a8d06
28d8913
1ce94a4
02a39ca
14b21b1
15bcc33
f518d7e
01e3b0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The account index is supposed to be a Number, not a String. |
||
* @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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need JSDocs here. |
||
return this.client.getAccountIndex(this.id, account); | ||
} | ||
|
||
/** | ||
* Create account. | ||
* @param {String} name | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You will have to create a new method in the |
||
|
||
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); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem right @pinheadmz could you please take a look. |
||
|
||
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 []; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please specify that this method gets account by name.