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

fix(ref-imp): automatically create bitcoin wallet #1138

Merged
merged 1 commit into from
Jun 17, 2021
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
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';
thehenrytsai marked this conversation as resolved.
Show resolved Hide resolved
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