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
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
* @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
* @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