diff --git a/lib/bcoin/address.js b/lib/bcoin/address.js index 71ea239d..f0ce5a80 100644 --- a/lib/bcoin/address.js +++ b/lib/bcoin/address.js @@ -129,8 +129,8 @@ Address.prototype.removeKey = function removeKey(key) { key = utils.toBuffer(key); - var index = this.keys.map(function(pub, i) { - return utils.isEqual(pub, key) ? i : null; + var index = this.keys.map(function(k, i) { + return utils.isEqual(k, key) ? i : null; }).filter(function(i) { return i !== null; })[0]; @@ -157,12 +157,12 @@ Address.prototype.getPrivateKey = function getPrivateKey(enc) { return this.key.getPrivate(enc); }; -Address.toSecret = function toSecret(priv, compressed) { - return bcoin.keypair.toSecret(priv, compressed); +Address.toSecret = function toSecret(privateKey, compressed) { + return bcoin.keypair.toSecret(privateKey, compressed); }; -Address.fromSecret = function fromSecret(priv) { - return bcoin.keypair.fromSecret(priv); +Address.fromSecret = function fromSecret(privateKey) { + return bcoin.keypair.fromSecret(privateKey); }; Address.prototype.getScript = function getScript() { @@ -406,7 +406,7 @@ Address.prototype.ownInput = function ownInput(tx, index) { Address.prototype.scriptInputs = function scriptInputs(tx) { var self = this; - var pub = this.getPublicKey(); + var publicKey = this.getPublicKey(); var redeem = this.getScript(); return tx.inputs.reduce(function(total, input, i) { @@ -416,7 +416,7 @@ Address.prototype.scriptInputs = function scriptInputs(tx) { if (!self.ownOutput(input.prevout.tx, input.prevout.index)) return total; - if (tx.scriptInput(i, pub, redeem)) + if (tx.scriptInput(i, publicKey, redeem)) total++; return total; @@ -428,7 +428,7 @@ Address.prototype.signInputs = function signInputs(tx, type) { var key = this.key; var total = 0; - if (!key.priv) + if (!key.privateKey) return 0; return tx.inputs.reduce(function(total, input, i) { @@ -447,11 +447,11 @@ Address.prototype.signInputs = function signInputs(tx, type) { Address.prototype.sign = function sign(tx, type) { var self = this; - var pub = this.getPublicKey(); + var publicKey = this.getPublicKey(); var redeem = this.getScript(); var key = this.key; - if (!key.priv) + if (!key.privateKey) return 0; // Add signature script to each input @@ -463,7 +463,7 @@ Address.prototype.sign = function sign(tx, type) { if (!self.ownOutput(input.prevout.tx, input.prevout.index)) return total; - if (tx.scriptSig(i, key, pub, redeem, type)) + if (tx.scriptSig(i, key, publicKey, redeem, type)) total++; return total; @@ -526,7 +526,7 @@ Address.prototype.toJSON = function toJSON(encrypt) { }; Address.fromJSON = function fromJSON(json, decrypt) { - var priv, pub, xprivkey, multisig, compressed, key, w; + var w; assert.equal(json.v, 1); assert.equal(json.name, 'address'); @@ -538,7 +538,6 @@ Address.fromJSON = function fromJSON(json, decrypt) { label: json.label, change: json.change, key: bcoin.keypair.fromJSON(json.key, decrypt), - multisig: multisig, type: json.type, subtype: json.subtype, redeem: json.redeem ? utils.toArray(json.redeem, 'hex') : null, diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 02a9ce5c..01a5ddd7 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -1003,11 +1003,13 @@ HDPublicKey.prototype.deriveString = function deriveString(path) { return this.pair.validate.apply(this.pair, arguments); }; - HD.prototype.getPublic = function getPublic() { + HD.prototype.getPublic = + HD.prototype.getPublicKey = function getPublicKey() { return this.pair.getPublic.apply(this.pair, arguments); }; - HD.prototype.getPrivate = function getPrivate() { + HD.prototype.getPrivate = + HD.prototype.getPrivateKey = function getPrivateKey() { return this.pair.getPrivate.apply(this.pair, arguments); }; @@ -1028,6 +1030,14 @@ HDPublicKey.prototype.deriveString = function deriveString(path) { }); }); +HDPrivateKey.prototype.toSecret = function toSecret() { + return bcoin.keypair.toSecret(this.privateKey, true); +}; + +HDPrivateKey.fromSecret = function fromSecret(privateKey) { + return bcoin.keypair.fromSecret(privateKey); +}; + /** * Helpers */ diff --git a/lib/bcoin/keypair.js b/lib/bcoin/keypair.js index f38281d2..47eb51e6 100644 --- a/lib/bcoin/keypair.js +++ b/lib/bcoin/keypair.js @@ -25,12 +25,8 @@ function KeyPair(options) { if (options instanceof KeyPair) return options; - if (options.key instanceof KeyPair) - return options.key; - - this.options = options; - this.pair = null; - this.compressed = options.compressed !== false; + if (options instanceof bcoin.ecdsa.keypair) + options = { pair: options }; if (options.key) options.pair = options.key; @@ -41,6 +37,13 @@ function KeyPair(options) { if (options.pub) options.publicKey = options.pub; + if (options.key instanceof KeyPair) + return options.key; + + this.options = options; + this.pair = null; + this.compressed = options.compressed !== false; + if (options.passphrase) options.entropy = utils.sha256(options.passphrase); @@ -48,6 +51,10 @@ function KeyPair(options) { this.pair = options.privateKey.pair; } else if (options.publicKey instanceof bcoin.hd.publicKey) { this.pair = options.publicKey.pair; + } else if (options.pair instanceof bcoin.hd.privateKey) { + this.pair = options.pair.pair; + } else if (options.pair instanceof bcoin.hd.publicKey) { + this.pair = options.pair.pair; } else if (options.pair) { assert(options.pair instanceof bcoin.ecdsa.keypair); this.pair = options.pair; @@ -65,65 +72,79 @@ function KeyPair(options) { } KeyPair.prototype.__defineGetter__('priv', function() { - return this.pair.getPrivate(); + return this.pair.priv; }); KeyPair.prototype.__defineGetter__('pub', function() { - return this.pair.getPublic(); + return this.pair.pub; }); KeyPair.prototype.__defineGetter__('privateKey', function() { - return this.pair.getPrivate(); + return this.getPrivateKey(); }); KeyPair.prototype.__defineGetter__('publicKey', function() { - return this.pair.getPublic(); + return this.getPublicKey(); }); -KeyPair.prototype.getPrivate = function getPrivate(enc) { - var priv = this.pair.getPrivate(); - - if (!priv) - return; - - priv = priv.toArray(); - - if (enc === 'base58') - return KeyPair.toSecret(priv, this.compressed); - - if (enc === 'hex') - return utils.toHex(priv); - - return priv; +KeyPair.prototype.validate = function validate() { + return this.pair.validate.apply(this.pair, arguments); }; -KeyPair.prototype.getPublic = function getPublic(enc) { - var pub = this.pair.getPublic(this.compressed, 'array'); +KeyPair.prototype.sign = function sign(msg) { + return this.pair.sign.apply(this.pair, arguments); +}; + +KeyPair.prototype.verify = function verify(msg, signature) { + return this.pair.verify.apply(this.pair, arguments); +}; + +KeyPair.prototype.getPrivate = +KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) { + var privateKey = this.pair.getPrivate(); + + if (!privateKey) + return; + + privateKey = privateKey.toArray(); if (enc === 'base58') - return utils.toBase58(pub); + return KeyPair.toSecret(privateKey, this.compressed); if (enc === 'hex') - return utils.toHex(pub); + return utils.toHex(privateKey); - return pub; + return privateKey; +}; + +KeyPair.prototype.getPublic = +KeyPair.prototype.getPublicKey = function getPublicKey(enc) { + var publicKey = this.pair.getPublic(this.compressed, 'array'); + + if (enc === 'base58') + return utils.toBase58(publicKey); + + if (enc === 'hex') + return utils.toHex(publicKey); + + return publicKey; }; KeyPair.prototype.toSecret = function toSecret() { return KeyPair.toSecret(this.getPrivate(), this.compressed); }; -KeyPair.toSecret = function toSecret(priv, compressed) { +KeyPair.toSecret = function toSecret(privateKey, compressed) { var arr, chk; // We'll be using ncompressed public key as an address arr = [network.prefixes.privkey]; // 0-pad key - while (arr.length + priv.length < 33) + while (arr.length + privateKey.length < 33) arr.push(0); - arr = arr.concat(priv); + arr = arr.concat(privateKey); if (compressed !== false) arr.push(1); @@ -133,29 +154,41 @@ KeyPair.toSecret = function toSecret(priv, compressed) { return utils.toBase58(arr.concat(chk)); }; -KeyPair.fromSecret = function fromSecret(priv) { +KeyPair.fromSecret = function fromSecret(privateKey) { var key, compressed; - key = utils.fromBase58(priv); + key = utils.fromBase58(privateKey); 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); + privateKey = key.slice(1, -1); compressed = true; } else { - priv = key.slice(1); + privateKey = key.slice(1); compressed = false; } return new KeyPair({ - privateKey: priv, + privateKey: privateKey, compressed: compressed }); }; +KeyPair.verify = function verify(msg, sig, key) { + try { + return bcoin.ecdsa.verify(msg, sig, key); + } catch (e) { + return false; + } +}; + +KeyPair.sign = function sign(msg, key) { + return bcoin.ecdsa.sign(msg, key.priv); +}; + KeyPair.prototype.toJSON = function toJSON(encrypt) { var json = { v: 1, @@ -164,19 +197,19 @@ KeyPair.prototype.toJSON = function toJSON(encrypt) { }; if (this.pair.priv) { - json.priv = encrypt - ? encrypt(this.getPrivate('base58')) - : this.getPrivate('base58'); + json.privateKey = encrypt + ? encrypt(this.toSecret()) + : this.toSecret(); return json; } - json.pub = this.getPublic('hex'); + json.publicKey = this.getPublicKey('base58'); + return json; }; KeyPair.fromJSON = function fromJSON(json, decrypt) { - var key, priv, pub, compressed, xprivkey; - var path = {}; + var privateKey, publicKey, compressed; assert.equal(json.v, 1); assert.equal(json.name, 'keypair'); @@ -184,25 +217,18 @@ KeyPair.fromJSON = function fromJSON(json, decrypt) { if (json.encrypted && !decrypt) throw new Error('Cannot decrypt address'); - if (json.priv) { - priv = json.priv; + if (json.privateKey) { + privateKey = json.privateKey; if (json.encrypted) - priv = decrypt(priv); - - key = KeyPair.fromSecret(json.priv); - priv = key.priv; - compressed = key.compressed; - return new KeyPair({ - privateKey: priv, - compressed: compressed - }); + privateKey = decrypt(privateKey); + return KeyPair.fromSecret(privateKey); } - if (json.pub) { - pub = bcoin.utils.toArray(json.pub, 'hex'); - compressed = pub[0] !== 0x04; + if (json.publicKey) { + publicKey = utils.fromBase58(json.publicKey); + compressed = publicKey[0] !== 0x04; return new KeyPair({ - publicKey: pub, + publicKey: publicKey, compressed: compressed }); } diff --git a/lib/bcoin/wallet.js b/lib/bcoin/wallet.js index 05804627..da5feb9c 100644 --- a/lib/bcoin/wallet.js +++ b/lib/bcoin/wallet.js @@ -33,8 +33,8 @@ function Wallet(options) { if (options.hd) { options.master = options.hd !== true - ? bcoin.hd.priv(options.hd) - : bcoin.hd.priv(); + ? bcoin.hd.privateKey(options.hd) + : bcoin.hd.privateKey(); delete options.hd; } @@ -177,7 +177,8 @@ function Wallet(options) { // Generate the last known receiving address key = this.createKey(false, Math.max(0, this.addressDepth - 1)); this.current = bcoin.address({ - priv: key.priv, + privateKey: key.privateKey, + publicKey: key.publicKey, type: this.type, subtype: this.subtype, m: this.m, @@ -201,7 +202,7 @@ function Wallet(options) { key = this.createKey(); this._firstKey = key; this.current = bcoin.address({ - priv: key.priv, + privateKey: key.privateKey, type: this.type, subtype: this.subtype, m: this.m, @@ -238,7 +239,7 @@ Wallet.prototype.getID = function() { return this.addresses[0].getKeyAddress(); if (this._firstKey) - return bcoin.address.key2addr(this._firstKey.pub); + return bcoin.address.key2addr(this._firstKey.publicKey); assert(false); }; @@ -290,18 +291,18 @@ Wallet.prototype._initAddresses = function() { Wallet.prototype.addKey = function addKey(key) { var hdKey, has, i; - if (bcoin.hd.priv.isExtended(key)) - key = bcoin.hd.priv(key); - else if (bcoin.hd.pub.isExtended(key)) - key = bcoin.hd.pub(key); + if (bcoin.hd.privateKey.isExtended(key)) + key = bcoin.hd.privateKey(key); + else if (bcoin.hd.publicKey.isExtended(key)) + key = bcoin.hd.publicKey(key); if (key instanceof bcoin.keypair) key = key.hd; - if (key instanceof bcoin.hd.priv) + if (key instanceof bcoin.hd.privateKey) key = key.hdpub; - if (key instanceof bcoin.hd.pub) { + if (key instanceof bcoin.hd.publicKey) { hdKey = key; key = hdKey.publicKey; } @@ -310,8 +311,8 @@ Wallet.prototype.addKey = function addKey(key) { if (!hdKey || !hdKey.isPurpose45()) throw new Error('Must add HD purpose keys to BIP45 wallet.'); - has = this.purposeKeys.some(function(pub) { - return pub.xpubkey === hdKey.xpubkey; + has = this.purposeKeys.some(function(k) { + return k.xpubkey === hdKey.xpubkey; }); if (has) @@ -383,18 +384,18 @@ Wallet.prototype.removeKey = function removeKey(key) { assert(!this._keysFinalized); - if (bcoin.hd.priv.isExtended(key)) - key = bcoin.hd.priv(key); - else if (bcoin.hd.pub.isExtended(key)) - key = bcoin.hd.pub(key); + if (bcoin.hd.privateKey.isExtended(key)) + key = bcoin.hd.privateKey(key); + else if (bcoin.hd.publicKey.isExtended(key)) + key = bcoin.hd.publicKey(key); if (key instanceof bcoin.keypair) key = key.hd; - if (key instanceof bcoin.hd.priv) + if (key instanceof bcoin.hd.privateKey) key = key.hdpub; - if (key instanceof bcoin.hd.pub) { + if (key instanceof bcoin.hd.publicKey) { hdKey = key; key = hd.publicKey; } @@ -403,8 +404,8 @@ Wallet.prototype.removeKey = function removeKey(key) { if (!hdKey || !hdKey.isPurpose45()) throw new Error('Must add HD purpose keys to BIP45 wallet.'); - index = this.purposeKeys.map(function(pub, i) { - return pub.xpubkey === hdKey.xpubkey ? i : null; + index = this.purposeKeys.map(function(k, i) { + return k.xpubkey === hdKey.xpubkey ? i : null; }).filter(function(i) { return i !== null; })[0]; @@ -419,8 +420,8 @@ Wallet.prototype.removeKey = function removeKey(key) { key = utils.toBuffer(key); - index = this.keys.map(function(pub, i) { - return utils.isEqual(pub, key) ? i : null; + index = this.keys.map(function(k, i) { + return utils.isEqual(k, key) ? i : null; }).filter(function(i) { return i !== null; })[0]; @@ -514,8 +515,8 @@ Wallet.prototype.createAddress = function createAddress(change, index) { assert(this._initialized); var options = { - priv: key.priv, - pub: key.pub, + privateKey: key.privateKey, + publicKey: key.publicKey, type: this.type, subtype: this.subtype, m: this.m, @@ -549,7 +550,7 @@ Wallet.prototype.createAddress = function createAddress(change, index) { if (i !== this.cosignerIndex) options.keys.push(key); }, this); - options.keys.push(key.pub); + options.keys.push(key.publicKey); } address = this.addAddress(options); @@ -671,8 +672,8 @@ Wallet.prototype.createKey = function createKey(change, index) { } key = bcoin.ecdsa.genKeyPair(); return { - priv: key.getPrivate().toArray(), - pub: key.getPublic(true, 'array') + privateKey: key.getPrivate().toArray(), + publicKey: key.getPublic(true, 'array') }; } @@ -691,8 +692,8 @@ Wallet.prototype.createKey = function createKey(change, index) { } return { - priv: key.privateKey, - pub: key.publicKey + privateKey: key.privateKey, + publicKey: key.publicKey }; }; @@ -959,7 +960,7 @@ Wallet.prototype.toJSON = function toJSON(encrypt) { }; Wallet.fromJSON = function fromJSON(json, decrypt) { - var priv, pub, xprivkey, multisig, compressed, key, w, i; + var w; assert.equal(json.v, 3); assert.equal(json.name, 'wallet'); @@ -991,12 +992,12 @@ Wallet.fromJSON = function fromJSON(json, decrypt) { }; // Compat - Legacy -Wallet.toSecret = function toSecret(priv, compressed) { - return bcoin.keypair.toSecret(priv, compressed); +Wallet.toSecret = function toSecret(privateKey, compressed) { + return bcoin.keypair.toSecret(privateKey, compressed); }; -Wallet.fromSecret = function fromSecret(priv) { - return bcoin.keypair.fromSecret(priv); +Wallet.fromSecret = function fromSecret(privateKey) { + return bcoin.keypair.fromSecret(privateKey); }; Wallet.key2hash = function key2hash(key) {