From be23e91803f3ca7c8e527c3087559a0c23b2a642 Mon Sep 17 00:00:00 2001 From: JP Richardson Date: Sat, 14 Jun 2014 11:57:29 -0500 Subject: [PATCH] lib/hdkey: cleaned up serialize() --- lib/hdkey.js | 38 ++++++++++++++------------------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/lib/hdkey.js b/lib/hdkey.js index 8099162..4bf2d46 100644 --- a/lib/hdkey.js +++ b/lib/hdkey.js @@ -182,30 +182,20 @@ HDKey.fromMasterSeed = function(seedBuffer, versions) { } 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 + // => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33) + var buffer = new Buffer(LEN) + + buffer.writeUInt32BE(version, 0) + buffer.writeUInt8(hdkey.depth, 4) + + var fingerprint = hdkey.depth ? hdkey.parentFingerprint : 0x00000000 + buffer.writeUInt32BE(fingerprint, 5) + buffer.writeUInt32BE(hdkey.index, 9) + + hdkey.chainCode.copy(buffer, 13) + key.copy(buffer, 45) + + return buffer } function hash160(buf) {