diff --git a/src/index.js b/src/index.js index 827b766..bbf6aa0 100644 --- a/src/index.js +++ b/src/index.js @@ -128,12 +128,14 @@ class Client { if (isBatch) { multiwallet = _.some(input, command => { - return _.get(this.methods[command.method], 'features.multiwallet.supported', false) === true; + const method = _.toLower(command.method); + + return _.get(this.methods[method], 'features.multiwallet.supported', false) === true; }); - body = input.map((method, index) => this.requester.prepare({ - method: method.method, - parameters: method.parameters, + body = input.map((command, index) => this.requester.prepare({ + method: command.method, + parameters: command.parameters, suffix: index })); } else { @@ -141,7 +143,7 @@ class Client { parameters = parameters[0]; } - multiwallet = _.get(this.methods[input], 'features.multiwallet.supported', false) === true; + multiwallet = _.get(this.methods[_.toLower(input)], 'features.multiwallet.supported', false) === true; body = this.requester.prepare({ method: input, parameters }); } diff --git a/test/command_wallet_routing_test.js b/test/command_wallet_routing_test.js new file mode 100644 index 0000000..266c6d6 --- /dev/null +++ b/test/command_wallet_routing_test.js @@ -0,0 +1,78 @@ + +/** + * Module dependencies. + */ + +const Client = require('../src/index'); +const should = require('should'); + +/** + * Test `Client#command` wallet routing. + */ + +describe('Client#command wallet routing', () => { + function createClient() { + const client = new Client({ + host: 'localhost', + password: 'bar', + port: 18453, + username: 'foo', + version: '0.17.0', + wallet: 'wallet.dat' + }); + + client.request.postAsync = request => { + client.lastRequest = request; + + const isBatch = Array.isArray(JSON.parse(request.body)); + const body = isBatch ? [{ error: null, id: '1-0', result: 0 }] : { error: null, id: '1', result: 0 }; + + return { + body: JSON.stringify(body), + headers: { 'content-type': 'application/json' }, + statusCode: 200 + }; + }; + + return client; + } + + it('routes mixed-case single wallet commands through the selected wallet', async () => { + const client = createClient(); + + const result = await client.command('getBalance'); + + should(result).equal(0); + should(client.lastRequest.uri).equal('/wallet/wallet.dat'); + }); + + it('routes mixed-case batch wallet commands through the selected wallet', async () => { + const client = createClient(); + + const result = await client.command([{ method: 'getBalance', parameters: [] }]); + + should(result).deepEqual([0]); + should(client.lastRequest.uri).equal('/wallet/wallet.dat'); + }); + + it('routes mixed-case batch commands through the selected wallet when any entry is wallet-scoped', async () => { + const client = createClient(); + + const result = await client.command([ + { method: 'getBlockHash', parameters: [0] }, + { method: 'getBalance', parameters: [] } + ]); + + should(result).deepEqual([0]); + should(client.lastRequest.uri).equal('/wallet/wallet.dat'); + }); + + it('does not route mixed-case non-wallet commands through the selected wallet', async () => { + const client = createClient(); + + const result = await client.command('getBlockHash', 0); + + should(result).equal(0); + should(client.lastRequest.uri).equal('/'); + }); +});