]> git.codecow.com Git - libnemo.git/commitdiff
Fix signing calls to also pass index as u32.
authorChris Duncan <chris@codecow.com>
Sat, 8 Aug 2026 06:33:45 +0000 (23:33 -0700)
committerChris Duncan <chris@codecow.com>
Sat, 8 Aug 2026 06:33:45 +0000 (23:33 -0700)
src/lib/vault/parsers.ts
src/lib/vault/vault-worker.ts
src/lib/wallet/accounts.ts
src/lib/wallet/sign.ts

index dc0705ff4c1c9e688bdd87cc874e31589e2b11e3..1fd9405f57f67c381fdb6736ea127d5e8fff7320 100644 (file)
@@ -81,11 +81,11 @@ export function parseData (action: string, data: Record<string, unknown>) {
                        delete data.encrypted
                }
 
-               // Index for child account to derive or sign
-               if ((action === 'derive' || action === 'sign') && !(data.indexes instanceof ArrayBuffer)) {
+               // Index(es) for child account to derive or sign
+               if ((action === 'derive' || action === 'sign') && !(data.index instanceof ArrayBuffer)) {
                        throw new TypeError('Index is required to derive an account private key')
                }
-               const indexes = typeof data.indexes instanceof ArrayBuffer
+               const index = data.index instanceof ArrayBuffer
                        ? new Uint32Array(data.index)
                        : undefined
 
@@ -114,7 +114,7 @@ export function parseData (action: string, data: Record<string, unknown>) {
                        ? data.timeout
                        : undefined
 
-               return { seed, mnemonicPhrase, mnemonicSalt, encrypted, index, count, message, timeout }
+               return { seed, mnemonicPhrase, mnemonicSalt, encrypted, index, message, timeout }
        } catch (err) {
                new Uint8Array(seed ?? []).fill(0)
                new Uint8Array(encrypted ?? []).fill(0)
index 028bbe2a6b9b416ffa9b7a95ff17448c59752df6..8f166a253b8e524f6dac897c689468e884275836 100644 (file)
@@ -40,7 +40,7 @@ const listener = (event: MessageEvent<any>): void => {
        const id = parseId(action, data)
        const iv = parseIv(action, data)
        const parsed = parseData(action, data)
-       const { seed, mnemonicPhrase, mnemonicSalt, indexes, encrypted, message, timeout } = parsed
+       const { seed, mnemonicPhrase, mnemonicSalt, index, encrypted, message, timeout } = parsed
        passkey(action, keySalt, data)
                .then((key: CryptoKey | undefined): Promise<Record<string, boolean | number | ArrayBuffer> | void> => {
                        switch (action) {
@@ -56,7 +56,7 @@ const listener = (event: MessageEvent<any>): void => {
                                        return create(type, id, key, keySalt, mnemonicSalt)
                                }
                                case 'derive': {
-                                       return derive(indexes)
+                                       return derive(index)
                                }
                                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 (indexes?: Uint32Array): Promise<Record<string, number | ArrayBuffer>> {
+function derive (index?: number | Uint32Array): Promise<Record<string, number | ArrayBuffer>> {
        try {
                _timer.pause()
                if (_locked) {
@@ -182,17 +182,17 @@ function derive (indexes?: Uint32Array): Promise<Record<string, number | ArrayBu
                if (_type !== 'BIP-44' && _type !== 'BLAKE2b' && _type !== 'Exodus') {
                        throw new Error('Invalid wallet type')
                }
-               if (indexes == null) {
+               if (index == null) {
                        throw new Error('Invalid wallet account index(es)')
                }
                const promises = []
-               const values = indexes.values()
-               for (const index of values) {
-                       promises.push(_ckd(index).then(result => {
+               const values = typeof index === 'number' ? [index] : index.values()
+               for (const i of values) {
+                       promises.push(_ckd(i).then(result => {
                                const prv = new Uint8Array(result)
                                const pub = nano25519_derive(prv)
                                prv.fill(0)
-                               return { index, publicKey: pub.buffer }
+                               return { index: i, publicKey: pub.buffer }
                        }))
                }
                return Promise.all(promises)
@@ -252,7 +252,7 @@ function lock (): Promise<Record<string, boolean>> {
  * Derives the account private key at a specified index, signs the input data,
  * and returns a signature. The wallet must be unlocked prior to verification.
  */
-function sign (index?: number, data?: ArrayBuffer): Promise<Record<string, ArrayBuffer>> {
+function sign (index?: Uint32Array, data?: ArrayBuffer): Promise<Record<string, ArrayBuffer>> {
        try {
                _timer.pause()
                if (_locked) {
@@ -267,7 +267,7 @@ function sign (index?: number, data?: ArrayBuffer): Promise<Record<string, Array
                if (data == null) {
                        throw new Error('Data to sign not found')
                }
-               return _ckd(index).then(result => {
+               return _ckd(index[0]).then(result => {
                        const prv = new Uint8Array(result)
                        const pub = nano25519_derive(prv)
                        const sk = new Uint8Array([...prv, ...pub])
index 0ec8d86805c0da32742c002d3eb5a5fca75bdf41..ed5fc1651930140158216bb5eac8302732b1f9bb 100644 (file)
@@ -46,12 +46,11 @@ export async function _accounts (type: WalletType, accounts: Map<number, Account
                                accounts.set(index, account)
                        }
                } else {
-                       const indexes = new Uint32Array(missing).buffer
+                       const { buffer } = new Uint32Array(missing)
                        const publicKeys = await vault.request<ArrayBuffer>({
                                action: 'derive',
-                               indexes
+                               index: buffer
                        })
-                       console.log(publicKeys)
                        for (const [index, publicKey] of Object.entries(publicKeys)) {
                                if (typeof Number(index) === 'number' && publicKey instanceof ArrayBuffer) {
                                        const account = new Account(publicKey)
index 6cf000d455d075efd190de0a7b97e6205cee84a4..500b035e8166999761e2444da19cccf23ec10c7e 100644 (file)
@@ -40,7 +40,7 @@ export async function _signBlock (wallet: Wallet, vault: Vault, index: unknown,
                } else {
                        const { signature } = await vault.request<ArrayBuffer>({
                                action: 'sign',
-                               index,
+                               index: new Uint32Array([index]).buffer,
                                message: hex.toBuffer(block.hash)
                        })
                        block.signature = bytes.toHex(new Uint8Array(signature))
@@ -74,7 +74,7 @@ export async function _signData (wallet: Wallet, vault: Vault, index: unknown, a
                }
                const { signature } = await vault.request<ArrayBuffer>({
                        action: 'sign',
-                       index,
+                       index: new Uint32Array([index]).buffer,
                        message: utf8.toBuffer(data)
                })
                return bytes.toHex(new Uint8Array(signature))