Skip to content
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

Added support for default wallet functions in multiwallet environment #117

Merged
merged 1 commit into from
Jul 21, 2022
Merged
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
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
-rest
-rpcallowip=::/0
-rpcpassword=bar
-wallet
-wallet=wallet1
-wallet=wallet2
-rpcport=18443
Expand Down
12 changes: 11 additions & 1 deletion 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 @@ -61,6 +62,7 @@ class Client {
}

this.agentOptions = agentOptions;
this.allowDefaultWallet = allowDefaultWallet;
this.auth = (password || username) && { pass: password, user: username };
this.hasNamedParametersSupport = false;
this.headers = headers;
Expand Down Expand Up @@ -144,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: `${multiwallet && this.wallet ? `/wallet/${this.wallet}` : '/'}`
uri
}));
}

Expand Down
31 changes: 28 additions & 3 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 @@ -50,7 +51,7 @@ describe('Multi Wallet', () => {
it('should return a list of currently loaded wallets', async () => {
const wallets = await client.listWallets();

wallets.should.eql(['wallet1', 'wallet2']);
wallets.should.eql(['', 'wallet1', 'wallet2']);
});
});
});
Expand Down Expand Up @@ -230,17 +231,41 @@ describe('Multi Wallet', () => {

const response = await client.command(batch);

response.should.eql([0, ['wallet1', 'wallet2'], ['wallet1', 'wallet2']]);
response.should.eql([0, ['', 'wallet1', 'wallet2'], ['', 'wallet1', 'wallet2']]);
});

it('should return an error if one of the request fails', async () => {
const batch = [{ method: 'validateaddress' }, { method: 'listwallets' }];

const [validateAddressError, listWallets] = await client.command(batch);

listWallets.should.eql(['wallet1', 'wallet2']);
listWallets.should.eql(['', 'wallet1', 'wallet2']);
validateAddressError.should.be.an.instanceOf(RpcError);
validateAddressError.code.should.equal(-1);
});
});

describe('default wallet', () => {
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 with `allowDefaultWallet` as `false`', async () => {
const client = new Client(defaults({ version: '0.17.0' }, config.bitcoinMultiWallet));

try {
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).');
}
});
});
});