From 8cd97fcfe209b6e60ee8f575e1a2dcf85f535cb2 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 17 Nov 2024 00:36:41 -0800 Subject: [PATCH] Web Worker showed no performance benefit, so revert it. --- package.json | 2 +- src/lib/safe.ts | 2 ++ src/lib/wallet.ts | 13 ++++++------- test/derive-accounts.test.mjs | 4 +--- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 51bc638..ce5973c 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "url": "git+https://zoso.dev/libnemo.git" }, "scripts": { - "build": "rm -rf dist && tsc && esbuild main.min=dist/main.js global.min=dist/global.js ckd=dist/lib/ckd.js --outdir=dist --target=es2022 --format=esm --platform=node --bundle --minify --sourcemap", + "build": "rm -rf dist && tsc && esbuild main.min=dist/main.js global.min=dist/global.js --outdir=dist --target=es2022 --format=esm --platform=browser --bundle --minify --sourcemap", "test": "npm run build && node --test --test-force-exit --env-file .env", "test:coverage": "npm run test -- --experimental-test-coverage", "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" diff --git a/src/lib/safe.ts b/src/lib/safe.ts index 8654756..3a417c0 100644 --- a/src/lib/safe.ts +++ b/src/lib/safe.ts @@ -3,11 +3,13 @@ import { buffer, hex, utf8 } from './convert.js' import { Entropy } from './entropy.js' +// import { Thread } from './thread.js' const { subtle } = globalThis.crypto const ERR_MSG = 'Failed to store item in Safe' export class Safe { #storage = globalThis.sessionStorage + // #thread = new Thread(new URL('./ckd.js', import.meta.url)) /** * Encrypts data with a password and stores it in the Safe. diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 1a4f5cc..b180edb 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -1,11 +1,13 @@ // SPDX-FileCopyrightText: 2024 Chris Duncan // SPDX-License-Identifier: GPL-3.0-or-later +import blake2b from 'blake2b-wasm' import { Account } from './account.js' +import { nanoCKD } from './bip32-key-derivation.js' import { Bip39Mnemonic } from './bip39-mnemonic.js' import { ADDRESS_GAP, SEED_LENGTH_BIP44, SEED_LENGTH_BLAKE2B } from './constants.js' +import { dec, hex } from './convert.js' import { Entropy } from './entropy.js' -import { Thread } from './thread.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' import type { Ledger } from './ledger.js' @@ -231,14 +233,12 @@ abstract class Wallet { */ export class Bip44Wallet extends Wallet { static #isInternal: boolean = false - #thread: Thread constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) { if (!Bip44Wallet.#isInternal) { throw new Error(`Bip44Wallet cannot be instantiated directly. Use 'await Bip44Wallet.create()' instead.`) } super(seed, mnemonic, id) - this.#thread = new Thread(new URL('./ckd.js', import.meta.url)) Bip44Wallet.#isInternal = false } @@ -390,7 +390,7 @@ export class Bip44Wallet extends Wallet { * @returns {Promise} */ async ckd (index: number): Promise { - const key = await this.#thread.work({ type: 'bip44', seed: this.seed, index }) + const key = await nanoCKD(this.seed, index) if (typeof key !== 'string') { throw new TypeError('BIP-44 child key derivation returned invalid data') } @@ -416,14 +416,12 @@ export class Bip44Wallet extends Wallet { */ export class Blake2bWallet extends Wallet { static #isInternal: boolean = false - #thread: Thread constructor (seed: string, mnemonic?: Bip39Mnemonic, id?: string) { if (!Blake2bWallet.#isInternal) { throw new Error(`Blake2bWallet cannot be instantiated directly. Use 'await Blake2bWallet.create()' instead.`) } super(seed, mnemonic, id) - this.#thread = new Thread(new URL('./ckd.js', import.meta.url)) Blake2bWallet.#isInternal = false } @@ -537,7 +535,8 @@ export class Blake2bWallet extends Wallet { * @returns {Promise} */ async ckd (index: number): Promise { - const key = await this.#thread.work({ type: 'blake2b', seed: this.seed, index }) + const input = `${this.seed}${dec.toHex(index, 8)}` + const key = blake2b().update(hex.toBytes(input)).digest('hex') if (typeof key !== 'string') { throw new TypeError('BLAKE2b child key derivation returned invalid data') } diff --git a/test/derive-accounts.test.mjs b/test/derive-accounts.test.mjs index 49a4a47..e8e7948 100644 --- a/test/derive-accounts.test.mjs +++ b/test/derive-accounts.test.mjs @@ -9,8 +9,6 @@ import { strict as assert } from 'assert' import { NANO_TEST_VECTORS } from './TEST_VECTORS.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from '../dist/main.js' -const skip = true - // WARNING: Do not send any funds to the test vectors below // Test vectors from https://docs.nano.org/integration-guides/key-management/ and elsewhere describe('derive child accounts from the same seed', async function () { @@ -90,7 +88,7 @@ describe('Ledger device accounts', { skip: true }, async () => { }) }) -describe('child key derivation performance', { skip }, async () => { +describe('child key derivation performance', { skip: true }, async () => { it('performance test of BIP-44 ckd', async function () { const wallet = await Bip44Wallet.create(NANO_TEST_VECTORS.PASSWORD) await wallet.unlock(NANO_TEST_VECTORS.PASSWORD) -- 2.34.1