Skip to content

Commit

Permalink
ref: var => let (for upcoming forEach => async for changes)
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ ONeal committed Feb 3, 2023
1 parent 7472aad commit 2d19b28
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions lib/hdkey.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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;
Expand All @@ -160,20 +160,20 @@ 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
if (!_privateKey) {
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)
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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",
Expand All @@ -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(
Expand Down Expand Up @@ -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);

Expand All @@ -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();
}

Expand Down

0 comments on commit 2d19b28

Please sign in to comment.