diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index d8c5a7b9..f3f4662c 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -225,25 +225,55 @@ Wallet.prototype.getPrivateKey = function getPrivateKey(enc) { if (!enc) return priv; - if (enc === 'base58') { - // We'll be using ncompressed public key as an address - arr = [ network.prefixes.privkey ]; + if (enc === 'base58') + return Wallet.toSecret(priv, this.compressed); + else if (enc === 'hex') + return utils.toHex(priv); + else + return priv; +}; - // 0-pad key - while (arr.length + priv.length < 33) - arr.push(0); +Wallet.toSecret = function toSecret(priv, compressed) { + var arr, chk; - arr = arr.concat(priv); + // We'll be using ncompressed public key as an address + arr = [network.prefixes.privkey]; - if (this.compressed) - arr.push(1); + // 0-pad key + while (arr.length + priv.length < 33) + arr.push(0); - chk = utils.checksum(arr); + arr = arr.concat(priv); - return utils.toBase58(arr.concat(chk)); + if (compressed) + arr.push(1); + + chk = utils.checksum(arr); + + return utils.toBase58(arr.concat(chk)); +}; + +Wallet.fromSecret = function fromSecret(priv) { + var key, compressed; + + key = bcoin.utils.fromBase58(priv); + assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4)))); + assert.equal(key[0], network.prefixes.privkey); + + key = key.slice(0, -4); + if (key.length === 34) { + assert.equal(key[33], 1); + priv = key.slice(1, -1); + compressed = true; + } else { + priv = key.slice(1); + compressed = false; } - return priv; + return { + priv: priv, + compressed: compressed + }; }; Wallet.prototype.getScript = function getScript() { @@ -630,7 +660,7 @@ Wallet.prototype.toJSON = function toJSON(encrypt) { label: this.label, address: this.getAddress(), balance: utils.toBTC(this.balance()), - pub: this.getPublicKey('base58'), + pub: this.getPublicKey('hex'), priv: encrypt ? encrypt(this.getPrivateKey('base58')) : this.getPrivateKey('base58'), @@ -639,6 +669,7 @@ Wallet.prototype.toJSON = function toJSON(encrypt) { : null, type: this.type, subtype: this.subtype, + redeem: this.redeem ? utils.toHex(this.redeem) : null, keys: this.keys.map(utils.toBase58), m: this.m, n: this.n, @@ -663,21 +694,11 @@ Wallet.fromJSON = function fromJSON(json, decrypt) { if (json.encrypted) priv = decrypt(priv); - key = bcoin.utils.fromBase58(priv); - assert(utils.isEqual(key.slice(-4), utils.checksum(key.slice(0, -4)))); - assert.equal(key[0], network.prefixes.privkey); - - key = key.slice(0, -4); - if (key.length === 34) { - assert.equal(key[33], 1); - priv = key.slice(1, -1); - compressed = true; - } else { - priv = key.slice(1); - compressed = false; - } + key = Wallet.fromSecret(json.priv); + priv = key.priv; + compressed = key.compressed; } else { - pub = bcoin.utils.fromBase58(json.pub); + pub = bcoin.utils.toArray(json.pub, 'hex'); compressed = pub[0] !== 0x04; } @@ -696,6 +717,7 @@ Wallet.fromJSON = function fromJSON(json, decrypt) { multisig: multisig, type: json.type, subtype: json.subtype, + redeem: json.redeem ? utils.toArray(json.redeem, 'hex') : null, keys: json.keys.map(utils.fromBase58), m: json.m, n: json.n