diff --git a/lib/bcoin/ec.js b/lib/bcoin/ec.js index 594d42fb..40fa01de 100644 --- a/lib/bcoin/ec.js +++ b/lib/bcoin/ec.js @@ -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); diff --git a/lib/bcoin/hd.js b/lib/bcoin/hd.js index 4b7add0b..f5959bc1 100644 --- a/lib/bcoin/hd.js +++ b/lib/bcoin/hd.js @@ -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 */ diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index c3596fe7..89cfe454 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -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)); +};