Skip to content

Commit

Permalink
[please amend] fix the approach
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobranco committed Jul 20, 2022
1 parent 18cea60 commit 2564d0c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
16 changes: 12 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const promisify = fn => (...args) => new Promise((resolve, reject) => {
class Client {
constructor({
agentOptions,
allowDefaultWallet = false,
headers = false,
host = 'localhost',
logger = debugnyan('bitcoin-core'),
Expand All @@ -54,14 +55,14 @@ class Client {
timeout = 30000,
username,
version,
wallet,
useWalletURL = false,
wallet
} = {}) {
if (!_.has(networks, network)) {
throw new Error(`Invalid network name "${network}"`, { network });
}

this.agentOptions = agentOptions;
this.allowDefaultWallet = allowDefaultWallet;
this.auth = (password || username) && { pass: password, user: username };
this.hasNamedParametersSupport = false;
this.headers = headers;
Expand All @@ -74,7 +75,6 @@ class Client {
};
this.timeout = timeout;
this.wallet = wallet;
this.useWalletURL = useWalletURL

// Version handling.
if (version) {
Expand Down Expand Up @@ -146,10 +146,18 @@ class Client {
body = this.requester.prepare({ method: input, parameters });
}

let uri = '/';

if (multiwallet && this.wallet) {
uri = `/wallet/${this.wallet}`;
} else if (multiwallet && !this.wallet && this.allowDefaultWallet) {
uri = '/wallet/';
}

return this.parser.rpc(await this.request.postAsync({
auth: _.pickBy(this.auth, _.identity),
body: JSON.stringify(body),
uri: `${(this.useWalletURL || (multiwallet && this.wallet)) ? `/wallet/${this.wallet}` : '/'}`
uri
}));
}

Expand Down
21 changes: 12 additions & 9 deletions test/multi_wallet_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { defaults } from 'lodash';
import Client from '../src/index';
import RpcError from '../src/errors/rpc-error';
import config from './config';
import should from 'should';

/**
* Test instance.
Expand Down Expand Up @@ -245,24 +246,26 @@ describe('Multi Wallet', () => {
});

describe('default wallet', () => {
it('should return balance for the default wallet with multiple wallets loaded', async () => {
const client = new Client(defaults({ version: '0.17.0', wallet: 'wallet1', useWalletURL: true }, config.bitcoinMultiWallet));
it('should return the balance for the default wallet with multiple wallets loaded if `allowDefaultWallet` is true', async () => {
const client = new Client(defaults({ allowDefaultWallet: true, version: '0.17.0' }, config.bitcoinMultiWallet));

const balance = await client.getBalance();

balance.should.be.aboveOrEqual(0);
})
});

it('should fail getting balance for default wallet without useWalletURL param', async () => {
const client = new Client(defaults({ version: '0.17.0', wallet: 'wallet1' }, config.bitcoinMultiWallet));
it('should fail getting balance for default wallet with `allowDefaultWallet` as `false`', async () => {
const client = new Client(defaults({ version: '0.17.0' }, config.bitcoinMultiWallet));

try {
await client.getBalance()
await client.getBalance();

should.fail();
} catch (error) {
error.should.be.an.instanceOf(RpcError);
error.code.should.be.equal(-19);
error.message.should.containEql('Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).')
error.message.should.containEql('Wallet file not specified (must request wallet RPC through /wallet/<filename> uri-path).');
}
})
})
});
});
});

0 comments on commit 2564d0c

Please sign in to comment.