From 549e2b906343d77b65b70b914f7455bb7eae3823 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sat, 16 Nov 2024 17:16:39 -0800 Subject: [PATCH] Start substitution of blakejs with blake2b package. --- package-lock.json | 41 +++++++++++++++++++++++++++++++++++++++++ package.json | 2 ++ src/lib/ckd.ts | 9 +++++---- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 8fed5d2..6f7bc96 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,9 +9,11 @@ "version": "0.0.19", "license": "(GPL-3.0-or-later AND MIT)", "dependencies": { + "blake2b": "^2.1.4", "blakejs": "^1.2.1" }, "devDependencies": { + "@types/blake2b": "^2.1.3", "@types/node": "^22.8.6", "@types/w3c-web-hid": "^1.0.6", "@types/w3c-web-usb": "^1.0.10", @@ -517,6 +519,13 @@ "license": "Apache-2.0", "optional": true }, + "node_modules/@types/blake2b": { + "version": "2.1.3", + "resolved": "https://registry.npmjs.org/@types/blake2b/-/blake2b-2.1.3.tgz", + "integrity": "sha512-MFCdX0MNxFBP/xEILO5Td0kv6nI7+Q2iRWZbTL/yzH2/eDVZS5Wd1LHdsmXClvsCyzqaZfHFzZaN6BUeUCfSDA==", + "dev": true, + "license": "MIT" + }, "node_modules/@types/node": { "version": "22.8.6", "resolved": "https://registry.npmjs.org/@types/node/-/node-22.8.6.tgz", @@ -548,6 +557,32 @@ "dev": true, "license": "MIT" }, + "node_modules/b4a": { + "version": "1.6.7", + "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.6.7.tgz", + "integrity": "sha512-OnAYlL5b7LEkALw87fUVafQw5rVR9RjwGd4KUwNQ6DrrNmaVaUCgLipfVlzrPQ4tWOR9P0IXGNOx50jYCCdSJg==", + "license": "Apache-2.0" + }, + "node_modules/blake2b": { + "version": "2.1.4", + "resolved": "https://registry.npmjs.org/blake2b/-/blake2b-2.1.4.tgz", + "integrity": "sha512-AyBuuJNI64gIvwx13qiICz6H6hpmjvYS5DGkG6jbXMOT8Z3WUJ3V1X0FlhIoT1b/5JtHE3ki+xjtMvu1nn+t9A==", + "license": "ISC", + "dependencies": { + "blake2b-wasm": "^2.4.0", + "nanoassert": "^2.0.0" + } + }, + "node_modules/blake2b-wasm": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/blake2b-wasm/-/blake2b-wasm-2.4.0.tgz", + "integrity": "sha512-S1kwmW2ZhZFFFOghcx73+ZajEfKBqhP82JMssxtLVMxlaPea1p9uoLiUZ5WYyHn0KddwbLc+0vh4wR0KBNoT5w==", + "license": "MIT", + "dependencies": { + "b4a": "^1.0.1", + "nanoassert": "^2.0.0" + } + }, "node_modules/blakejs": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.2.1.tgz", @@ -604,6 +639,12 @@ "node": ">=0.8.x" } }, + "node_modules/nanoassert": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/nanoassert/-/nanoassert-2.0.0.tgz", + "integrity": "sha512-7vO7n28+aYO4J+8w96AzhmU8G+Y/xpPDJz/se19ICsqj/momRbb9mh9ZUtkoJ5X3nTnPdhEJyc0qnM6yAsHBaA==", + "license": "ISC" + }, "node_modules/rxjs": { "version": "7.8.1", "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.1.tgz", diff --git a/package.json b/package.json index fc3ad00..8350511 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ "test:coverage:report": "npm run test:coverage -- --test-reporter=lcov --test-reporter-destination=coverage.info && genhtml coverage.info --output-directory test/coverage && rm coverage.info && xdg-open test/coverage/index.html" }, "dependencies": { + "blake2b": "^2.1.4", "blakejs": "^1.2.1" }, "optionalDependencies": { @@ -53,6 +54,7 @@ "@ledgerhq/hw-transport-webusb": "^6.29.4" }, "devDependencies": { + "@types/blake2b": "^2.1.3", "@types/node": "^22.8.6", "@types/w3c-web-hid": "^1.0.6", "@types/w3c-web-usb": "^1.0.10", diff --git a/src/lib/ckd.ts b/src/lib/ckd.ts index 377aacb..8e40b9d 100644 --- a/src/lib/ckd.ts +++ b/src/lib/ckd.ts @@ -1,9 +1,9 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later +import blake2b from 'blake2b' import { nanoCKD } from './bip32-key-derivation.js' -import { bytes, dec } from './convert.js' -import Tools from './tools.js' +import { dec, hex } from './convert.js' if (globalThis.Worker == null) { const { isMainThread, parentPort } = await import('node:worker_threads') @@ -50,6 +50,7 @@ async function ckdBip44 (seed: string, index: number): Promise { * @returns {Promise} */ async function ckdBlake2b (seed: string, index: number): Promise { - const hash = await Tools.blake2b([seed, dec.toHex(index, 8)]) - return bytes.toHex(hash) + const input = `${seed}${dec.toHex(index, 8)}` + const hash = blake2b(32).update(hex.toBytes(input)).digest('hex') + return hash } -- 2.34.1