]> zoso.dev Git - libnemo.git/commitdiff
Refactor wallet account methods to start building out for NaCl worker implementation.
authorChris Duncan <chris@zoso.dev>
Thu, 28 Nov 2024 10:40:52 +0000 (02:40 -0800)
committerChris Duncan <chris@zoso.dev>
Thu, 28 Nov 2024 10:40:52 +0000 (02:40 -0800)
src/lib/wallet.ts
src/lib/workers.ts

index 03e0974fdecdc4630a0f99796790ccaa50b499fc..e052176e40952a1150d22d4733ad0877da98c7d6 100644 (file)
@@ -9,9 +9,14 @@ import { Entropy } from './entropy.js'
 import { Pool } from './pool.js'\r
 import { Rpc } from './rpc.js'\r
 import { Safe } from './safe.js'\r
-import { ckdBip44 } from './workers.js'\r
+import { ckdBip44, nanoNacl } from './workers.js'\r
 import type { Ledger } from './ledger.js'\r
 \r
+type KeyPair = {\r
+       publicKey?: string,\r
+       privateKey?: string,\r
+       index?: number\r
+}\r
 /**\r
 * Represents a wallet containing numerous Nano accounts derived from a single\r
 * source, the form of which can vary based on the type of wallet. The Wallet\r
@@ -24,6 +29,7 @@ abstract class Wallet {
        #id: Entropy\r
        #locked: boolean = true\r
        #mnemonic: Bip39Mnemonic | null\r
+       #pool: Pool\r
        #safe: Safe\r
        #seed: string | null\r
        get id () { return this.#id.hex }\r
@@ -42,7 +48,7 @@ abstract class Wallet {
                return ''\r
        }\r
 \r
-       abstract ckd (index: number | number[]): Promise<Account> | Promise<Account[]>\r
+       abstract ckd (index: number | number[]): Promise<KeyPair> | Promise<KeyPair[]>\r
 \r
        constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) {\r
                if (this.constructor === Wallet) {\r
@@ -53,6 +59,7 @@ abstract class Wallet {
                        ? new Entropy(id)\r
                        : new Entropy(16)\r
                this.#mnemonic = mnemonic ?? null\r
+               this.#pool = new Pool(nanoNacl)\r
                this.#safe = new Safe()\r
                this.#seed = seed ?? null\r
        }\r
@@ -89,13 +96,19 @@ abstract class Wallet {
                if (indexes.length > 0) {\r
                        let results = await this.ckd(indexes)\r
                        if (!Array.isArray(results)) results = [results]\r
-                       for (const result of results) {\r
-                               if (result.index == null) {\r
-                                       throw new RangeError('Account key derived but index missing')\r
-                               } else {\r
-                                       this.#accounts[result.index] = result\r
-                               }\r
+                       const data: any = []\r
+                       results.forEach(r => data.push({ privateKey: r.privateKey, index: r.index }))\r
+                       let now = performance.now()\r
+                       const keypairs: [{ publicKey: string, privateKey: string, index: number }] = await this.#pool.work(data)\r
+                       console.log(`accounts: ${-now + (now = performance.now())} ms`)\r
+                       for (const keypair of keypairs) {\r
+                               if (keypair.publicKey == null) throw new RangeError('Account public key missing')\r
+                               if (keypair.privateKey == null) throw new RangeError('Account private key missing')\r
+                               if (keypair.index == null) throw new RangeError('Account keys derived but index missing')\r
+                               const { publicKey, privateKey, index } = keypair\r
+                               this.#accounts[keypair.index] = Account.fromKnownKeys(publicKey, privateKey, index)\r
                        }\r
+                       console.log(`done: ${-now + (now = performance.now())} ms`)\r
                }\r
                return this.#accounts.slice(from, to + 1)\r
        }\r
@@ -415,23 +428,14 @@ export class Bip44Wallet extends Wallet {
        * @param {number} index - Index of the account\r
        * @returns {Promise<Account>}\r
        */\r
-       async ckd (index: number | number[]): Promise<Account[]> {\r
+       async ckd (index: number | number[]): Promise<KeyPair[]> {\r
                if (!Array.isArray(index)) index = [index]\r
                const data: any = []\r
                index.forEach(i => data.push({ seed: this.seed, index: i }))\r
                let now = performance.now()\r
-               const privateKeys: [{ index: number, key: string }] = await this.#pool.work(data)\r
+               const privateKeys: KeyPair[] = await this.#pool.work(data)\r
                console.log(`ckd: ${-now + (now = performance.now())} ms`)\r
-               const accounts: Account[] = []\r
-               for (const pk of privateKeys) {\r
-                       const { key, index } = pk\r
-                       if (typeof key !== 'string') {\r
-                               throw new TypeError('BIP-44 child key derivation returned invalid data')\r
-                       }\r
-                       accounts.push(await Account.fromPrivateKey(key, index))\r
-               }\r
-               console.log(`accounts: ${-now + (now = performance.now())} ms`)\r
-               return accounts\r
+               return privateKeys\r
        }\r
 }\r
 \r
@@ -571,7 +575,7 @@ export class Blake2bWallet extends Wallet {
        * @param {number} index - Index of the account\r
        * @returns {Promise<Account>}\r
        */\r
-       async ckd (index: number | number[]): Promise<Account[]> {\r
+       async ckd (index: number | number[]): Promise<KeyPair[]> {\r
                if (!Array.isArray(index)) index = [index]\r
                const data: any = []\r
                let now = performance.now()\r
@@ -584,16 +588,7 @@ export class Blake2bWallet extends Wallet {
                        return { key, index }\r
                })\r
                console.log(`ckd: ${-now + (now = performance.now())} ms`)\r
-               const accounts: Account[] = []\r
-               for (const result of results) {\r
-                       const { key, index } = result\r
-                       if (typeof key !== 'string') {\r
-                               throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
-                       }\r
-                       accounts.push(await Account.fromPrivateKey(key, index))\r
-               }\r
-               console.log(`accounts: ${-now + (now = performance.now())} ms`)\r
-               return accounts\r
+               return results\r
        }\r
 }\r
 \r
@@ -658,10 +653,10 @@ export class LedgerWallet extends Wallet {
        * @param {number} index - Index of the account\r
        * @returns {Promise<Account>}\r
        */\r
-       async ckd (index: number): Promise<Account> {\r
+       async ckd (index: number): Promise<KeyPair> {\r
                const { status, publicKey } = await this.ledger.account(index)\r
                if (status === 'OK' && publicKey != null) {\r
-                       return await Account.fromPublicKey(publicKey, index)\r
+                       return { publicKey, index }\r
                }\r
                throw new Error(`Error getting Ledger account: ${status}`)\r
        }\r
index 5e1cbf2ae97ebe7ac1cc2783607acee8be2e3130..02bc0568de00d5f26b7dbab323329a454c4bbcac 100644 (file)
@@ -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 }