Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,20 +128,22 @@ 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 {
if (this.hasNamedParametersSupport && parameters.length === 1 && _.isPlainObject(parameters[0])) {
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 });
}

Expand Down
78 changes: 78 additions & 0 deletions test/command_wallet_routing_test.js
Original file line number Diff line number Diff line change
@@ -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('/');
});
});