faster hd deserialization.

This commit is contained in:
Christopher Jeffrey 2016-02-23 20:16:36 -08:00
parent 72a877cc49
commit 5cb06c6cbd
3 changed files with 26 additions and 30 deletions

View File

@ -46,10 +46,6 @@ ec.random = function random(size) {
return new Buffer(elliptic.rand(size));
};
bn.prototype.toBuffer = function toBuffer(order, size) {
return new Buffer(this.toArray(order, size));
};
ec.verify = function verify(msg, sig, key, historical) {
if (!Buffer.isBuffer(sig))
return false;
@ -170,8 +166,7 @@ function getLength(buf, p) {
ec.isLowS = function isLowS(sig) {
if (!sig.s) {
if (!Buffer.isBuffer(sig))
return false;
assert(Buffer.isBuffer(sig));
try {
sig = new bcoin.ecdsa.signature(sig);

View File

@ -611,24 +611,24 @@ HDPrivateKey.prototype._unbuild = function _unbuild(xkey) {
var off = 0;
var hash;
data.version = utils.readU32BE(raw, off);
data.version = raw.slice(off, off + 4);
off += 4;
data.depth = raw[off];
data.depth = raw.slice(off, off + 1);
off += 1;
data.parentFingerPrint = utils.readU32BE(raw, off);
data.parentFingerPrint = raw.slice(off, off + 4);
off += 4;
data.childIndex = utils.readU32BE(raw, off);
data.childIndex = raw.slice(off, off + 4);
off += 4;
data.chainCode = raw.slice(off, off + 32);
off += data.chainCode.length;
off += 32;
off += 1; // nul byte
data.privateKey = raw.slice(off, off + 32);
off += data.privateKey.length;
data.checksum = utils.readU32BE(raw, off);
off += 32;
data.checksum = raw.slice(off, off + 4);
off += 4;
hash = utils.dsha256(raw.slice(0, -4));
if (data.checksum !== utils.readU32BE(hash, 0))
hash = utils.dsha256(raw.slice(0, -4)).slice(0, 4);
if (!utils.isEqual(data.checksum, hash))
throw new Error('checksum mismatch');
if (data.version === network.main.prefixes.xprivkey)
@ -989,23 +989,23 @@ HDPublicKey.prototype._unbuild = function _unbuild(xkey) {
var off = 0;
var hash;
data.version = utils.readU32BE(raw, off);
data.version = raw.slice(off, off + 4);
off += 4;
data.depth = raw[off];
data.depth = raw.slice(off, off + 1);
off += 1;
data.parentFingerPrint = utils.readU32BE(raw, off);
data.parentFingerPrint = raw.slice(off, off + 4);
off += 4;
data.childIndex = utils.readU32BE(raw, off);
data.childIndex = raw.slice(off, off + 4);
off += 4;
data.chainCode = raw.slice(off, off + 32);
off += data.chainCode.length;
off += 32;
data.publicKey = raw.slice(off, off + 33);
off += data.publicKey.length;
data.checksum = utils.readU32BE(raw, off);
off += 33;
data.checksum = raw.slice(off, off + 4);
off += 4;
hash = utils.dsha256(raw.slice(0, -4));
if (data.checksum !== utils.readU32BE(hash, 0))
hash = utils.dsha256(raw.slice(0, -4)).slice(0, 4);
if (!utils.isEqual(data.checksum, hash))
throw new Error('checksum mismatch');
if (data.version === network.main.prefixes.xpubkey)
@ -1153,11 +1153,11 @@ HDPublicKey.prototype.deriveString = function deriveString(path) {
};
HD.prototype.sign = function sign() {
return this.key.sign.apply(this.key, arguments);
return bcoin.keypair.prototype.sign.apply(this, arguments);
};
HD.prototype.verify = function verify() {
return this.key.verify.apply(this.key, arguments);
return bcoin.keypair.prototype.verify.apply(this, arguments);
};
HD.prototype.compressed = true;
@ -1167,10 +1167,6 @@ HDPrivateKey.prototype.toSecret = function toSecret() {
return bcoin.keypair.toSecret.call(this);
};
HDPrivateKey.fromSecret = function fromSecret(privateKey) {
return bcoin.keypair.fromSecret(privateKey);
};
/**
* Helpers
*/

View File

@ -1622,3 +1622,8 @@ utils.getMerkleRoot = function getMerkleRoot(items) {
return tree[tree.length - 1];
};
// Hook into bn here to ensure we get a toBuffer() method.
bn.prototype.toBuffer = function toBuffer(order, size) {
return new Buffer(this.toArray(order, size));
};