From 78f9ca97d9c335b34fc3ae9a17e568bde6e9d7e2 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 28 Nov 2024 02:40:52 -0800 Subject: [PATCH] Refactor wallet account methods to start building out for NaCl worker implementation. --- src/lib/wallet.ts | 61 +++++++++++++++++++++------------------------- src/lib/workers.ts | 4 +-- 2 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 03e0974..e052176 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -9,9 +9,14 @@ import { Entropy } from './entropy.js' import { Pool } from './pool.js' import { Rpc } from './rpc.js' import { Safe } from './safe.js' -import { ckdBip44 } from './workers.js' +import { ckdBip44, nanoNacl } from './workers.js' import type { Ledger } from './ledger.js' +type KeyPair = { + publicKey?: string, + privateKey?: string, + index?: number +} /** * Represents a wallet containing numerous Nano accounts derived from a single * source, the form of which can vary based on the type of wallet. The Wallet @@ -24,6 +29,7 @@ abstract class Wallet { #id: Entropy #locked: boolean = true #mnemonic: Bip39Mnemonic | null + #pool: Pool #safe: Safe #seed: string | null get id () { return this.#id.hex } @@ -42,7 +48,7 @@ abstract class Wallet { return '' } - abstract ckd (index: number | number[]): Promise | Promise + abstract ckd (index: number | number[]): Promise | Promise constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) { if (this.constructor === Wallet) { @@ -53,6 +59,7 @@ abstract class Wallet { ? new Entropy(id) : new Entropy(16) this.#mnemonic = mnemonic ?? null + this.#pool = new Pool(nanoNacl) this.#safe = new Safe() this.#seed = seed ?? null } @@ -89,13 +96,19 @@ abstract class Wallet { if (indexes.length > 0) { let results = await this.ckd(indexes) if (!Array.isArray(results)) results = [results] - for (const result of results) { - if (result.index == null) { - throw new RangeError('Account key derived but index missing') - } else { - this.#accounts[result.index] = result - } + const data: any = [] + results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index })) + let now = performance.now() + const keypairs: [{ publicKey: string, privateKey: string, index: number }] = await this.#pool.work(data) + console.log(`accounts: ${-now + (now = performance.now())} ms`) + for (const keypair of keypairs) { + if (keypair.publicKey == null) throw new RangeError('Account public key missing') + if (keypair.privateKey == null) throw new RangeError('Account private key missing') + if (keypair.index == null) throw new RangeError('Account keys derived but index missing') + const { publicKey, privateKey, index } = keypair + this.#accounts[keypair.index] = Account.fromKnownKeys(publicKey, privateKey, index) } + console.log(`done: ${-now + (now = performance.now())} ms`) } return this.#accounts.slice(from, to + 1) } @@ -415,23 +428,14 @@ export class Bip44Wallet extends Wallet { * @param {number} index - Index of the account * @returns {Promise} */ - async ckd (index: number | number[]): Promise { + async ckd (index: number | number[]): Promise { if (!Array.isArray(index)) index = [index] const data: any = [] index.forEach(i => data.push({ seed: this.seed, index: i })) let now = performance.now() - const privateKeys: [{ index: number, key: string }] = await this.#pool.work(data) + const privateKeys: KeyPair[] = await this.#pool.work(data) console.log(`ckd: ${-now + (now = performance.now())} ms`) - const accounts: Account[] = [] - for (const pk of privateKeys) { - const { key, index } = pk - if (typeof key !== 'string') { - throw new TypeError('BIP-44 child key derivation returned invalid data') - } - accounts.push(await Account.fromPrivateKey(key, index)) - } - console.log(`accounts: ${-now + (now = performance.now())} ms`) - return accounts + return privateKeys } } @@ -571,7 +575,7 @@ export class Blake2bWallet extends Wallet { * @param {number} index - Index of the account * @returns {Promise} */ - async ckd (index: number | number[]): Promise { + async ckd (index: number | number[]): Promise { if (!Array.isArray(index)) index = [index] const data: any = [] let now = performance.now() @@ -584,16 +588,7 @@ export class Blake2bWallet extends Wallet { return { key, index } }) console.log(`ckd: ${-now + (now = performance.now())} ms`) - const accounts: Account[] = [] - for (const result of results) { - const { key, index } = result - if (typeof key !== 'string') { - throw new TypeError('BLAKE2b child key derivation returned invalid data') - } - accounts.push(await Account.fromPrivateKey(key, index)) - } - console.log(`accounts: ${-now + (now = performance.now())} ms`) - return accounts + return results } } @@ -658,10 +653,10 @@ export class LedgerWallet extends Wallet { * @param {number} index - Index of the account * @returns {Promise} */ - async ckd (index: number): Promise { + async ckd (index: number): Promise { const { status, publicKey } = await this.ledger.account(index) if (status === 'OK' && publicKey != null) { - return await Account.fromPublicKey(publicKey, index) + return { publicKey, index } } throw new Error(`Error getting Ledger account: ${status}`) } diff --git a/src/lib/workers.ts b/src/lib/workers.ts index 5e1cbf2..02bc056 100644 --- a/src/lib/workers.ts +++ b/src/lib/workers.ts @@ -1,4 +1,4 @@ import ckdBip44 from './workers/ckdBip44.js' -// import './workers/nano-nacl.js' +import nanoNacl from './workers/nano-nacl.js' // import './workers/passkey.js' -export { ckdBip44 } +export { ckdBip44, nanoNacl } -- 2.34.1