Skip to content

Commit

Permalink
HDNode: avoid creating multiple buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Aug 20, 2015
1 parent b866dc8 commit e3f21eb
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions src/hdnode.js
Expand Up @@ -90,23 +90,21 @@ HDNode.fromBase58 = function (string, networks) {

// 32 bytes: the chain code
var chainCode = buffer.slice(13, 45)
var data, keyPair
var keyPair

// 33 bytes: private key data (0x00 + k)
if (version === network.bip32.private) {
if (buffer.readUInt8(45) !== 0x00) throw new Error('Invalid private key')

data = buffer.slice(46, 78)
var d = BigInteger.fromBuffer(data)
var d = BigInteger.fromBuffer(buffer.slice(46, 78))

keyPair = new ECPair(d, null, {
network: network
})

// 33 bytes: public key data (0x02 + X or 0x03 + X)
} else {
data = buffer.slice(45, 78)
var Q = ecurve.Point.decodeFrom(curve, data)
var Q = ecurve.Point.decodeFrom(curve, buffer.slice(45, 78))
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.
Expand Down Expand Up @@ -194,29 +192,23 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
HDNode.prototype.derive = function (index) {
var isHardened = index >= HDNode.HIGHEST_BIT
var indexBuffer = new Buffer(4)
indexBuffer.writeUInt32BE(index, 0)

var data
var data = new Buffer(37)

// Hardened child
if (isHardened) {
if (!this.keyPair.d) throw new TypeError('Could not derive hardened child key')

// data = 0x00 || ser256(kpar) || ser32(index)
data = Buffer.concat([
this.keyPair.d.toBuffer(33),
indexBuffer
])
data[0] = 0x00
this.keyPair.d.toBuffer(32).copy(data, 1)
data.writeUInt32BE(index, 33)

// Normal child
} else {
// data = serP(point(kpar)) || ser32(index)
// = serP(Kpar) || ser32(index)
data = Buffer.concat([
this.keyPair.getPublicKeyBuffer(),
indexBuffer
])
this.keyPair.getPublicKeyBuffer().copy(data, 0)
data.writeUInt32BE(index, 33)
}

var I = createHmac('sha512', this.chainCode).update(data).digest()
Expand Down

0 comments on commit e3f21eb

Please sign in to comment.