From: Chris Duncan Date: Mon, 10 Aug 2026 05:58:43 +0000 (-0700) Subject: Breaking change, use sane zero-based account index range. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=778ee5425d1883a8a32478dfff3f8bfa77a372d4;p=libnemo.git Breaking change, use sane zero-based account index range. --- diff --git a/src/lib/vault/vault-worker.ts b/src/lib/vault/vault-worker.ts index e2faa78..83bac32 100644 --- a/src/lib/vault/vault-worker.ts +++ b/src/lib/vault/vault-worker.ts @@ -198,8 +198,8 @@ function derive (index?: Uint32Array): Promise MAX_DERIVATIONS + 1) { - throw new VaultError('Maximum 1000 accounts per call') + if (index.length > MAX_DERIVATIONS) { + throw new VaultError(`Maximum ${MAX_DERIVATIONS} accounts per call`) } const promises = [] const values = typeof index === 'number' ? [index] : index.values() diff --git a/src/lib/wallet/accounts.ts b/src/lib/wallet/accounts.ts index 4420675..58b36be 100644 --- a/src/lib/wallet/accounts.ts +++ b/src/lib/wallet/accounts.ts @@ -19,18 +19,18 @@ export async function _accounts (type: WalletType, accounts: Map to) [from as number, to as number] = [to, from] - if (from < 0 || 0xffffffff < from || to < 0 || 0xffffffff < to) { + if (from < 0 || 0x100000000 < from || to < 0 || 0x100000000 < to) { throw new TypeError('Invalid account range', { cause: `${from}-${to}` }) } if (to - from > 100) { console.warn('libnemo performance may degrade when deriving many accounts at once') } if (to - from > MAX_DERIVATIONS) { - throw new RangeError('Maximum 1000 accounts per call') + throw new RangeError(`Maximum ${MAX_DERIVATIONS} accounts per call`) } const output = new Map() const missing: number[] = [] - for (let i = from; i <= to; i++) { + for (let i = from; i < to; i++) { const account = accounts.get(i) if (account == null) { missing.push(i) diff --git a/src/lib/wallet/index.ts b/src/lib/wallet/index.ts index e0e909c..5fcdd9f 100644 --- a/src/lib/wallet/index.ts +++ b/src/lib/wallet/index.ts @@ -251,7 +251,8 @@ export class Wallet { /** * Retrieves an account from a wallet using its child key derivation function. - * Defaults to the first account at index 0. + * Defaults to the first account at index 0. If a range of indexes is passed, + * derives up to but not including the larger value. * * The returned object will have keys corresponding with the requested range * of account indexes. The value of each key will be the Account derived for @@ -264,7 +265,7 @@ export class Wallet { * // { address: <...>, publicKey: <...>, index: 1, } * ``` * - * @param {number} index - Wallet index of account. Default: 0 + * @param {number} index - Wallet index of account. Default: 0; Maximum: 2³²-1 * @returns Promise for the Account at the specified index */ async account (index: number = 0): Promise { @@ -273,7 +274,7 @@ export class Wallet { /** * Retrieves accounts from a wallet using its child key derivation function. - * Defaults to the first account at index 0. + * Defaults to the first account at index 0. If * * Derives a maximum of 1000 accounts per call and will throw an error if * exceeded. This is not a hard limit since the method can be called multiple @@ -285,7 +286,7 @@ export class Wallet { * that index in the wallet. * * ``` - * const accounts = await wallet.accounts(0, 1)) + * const accounts = await wallet.accounts(0, 2)) * // outputs the first and second account of the wallet * console.log(accounts) * // { @@ -309,10 +310,10 @@ export class Wallet { * * If `from` is greater than `to`, their values will be swapped. * @param {number} [from=0] - Start index of accounts. Default: 0 - * @param {number} [to=from] - End index of accounts. Default: `from` + * @param {number} [to=from+1] - End index of accounts. Default: `from + 1` * @returns {Promise>} Promise for a list of Accounts at the specified indexes */ - async accounts (from: number = 0, to: number = from): Promise> { + async accounts (from: number = 0, to: number = from + 1): Promise> { return _accounts(this.type, this.#accounts, this.#vault, from, to) }