Skip to content

Commit

Permalink
fix(ref-imp): automatically create bitcoin wallet (#1138)
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen committed Jun 17, 2021
1 parent bd5445c commit 674d753
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/bitcoin/BitcoinClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ export default class BitcoinClient {
* Initialize this bitcoin client.
*/
public async initialize (): Promise<void> {

// Create wallet has to be called because as of bitcoin v0.21, a default wallet is no longer automatically created
// https://github.com/bitcoin/bitcoin/pull/15454
await this.createWallet();
const walletAddress = this.bitcoinWallet.getAddress();

Logger.info(`Checking if bitcoin contains a wallet for ${walletAddress}`);
Expand Down Expand Up @@ -227,6 +229,23 @@ export default class BitcoinClient {
};
}

private async createWallet () {
const walletNameToUse = 'sidetreeDefaultWallet';
const request = {
method: 'createwallet',
params: [walletNameToUse] // the wallet name
};

const response = await this.rpcCall(request, true);
if (response.error === null) {
Logger.info(`Wallet created with name "${walletNameToUse}".`);
} else {
// for the list of possible errors: https://github.com/bitcoin/bitcoin/blob/master/src/rpc/protocol.h
Logger.error(`Error code ${response.error.code} occured while attempting to create bitcoin wallet: ${response.error.message}`);
}

}

/**
* Gets the block data for the given block hash.
* @param hash The hash of the block
Expand Down
30 changes: 30 additions & 0 deletions tests/bitcoin/BitcoinClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import BitcoinLockTransactionModel from '../../lib/bitcoin/models/BitcoinLockTra
import BitcoinTransactionModel from '../../lib/bitcoin/models/BitcoinTransactionModel';
import BitcoinWallet from '../../lib/bitcoin/BitcoinWallet';
import IBitcoinWallet from '../../lib/bitcoin/interfaces/IBitcoinWallet';
import Logger from '../../lib/common/Logger';
import ReadableStream from '../../lib/common/ReadableStream';

describe('BitcoinClient', async () => {
Expand Down Expand Up @@ -156,19 +157,24 @@ describe('BitcoinClient', async () => {
return Promise.resolve(undefined);
});

const createWalletSpy = spyOn(bitcoinClient as any, 'createWallet');

await bitcoinClient.initialize();
expect(walletExistsSpy).toHaveBeenCalled();
expect(importSpy).toHaveBeenCalled();
expect(createWalletSpy).toHaveBeenCalled();
});

it('should not import key if the wallet already exist', async () => {
const walletExistsSpy = spyOn(bitcoinClient as any, 'isAddressAddedToWallet').and.returnValue(Promise.resolve(true));

const importSpy = spyOn(bitcoinClient as any, 'addWatchOnlyAddressToWallet');
const createWalletSpy = spyOn(bitcoinClient as any, 'createWallet');

await bitcoinClient.initialize();
expect(walletExistsSpy).toHaveBeenCalled();
expect(importSpy).not.toHaveBeenCalled();
expect(createWalletSpy).toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -319,6 +325,30 @@ describe('BitcoinClient', async () => {
});
});

describe('createWallet', () => {
it('should create a wallet', async () => {
const rpcSpy = spyOn(bitcoinClient as any, 'rpcCall').and.returnValue({ error: null });
const loggerSpy = spyOn(Logger, 'info');
await bitcoinClient['createWallet']();
expect(rpcSpy).toHaveBeenCalledWith({
method: 'createwallet',
params: ['sidetreeDefaultWallet']
}, true);
expect(loggerSpy).toHaveBeenCalledWith(`Wallet created with name "sidetreeDefaultWallet".`);
});

it('should log error when create wallet fails', async () => {
const rpcSpy = spyOn(bitcoinClient as any, 'rpcCall').and.returnValue({ error: { code: 123, message: 'fake test error' } });
const loggerSpy = spyOn(Logger, 'error');
await bitcoinClient['createWallet']();
expect(rpcSpy).toHaveBeenCalledWith({
method: 'createwallet',
params: ['sidetreeDefaultWallet']
}, true);
expect(loggerSpy).toHaveBeenCalledWith(`Error code 123 occured while attempting to create bitcoin wallet: fake test error`);
});
});

describe('getBlock', () => {
it('should get the block data.', async () => {
const transaction = generateBitcoreTransactionWrapper(bitcoinWalletImportString);
Expand Down

0 comments on commit 674d753

Please sign in to comment.