improve keypair.

This commit is contained in:
Christopher Jeffrey 2016-02-23 03:46:51 -08:00
parent f4f6c79bfd
commit 83239112e4
5 changed files with 46 additions and 98 deletions

View File

@ -33,7 +33,7 @@ function Address(options) {
this.derived = !!options.derived; this.derived = !!options.derived;
this.addressMap = null; this.addressMap = null;
this.key = options.pair || options.key || bcoin.keypair(options); this.key = options.key || bcoin.keypair(options);
this.path = options.path; this.path = options.path;
this.change = !!options.change; this.change = !!options.change;
this.index = options.index; this.index = options.index;

View File

@ -15,17 +15,20 @@ var ec = exports;
*/ */
ec.generate = function generate(options) { ec.generate = function generate(options) {
var priv, pub; var key, priv, pub;
if (bcoin.secp256k1 && bcoin.crypto) { if (bcoin.secp256k1 && bcoin.crypto) {
do { do {
priv = bcoin.crypto.randomBytes(32); priv = bcoin.crypto.randomBytes(32);
} while (!bcoin.secp256k1.privateKeyVerify(priv)); } while (!bcoin.secp256k1.privateKeyVerify(priv));
pub = bcoin.secp256k1.publicKeyCreate(priv, true); pub = bcoin.secp256k1.publicKeyCreate(priv, true);
return bcoin.keypair({ priv: priv, pub: pub }); } else {
key = bcoin.ecdsa.genKeyPair();
priv = new Buffer(key.getPrivate().toArray('be', 32));
pub = new Buffer(key.getPublic(true, 'array'));
} }
return bcoin.keypair(options); return { privateKey: priv, publicKey: pub };
}; };
ec.verify = function verify(msg, sig, key, historical) { ec.verify = function verify(msg, sig, key, historical) {

View File

@ -614,7 +614,7 @@ HDPrivateKey.prototype._seed = function _seed(seed) {
HDPrivateKey.prototype._generate = function _generate(privateKey, entropy) { HDPrivateKey.prototype._generate = function _generate(privateKey, entropy) {
if (!privateKey) if (!privateKey)
privateKey = bcoin.ec.generate().getPrivateKey(); privateKey = bcoin.ec.generate().privateKey;
if (utils.isHex(privateKey)) if (utils.isHex(privateKey))
privateKey = new Buffer(privateKey, 'hex'); privateKey = new Buffer(privateKey, 'hex');
@ -672,7 +672,7 @@ HDPrivateKey.prototype._unbuild = function _unbuild(xkey) {
HDPrivateKey.prototype._build = function _build(data) { HDPrivateKey.prototype._build = function _build(data) {
var sequence = new Buffer(82); var sequence = new Buffer(82);
var off = 0; var off = 0;
var checksum, xprivkey, pair, privateKey, publicKey, size, fingerPrint; var checksum, xprivkey, key, privateKey, publicKey, size, fingerPrint;
off += utils.copy(data.version, sequence, off); off += utils.copy(data.version, sequence, off);
off += utils.copy(data.depth, sequence, off); off += utils.copy(data.depth, sequence, off);
@ -688,9 +688,9 @@ HDPrivateKey.prototype._build = function _build(data) {
xprivkey = utils.toBase58(sequence); xprivkey = utils.toBase58(sequence);
pair = bcoin.keypair({ privateKey: data.privateKey }); key = bcoin.keypair({ privateKey: data.privateKey });
privateKey = pair.getPrivateKey(); privateKey = key.getPrivateKey();
publicKey = pair.getPublicKey(); publicKey = key.getPublicKey();
size = constants.hd.parentFingerPrintSize; size = constants.hd.parentFingerPrintSize;
fingerPrint = utils.ripesha(publicKey).slice(0, size); fingerPrint = utils.ripesha(publicKey).slice(0, size);
@ -722,7 +722,7 @@ HDPrivateKey.prototype._build = function _build(data) {
this.hdPrivateKey = this; this.hdPrivateKey = this;
this.xpubkey = this.hdPublicKey.xpubkey; this.xpubkey = this.hdPublicKey.xpubkey;
this.pair = bcoin.keypair({ privateKey: this.privateKey }); this.key = bcoin.keypair({ privateKey: this.privateKey });
}; };
HDPrivateKey.prototype.derive = function derive(index, hardened) { HDPrivateKey.prototype.derive = function derive(index, hardened) {
@ -1081,11 +1081,11 @@ HDPublicKey.prototype._build = function _build(data) {
this.fingerPrint = fingerPrint; this.fingerPrint = fingerPrint;
this.xprivkey = data.xprivkey; this.xprivkey = data.xprivkey;
this.pair = bcoin.keypair({ publicKey: this.publicKey }); this.key = bcoin.keypair({ publicKey: this.publicKey });
}; };
HDPublicKey.prototype.derive = function derive(index, hardened) { HDPublicKey.prototype.derive = function derive(index, hardened) {
var cached, data, hash, leftPart, chainCode, pair, point, publicKey, child; var cached, data, hash, leftPart, chainCode, key, point, publicKey, child;
var off = 0; var off = 0;
if (typeof index === 'string') if (typeof index === 'string')
@ -1110,8 +1110,8 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
leftPart = new bn(hash.slice(0, 32)); leftPart = new bn(hash.slice(0, 32));
chainCode = hash.slice(32, 64); chainCode = hash.slice(32, 64);
pair = bcoin.keypair({ publicKey: this.publicKey }); key = bcoin.keypair({ publicKey: this.publicKey });
point = ec.curve.g.mul(leftPart).add(pair.publicPoint); point = ec.curve.g.mul(leftPart).add(key.publicPoint);
publicKey = bcoin.keypair({ publicKey: point }).getPublicKey(); publicKey = bcoin.keypair({ publicKey: point }).getPublicKey();
child = new HDPublicKey({ child = new HDPublicKey({
@ -1162,10 +1162,6 @@ HDPublicKey.prototype.deriveString = function deriveString(path) {
*/ */
[HDPrivateKey, HDPublicKey].forEach(function(HD) { [HDPrivateKey, HDPublicKey].forEach(function(HD) {
HD.prototype.validate = function validate() {
return this.pair.validate.apply(this.pair, arguments);
};
HD.prototype.getPublicKey = function getPublicKey() { HD.prototype.getPublicKey = function getPublicKey() {
return bcoin.keypair.prototype.getPublicKey.apply(this, arguments); return bcoin.keypair.prototype.getPublicKey.apply(this, arguments);
}; };
@ -1174,20 +1170,20 @@ HDPublicKey.prototype.deriveString = function deriveString(path) {
return bcoin.keypair.prototype.getPrivateKey.apply(this, arguments); return bcoin.keypair.prototype.getPrivateKey.apply(this, arguments);
}; };
HD.prototype.sign = function sign(msg) { HD.prototype.sign = function sign() {
return this.pair.sign.apply(this.pair, arguments); return this.key.sign.apply(this.key, arguments);
}; };
HD.prototype.verify = function verify(msg, signature) { HD.prototype.verify = function verify() {
return this.pair.verify.apply(this.pair, arguments); return this.key.verify.apply(this.key, arguments);
}; };
HD.prototype.__defineGetter__('publicPoint', function() { HD.prototype.__defineGetter__('publicPoint', function() {
return this.pair.publicPoint; return this.key.publicPoint;
}); });
HD.prototype.__defineGetter__('privatePoint', function() { HD.prototype.__defineGetter__('privatePoint', function() {
return this.pair.privatePoint; return this.key.privatePoint;
}); });
HD.prototype.compressed = true; HD.prototype.compressed = true;

View File

@ -22,88 +22,41 @@ function KeyPair(options) {
if (!options) if (!options)
options = {}; options = {};
if (options instanceof KeyPair)
return options;
if (options instanceof bcoin.ecdsa.keypair)
options = { pair: options };
if (options.key)
options.pair = options.key;
if (options.priv)
options.privateKey = options.priv;
if (options.pub)
options.publicKey = options.pub;
if (options.pair instanceof KeyPair)
return options.pair;
this.options = options; this.options = options;
this.pair = null; this.key = null;
this.compressed = options.compressed !== false; this.compressed = options.compressed !== false;
if (options.passphrase) if (!options.privateKey && !options.publicKey)
options.entropy = utils.sha256(options.passphrase); throw new Error('No options for keypair');
if (options.privateKey instanceof bcoin.hd.privateKey) { this.key = bcoin.ecdsa.keyPair({
this.pair = options.privateKey.pair; priv: options.privateKey,
} else if (options.publicKey instanceof bcoin.hd.publicKey) { pub: options.publicKey
this.pair = options.publicKey.pair; });
} else if (options.pair instanceof bcoin.hd.privateKey) {
this.pair = options.pair.pair; this.privatePoint = this.key.getPrivate();
} else if (options.pair instanceof bcoin.hd.publicKey) { this.publicPoint = this.key.getPublic();
this.pair = options.pair.pair; this.privateKey = this.getPrivateKey();
} else if (options.pair) { this.publicKey = this.getPublicKey();
assert(options.pair instanceof bcoin.ecdsa.keypair);
this.pair = options.pair;
} else if (options.privateKey || options.publicKey) {
this.pair = bcoin.ecdsa.keyPair({
priv: options.privateKey,
pub: options.publicKey
});
} else {
this.pair = bcoin.ec.generate({
pers: options.personalization,
entropy: options.entropy
});
}
} }
KeyPair.prototype.__defineGetter__('privatePoint', function() { KeyPair.generate = function() {
return this.pair.getPrivate(); return new KeyPair(bcoin.ec.generate());
});
KeyPair.prototype.__defineGetter__('publicPoint', function() {
return this.pair.getPublic();
});
KeyPair.prototype.__defineGetter__('privateKey', function() {
return this.getPrivateKey();
});
KeyPair.prototype.__defineGetter__('publicKey', function() {
return this.getPublicKey();
});
KeyPair.prototype.validate = function validate() {
return this.pair.validate.apply(this.pair, arguments);
}; };
KeyPair.prototype.sign = function sign(msg) { KeyPair.prototype.sign = function sign(msg) {
return this.pair.sign.apply(this.pair, arguments); return bcoin.ec.sign(msg, this);
}; };
KeyPair.prototype.verify = function verify(msg, signature) { KeyPair.prototype.verify = function verify(msg, sig) {
return this.pair.verify.apply(this.pair, arguments); return bcoin.ec.verify(msg, sig, this);
}; };
KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) { KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) {
var privateKey; var privateKey;
if (!this._privateKey) { if (!this._privateKey) {
privateKey = this.pair.getPrivate(); privateKey = this.key.getPrivate();
if (!privateKey) if (!privateKey)
return; return;
@ -128,7 +81,7 @@ KeyPair.prototype.getPublicKey = function getPublicKey(enc) {
var publicKey; var publicKey;
if (!this._publicKey) if (!this._publicKey)
this._publicKey = new Buffer(this.pair.getPublic(this.compressed, 'array')); this._publicKey = new Buffer(this.key.getPublic(this.compressed, 'array'));
publicKey = this._publicKey; publicKey = this._publicKey;
@ -189,15 +142,11 @@ KeyPair.fromSecret = function fromSecret(privateKey) {
}; };
KeyPair.verify = function verify(msg, sig, key) { KeyPair.verify = function verify(msg, sig, key) {
try { return bcoin.ec.verify(msg, sig, key);
return bcoin.ec.verify(msg, sig, key);
} catch (e) {
return false;
}
}; };
KeyPair.sign = function sign(msg, key) { KeyPair.sign = function sign(msg, key) {
return bcoin.ec.sign(msg, key.priv); return bcoin.ec.sign(msg, key);
}; };
KeyPair.prototype.toJSON = function toJSON(passphrase) { KeyPair.prototype.toJSON = function toJSON(passphrase) {
@ -207,7 +156,7 @@ KeyPair.prototype.toJSON = function toJSON(passphrase) {
encrypted: passphrase ? true : false encrypted: passphrase ? true : false
}; };
if (this.pair.priv) { if (this.key.priv) {
json.privateKey = passphrase json.privateKey = passphrase
? utils.encrypt(this.toSecret(), passphrase) ? utils.encrypt(this.toSecret(), passphrase)
: this.toSecret(); : this.toSecret();

View File

@ -374,7 +374,7 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) {
options.keys.push(key.publicKey); options.keys.push(key.publicKey);
}, this); }, this);
address = bcoin.address(options); address = new bcoin.address(options);
this.addressMap[address.getKeyAddress()] = data.path; this.addressMap[address.getKeyAddress()] = data.path;