From 208eee91256198c02ae1273e48e95902724dc872 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Fri, 7 Aug 2026 22:10:20 -0700 Subject: [PATCH] Transfer account indexes to vault worker as typed array. --- src/lib/vault/parsers.ts | 14 +++----------- src/lib/vault/vault-worker.ts | 20 +++++++------------- src/lib/wallet/accounts.ts | 22 ++++++---------------- 3 files changed, 16 insertions(+), 40 deletions(-) diff --git a/src/lib/vault/parsers.ts b/src/lib/vault/parsers.ts index f910e5b..dc0705f 100644 --- a/src/lib/vault/parsers.ts +++ b/src/lib/vault/parsers.ts @@ -82,19 +82,11 @@ export function parseData (action: string, data: Record) { } // Index for child account to derive or sign - if ((action === 'derive' || action === 'sign') && typeof data.index !== 'number') { + if ((action === 'derive' || action === 'sign') && !(data.indexes instanceof ArrayBuffer)) { throw new TypeError('Index is required to derive an account private key') } - const index = typeof data.index === 'number' - ? data.index - : undefined - - // Number of public keys to derive - if (action === 'derive' && (typeof data.count !== 'number' || data.count < 1)) { - throw new TypeError('Count is required to derive a batch of keys') - } - const count = typeof data.count === 'number' - ? data.count + const indexes = typeof data.indexes instanceof ArrayBuffer + ? new Uint32Array(data.index) : undefined // Data to sign diff --git a/src/lib/vault/vault-worker.ts b/src/lib/vault/vault-worker.ts index 6363247..028bbe2 100644 --- a/src/lib/vault/vault-worker.ts +++ b/src/lib/vault/vault-worker.ts @@ -40,7 +40,7 @@ const listener = (event: MessageEvent): void => { const id = parseId(action, data) const iv = parseIv(action, data) const parsed = parseData(action, data) - const { seed, mnemonicPhrase, mnemonicSalt, index, count, encrypted, message, timeout } = parsed + const { seed, mnemonicPhrase, mnemonicSalt, indexes, encrypted, message, timeout } = parsed passkey(action, keySalt, data) .then((key: CryptoKey | undefined): Promise | void> => { switch (action) { @@ -56,7 +56,7 @@ const listener = (event: MessageEvent): void => { return create(type, id, key, keySalt, mnemonicSalt) } case 'derive': { - return derive(index, count) + return derive(indexes) } case 'load': { return load(type, id, key, keySalt, mnemonicPhrase ?? seed, mnemonicSalt) @@ -170,7 +170,7 @@ function create (type?: WalletType, id?: UUID, key?: CryptoKey, keySalt?: ArrayB * wallet seed at a specified index and then returns the public key. The wallet * must be unlocked prior to derivation. */ -function derive (index?: number, count?: number): Promise> { +function derive (indexes?: Uint32Array): Promise> { try { _timer.pause() if (_locked) { @@ -182,18 +182,12 @@ function derive (index?: number, count?: number): Promise (_type === 'BLAKE2b' ? 0xffffffff : 0x7fffffff)) { - throw new Error('Wallet account range exceeded ') + if (indexes == null) { + throw new Error('Invalid wallet account index(es)') } const promises = [] - for (let i = index; i < max; i++) { + const values = indexes.values() + for (const index of values) { promises.push(_ckd(index).then(result => { const prv = new Uint8Array(result) const pub = nano25519_derive(prv) diff --git a/src/lib/wallet/accounts.ts b/src/lib/wallet/accounts.ts index 1a74fd6..0ec8d86 100644 --- a/src/lib/wallet/accounts.ts +++ b/src/lib/wallet/accounts.ts @@ -25,18 +25,18 @@ export async function _accounts (type: WalletType, accounts: Map() - const indexes: number[] = [] + const missing: number[] = [] for (let i = from; i <= to; i++) { const account = accounts.get(i) if (account == null) { - indexes.push(i) + missing.push(i) } else { output.set(i, account) } } - if (indexes.length > 0) { + if (missing.length > 0) { if (type === 'Ledger') { - for (const index of indexes) { + for (const index of missing) { const { status, publicKey } = await Ledger.account(index) if (status !== 'OK' || publicKey == null) { throw new Error(`Error getting Ledger account: ${status}`) @@ -46,20 +46,10 @@ export async function _accounts (type: WalletType, accounts: Map({ - // action: 'derive', - // index - // })) - // } - // const publicKeys = await Promise.all(promises) - const min = Math.min(...indexes) - const count = Math.max(...indexes) - min + 1 + const indexes = new Uint32Array(missing).buffer const publicKeys = await vault.request({ action: 'derive', - index: min, - count + indexes }) console.log(publicKeys) for (const [index, publicKey] of Object.entries(publicKeys)) { -- 2.52.0