]> zoso.dev Git - libnemo.git/commitdiff
Create new ckd functions for BIP-44 and BLAKE2b. Set up class for A/B performance...
authorChris Duncan <chris@zoso.dev>
Thu, 21 Nov 2024 05:54:25 +0000 (21:54 -0800)
committerChris Duncan <chris@zoso.dev>
Thu, 21 Nov 2024 05:54:25 +0000 (21:54 -0800)
src/lib/wallet.ts

index 723083fbc6ad3380ef7cfbef7c01b0eeee6d83f9..2581de74549cc64a3aebf6fa9b6713499adf0b98 100644 (file)
@@ -43,7 +43,7 @@ abstract class Wallet {
                return ''\r
        }\r
 \r
-       abstract ckd (index: number): Promise<Account>\r
+       abstract ckd (index: number | number[]): Promise<Account> | Promise<Account[]>\r
 \r
        constructor (seed?: string, mnemonic?: Bip39Mnemonic, id?: string) {\r
                if (this.constructor === Wallet) {\r
@@ -81,9 +81,20 @@ abstract class Wallet {
                        from = to\r
                        to = swap\r
                }\r
+               const indexes = []\r
                for (let i = from; i <= to; i++) {\r
                        if (this.#accounts[i] == null) {\r
-                               this.#accounts[i] = await this.ckd(i)\r
+                               // this.#accounts[i] = await this.ckd(i)\r
+                               indexes.push(i)\r
+                       }\r
+               }\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
+                               console.error('Wallet account calculated with null index')\r
+                       } else {\r
+                               this.#accounts[result.index] = result\r
                        }\r
                }\r
                return this.#accounts.slice(from, to + 1)\r
@@ -404,12 +415,53 @@ export class Bip44Wallet extends Wallet {
        * @param {number} index - Index of the account\r
        * @returns {Promise<Account>}\r
        */\r
-       async ckd (index: number): Promise<Account> {\r
-               const key = await nanoCKD(this.seed, index)\r
-               if (typeof key !== 'string') {\r
-                       throw new TypeError('BIP-44 child key derivation returned invalid data')\r
+       async ckd (index: number | number[]): Promise<Account[]> {\r
+               if (!Array.isArray(index)) index = [index]\r
+               const keys = []\r
+               let now = performance.now()\r
+               for (const i of index) {\r
+                       const key = await nanoCKD(this.seed, i)\r
+                       if (typeof key !== 'string') {\r
+                               throw new TypeError('BIP-44 child key derivation returned invalid data')\r
+                       }\r
+                       keys.push({ key, i })\r
+               }\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('bip44 ckd done')\r
+               const accounts = []\r
+               for (const key of keys) {\r
+                       accounts.push(await Account.fromPrivateKey(key.key, key.i))\r
+               }\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('bip44 account list done')\r
+               return accounts\r
+       }\r
+\r
+       /**\r
+       * Derives BIP-44 Nano account private keys.\r
+       *\r
+       * @param {number} index - Index of the account\r
+       * @returns {Promise<Account>}\r
+       */\r
+       async ckd_new (index: number | number[]): Promise<Account[]> {\r
+               if (!Array.isArray(index)) index = [index]\r
+               const data: any = []\r
+               let now = performance.now()\r
+               index.forEach(i => data.push({ seed: this.seed, index: i }))\r
+               const results: [{ index: number, key: string }] = await this.#pool.work(data)\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('bip44 ckd done')\r
+               const accounts = []\r
+               for (const result of results) {\r
+                       const { index, key } = result\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
-               return Account.fromPrivateKey(key, index)\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('bip44 account list done')\r
+               return accounts\r
        }\r
 }\r
 \r
@@ -551,13 +603,54 @@ export class Blake2bWallet extends Wallet {
        * @param {number} index - Index of the account\r
        * @returns {Promise<Account>}\r
        */\r
-       async ckd (index: number): Promise<Account> {\r
-               const input = `${this.seed}${dec.toHex(index, 8)}`\r
-               const key = blake2b(32).update(hex.toBytes(input)).digest('hex')\r
-               if (typeof key !== 'string') {\r
-                       throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
+       async ckd (index: number | number[]): Promise<Account[]> {\r
+               if (!Array.isArray(index)) index = [index]\r
+               const keys = []\r
+               let now = performance.now()\r
+               for (const i of index) {\r
+                       const input = `${this.seed}${dec.toHex(i, 8)}`\r
+                       const key = blake2b(32).update(hex.toBytes(input)).digest('hex')\r
+                       if (typeof key !== 'string') {\r
+                               throw new TypeError('BLAKE2b child key derivation returned invalid data')\r
+                       }\r
+                       keys.push({ key, i })\r
+               }\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('blake2b ckd done')\r
+               const accounts = []\r
+               for (const key of keys) {\r
+                       accounts.push(await Account.fromPrivateKey(key.key, key.i))\r
+               }\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('blake2b account list done')\r
+               return accounts\r
+       }\r
+\r
+       /**\r
+       * Derives BLAKE2b account private keys.\r
+       *\r
+       * @param {number} index - Index of the account\r
+       * @returns {Promise<Account>}\r
+       */\r
+       async ckd_new (index: number | number[]): Promise<Account[]> {\r
+               if (!Array.isArray(index)) index = [index]\r
+               const data: any = []\r
+               let now = performance.now()\r
+               index.forEach(i => data.push({ seed: this.seed, index: i }))\r
+               const results: [{ index: number, key: string }] = await this.#pool.work(data)\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('blake2b ckd done')\r
+               const accounts = []\r
+               for (const result of results) {\r
+                       const { index, key } = 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
-               return Account.fromPrivateKey(key, index)\r
+               console.log(-now + (now = performance.now()), 'ms')\r
+               console.log('blake2b account list done')\r
+               return accounts\r
        }\r
 }\r
 \r