From 517a5d488b62358cdba91e6c73a118fde119f80c Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 3 Feb 2016 21:49:32 -0800 Subject: [PATCH] cleanup --- lib/bcoin/address.js | 41 ---------------- lib/bcoin/hd.js | 111 +++++++++++++++++++++++++++++++++++++++++++ lib/bcoin/wallet.js | 28 +++++------ 3 files changed, 122 insertions(+), 58 deletions(-) diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index ebd09ded..398f54f7 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -38,7 +38,6 @@ function Address(options) { this.key = bcoin.keypair({ priv: options.priv, pub: options.pub, - hd: options.hd, key: options.key, personalization: options.personalization, entropy: options.entropy, @@ -229,32 +228,6 @@ Address.prototype.getPublicKey = function getPublicKey(enc) { return this.key.getPublic(enc); }; -Address.prototype.getHDPublicKey = function getPublicKey(enc) { - if (!this.key.hd) - return; - - if (enc === 'base58') - return this.key.hd.xpubkey; - - if (this.key.hd.isPublic) - return this.key.hd; - - return this.key.hd.hdpub; -}; - -Address.prototype.getHDPrivateKey = function getPublicKey(enc) { - if (!this.key.hd) - return; - - if (!this.key.hd.isPrivate) - return; - - if (enc === 'base58') - return this.key.hd.xprivkey; - - return this.key.hd; -}; - Address.prototype.getKeyHash = function getKeyHash() { if (this._hash) return this._hash; @@ -535,20 +508,6 @@ Address.prototype.__defineGetter__('address', function() { return this.getAddress(); }); -Address.prototype.__defineGetter__('realType', function() { - if (this.type === 'scripthash') - return this.subtype; - return this.type; -}); - -Address.prototype.__defineGetter__('hdPrivateKey', function() { - return this.getHDPrivateKey(); -}); - -Address.prototype.__defineGetter__('hdPublicKey', function() { - return this.getHDPublicKey(); -}); - Address.prototype.toJSON = function toJSON(encrypt) { return { v: 1, diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 4dce6773..22c7f25f 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -756,6 +756,117 @@ HDPrivateKey.prototype.deriveString = function deriveString(path) { return child; }; +HDPrivateKey.prototype.toJSON = function toJSON(encrypt) { + var json = { + v: 1, + name: 'keypair', + encrypted: encrypt ? true : false + }; + + if (this.hd) { + if (this.hd.xprivkey) { + if (this.hd.seed) { + json.mnemonic = encrypt + ? encrypt(this.hd.seed.mnemonic) + : this.hd.seed.mnemonic; + json.passphrase = encrypt + ? encrypt(this.hd.seed.passphrase) + : this.hd.seed.passphrase; + return json; + } + json.xpriv = encrypt + ? encrypt(this.hd.xprivkey) + : this.hd.xprivkey; + return json; + } + + json.xpub = this.hd.xpubkey; + + return json; + } + + if (this._key.priv) { + json.priv = encrypt + ? encrypt(this.getPrivate('base58')) + : this.getPrivate('base58'); + return json; + } + + json.pub = this.getPublic('hex'); + return json; +}; + +HDPrivateKey.fromJSON = function fromJSON(json, decrypt) { + var key, priv, pub, compressed, xprivkey; + var path = {}; + + assert.equal(json.v, 1); + assert.equal(json.name, 'keypair'); + + if (json.encrypted && !decrypt) + throw new Error('Cannot decrypt address'); + + if (json.mnemonic) { + return new KeyPair({ + key: bcoin.hd.priv({ + seed: bcoin.hd.seed({ + mnemonic: json.encrypted + ? decrypt(json.mnemonic) + : json.mnemonic, + passphrase: json.encrypted + ? decrypt(json.passphrase) + : json.passphrase + }) + }) + }); + } + + if (json.xpriv) { + xprivkey = json.xpriv; + if (json.encrypted) + xprivkey = decrypt(xprivkey); + return new KeyPair({ + key: bcoin.hd.priv({ + xkey: xprivkey + }) + }); + } + + if (json.xpub) { + return new KeyPair({ + key: bcoin.hd.pub({ + xkey: json.xpub + }) + }); + } + + if (json.priv) { + priv = json.priv; + if (json.encrypted) + priv = decrypt(priv); + + key = KeyPair.fromSecret(json.priv); + priv = key.priv; + compressed = key.compressed; + return new KeyPair({ + priv: priv, + compressed: compressed + }); + } + + if (json.pub) { + pub = bcoin.utils.toArray(json.pub, 'hex'); + compressed = pub[0] !== 0x04; + return new KeyPair({ + pub: pub, + compressed: compressed + }); + } + + assert(false); +}; + + /** * HD Public Key */ diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index ec529740..2fef1fd2 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -464,10 +464,6 @@ Wallet.prototype._init = function init() { }); }; -Wallet.prototype.__defineGetter__('primary', function() { - return this.current; -}); - Wallet.prototype._getAddressTable = function() { var addresses = {}; var i, address; @@ -636,25 +632,23 @@ Wallet.prototype.removeAddress = function removeAddress(address) { }; Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { - return this.primary.getPrivateKey(enc); + return this.current.getPrivateKey(enc); }; Wallet.prototype.getScript = function getScript() { - return this.primary.getScript(); + return this.current.getScript(); }; -Wallet.prototype.getScriptHash = -Wallet.prototype.getScripthash = function getScripthash() { - return this.primary.getScriptHash(); +Wallet.prototype.getScriptHash = function getScriptHash() { + return this.current.getScriptHash(); }; -Wallet.prototype.getScriptAddress = -Wallet.prototype.getScriptaddress = function getScriptaddress() { +Wallet.prototype.getScriptAddress = function getScriptAddress() { return this.current.getScriptAddress(); }; Wallet.prototype.getPublicKey = function getPublicKey(enc) { - return this.primary.getPublicKey(enc); + return this.current.getPublicKey(enc); }; Wallet.prototype.createKey = function createKey(change, index) { @@ -695,16 +689,16 @@ Wallet.prototype.createKey = function createKey(change, index) { Wallet.prototype.getKeyHash = Wallet.prototype.getKeyhash = function getKeyhash() { - return this.primary.getKeyHash(); + return this.current.getKeyHash(); }; Wallet.prototype.getKeyAddress = Wallet.prototype.getKeyaddress = function getKeyaddress() { - return this.primary.getKeyAddress(); + return this.current.getKeyAddress(); }; Wallet.prototype.getHash = function getHash() { - return this.primary.getHash(); + return this.current.getHash(); }; Wallet.prototype.getAddress = function getAddress() { @@ -734,12 +728,12 @@ Wallet.prototype.fill = function fill(tx, address, fee) { items = unspent.filter(function(item) { var output = item.tx.outputs[item.index]; if (bcoin.script.isScripthash(output.script)) { - if (this.primary.type === 'scripthash') + if (this.current.type === 'scripthash') return true; return false; } if (bcoin.script.isMultisig(output.script)) { - if (this.primary.n > 1) + if (this.current.n > 1) return true; return false; }