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

Wallet: require public key for watchonly #639

Closed
wants to merge 4 commits into from
Closed
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Bcoin Release Notes & Changelog

## v1.0.x

### Wallet API changes
braydonf marked this conversation as resolved.
Show resolved Hide resolved

Creating a watch-only wallet now requires an `account-key` (or `accountKey`)
argument. This is to prevent bcoin from generating keys and addresses the user
can not spend from.

## v1.0.0

### Migration
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ class Wallet extends EventEmitter {
await this.unlock(passphrase);

let key;
if (this.watchOnly && options.accountKey) {
if (this.watchOnly) {
key = options.accountKey;

if (typeof key === 'string')
Expand Down
28 changes: 26 additions & 2 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
const KEY2 = 'xprv9s21ZrQH143K3mqiSThzPtWAabQ22Pjp3uSNnZ53A5bQ4udp'
+ 'faKekc2m4AChLYH1XDzANhrSdxHYWUeTWjYJwFwWFyHkTMnMeAcW4JyRCZa';

// abandon abandon... about key at m'/44'/0'/0'
const PUBKEY = 'xpub6BosfCnifzxcFwrSzQiqu2DBVTshkCXacvNsWGYJVVhhaw'
+ 'A7d4R5WSWGFNbi8Aw6ZRc1brxMyWMzG3DSSSSoekkudhUd9yLb6qx39T9nMdj';

const enabled = true;
const workers = new WorkerPool({ enabled });
const wdb = new WalletDB({ workers });
Expand Down Expand Up @@ -1301,12 +1305,31 @@ describe('Wallet', function() {
importedKey = key;
});

it('should require account key to create watch only wallet', async () => {
let err = null;

try {
await wdb.create({
watchOnly: true
});
} catch (e) {
err = e;
}

assert(err);
assert.strictEqual(
err.message,
'Must add HD public keys to watch only wallet.'
);
});

it('should import pubkey', async () => {
const key = KeyRing.generate();
const pub = new KeyRing(key.publicKey);

const wallet = await wdb.create({
watchOnly: true
watchOnly: true,
accountKey: PUBKEY
});

await wallet.importKey('default', pub);
Expand All @@ -1322,7 +1345,8 @@ describe('Wallet', function() {
const key = KeyRing.generate();

const wallet = await wdb.create({
watchOnly: true
watchOnly: true,
accountKey: PUBKEY
});

await wallet.importAddress('default', key.getAddress());
Expand Down