diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index ccc305f0..efb14b1c 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -91,7 +91,6 @@ var global = utils.global; * @property {Function} chain - {@link Chain} constructor. * @property {Function} mempool - {@link Mempool} constructor. * @property {Function} mempoolentry - {@link MempoolEntry} constructor. - * @property {Function} keypair - {@link KeyPair} constructor. * @property {Function} hd - {@link HD} constructor. * @property {Function} address - {@link Address} constructor. * @property {Function} wallet - {@link Wallet} constructor. @@ -164,9 +163,8 @@ function Environment() { this.chain = require('./chain'); this.mempool = require('./mempool'); this.mempoolentry = this.mempool.MempoolEntry; - this.keypair = require('./keypair'); - this.hd = require('./hd'); this.keyring = require('./keyring'); + this.hd = require('./hd'); this.wallet = require('./wallet'); this.account = this.wallet.Account; this.walletdb = require('./walletdb'); diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 779a6b2f..cc059edc 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -84,7 +84,7 @@ var ec = require('./ec'); var assert = utils.assert; var constants = bcoin.protocol.constants; var networks = bcoin.protocol.network; -var KeyPair = bcoin.keypair; +var KeyRing = bcoin.keyring; var LRU = require('./lru'); var BufferWriter = require('./writer'); var BufferReader = require('./reader'); @@ -2141,7 +2141,7 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { * @returns {Buffer} */ - HD.prototype.getPrivateKey = KeyPair.prototype.getPrivateKey; + HD.prototype.getPrivateKey = KeyRing.prototype.getPrivateKey; /** * Get public key. @@ -2151,7 +2151,7 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { * @returns {Buffer} */ - HD.prototype.getPublicKey = KeyPair.prototype.getPublicKey; + HD.prototype.getPublicKey = KeyRing.prototype.getPublicKey; /** * Sign message. @@ -2161,7 +2161,7 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { * @returns {Buffer} */ - HD.prototype.sign = KeyPair.prototype.sign; + HD.prototype.sign = KeyRing.prototype.sign; /** * Verify message. @@ -2172,7 +2172,16 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { * @returns {Boolean} */ - HD.prototype.verify = KeyPair.prototype.verify; + HD.prototype.verify = KeyRing.prototype.verify; + + /** + * Convert HDPrivateKey to a KeyRing. + * @returns {KeyRing} + */ + + HD.prototype.toKeyRing = function toKeyRing() { + return new KeyRing(this.privateKey || this.publicKey, this.network); + }; /** * Whether the key prefers a @@ -2186,22 +2195,13 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) { HD.prototype.compressed = true; }); -/** - * Convert HDPrivateKey to a KeyPair. - * @returns {KeyPair} - */ - -HDPrivateKey.prototype.toKeyPair = function toKeyPair() { - return new KeyPair(this); -}; - /** * Convert HDPrivateKey to CBitcoinSecret. * @returns {Base58String} */ HDPrivateKey.prototype.toSecret = function toSecret(network) { - return this.toKeyPair().toSecret(network); + return this.toKeyRing().toSecret(network); }; /* diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index 8f646b38..8b792cc1 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -2373,11 +2373,11 @@ RPC.prototype.signmessagewithprivkey = function signmessagewithprivkey(args, cal key = toString(args[0]); msg = toString(args[1]); - key = bcoin.keypair.fromSecret(key).getPrivateKey(); + key = bcoin.keyring.fromSecret(key); msg = new Buffer(RPC.magic + msg, 'utf8'); msg = utils.hash256(msg); - sig = bcoin.ec.sign(msg, key); + sig = key.sign(msg); callback(null, sig.toString('base64')); }; diff --git a/lib/bcoin/keypair.js b/lib/bcoin/keypair.js deleted file mode 100644 index 3d87616f..00000000 --- a/lib/bcoin/keypair.js +++ /dev/null @@ -1,372 +0,0 @@ -/*! - * keypair.js - keypair object for bcoin - * Copyright (c) 2014-2015, Fedor Indutny (MIT License) - * Copyright (c) 2014-2016, Christopher Jeffrey (MIT License). - * https://github.com/bcoin-org/bcoin - */ - -'use strict'; - -var bcoin = require('./env'); -var utils = require('./utils'); -var assert = utils.assert; -var network = bcoin.protocol.network; -var BufferWriter = require('./writer'); -var BufferReader = require('./reader'); - -/** - * Represents an ecdsa keypair. - * @exports KeyPair - * @constructor - * @param {Object} options - * @param {Buffer?} options.privateKey - * @param {Buffer?} options.publicKey - * @param {Boolean?} options.compressed - * @param {(Network|NetworkType)?} options.network - * @property {Buffer} privateKey - * @property {Buffer} publicKey - * @property {Boolean} compressed - * @property {Network} network - */ - -function KeyPair(options) { - if (!(this instanceof KeyPair)) - return new KeyPair(options); - - this.network = bcoin.network.get(); - this.compressed = true; - this.privateKey = null; - this.publicKey = null; - - if (options) - this.fromOptions(options); -} - -/** - * Inject properties from options object. - * @private - * @param {Object} options - */ - -KeyPair.prototype.fromOptions = function fromOptions(options) { - if (options.privateKey) { - return this.fromPrivate( - options.privateKey, - options.compressed, - options.network); - } - - if (options.publicKey) - return this.fromPublic(options.publicKey, options.network); - - throw new Error('Must provide a key.'); -}; - -/** - * Instantiate key pair from options object. - * @param {Object} options - * @returns {KeyPair} - */ - -KeyPair.fromOptions = function fromOptions(options) { - return new KeyPair().fromOptions(options); -}; - -/** - * Generate a keypair. - * @param {(Network|NetworkType)?} network - * @returns {KeyPair} - */ - -KeyPair.generate = function(network) { - var key = new KeyPair(); - key.network = bcoin.network.get(network); - key.privateKey = bcoin.ec.generatePrivateKey(); - key.publicKey = bcoin.ec.publicKeyCreate(key.privateKey, true); - return key; -}; - -/** - * Inject data from private key. - * @private - * @param {Buffer} privateKey - * @param {Boolean?} compressed - * @param {(NetworkType|Network}) network - */ - -KeyPair.prototype.fromPrivate = function fromPrivate(privateKey, compressed, network) { - assert(Buffer.isBuffer(privateKey)); - this.network = bcoin.network.get(network); - this.privateKey = privateKey; - this.compressed = compressed !== false; - this.publicKey = bcoin.ec.publicKeyCreate(this.privateKey, this.compressed); - return this; -}; - -/** - * Instantiate key pair from a private key. - * @param {Buffer} privateKey - * @param {Boolean?} compressed - * @param {(NetworkType|Network}) network - * @returns {KeyPair} - */ - -KeyPair.fromPrivate = function fromPrivate(privateKey, compressed, network) { - return new KeyPair().fromPrivate(privateKey, compressed, network); -}; - -/** - * Inject data from public key. - * @private - * @param {Buffer} privateKey - * @param {(NetworkType|Network}) network - */ - -KeyPair.prototype.fromPublic = function fromPublic(publicKey, network) { - assert(Buffer.isBuffer(publicKey)); - this.network = bcoin.network.get(network); - this.publicKey = publicKey; - this.compressed = publicKey[0] <= 0x03; - return this; -}; - -/** - * Instantiate key pair from a public key. - * @param {Buffer} publicKey - * @param {(NetworkType|Network}) network - * @returns {KeyPair} - */ - -KeyPair.fromPublic = function fromPublic(publicKey, network) { - return new KeyPair().fromPublic(publicKey, network); -}; - -/** - * Sign a message. - * @param {Buffer} msg - * @returns {Buffer} Signature in DER format. - */ - -KeyPair.prototype.sign = function sign(msg) { - assert(this.privateKey, 'Cannot sign without private key.'); - return bcoin.ec.sign(msg, this.getPrivateKey()); -}; - -/** - * Verify a message. - * @param {Buffer} msg - * @param {Buffer} sig - Signature in DER format. - * @returns {Boolean} - */ - -KeyPair.prototype.verify = function verify(msg, sig) { - return bcoin.ec.verify(msg, sig, this.getPublicKey()); -}; - -/** - * Get private key. - * @param {String?} enc - Can be `"hex"`, `"base58"`, or `null`. - * @returns {Buffer} Private key. - */ - -KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) { - if (!this.privateKey) - return; - - if (enc === 'base58') - return this.toSecret(); - - if (enc === 'hex') - return this.privateKey.toString('hex'); - - return this.privateKey; -}; - -/** - * Get public key. - * @param {String?} enc - Can be `"hex"`, or `null`. - * @returns {Buffer} Public key. - */ - -KeyPair.prototype.getPublicKey = function getPublicKey(enc) { - if (enc === 'base58') - return utils.toBase58(this.publicKey); - - if (enc === 'hex') - return this.publicKey.toString('hex'); - - return this.publicKey; -}; - -/** - * Convert key to a CBitcoinSecret. - * @param {(Network|NetworkType)?} network - * @returns {Buffer} - */ - -KeyPair.prototype.toRaw = function toRaw(network, writer) { - var p = new BufferWriter(writer); - - assert(this.privateKey, 'Cannot serialize without private key.'); - - if (!network) - network = this.network; - - network = bcoin.network.get(network); - - p.writeU8(network.keyPrefix.privkey); - p.writeBytes(this.getPrivateKey()); - - if (this.compressed) - p.writeU8(1); - - p.writeChecksum(); - - if (!writer) - p = p.render(); - - return p; -}; - -/** - * Convert key to a CBitcoinSecret. - * @param {(Network|NetworkType)?} network - * @returns {Base58String} - */ - -KeyPair.prototype.toSecret = function toSecret(network) { - return utils.toBase58(this.toRaw(network)); -}; - -/** - * Inject properties from serialized CBitcoinSecret. - * @private - * @param {Buffer} data - */ - -KeyPair.prototype.fromRaw = function fromRaw(data) { - var p = new BufferReader(data, true); - var i, prefix, version, type, key, compressed; - - version = p.readU8(); - - for (i = 0; i < network.types.length; i++) { - type = network.types[i]; - prefix = network[type].keyPrefix.privkey; - if (version === prefix) - break; - } - - assert(i < network.types.length, 'Network not found.'); - - key = p.readBytes(32); - - if (p.left() > 4) { - assert(p.readU8() === 1, 'Bad compression flag.'); - compressed = true; - } else { - compressed = false; - } - - p.verifyChecksum(); - - assert(bcoin.ec.privateKeyVerify(key)); - - return this.fromPrivate(key, compressed, type); -}; - -/** - * Inject properties from serialized CBitcoinSecret. - * @private - * @param {Base58String} secret - */ - -KeyPair.prototype.fromSecret = function fromSecret(secret) { - return this.fromRaw(utils.fromBase58(secret)); -}; - -/** - * Instantiate a key pair from a serialized CBitcoinSecret. - * @param {Buffer} data - * @returns {KeyPair} - */ - -KeyPair.fromRaw = function fromRaw(data, enc) { - if (typeof data === 'string') - data = new Buffer(data, enc); - return new KeyPair().fromRaw(data); -}; - -/** - * Instantiate a key pair from a serialized CBitcoinSecret. - * @param {Base58String} secret - * @returns {KeyPair} - */ - -KeyPair.fromSecret = function fromSecret(secret) { - return new KeyPair().fromSecret(secret); -}; - -/** - * Convert the keypair to an object suitable - * for JSON serialization. - * @returns {Object} - */ - -KeyPair.prototype.toJSON = function toJSON() { - return { - network: this.network.type, - compressed: this.compressed, - privateKey: this.privateKey ? this.toSecret() : null, - publicKey: this.getPublicKey('base58') - }; -}; - -/** - * Inject properties from json object. - * @private - * @param {Object} json - */ - -KeyPair.prototype.fromJSON = function fromJSON(json) { - var key; - - if (json.privateKey) - return this.fromSecret(json.privateKey); - - if (json.publicKey) { - key = utils.fromBase58(json.publicKey); - assert(bcoin.ec.publicKeyVerify(key)); - return this.fromPublic(key, json.network); - } - - assert(false, 'Could not parse KeyPair JSON.'); -}; - -/** - * Instantiate a key pair from a jsonified object. - * @param {Object} json - The jsonified key pair object. - * @returns {KeyPair} - */ - -KeyPair.fromJSON = function fromJSON(json) { - return new KeyPair().fromJSON(json); -}; - -/** - * Test whether an object is a key pair. - * @param {Object?} obj - * @returns {Boolean} - */ - -KeyPair.isKeyPair = function isKeyPair(obj) { - return obj - && obj.privateKey !== undefined - && typeof obj.fromSecret === 'function'; -}; - -/* - * Expose - */ - -module.exports = KeyPair; diff --git a/lib/bcoin/keyring.js b/lib/bcoin/keyring.js index 1f61a467..d923fc19 100644 --- a/lib/bcoin/keyring.js +++ b/lib/bcoin/keyring.js @@ -288,6 +288,25 @@ KeyRing.fromSecret = function fromSecret(data) { return new KeyRing().fromSecret(data); }; +/** + * Get private key. + * @param {String?} enc - Can be `"hex"`, `"base58"`, or `null`. + * @returns {Buffer} Private key. + */ + +KeyRing.prototype.getPrivateKey = function getPrivateKey(enc) { + if (!this.privateKey) + return; + + if (enc === 'base58') + return this.toSecret(); + + if (enc === 'hex') + return this.privateKey.toString('hex'); + + return this.privateKey; +}; + /** * Get public key. * @param {String?} enc - `"hex"` or `null`. diff --git a/lib/bcoin/mtx.js b/lib/bcoin/mtx.js index f4a2881f..67301b71 100644 --- a/lib/bcoin/mtx.js +++ b/lib/bcoin/mtx.js @@ -387,7 +387,7 @@ MTX.prototype.scriptVector = function scriptVector(prev, vector, ring) { /** * Sign an input. * @param {Number} index - Index of input being signed. - * @param {HDPrivateKey|KeyPair|Buffer} key - Private key. + * @param {Buffer} key - Private key. * @param {SighashType} type * @returns {Boolean} Whether the input was able to be signed. */ diff --git a/lib/bcoin/protocol/framer.js b/lib/bcoin/protocol/framer.js index 9745bbf8..ed328fbb 100644 --- a/lib/bcoin/protocol/framer.js +++ b/lib/bcoin/protocol/framer.js @@ -707,7 +707,6 @@ Framer.addr = function addr(hosts, writer) { /** * Create an alert packet (without a header). * @param {AlertPacket} data - * @param {Buffer|KeyPair} key * @param {BufferWriter?} writer - A buffer writer to continue writing from. * @returns {Buffer} Returns a BufferWriter if `writer` was passed in. */