]> git.codecow.com Git - libnemo.git/commitdiff
Adjust Ledger constants.
authorChris Duncan <chris@codecow.com>
Mon, 18 May 2026 13:41:55 +0000 (06:41 -0700)
committerChris Duncan <chris@codecow.com>
Mon, 18 May 2026 13:41:55 +0000 (06:41 -0700)
src/lib/ledger/account.ts
src/lib/ledger/cache.ts
src/lib/ledger/close.ts
src/lib/ledger/disconnect.ts
src/lib/ledger/index.ts
src/lib/ledger/open.ts
src/lib/ledger/sign.ts
src/lib/ledger/version.ts

index f534011482b10aca3450ef3e7f6b796894b198ea..ba155a61bf1eea0dc2590763da6d791f35df826a 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { APDU_CODES, DERIVATION_PATH, LedgerAccountResponse, LedgerTransport, STATUS_CODES, listenTimeout, openTimeout } from '.'
+import { APDU_CODES, DERIVATION_PATH, LedgerAccountResponse, LedgerTransport, STATUS_CODES, LISTEN_TIMEOUT, OPEN_TIMEOUT } from '.'
 import { HARDENED_OFFSET } from '../constants'
 import { bytes, dec } from '../convert'
 
@@ -13,7 +13,7 @@ export async function _account (transport: LedgerTransport, index: number = 0, s
                const account = dec.toBytes(index + HARDENED_OFFSET, 4)
                const data = new Uint8Array([...DERIVATION_PATH, ...account])
 
-               const t = await transport.create(openTimeout, listenTimeout)
+               const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
                const response = await t
                        .send(APDU_CODES.class, APDU_CODES.account, show ? 1 : 0, APDU_CODES.paramUnused, data as Buffer)
                        .catch((err: any) => dec.toBytes(err.statusCode))
index 98bd68ddab7ff26044a344593dfe482d601e11b9..fb3276819f54a85feb6d528d0332edbe80800d0e 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { APDU_CODES, LedgerResponse, LedgerTransport, STATUS_CODES, listenTimeout, openTimeout } from '.'
+import { APDU_CODES, LedgerResponse, LedgerTransport, STATUS_CODES, LISTEN_TIMEOUT, OPEN_TIMEOUT } from '.'
 import { Account } from '../account'
 import { Block } from '../block'
 import { BIP44_COIN_NANO, BIP44_PURPOSE, HARDENED_OFFSET } from '../constants'
@@ -37,7 +37,7 @@ export async function _cache (transport: LedgerTransport, index: number = 0, blo
                        const signature = hex.toBytes(block.signature, 64)
                        const data = new Uint8Array([APDU_CODES.bip32DerivationLevel, ...purpose, ...coin, ...account, ...previous, ...link, ...representative, ...balance, ...signature])
 
-                       const t = await transport.create(openTimeout, listenTimeout)
+                       const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
                        const response = await t
                                .send(APDU_CODES.class, APDU_CODES.cacheBlock, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer)
                                .then((res: Buffer) => bytes.toDec(res))
index 18ffd7a454ecd7b6ecbe95de5629b3b8d928f7f9..3a729cb2e41d7da2c866928955bafb03908f1245 100644 (file)
@@ -1,11 +1,11 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { LedgerResponse, LedgerTransport, openTimeout, listenTimeout, APDU_CODES, STATUS_CODES } from '.'
+import { LedgerResponse, LedgerTransport, OPEN_TIMEOUT, LISTEN_TIMEOUT, APDU_CODES, STATUS_CODES } from '.'
 import { bytes } from '../convert'
 
 export async function _close (transport: LedgerTransport): Promise<LedgerResponse> {
-       const t = await transport.create(openTimeout, listenTimeout)
+       const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
        const response = await t
                .send(0xb0, 0xa7, APDU_CODES.paramUnused, APDU_CODES.paramUnused)
                .then((res: Buffer) => bytes.toDec(res))
index 4d32f9f9cf3d798a992333e3b000526a02e91de4..a6389f1caee165a1488d6694e5827891814098ef 100644 (file)
@@ -1,14 +1,14 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { ledgerVendorId } from '.'
+import { LEDGER_VENDOR_ID } from '.'
 
 export async function _disconnect (): Promise<void> {
        try {
                const hidDevices = await navigator?.hid?.getDevices?.() ?? []
                const usbDevices = await navigator?.usb?.getDevices?.() ?? []
                const devices = [...hidDevices, ...usbDevices]
-                       .filter(device => device.vendorId === ledgerVendorId)
+                       .filter(device => device.vendorId === LEDGER_VENDOR_ID)
                await Promise.allSettled(devices.map(device => device.forget?.() ?? Promise.resolve()))
        } catch (err) {
                console.warn('Ledger.disconnect()', err)
index 3ed69974882a4186b0458dacaabb953a735e7633..969c75fd88651c147d8aec7dda410faff250af9c 100644 (file)
@@ -57,7 +57,11 @@ export const DERIVATION_PATH: Uint8Array = new Uint8Array([
  * Vendor ID assigned to Ledger for HID and USB interfaces.
  * https://github.com/LedgerHQ/ledger-live/blob/develop/libs/ledgerjs/packages/devices/src/index.ts#L164
  */
-export const ledgerVendorId: 0x2c97 = 0x2c97
+export const LEDGER_VENDOR_ID: 0x2c97 = 0x2c97
+
+export const LISTEN_TIMEOUT: 30000 = 30000
+
+export const OPEN_TIMEOUT: 3000 = 3000
 
 export const STATUS_CODES: Readonly<Record<number, string>> = Object.freeze({
        ...Object.fromEntries(Object.entries(StatusCodes).map(([k, v]) => [+v, k])),
@@ -67,10 +71,6 @@ export const STATUS_CODES: Readonly<Record<number, string>> = Object.freeze({
        0x6a82: 'CACHE_MISS'
 })
 
-export const listenTimeout: 30000 = 30000
-
-export const openTimeout: 3000 = 3000
-
 /**
  * Ledger hardware wallet created by communicating with a Ledger device via ADPU
  * calls. This wallet does not feature any seed nor mnemonic phrase as all
@@ -368,9 +368,9 @@ export class Ledger {
        static async #poll (): Promise<void> {
                try {
                        const isHidPaired = (await navigator.hid?.getDevices?.() ?? [])
-                               .some(device => device.vendorId === ledgerVendorId)
+                               .some(device => device.vendorId === LEDGER_VENDOR_ID)
                        const isUsbPaired = (await navigator.usb?.getDevices?.() ?? [])
-                               .some(device => device.vendorId === ledgerVendorId)
+                               .some(device => device.vendorId === LEDGER_VENDOR_ID)
                        if ((this.#transport === TransportHID && isHidPaired)
                                || (this.#transport === TransportUSB && isUsbPaired)) {
                                await this.connect()
index 5ffc2b650df9dd75bff7d290a544e0762c2be47e..1e6023b7568b08906516004ae79c6d679d15e543 100644 (file)
@@ -1,12 +1,12 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { LedgerResponse, LedgerTransport, openTimeout, listenTimeout, APDU_CODES, STATUS_CODES } from '.'
+import { LedgerResponse, LedgerTransport, OPEN_TIMEOUT, LISTEN_TIMEOUT, APDU_CODES, STATUS_CODES } from '.'
 import { bytes } from '../convert'
 
 export async function _open (transport: LedgerTransport): Promise<LedgerResponse> {
        const name = new TextEncoder().encode('Nano')
-       const t = await transport.create(openTimeout, listenTimeout)
+       const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
        const response = await t
                .send(0xe0, 0xd8, APDU_CODES.paramUnused, APDU_CODES.paramUnused, name as Buffer)
                .then((res: Buffer) => bytes.toDec(res))
index 5bb3d26ffaf4fa4a3115700fbf745c3c23ac0785..a52082279edb85b466d7e17f36fe82377e84629b 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { APDU_CODES, DERIVATION_PATH, LedgerSignResponse, LedgerTransport, STATUS_CODES, listenTimeout, openTimeout } from '.'
+import { APDU_CODES, DERIVATION_PATH, LedgerSignResponse, LedgerTransport, STATUS_CODES, LISTEN_TIMEOUT, OPEN_TIMEOUT } from '.'
 import { Account } from '../account'
 import { Block } from '../block'
 import { HARDENED_OFFSET } from '../constants'
@@ -38,7 +38,7 @@ export async function signBlock (transport: LedgerTransport, index: number, bloc
                        const balance = hex.toBytes(BigInt(block.balance).toString(16), 16)
                        const data = new Uint8Array([...DERIVATION_PATH, ...account, ...previous, ...link, ...representative, ...balance])
 
-                       const t = await transport.create(openTimeout, listenTimeout)
+                       const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
                        const response = await t
                                .send(APDU_CODES.class, APDU_CODES.signBlock, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer)
                                .catch((err: any) => dec.toBytes(err.statusCode))
@@ -83,7 +83,7 @@ export async function signNonce (transport: LedgerTransport, index: number, nonc
                const derivationAccount = dec.toBytes(index + HARDENED_OFFSET, 4)
                const data = new Uint8Array([...DERIVATION_PATH, ...derivationAccount, ...nonce])
 
-               const t = await transport.create(openTimeout, listenTimeout)
+               const t = await transport.create(OPEN_TIMEOUT, LISTEN_TIMEOUT)
                const response = await t
                        .send(APDU_CODES.class, APDU_CODES.signNonce, APDU_CODES.paramUnused, APDU_CODES.paramUnused, data as Buffer)
                        .catch((err: any) => dec.toBytes(err.statusCode))
index 3315246cc693069c9ebd5047b0e81e37d8882f70..ccbd2b7842624a0ef30e4ad29b0dcf36c2fed2e8 100644 (file)
@@ -1,7 +1,7 @@
 //! SPDX-FileCopyrightText: 2025 Chris Duncan <chris@codecow.com>
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
-import { APDU_CODES, LedgerResponse, LedgerTransport, STATUS_CODES, listenTimeout, openTimeout } from '.'
+import { APDU_CODES, LedgerResponse, LedgerTransport, STATUS_CODES, LISTEN_TIMEOUT, OPEN_TIMEOUT } from '.'
 import { bytes, dec } from '../convert'
 import { queue } from './queue'
 
@@ -20,7 +20,7 @@ interface LedgerVersionResponse extends LedgerResponse {
 export async function version (transport: LedgerTransport): Promise<LedgerVersionResponse> {
        return queue<LedgerVersionResponse>(async () => {
                try {
-                       const t = await transport.create(openTimeout, listenTimeout)
+                       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))