Skip to content

Commit

Permalink
Merge pull request #639 from pinheadmz/watch
Browse files Browse the repository at this point in the history
Wallet: require public key for watchonly
  • Loading branch information
braydonf committed Feb 11, 2019
2 parents 7c64fd8 + bf32e86 commit 375965c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
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

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

0 comments on commit 375965c

Please sign in to comment.