]> zoso.dev Git - libnemo.git/commitdiff
Web worker doesn't pass complex objects like Account, so return private key instead...
authorChris Duncan <chris@zoso.dev>
Tue, 12 Nov 2024 21:51:14 +0000 (13:51 -0800)
committerChris Duncan <chris@zoso.dev>
Tue, 12 Nov 2024 21:51:14 +0000 (13:51 -0800)
package.json
src/lib/ckd.ts
src/lib/pool.ts
src/lib/wallet.ts

index 7dee5a72231f659ce0155f8d152534618996d63c..d46f9d6aeb2ed57c82ded6aac5c33dcf06dfda31 100644 (file)
@@ -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"
index 2b7508bcb20d05daf6d5c3809218bf76eaf3ca7d..3338d494051b9aea03b48e0485cd28403fec782e 100644 (file)
@@ -14,19 +14,14 @@ import type { Ledger } from './ledger.js'
 * @returns {Promise<Account>}
 */
 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<Account>}
 */
-async function ckdBip44 (seed: string, index: number): Promise<Account> {
-       const key = await nanoCKD(seed, index)
-       return await Account.fromPrivateKey(key, index)
+async function ckdBip44 (seed: string, index: number): Promise<string> {
+       return nanoCKD(seed, index)
 }
 
 /**
@@ -49,22 +43,7 @@ async function ckdBip44 (seed: string, index: number): Promise<Account> {
 * @param {number} index - Index of the account
 * @returns {Promise<Account>}
 */
-async function ckdBlake2b (seed: string, index: number): Promise<Account> {
+async function ckdBlake2b (seed: string, index: number): Promise<string> {
        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<Account>}
-*/
-export async function ckdLedger (ledger: Ledger, index: number): Promise<Account> {
-       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)
 }
index fbdc10a50baa1d1f3908804ff133d217e6df6187..6d4690fca5eb2b7af74e7fc81a012c7207ee0480 100644 (file)
@@ -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)
        }
 
index 5da5323d25ea0fdfdbab54dcdede827aa96164d8..bebb491c40d8283017f624ca9c0656af57c20ae1 100644 (file)
@@ -393,9 +393,11 @@ export class Bip44Wallet extends Wallet {
        * @returns {Promise<Account>}\r
        */\r
        async ckd (index: number): Promise<Account> {\r
-               // const key = await nanoCKD(this.seed, index)\r
-               // return await Account.fromPrivateKey(key, index)\r
-               return await ckdPool.work({ type: 'bip44', seed: this.seed, index })\r
+               const key = await ckdPool.work({ type: 'bip44', seed: this.seed, index })\r
+               if (typeof key !== 'string') {\r
+                       throw new TypeError('BIP-44 child key derivation returned invalid data')\r
+               }\r
+               return await Account.fromPrivateKey(key, index)\r
        }\r
 }\r
 \r
@@ -536,10 +538,11 @@ export class Blake2bWallet extends Wallet {
        * @returns {Promise<Account>}\r
        */\r
        async ckd (index: number): Promise<Account> {\r
-               const hash = await Tools.blake2b([this.seed, dec.toHex(index, 8)])\r
-               const key = bytes.toHex(hash)\r
+               const key = await ckdPool.work({ type: 'blake2b', seed: this.seed, index })\r
+               if (typeof key !== 'string') {\r
+                       throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
+               }\r
                return await Account.fromPrivateKey(key, index)\r
-               // return await ckdPool.work({ type: 'blake2b', seed: this.seed, index })\r
        }\r
 }\r
 \r