]> git.codecow.com Git - libnemo.git/commitdiff
Breaking change, use sane zero-based account index range.
authorChris Duncan <chris@codecow.com>
Mon, 10 Aug 2026 05:58:43 +0000 (22:58 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 10 Aug 2026 05:58:43 +0000 (22:58 -0700)
src/lib/vault/vault-worker.ts
src/lib/wallet/accounts.ts
src/lib/wallet/index.ts

index e2faa78f526d945626e5f992008c77e4d59d780d..83bac32189628631fe5d820d2712f1c903ab48fa 100644 (file)
@@ -198,8 +198,8 @@ function derive (index?: Uint32Array): Promise<Record<string, number | ArrayBuff
                if (index == null) {
                        throw new VaultError('Invalid wallet account index(es)')
                }
-               if (index.length > 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()
index 4420675adaf04804751a2fd63ae609140390854e..58b36bedcfb684de6afef85d654c3788aa914f23 100644 (file)
@@ -19,18 +19,18 @@ export async function _accounts (type: WalletType, accounts: Map<number, Account
                throw new Error('Invalid account range end argument', { cause: `${typeof to} ${to}` })
        }
        if (from > 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<number, Account>()
        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)
index e0e909ca75d5f84c61bab56628c66ddb07ec345b..5fcdd9f7cd22ebcec889dc09604fe840a77d3d67 100644 (file)
@@ -251,7 +251,8 @@ export class Wallet {
 \r
        /**\r
        * Retrieves an account from a wallet using its child key derivation function.\r
-       * Defaults to the first account at index 0.\r
+       * Defaults to the first account at index 0. If a range of indexes is passed,\r
+       * derives up to but not including the larger value.\r
        *\r
        * The returned object will have keys corresponding with the requested range\r
        * of account indexes. The value of each key will be the Account derived for\r
@@ -264,7 +265,7 @@ export class Wallet {
        * // { address:  <...>, publicKey: <...>, index: 1, <etc...> }\r
        * ```\r
        *\r
-       * @param {number} index - Wallet index of account. Default: 0\r
+       * @param {number} index - Wallet index of account. Default: 0; Maximum: 2³²-1\r
        * @returns Promise for the Account at the specified index\r
        */\r
        async account (index: number = 0): Promise<Account> {\r
@@ -273,7 +274,7 @@ export class Wallet {
 \r
        /**\r
        * Retrieves accounts from a wallet using its child key derivation function.\r
-       * Defaults to the first account at index 0.\r
+       * Defaults to the first account at index 0. If\r
        *\r
        * Derives a maximum of 1000 accounts per call and will throw an error if\r
        * exceeded. This is not a hard limit since the method can be called multiple\r
@@ -285,7 +286,7 @@ export class Wallet {
        * that index in the wallet.\r
        *\r
        * ```\r
-       * const accounts = await wallet.accounts(0, 1))\r
+       * const accounts = await wallet.accounts(0, 2))\r
        * // outputs the first and second account of the wallet\r
        * console.log(accounts)\r
        * // {\r
@@ -309,10 +310,10 @@ export class Wallet {
        *\r
        * If `from` is greater than `to`, their values will be swapped.\r
        * @param {number} [from=0] - Start index of accounts. Default: 0\r
-       * @param {number} [to=from] - End index of accounts. Default: `from`\r
+       * @param {number} [to=from+1] - End index of accounts. Default: `from + 1`\r
        * @returns {Promise<Map<number, Account>>} Promise for a list of Accounts at the specified indexes\r
        */\r
-       async accounts (from: number = 0, to: number = from): Promise<Map<number, Account>> {\r
+       async accounts (from: number = 0, to: number = from + 1): Promise<Map<number, Account>> {\r
                return _accounts(this.type, this.#accounts, this.#vault, from, to)\r
        }\r
 \r