keypair object.

This commit is contained in:
Christopher Jeffrey 2016-07-02 05:24:50 -07:00
parent 33ed409529
commit f01a1a0c6e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 94 additions and 47 deletions

View File

@ -1958,23 +1958,21 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) {
* Get private key. * Get private key.
* @memberof HDPrivateKey# * @memberof HDPrivateKey#
* @memberof HDPublicKey# * @memberof HDPublicKey#
* @method
* @returns {Buffer} * @returns {Buffer}
*/ */
HD.prototype.getPrivateKey = function getPrivateKey() { HD.prototype.getPrivateKey = KeyPair.prototype.getPrivateKey;
return KeyPair.prototype.getPrivateKey.apply(this, arguments);
};
/** /**
* Get public key. * Get public key.
* @memberof HDPrivateKey# * @memberof HDPrivateKey#
* @memberof HDPublicKey# * @memberof HDPublicKey#
* @method
* @returns {Buffer} * @returns {Buffer}
*/ */
HD.prototype.getPublicKey = function getPublicKey() { HD.prototype.getPublicKey = KeyPair.prototype.getPublicKey;
return KeyPair.prototype.getPublicKey.apply(this, arguments);
};
/** /**
* Sign message. * Sign message.
@ -1984,9 +1982,7 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) {
* @returns {Buffer} * @returns {Buffer}
*/ */
HD.prototype.sign = function sign() { HD.prototype.sign = KeyPair.prototype.sign;
return KeyPair.prototype.sign.apply(this, arguments);
};
/** /**
* Verify message. * Verify message.
@ -1994,13 +1990,19 @@ HDPublicKey.isHDPublicKey = function isHDPublicKey(obj) {
* @memberof HDPublicKey# * @memberof HDPublicKey#
* @param {Buffer} msg * @param {Buffer} msg
* @param {Buffer} sig * @param {Buffer} sig
* @param {Buffer} key
* @returns {Boolean} * @returns {Boolean}
*/ */
HD.prototype.verify = function verify() { HD.prototype.verify = KeyPair.prototype.verify;
return KeyPair.prototype.verify.apply(this, arguments);
}; /**
* Whether the key prefers a
* compressed public key.
* Always true.
* @memberof HDPrivateKey#
* @memberof HDPublicKey#
* @type {Boolean}
*/
HD.prototype.compressed = true; HD.prototype.compressed = true;
}); });
@ -2019,8 +2021,8 @@ HDPrivateKey.prototype.toKeyPair = function toKeyPair() {
* @returns {Base58String} * @returns {Base58String}
*/ */
HDPrivateKey.prototype.toSecret = function toSecret() { HDPrivateKey.prototype.toSecret = function toSecret(network) {
return this.toKeyPair().toSecret(); return this.toKeyPair().toSecret(network);
}; };
/* /*

View File

@ -49,26 +49,17 @@ function KeyPair(options) {
*/ */
KeyPair.prototype.fromOptions = function fromOptions(options) { KeyPair.prototype.fromOptions = function fromOptions(options) {
assert(options.privateKey || options.publicKey); if (options.privateKey) {
assert(!options.privateKey || Buffer.isBuffer(options.privateKey)); return this.fromPrivate(
assert(!options.publicKey || Buffer.isBuffer(options.publicKey)); options.privateKey,
options.compressed,
if (options.compressed != null) options.network);
this.compressed = options.compressed;
if (options.network)
this.network = bcoin.network.get(options.network);
this.privateKey = options.privateKey;
this.publicKey = options.publicKey;
if (!this.publicKey) {
this.publicKey = bcoin.ec.publicKeyCreate(
this.privateKey, this.compressed
);
} }
return this; if (options.publicKey)
return this.fromPublic(options.publicKey, options.network);
throw new Error('Must provide a key.');
}; };
/** /**
@ -95,6 +86,61 @@ KeyPair.generate = function(network) {
return key; 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. * Sign a message.
* @param {Buffer} msg * @param {Buffer} msg
@ -102,6 +148,7 @@ KeyPair.generate = function(network) {
*/ */
KeyPair.prototype.sign = function sign(msg) { KeyPair.prototype.sign = function sign(msg) {
assert(this.privateKey, 'Cannot sign without private key.');
return bcoin.ec.sign(msg, this.getPrivateKey()); return bcoin.ec.sign(msg, this.getPrivateKey());
}; };
@ -160,6 +207,8 @@ KeyPair.prototype.getPublicKey = function getPublicKey(enc) {
KeyPair.prototype.toRaw = function toRaw(network, writer) { KeyPair.prototype.toRaw = function toRaw(network, writer) {
var p = new BufferWriter(writer); var p = new BufferWriter(writer);
assert(this.privateKey, 'Cannot serialize without private key.');
if (!network) if (!network)
network = this.network; network = this.network;
@ -197,7 +246,7 @@ KeyPair.prototype.toSecret = function toSecret(network) {
KeyPair.prototype.fromRaw = function fromRaw(data) { KeyPair.prototype.fromRaw = function fromRaw(data) {
var p = new BufferReader(data, true); var p = new BufferReader(data, true);
var i, prefix, version, type; var i, prefix, version, type, key, compressed;
version = p.readU8(); version = p.readU8();
@ -210,23 +259,20 @@ KeyPair.prototype.fromRaw = function fromRaw(data) {
assert(i < network.types.length, 'Network not found.'); assert(i < network.types.length, 'Network not found.');
this.network = bcoin.network.get(type); key = p.readBytes(32);
this.privateKey = p.readBytes(32);
if (p.left() > 4) { if (p.left() > 4) {
assert(p.readU8() === 1, 'Bad compression flag.'); assert(p.readU8() === 1, 'Bad compression flag.');
this.compressed = true; compressed = true;
} else { } else {
this.compressed = false; compressed = false;
} }
p.verifyChecksum(); p.verifyChecksum();
this.publicKey = bcoin.ec.publicKeyCreate( assert(bcoin.ec.privateKeyVerify(key));
this.privateKey, this.compressed
);
return this; return this.fromPrivate(key, compressed, type);
}; };
/** /**
@ -281,16 +327,15 @@ KeyPair.prototype.toJSON = function toJSON() {
*/ */
KeyPair.prototype.fromJSON = function fromJSON(json) { KeyPair.prototype.fromJSON = function fromJSON(json) {
this.network = bcoin.network.get(json.network); var key;
this.compressed = json.compressed;
if (json.privateKey) if (json.privateKey)
return this.fromSecret(json.privateKey); return this.fromSecret(json.privateKey);
if (json.publicKey) { if (json.publicKey) {
this.publicKey = utils.fromBase58(json.publicKey); key = utils.fromBase58(json.publicKey);
this.compressed = this.publicKey[0] !== 0x04; assert(bcoin.ec.publicKeyVerify(key));
return this; return this.fromPublic(key, json.network);
} }
assert(false, 'Could not parse KeyPair JSON.'); assert(false, 'Could not parse KeyPair JSON.');