diff --git a/lib/hdkey.js b/lib/hdkey.js index e849d38..04e93ba 100644 --- a/lib/hdkey.js +++ b/lib/hdkey.js @@ -43,23 +43,23 @@ var HDKey = ("object" === typeof module && exports) || {}; (function (window, HDKey) { "use strict"; - var crypto = require("crypto"); - var bs58check = require("bs58check"); - var RIPEMD160 = require("ripemd160"); - var secp256k1 = require("secp256k1"); + let crypto = require("crypto"); + let bs58check = require("bs58check"); + let RIPEMD160 = require("ripemd160"); + let secp256k1 = require("secp256k1"); - var MASTER_SECRET = Buffer.from("Bitcoin seed", "utf8"); - var HARDENED_OFFSET = 0x80000000; - var LEN = 78; + let MASTER_SECRET = Buffer.from("Bitcoin seed", "utf8"); + let HARDENED_OFFSET = 0x80000000; + let LEN = 78; // Bitcoin hardcoded by default, can use package `coininfo` for others - var BITCOIN_VERSIONS = { private: 0x0488ade4, public: 0x0488b21e }; + let BITCOIN_VERSIONS = { private: 0x0488ade4, public: 0x0488b21e }; HDKey.create = function (versions) { /** @type {hdkey} */ - var hdkey = {}; + let hdkey = {}; /** @type {Buffer?} */ - var _privateKey = null; + let _privateKey = null; hdkey.versions = versions || BITCOIN_VERSIONS; hdkey.depth = 0; @@ -95,7 +95,7 @@ var HDKey = ("object" === typeof module && exports) || {}; ); assert(secp256k1.publicKeyVerify(value) === true, "Invalid public key"); // force compressed point (performs public key verification) - var publicKey = + let publicKey = value.length === 65 ? secp256k1.publicKeyConvert(value, true) : value; hdkey._setPublicKey(publicKey); }; @@ -138,16 +138,16 @@ var HDKey = ("object" === typeof module && exports) || {}; return hdkey; } - var entries = path.split("/"); - var _hdkey = hdkey; + let entries = path.split("/"); + let _hdkey = hdkey; entries.forEach(function (c, i) { if (i === 0) { assert(/^[mM]{1}/.test(c), 'Path must start with "m" or "M"'); return; } - var hardened = c.length > 1 && c[c.length - 1] === "'"; - var childIndex = parseInt(c, 10); // & (HARDENED_OFFSET - 1) + let hardened = c.length > 1 && c[c.length - 1] === "'"; + let childIndex = parseInt(c, 10); // & (HARDENED_OFFSET - 1) assert(childIndex < HARDENED_OFFSET, "Invalid index"); if (hardened) { childIndex += HARDENED_OFFSET; @@ -160,11 +160,11 @@ var HDKey = ("object" === typeof module && exports) || {}; }; hdkey.deriveChild = function (index) { - var isHardened = index >= HARDENED_OFFSET; - var indexBuffer = Buffer.allocUnsafe(4); + let isHardened = index >= HARDENED_OFFSET; + let indexBuffer = Buffer.allocUnsafe(4); indexBuffer.writeUInt32BE(index, 0); - var data; + let data; if (isHardened) { // Hardened child @@ -172,8 +172,8 @@ var HDKey = ("object" === typeof module && exports) || {}; throw new Error("Could not derive hardened child key"); } - var pk = _privateKey; - var zb = Buffer.alloc(1, 0); + let pk = _privateKey; + let zb = Buffer.alloc(1, 0); pk = Buffer.concat([zb, pk]); // data = 0x00 || ser256(kpar) || ser32(index) @@ -185,14 +185,14 @@ var HDKey = ("object" === typeof module && exports) || {}; data = Buffer.concat([hdkey.publicKey, indexBuffer]); } - var I = crypto + let I = crypto .createHmac("sha512", hdkey.chainCode) .update(data) .digest(); - var IL = I.slice(0, 32); - var IR = I.slice(32); + let IL = I.slice(0, 32); + let IR = I.slice(32); - var hd = HDKey.create(hdkey.versions); + let hd = HDKey.create(hdkey.versions); // Private parent key -> private child key if (_privateKey) { @@ -275,14 +275,14 @@ var HDKey = ("object" === typeof module && exports) || {}; }; HDKey.fromMasterSeed = function (seedBuffer, versions) { - var I = crypto + let I = crypto .createHmac("sha512", MASTER_SECRET) .update(seedBuffer) .digest(); - var IL = I.slice(0, 32); - var IR = I.slice(32); + let IL = I.slice(0, 32); + let IR = I.slice(32); - var hdkey = HDKey.create(versions); + let hdkey = HDKey.create(versions); hdkey.chainCode = IR; hdkey.setPrivateKey(IL); @@ -293,11 +293,11 @@ var HDKey = ("object" === typeof module && exports) || {}; // => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33) versions = versions || BITCOIN_VERSIONS; skipVerification = skipVerification || false; - var hdkey = HDKey.create(versions); + let hdkey = HDKey.create(versions); - var keyBuffer = bs58check.decode(base58key); + let keyBuffer = bs58check.decode(base58key); - var version = keyBuffer.readUInt32BE(0); + let version = keyBuffer.readUInt32BE(0); assert( version === versions.private || version === versions.public, "Version mismatch: does not match private or public", @@ -308,7 +308,7 @@ var HDKey = ("object" === typeof module && exports) || {}; hdkey.index = keyBuffer.readUInt32BE(9); hdkey.chainCode = keyBuffer.slice(13, 45); - var key = keyBuffer.slice(45); + let key = keyBuffer.slice(45); if (key.readUInt8(0) === 0) { // private assert( @@ -352,12 +352,12 @@ var HDKey = ("object" === typeof module && exports) || {}; */ function serialize(hdkey, version, key) { // => version(4) || depth(1) || fingerprint(4) || index(4) || chain(32) || key(33) - var buffer = Buffer.allocUnsafe(LEN); + let buffer = Buffer.allocUnsafe(LEN); buffer.writeUInt32BE(version, 0); buffer.writeUInt8(hdkey.depth, 4); - var fingerprint = hdkey.depth ? hdkey.parentFingerprint : 0x00000000; + let fingerprint = hdkey.depth ? hdkey.parentFingerprint : 0x00000000; buffer.writeUInt32BE(fingerprint, 5); buffer.writeUInt32BE(hdkey.index, 9); @@ -372,7 +372,7 @@ var HDKey = ("object" === typeof module && exports) || {}; * @returns {Buffer} */ function hash160(buf) { - var sha = crypto.createHash("sha256").update(buf).digest(); + let sha = crypto.createHash("sha256").update(buf).digest(); return new RIPEMD160().update(sha).digest(); }