diff --git a/lib/hdkey.js b/lib/hdkey.js index caf60b1..cc3e5ca 100644 --- a/lib/hdkey.js +++ b/lib/hdkey.js @@ -54,69 +54,13 @@ Object.defineProperty(HDKey.prototype, 'publicKey', { Object.defineProperty(HDKey.prototype, 'privateOld', { get: function() { - // Version - var version = VERSIONS.private - var buffer = new Buffer(LEN) - - // 4 bytes: version bytes - buffer.writeUInt32BE(version, 0) - - // Depth - // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, .... - buffer.writeUInt8(this.depth, 4) - - // 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) - var fingerprint = this.depth ? this.parentFingerprint : 0x00000000 - buffer.writeUInt32BE(fingerprint, 5) - - // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. - // This is encoded in Big endian. (0x00000000 if master key) - buffer.writeUInt32BE(this.index, 9) - - // 32 bytes: the chain code - this.chainCode.copy(buffer, 13) - - // 33 bytes: the public key or private key data - assert(this.privateKey, 'Missing private key') - - // 0x00 + k for private keys - buffer.writeUInt8(0, 45) - this.privateKey.copy(buffer, 46) - - - return buffer + return serialize(this, this.versions.private, Buffer.concat([new Buffer([0]), this.privateKey])) } }) Object.defineProperty(HDKey.prototype, 'publicOld', { get: function() { - // Version - var version = VERSIONS.public - var buffer = new Buffer(LEN) - - // 4 bytes: version bytes - buffer.writeUInt32BE(version, 0) - - // Depth - // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, .... - buffer.writeUInt8(this.depth, 4) - - // 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) - var fingerprint = this.depth ? this.parentFingerprint : 0x00000000 - buffer.writeUInt32BE(fingerprint, 5) - - // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. - // This is encoded in Big endian. (0x00000000 if master key) - buffer.writeUInt32BE(this.index, 9) - - // 32 bytes: the chain code - this.chainCode.copy(buffer, 13) - - // X9.62 encoding for public keys - var buf = this.publicKey - buf.copy(buffer, 45) - - return buffer + return serialize(this, this.versions.public, this.publicKey) } }) @@ -243,9 +187,30 @@ HDKey.fromMasterSeed = function(seedBuffer, versions) { return hdkey } -//temporary -function setPrivPub(hd, privKey) { - hd.priv = privKey +function serialize(hdkey, version, key) { + var buffer = new Buffer(LEN) + + // 4 bytes: version bytes + buffer.writeUInt32BE(version, 0) + + // Depth + // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, .... + buffer.writeUInt8(hdkey.depth, 4) + + // 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) + var fingerprint = hdkey.depth ? hdkey.parentFingerprint : 0x00000000 + buffer.writeUInt32BE(fingerprint, 5) + + // 4 bytes: child number. This is the number i in xi = xpar/i, with xi the key being serialized. + // This is encoded in Big endian. (0x00000000 if master key) + buffer.writeUInt32BE(hdkey.index, 9) + + // 32 bytes: the chain code + hdkey.chainCode.copy(buffer, 13) + // 0x00 + k for private keys + key.copy(buffer, 45) + + return buffer }