HDNode: avoid creating multiple buffers

This commit is contained in:
Daniel Cousens 2015-08-20 14:23:27 +10:00
parent b866dc8f78
commit e3f21ebeb2

View file

@ -90,14 +90,13 @@ HDNode.fromBase58 = function (string, networks) {
// 32 bytes: the chain code // 32 bytes: the chain code
var chainCode = buffer.slice(13, 45) var chainCode = buffer.slice(13, 45)
var data, keyPair var keyPair
// 33 bytes: private key data (0x00 + k) // 33 bytes: private key data (0x00 + k)
if (version === network.bip32.private) { if (version === network.bip32.private) {
if (buffer.readUInt8(45) !== 0x00) throw new Error('Invalid private key') if (buffer.readUInt8(45) !== 0x00) throw new Error('Invalid private key')
data = buffer.slice(46, 78) var d = BigInteger.fromBuffer(buffer.slice(46, 78))
var d = BigInteger.fromBuffer(data)
keyPair = new ECPair(d, null, { keyPair = new ECPair(d, null, {
network: network network: network
@ -105,8 +104,7 @@ HDNode.fromBase58 = function (string, networks) {
// 33 bytes: public key data (0x02 + X or 0x03 + X) // 33 bytes: public key data (0x02 + X or 0x03 + X)
} else { } else {
data = buffer.slice(45, 78) var Q = ecurve.Point.decodeFrom(curve, buffer.slice(45, 78))
var Q = ecurve.Point.decodeFrom(curve, data)
if (!Q.compressed) throw new Error('Invalid public key') if (!Q.compressed) throw new Error('Invalid public key')
// Verify that the X coordinate in the public point corresponds to a point on the curve. // Verify that the X coordinate in the public point corresponds to a point on the curve.
@ -194,29 +192,23 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions // https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
HDNode.prototype.derive = function (index) { HDNode.prototype.derive = function (index) {
var isHardened = index >= HDNode.HIGHEST_BIT var isHardened = index >= HDNode.HIGHEST_BIT
var indexBuffer = new Buffer(4) var data = new Buffer(37)
indexBuffer.writeUInt32BE(index, 0)
var data
// Hardened child // Hardened child
if (isHardened) { if (isHardened) {
if (!this.keyPair.d) throw new TypeError('Could not derive hardened child key') if (!this.keyPair.d) throw new TypeError('Could not derive hardened child key')
// data = 0x00 || ser256(kpar) || ser32(index) // data = 0x00 || ser256(kpar) || ser32(index)
data = Buffer.concat([ data[0] = 0x00
this.keyPair.d.toBuffer(33), this.keyPair.d.toBuffer(32).copy(data, 1)
indexBuffer data.writeUInt32BE(index, 33)
])
// Normal child // Normal child
} else { } else {
// data = serP(point(kpar)) || ser32(index) // data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index) // = serP(Kpar) || ser32(index)
data = Buffer.concat([ this.keyPair.getPublicKeyBuffer().copy(data, 0)
this.keyPair.getPublicKeyBuffer(), data.writeUInt32BE(index, 33)
indexBuffer
])
} }
var I = createHmac('sha512', this.chainCode).update(data).digest() var I = createHmac('sha512', this.chainCode).update(data).digest()