diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index b84fc032..8e1a54ab 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -238,6 +238,17 @@ HD.fromAny = function fromAny(options, networkType) { return HDPrivateKey.fromSeed(options, networkType); }; +/** + * Test whether an object is in the form of a base58 hd key. + * @param {String} data + * @returns {Boolean} + */ + +HD.isExtended = function isExtended(data) { + return HDPrivateKey.isExtended(data) + || HDPublicKey.isExtended(data); +}; + /** * LRU cache to avoid deriving keys twice. * @type {LRU} diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 5aa36c8f..da84ed05 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -44,9 +44,6 @@ var assert = utils.assert; var constants = bcoin.protocol.constants; var network = bcoin.protocol.network; var BufferWriter = require('./writer'); -var HD = bcoin.hd; -var HDPrivateKey = bcoin.hd.privateKey; -var HDPublicKey = bcoin.hd.publicKey; /** * HD BIP-44/45 wallet @@ -98,12 +95,12 @@ function Wallet(options) { if (options.master && typeof options.master === 'object' - && !(options.master instanceof HD)) { - options.master = HD.fromAny(options.master); + && !(options.master instanceof bcoin.hd)) { + options.master = bcoin.hd.fromAny(options.master); } if (!options.master) - options.master = HD.fromSeed(); + options.master = bcoin.hd.fromSeed(); this.options = options; this.provider = options.provider || null; @@ -272,20 +269,20 @@ Wallet.prototype.destroy = function destroy(callback) { Wallet.prototype.addKey = function addKey(key) { var index, i; + assert(key, 'Key required.'); + if (key instanceof bcoin.wallet) { assert(key.derivation === this.derivation); key = key.accountKey; } - if (HDPrivateKey.isExtended(key)) - key = HDPrivateKey.fromBase58(key); - else if (HDPublicKey.isExtended(key)) - key = HDPublicKey.fromBase58(key); + if (bcoin.hd.isExtended(key)) + key = bcoin.hd.fromBase58(key); - if (key instanceof HDPrivateKey) + if (key.hdPublicKey) key = key.hdPublicKey; - assert(key instanceof HD, 'Must add HD keys to wallet.'); + assert(key instanceof bcoin.hd, 'Must add HD keys to wallet.'); if (this.derivation === 'bip44') { if (!key || !key.isAccount44()) @@ -325,20 +322,20 @@ Wallet.prototype.removeKey = function removeKey(key) { assert(!this._keysFinalized); + assert(key, 'Key required.'); + if (key instanceof bcoin.wallet) { assert(key.derivation === this.derivation); key = key.accountKey; } - if (HDPrivateKey.isExtended(key)) - key = HDPrivateKey.fromBase58(key); - else if (HDPublicKey.isExtended(key)) - key = HDPublicKey.fromBase58(key); + if (bcoin.hd.isExtended(key)) + key = bcoin.hd.fromBase58(key); - if (key instanceof HDPrivateKey) + if (key.hdPublicKey) key = key.hdPublicKey; - assert(key instanceof HD, 'Must add HD keys to wallet.'); + assert(key instanceof bcoin.hd, 'Must add HD keys to wallet.'); if (this.derivation === 'bip44') { if (!key || !key.isAccount44())