]> git.codecow.com Git - libnemo.git/commitdiff
Enqueue from call, not within called function.
authorChris Duncan <chris@codecow.com>
Mon, 18 May 2026 17:09:20 +0000 (10:09 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 18 May 2026 17:09:20 +0000 (10:09 -0700)
src/lib/ledger/connect.ts
src/lib/ledger/version.ts

index 3b13d7d2dc27a9ddd6ce694a00c317d6caa42db5..aeb463dd17542a766fbf61e80693d14d35675749 100644 (file)
@@ -3,7 +3,8 @@
 
 import { LedgerStatus, LedgerTransport } from '.'
 import { _account } from './account'
-import { version } from './version'
+import { queue } from './queue'
+import { _version } from './version'
 
 /**
  * Check if the Nano app is currently open and set device status accordingly.
@@ -18,13 +19,13 @@ import { version } from './version'
 export async function _connect (transport: LedgerTransport): Promise<LedgerStatus> {
        let status: LedgerStatus = 'DISCONNECTED'
        try {
-               const v = await version(transport)
+               const v = await queue(async () => _version(transport))
                if (v.status === 'LOCKED_DEVICE') {
                        status = 'LOCKED'
                } else if (v.status !== 'OK') {
                        status = 'DISCONNECTED'
                } else if (v.name === 'Nano') {
-                       const a = await _account(transport)
+                       const a = await queue(async () => _account(transport))
                        if (a.status === 'OK') {
                                status = 'CONNECTED'
                        } else if (a.status === 'SECURITY_STATUS_NOT_SATISFIED') {
index ccbd2b7842624a0ef30e4ad29b0dcf36c2fed2e8..0c6745c78e3a0a1a2b4e849290ad4ce73dae95d6 100644 (file)
@@ -1,9 +1,8 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { APDU_CODES, LedgerResponse, LedgerTransport, STATUS_CODES, LISTEN_TIMEOUT, OPEN_TIMEOUT } from '.'
+import { APDU_CODES, LedgerResponse, LedgerTransport, LISTEN_TIMEOUT, OPEN_TIMEOUT, STATUS_CODES } from '.'
 import { bytes, dec } from '../convert'
-import { queue } from './queue'
 
 interface LedgerVersionResponse extends LedgerResponse {
        name: string | null,
@@ -17,30 +16,28 @@ interface LedgerVersionResponse extends LedgerResponse {
  *
  * @returns Status, process name, and version
  */
-export async function version (transport: LedgerTransport): Promise<LedgerVersionResponse> {
-       return queue<LedgerVersionResponse>(async () => {
-               try {
-                       const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
-                       const response = await t
-                               .send(0xb0, APDU_CODES.version, APDU_CODES.paramUnused, APDU_CODES.paramUnused)
-                               .catch((err: any) => dec.toBytes(err.statusCode))
-                               .finally(async () => await t.close()) as Uint8Array
+export async function _version (transport: LedgerTransport): Promise<LedgerVersionResponse> {
+       try {
+               const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
+               const response = await t
+                       .send(0xb0, APDU_CODES.version, APDU_CODES.paramUnused, APDU_CODES.paramUnused)
+                       .catch((err: any) => dec.toBytes(err.statusCode))
+                       .finally(async () => await t.close()) as Uint8Array
 
-                       const statusCode = bytes.toDec(response.slice(-2)) as number
-                       const status = STATUS_CODES[statusCode] ?? 'UNKNOWN_ERROR'
-                       if (status !== 'OK') {
-                               return { status, name: null, version: null }
-                       }
+               const statusCode = bytes.toDec(response.slice(-2)) as number
+               const status = STATUS_CODES[statusCode] ?? 'UNKNOWN_ERROR'
+               if (status !== 'OK') {
+                       return { status, name: null, version: null }
+               }
 
-                       const nameLength = response[1]
-                       const name = response.slice(2, 2 + nameLength).toString()
-                       const versionLength = response[2 + nameLength]
-                       const version = response.slice(2 + nameLength + 1, 2 + nameLength + 1 + versionLength).toString()
+               const nameLength = response[1]
+               const name = response.slice(2, 2 + nameLength).toString()
+               const versionLength = response[2 + nameLength]
+               const version = response.slice(2 + nameLength + 1, 2 + nameLength + 1 + versionLength).toString()
 
-                       return { status, name, version }
-               } catch (err: any) {
-                       console.error('Ledger.#version()', err)
-                       return { status: err.message, name: null, version: null }
-               }
-       })
+               return { status, name, version }
+       } catch (err: any) {
+               console.error('Ledger.#version()', err)
+               return { status: err.message, name: null, version: null }
+       }
 }