From 18300f5f85e8e11beb643fb176b78413d0bc2695 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Tue, 12 Nov 2024 13:51:14 -0800 Subject: [PATCH] Web worker doesn't pass complex objects like Account, so return private key instead and construct the account in the wallet. --- package.json | 2 +- src/lib/ckd.ts | 33 ++++++--------------------------- src/lib/pool.ts | 2 -- src/lib/wallet.ts | 15 +++++++++------ 4 files changed, 16 insertions(+), 36 deletions(-) diff --git a/package.json b/package.json index 7dee5a7..d46f9d6 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 --outdir=dist --target=es2022 --format=esm --platform=browser --bundle --minify --sourcemap", + "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=browser --bundle --minify --sourcemap", "test": "npm run build && node --test --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/ckd.ts b/src/lib/ckd.ts index 2b7508b..3338d49 100644 --- a/src/lib/ckd.ts +++ b/src/lib/ckd.ts @@ -14,19 +14,14 @@ import type { Ledger } from './ledger.js' * @returns {Promise} */ globalThis.onmessage = (event) => { - let result = null const { type, seed, index } = event.data switch (type) { case 'bip44': { - result = ckdBip44(seed, index).then(postMessage) + ckdBip44(seed, index).then(postMessage) break } case 'blake2b': { - result = ckdBlake2b(seed, index).then(postMessage) - break - } - case 'ledger': { - result = ckdLedger(seed, index).then(postMessage) + ckdBlake2b(seed, index).then(postMessage) break } } @@ -38,9 +33,8 @@ globalThis.onmessage = (event) => { * @param {number} index - Index of the account * @returns {Promise} */ -async function ckdBip44 (seed: string, index: number): Promise { - const key = await nanoCKD(seed, index) - return await Account.fromPrivateKey(key, index) +async function ckdBip44 (seed: string, index: number): Promise { + return nanoCKD(seed, index) } /** @@ -49,22 +43,7 @@ async function ckdBip44 (seed: string, index: number): Promise { * @param {number} index - Index of the account * @returns {Promise} */ -async function ckdBlake2b (seed: string, index: number): Promise { +async function ckdBlake2b (seed: string, index: number): Promise { const hash = await Tools.blake2b([seed, dec.toHex(index, 8)]) - const key = bytes.toHex(hash) - return await Account.fromPrivateKey(key, index) -} - -/** -* Gets the public key for an account from the Ledger device. -* -* @param {number} index - Index of the account -* @returns {Promise} -*/ -export async function ckdLedger (ledger: Ledger, index: number): Promise { - const { status, publicKey } = await ledger.account(index) - if (status === 'OK' && publicKey != null) { - return await Account.fromPublicKey(publicKey, index) - } - throw new Error(`Error getting Ledger account: ${status}`) + return bytes.toHex(hash) } diff --git a/src/lib/pool.ts b/src/lib/pool.ts index fbdc10a..6d4690f 100644 --- a/src/lib/pool.ts +++ b/src/lib/pool.ts @@ -41,7 +41,6 @@ export class Pool { #assign (thread: Thread, task: Task) { thread.isAvailable = false thread.worker.onmessage = (event) => { - console.log('work done') if (this.#tasks.length > 0) { const next = this.#tasks.shift() this.#assign(thread, next) @@ -50,7 +49,6 @@ export class Pool { } task.resolve(event.data) } - console.log('posting') thread.worker.postMessage(task.data) } diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 5da5323..bebb491 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -393,9 +393,11 @@ export class Bip44Wallet extends Wallet { * @returns {Promise} */ async ckd (index: number): Promise { - // const key = await nanoCKD(this.seed, index) - // return await Account.fromPrivateKey(key, index) - return await ckdPool.work({ type: 'bip44', seed: this.seed, index }) + const key = await ckdPool.work({ type: 'bip44', seed: this.seed, index }) + if (typeof key !== 'string') { + throw new TypeError('BIP-44 child key derivation returned invalid data') + } + return await Account.fromPrivateKey(key, index) } } @@ -536,10 +538,11 @@ export class Blake2bWallet extends Wallet { * @returns {Promise} */ async ckd (index: number): Promise { - const hash = await Tools.blake2b([this.seed, dec.toHex(index, 8)]) - const key = bytes.toHex(hash) + const key = await ckdPool.work({ type: 'blake2b', seed: this.seed, index }) + if (typeof key !== 'string') { + throw new TypeError('BLAKE2b child key derivation returned invalid data') + } return await Account.fromPrivateKey(key, index) - // return await ckdPool.work({ type: 'blake2b', seed: this.seed, index }) } } -- 2.34.1