Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 @@
# HSD Release Notes & Changelog

## vX.Y.Z

### 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.

## v0.0.0

### Notable Changes
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,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
43 changes: 29 additions & 14 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ const KeyRing = require('../lib/primitives/keyring');
const Input = require('../lib/primitives/input');
const Outpoint = require('../lib/primitives/outpoint');
const Script = require('../lib/script/script');
const HD = require('../lib/hd');
const PrivateKey = require('../lib/hd/private.js');

const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
+ 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ';
Expand All @@ -35,6 +37,7 @@ let importedWallet = null;
let importedKey = null;
let doubleSpendWallet = null;
let doubleSpendCoin = null;
let watchWallet = null;

function fromU32(num) {
const data = Buffer.allocUnsafe(4);
Expand Down Expand Up @@ -1175,37 +1178,49 @@ describe('Wallet', function() {
importedKey = key;
});

it('should require account key to create watch only wallet', async () => {
try {
watchWallet = await wdb.create({
watchOnly: true
});
} catch (e) {
assert.strictEqual(
e.message,
'Must add HD public keys to watch only wallet.'
);
}

const privateKey = PrivateKey.generate();
const xpub = privateKey.xpubkey('main');
watchWallet = await wdb.create({
watchOnly: true,
accountKey: xpub
});
});

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

const wallet = await wdb.create({
watchOnly: true
});
await watchWallet.importKey('default', pub);

await wallet.importKey('default', pub);

const path = await wallet.getPath(pub.getHash());
const path = await watchWallet.getPath(pub.getHash());
assert.bufferEqual(path.hash, pub.getHash());

const wkey = await wallet.getKey(pub.getHash());
const wkey = await watchWallet.getKey(pub.getHash());
assert(wkey);
});

it('should import address', async () => {
const key = KeyRing.generate();

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

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

const path = await wallet.getPath(key.getHash());
const path = await watchWallet.getPath(key.getHash());
assert(path);
assert.bufferEqual(path.hash, key.getHash());

const wkey = await wallet.getKey(key.getHash());
const wkey = await watchWallet.getKey(key.getHash());
assert(!wkey);
});

Expand Down