test: independent watch-only tests and assert error

This commit is contained in:
Braydon Fuller 2019-02-11 13:30:40 -08:00
parent 0090c59714
commit bf32e86abe
No known key found for this signature in database
GPG Key ID: F24F232D108B3AD4

View File

@ -18,7 +18,6 @@ const Input = require('../lib/primitives/input');
const Outpoint = require('../lib/primitives/outpoint'); const Outpoint = require('../lib/primitives/outpoint');
const Script = require('../lib/script/script'); const Script = require('../lib/script/script');
const HD = require('../lib/hd'); const HD = require('../lib/hd');
const PrivateKey = require('../lib/hd/private');
const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt' const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
+ 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ'; + 'qUP9iWfcHgJofs25xbaUpCps9GDXj83NiWvQCAkWQhVj5J4CorfnpKX94AZ';
@ -26,6 +25,10 @@ const KEY1 = 'xprv9s21ZrQH143K3Aj6xQBymM31Zb4BVc7wxqfUhMZrzewdDVCt'
const KEY2 = 'xprv9s21ZrQH143K3mqiSThzPtWAabQ22Pjp3uSNnZ53A5bQ4udp' const KEY2 = 'xprv9s21ZrQH143K3mqiSThzPtWAabQ22Pjp3uSNnZ53A5bQ4udp'
+ 'faKekc2m4AChLYH1XDzANhrSdxHYWUeTWjYJwFwWFyHkTMnMeAcW4JyRCZa'; + 'faKekc2m4AChLYH1XDzANhrSdxHYWUeTWjYJwFwWFyHkTMnMeAcW4JyRCZa';
// abandon abandon... about key at m'/44'/0'/0'
const PUBKEY = 'xpub6BosfCnifzxcFwrSzQiqu2DBVTshkCXacvNsWGYJVVhhaw'
+ 'A7d4R5WSWGFNbi8Aw6ZRc1brxMyWMzG3DSSSSoekkudhUd9yLb6qx39T9nMdj';
const enabled = true; const enabled = true;
const workers = new WorkerPool({ enabled }); const workers = new WorkerPool({ enabled });
const wdb = new WalletDB({ workers }); const wdb = new WalletDB({ workers });
@ -35,7 +38,6 @@ let importedWallet = null;
let importedKey = null; let importedKey = null;
let doubleSpendWallet = null; let doubleSpendWallet = null;
let doubleSpendCoin = null; let doubleSpendCoin = null;
let watchWallet = null;
function fromU32(num) { function fromU32(num) {
const data = Buffer.allocUnsafe(4); const data = Buffer.allocUnsafe(4);
@ -1304,48 +1306,56 @@ describe('Wallet', function() {
}); });
it('should require account key to create watch only wallet', async () => { it('should require account key to create watch only wallet', async () => {
let err = null;
try { try {
watchWallet = await wdb.create({ await wdb.create({
watchOnly: true watchOnly: true
}); });
} catch (e) { } catch (e) {
assert.strictEqual( err = e;
e.message,
'Must add HD public keys to watch only wallet.'
);
} }
const privateKey = PrivateKey.generate(); assert(err);
const xpub = privateKey.xpubkey('main'); assert.strictEqual(
watchWallet = await wdb.create({ err.message,
watchOnly: true, 'Must add HD public keys to watch only wallet.'
accountKey: xpub );
});
}); });
it('should import pubkey', async () => { it('should import pubkey', async () => {
const key = KeyRing.generate(); const key = KeyRing.generate();
const pub = new KeyRing(key.publicKey); const pub = new KeyRing(key.publicKey);
await watchWallet.importKey('default', pub); const wallet = await wdb.create({
watchOnly: true,
accountKey: PUBKEY
});
const path = await watchWallet.getPath(pub.getHash()); await wallet.importKey('default', pub);
const path = await wallet.getPath(pub.getHash());
assert.bufferEqual(path.hash, pub.getHash()); assert.bufferEqual(path.hash, pub.getHash());
const wkey = await watchWallet.getKey(pub.getHash()); const wkey = await wallet.getKey(pub.getHash());
assert(wkey); assert(wkey);
}); });
it('should import address', async () => { it('should import address', async () => {
const key = KeyRing.generate(); const key = KeyRing.generate();
await watchWallet.importAddress('default', key.getAddress()); const wallet = await wdb.create({
watchOnly: true,
accountKey: PUBKEY
});
const path = await watchWallet.getPath(key.getHash()); await wallet.importAddress('default', key.getAddress());
const path = await wallet.getPath(key.getHash());
assert(path); assert(path);
assert.bufferEqual(path.hash, key.getHash()); assert.bufferEqual(path.hash, key.getHash());
const wkey = await watchWallet.getKey(key.getHash()); const wkey = await wallet.getKey(key.getHash());
assert(!wkey); assert(!wkey);
}); });