improve keypair.
This commit is contained in:
parent
f4f6c79bfd
commit
83239112e4
@ -33,7 +33,7 @@ function Address(options) {
|
||||
this.derived = !!options.derived;
|
||||
this.addressMap = null;
|
||||
|
||||
this.key = options.pair || options.key || bcoin.keypair(options);
|
||||
this.key = options.key || bcoin.keypair(options);
|
||||
this.path = options.path;
|
||||
this.change = !!options.change;
|
||||
this.index = options.index;
|
||||
|
||||
@ -15,17 +15,20 @@ var ec = exports;
|
||||
*/
|
||||
|
||||
ec.generate = function generate(options) {
|
||||
var priv, pub;
|
||||
var key, priv, pub;
|
||||
|
||||
if (bcoin.secp256k1 && bcoin.crypto) {
|
||||
do {
|
||||
priv = bcoin.crypto.randomBytes(32);
|
||||
} while (!bcoin.secp256k1.privateKeyVerify(priv));
|
||||
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) {
|
||||
|
||||
@ -614,7 +614,7 @@ HDPrivateKey.prototype._seed = function _seed(seed) {
|
||||
|
||||
HDPrivateKey.prototype._generate = function _generate(privateKey, entropy) {
|
||||
if (!privateKey)
|
||||
privateKey = bcoin.ec.generate().getPrivateKey();
|
||||
privateKey = bcoin.ec.generate().privateKey;
|
||||
|
||||
if (utils.isHex(privateKey))
|
||||
privateKey = new Buffer(privateKey, 'hex');
|
||||
@ -672,7 +672,7 @@ HDPrivateKey.prototype._unbuild = function _unbuild(xkey) {
|
||||
HDPrivateKey.prototype._build = function _build(data) {
|
||||
var sequence = new Buffer(82);
|
||||
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.depth, sequence, off);
|
||||
@ -688,9 +688,9 @@ HDPrivateKey.prototype._build = function _build(data) {
|
||||
|
||||
xprivkey = utils.toBase58(sequence);
|
||||
|
||||
pair = bcoin.keypair({ privateKey: data.privateKey });
|
||||
privateKey = pair.getPrivateKey();
|
||||
publicKey = pair.getPublicKey();
|
||||
key = bcoin.keypair({ privateKey: data.privateKey });
|
||||
privateKey = key.getPrivateKey();
|
||||
publicKey = key.getPublicKey();
|
||||
|
||||
size = constants.hd.parentFingerPrintSize;
|
||||
fingerPrint = utils.ripesha(publicKey).slice(0, size);
|
||||
@ -722,7 +722,7 @@ HDPrivateKey.prototype._build = function _build(data) {
|
||||
this.hdPrivateKey = this;
|
||||
|
||||
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) {
|
||||
@ -1081,11 +1081,11 @@ HDPublicKey.prototype._build = function _build(data) {
|
||||
this.fingerPrint = fingerPrint;
|
||||
|
||||
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) {
|
||||
var cached, data, hash, leftPart, chainCode, pair, point, publicKey, child;
|
||||
var cached, data, hash, leftPart, chainCode, key, point, publicKey, child;
|
||||
var off = 0;
|
||||
|
||||
if (typeof index === 'string')
|
||||
@ -1110,8 +1110,8 @@ HDPublicKey.prototype.derive = function derive(index, hardened) {
|
||||
leftPart = new bn(hash.slice(0, 32));
|
||||
chainCode = hash.slice(32, 64);
|
||||
|
||||
pair = bcoin.keypair({ publicKey: this.publicKey });
|
||||
point = ec.curve.g.mul(leftPart).add(pair.publicPoint);
|
||||
key = bcoin.keypair({ publicKey: this.publicKey });
|
||||
point = ec.curve.g.mul(leftPart).add(key.publicPoint);
|
||||
publicKey = bcoin.keypair({ publicKey: point }).getPublicKey();
|
||||
|
||||
child = new HDPublicKey({
|
||||
@ -1162,10 +1162,6 @@ HDPublicKey.prototype.deriveString = function deriveString(path) {
|
||||
*/
|
||||
|
||||
[HDPrivateKey, HDPublicKey].forEach(function(HD) {
|
||||
HD.prototype.validate = function validate() {
|
||||
return this.pair.validate.apply(this.pair, arguments);
|
||||
};
|
||||
|
||||
HD.prototype.getPublicKey = function getPublicKey() {
|
||||
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);
|
||||
};
|
||||
|
||||
HD.prototype.sign = function sign(msg) {
|
||||
return this.pair.sign.apply(this.pair, arguments);
|
||||
HD.prototype.sign = function sign() {
|
||||
return this.key.sign.apply(this.key, arguments);
|
||||
};
|
||||
|
||||
HD.prototype.verify = function verify(msg, signature) {
|
||||
return this.pair.verify.apply(this.pair, arguments);
|
||||
HD.prototype.verify = function verify() {
|
||||
return this.key.verify.apply(this.key, arguments);
|
||||
};
|
||||
|
||||
HD.prototype.__defineGetter__('publicPoint', function() {
|
||||
return this.pair.publicPoint;
|
||||
return this.key.publicPoint;
|
||||
});
|
||||
|
||||
HD.prototype.__defineGetter__('privatePoint', function() {
|
||||
return this.pair.privatePoint;
|
||||
return this.key.privatePoint;
|
||||
});
|
||||
|
||||
HD.prototype.compressed = true;
|
||||
|
||||
@ -22,88 +22,41 @@ function KeyPair(options) {
|
||||
if (!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.pair = null;
|
||||
this.key = null;
|
||||
this.compressed = options.compressed !== false;
|
||||
|
||||
if (options.passphrase)
|
||||
options.entropy = utils.sha256(options.passphrase);
|
||||
if (!options.privateKey && !options.publicKey)
|
||||
throw new Error('No options for keypair');
|
||||
|
||||
if (options.privateKey instanceof bcoin.hd.privateKey) {
|
||||
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;
|
||||
} 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
|
||||
});
|
||||
}
|
||||
this.key = bcoin.ecdsa.keyPair({
|
||||
priv: options.privateKey,
|
||||
pub: options.publicKey
|
||||
});
|
||||
|
||||
this.privatePoint = this.key.getPrivate();
|
||||
this.publicPoint = this.key.getPublic();
|
||||
this.privateKey = this.getPrivateKey();
|
||||
this.publicKey = this.getPublicKey();
|
||||
}
|
||||
|
||||
KeyPair.prototype.__defineGetter__('privatePoint', function() {
|
||||
return this.pair.getPrivate();
|
||||
});
|
||||
|
||||
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.generate = function() {
|
||||
return new KeyPair(bcoin.ec.generate());
|
||||
};
|
||||
|
||||
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) {
|
||||
return this.pair.verify.apply(this.pair, arguments);
|
||||
KeyPair.prototype.verify = function verify(msg, sig) {
|
||||
return bcoin.ec.verify(msg, sig, this);
|
||||
};
|
||||
|
||||
KeyPair.prototype.getPrivateKey = function getPrivateKey(enc) {
|
||||
var privateKey;
|
||||
|
||||
if (!this._privateKey) {
|
||||
privateKey = this.pair.getPrivate();
|
||||
privateKey = this.key.getPrivate();
|
||||
|
||||
if (!privateKey)
|
||||
return;
|
||||
@ -128,7 +81,7 @@ KeyPair.prototype.getPublicKey = function getPublicKey(enc) {
|
||||
var 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;
|
||||
|
||||
@ -189,15 +142,11 @@ KeyPair.fromSecret = function fromSecret(privateKey) {
|
||||
};
|
||||
|
||||
KeyPair.verify = function verify(msg, sig, key) {
|
||||
try {
|
||||
return bcoin.ec.verify(msg, sig, key);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return bcoin.ec.verify(msg, sig, 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) {
|
||||
@ -207,7 +156,7 @@ KeyPair.prototype.toJSON = function toJSON(passphrase) {
|
||||
encrypted: passphrase ? true : false
|
||||
};
|
||||
|
||||
if (this.pair.priv) {
|
||||
if (this.key.priv) {
|
||||
json.privateKey = passphrase
|
||||
? utils.encrypt(this.toSecret(), passphrase)
|
||||
: this.toSecret();
|
||||
|
||||
@ -374,7 +374,7 @@ Wallet.prototype.deriveAddress = function deriveAddress(change, index) {
|
||||
options.keys.push(key.publicKey);
|
||||
}, this);
|
||||
|
||||
address = bcoin.address(options);
|
||||
address = new bcoin.address(options);
|
||||
|
||||
this.addressMap[address.getKeyAddress()] = data.path;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user