Skip to content

Commit

Permalink
fix(bitcoind): create default wallet when starting bitcoind
Browse files Browse the repository at this point in the history
  • Loading branch information
jamaljsr committed Jan 27, 2021
1 parent f59dd1b commit 548a138
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/lib/bitcoin/bitcoindService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,28 @@ describe('BitcoindService', () => {

beforeEach(() => {
// update the prototype of new classes to specify the return values
mockProto.listWallets = jest.fn().mockResolvedValue(['']);
mockProto.createWallet = jest.fn().mockResolvedValue({ name: '' });
mockProto.getBlockchainInfo = jest.fn().mockResolvedValue({ blocks: 10 });
mockProto.getWalletInfo = jest.fn().mockResolvedValue({ balance: 5 });
mockProto.getNewAddress = jest.fn().mockResolvedValue('abcdef');
mockProto.sendToAddress = jest.fn().mockResolvedValue('txid');
mockProto.generateToAddress = jest.fn().mockResolvedValue(['blockhash1']);
});

it('should create a default wallet', async () => {
mockProto.listWallets = jest.fn().mockResolvedValue([]);
await bitcoindService.createDefaultWallet(node);
expect(getInst().listWallets).toBeCalledTimes(1);
expect(getInst().createWallet).toBeCalledTimes(1);
});

it('should not create a default wallet', async () => {
await bitcoindService.createDefaultWallet(node);
expect(getInst().listWallets).toBeCalledTimes(1);
expect(getInst().createWallet).toBeCalledTimes(0);
});

it('should get blockchain info', async () => {
const info = await bitcoindService.getBlockchainInfo(node);
expect(getInst().getBlockchainInfo).toBeCalledTimes(1);
Expand Down
8 changes: 8 additions & 0 deletions src/lib/bitcoin/bitcoindService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class BitcoindService implements BitcoindLibrary {
});
}

async createDefaultWallet(node: BitcoinNode) {
const client = this.creatClient(node);
const wallets = await client.listWallets();
if (wallets.length === 0) {
await client.createWallet('');
}
}

async getBlockchainInfo(node: BitcoinNode) {
return await this.creatClient(node).getBlockchainInfo();
}
Expand Down
2 changes: 2 additions & 0 deletions src/store/models/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,8 @@ const networkModel: NetworkModel = {
actions.setStatus({ id, status: Status.Started, only: btc.name });
// connect each bitcoin node to it's peers so tx & block propagation is fast
await injections.bitcoindService.connectPeers(btc);
// create a default wallet since it's not automatic on v0.21.0 and up
await injections.bitcoindService.createDefaultWallet(btc);
await getStoreActions().bitcoind.getInfo(btc);
})
.catch(error =>
Expand Down
11 changes: 11 additions & 0 deletions src/types/bitcoin-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,7 @@ declare module 'bitcoin-core' {
| 'setnetworkactive'
| 'combinerawtransaction'
| 'createrawtransaction'
| 'createwallet'
| 'decoderawtransaction'
| 'decodescript'
| 'fundrawtransaction'
Expand Down Expand Up @@ -712,6 +713,16 @@ declare module 'bitcoin-core' {
replacable: boolean,
): Promise<string>;

createWallet(
wallet_name: string,
disable_private_keys?: boolean,
blank?: boolean,
passphrase?: string,
avoid_reuse?: boolean,
descriptors?: boolean,
load_on_startup?: boolean,
): Promise<{ name: string; warning: string }>;

/**
* @deprecated
*/
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export interface RepoServiceInjection {

export interface BitcoindLibrary {
waitUntilOnline: (node: BitcoinNode) => Promise<void>;
createDefaultWallet: (node: BitcoinNode) => Promise<void>;
getBlockchainInfo: (node: BitcoinNode) => Promise<ChainInfo>;
getWalletInfo: (node: BitcoinNode) => Promise<WalletInfo>;
getNewAddress: (node: BitcoinNode) => Promise<string>;
Expand Down
1 change: 1 addition & 0 deletions src/utils/tests/renderWithProviders.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const injections: StoreInjections = {
},
bitcoindService: {
waitUntilOnline: jest.fn(),
createDefaultWallet: jest.fn(),
getBlockchainInfo: jest.fn(),
getWalletInfo: jest.fn(),
getNewAddress: jest.fn(),
Expand Down

0 comments on commit 548a138

Please sign in to comment.