more hd refactoring.

This commit is contained in:
Christopher Jeffrey 2016-04-19 10:49:08 -07:00
parent c8e771d05b
commit 99d8f4d928
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 26 additions and 18 deletions

View File

@ -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}

View File

@ -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())