From 816f9b6890d2ad7bb755c9cf23a78f0f678898e1 Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Sun, 27 Jul 2025 00:28:44 -0700 Subject: [PATCH] Fix wallet destruction. Adjust Ledger browser support check. --- src/lib/wallets/ledger-wallet.ts | 13 +++++++++---- src/lib/wallets/wallet.ts | 2 +- src/types.d.ts | 13 ++++++------- test/test.ledger.mjs | 8 +------- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/lib/wallets/ledger-wallet.ts b/src/lib/wallets/ledger-wallet.ts index cd5c947..7076408 100644 --- a/src/lib/wallets/ledger-wallet.ts +++ b/src/lib/wallets/ledger-wallet.ts @@ -35,17 +35,21 @@ export class LedgerWallet extends Wallet { * Check which transport protocols are supported by the browser and return the * transport type according to the following priorities: Bluetooth, USB, HID. */ - static checkBrowserSupport (): void { + static get isUnsupported (): boolean { console.log('Checking browser Ledger support...') - if (typeof globalThis.navigator?.bluetooth?.getDevices === 'function') { - this.DynamicTransport = TransportBLE - } if (typeof globalThis.navigator?.usb?.getDevices === 'function') { this.DynamicTransport = TransportUSB + return false + } + if (typeof globalThis.navigator?.bluetooth?.getDevices === 'function') { + this.DynamicTransport = TransportBLE + return false } if (typeof globalThis.navigator?.hid?.getDevices === 'function') { this.DynamicTransport = TransportHID + return false } + return true } /** @@ -56,6 +60,7 @@ export class LedgerWallet extends Wallet { */ static async create (): Promise { try { + if (this.isUnsupported) throw new Error('Browser is unsupported') const id = await Entropy.create(16) LedgerWallet.#isInternal = true const wallet = new this(id) diff --git a/src/lib/wallets/wallet.ts b/src/lib/wallets/wallet.ts index c09a0e1..d4981c3 100644 --- a/src/lib/wallets/wallet.ts +++ b/src/lib/wallets/wallet.ts @@ -178,7 +178,7 @@ export abstract class Wallet { await SafeWorker.request({ store: 'Wallet', method: 'destroy', - name: this.id + names: this.id }) } catch (err) { console.error(err) diff --git a/src/types.d.ts b/src/types.d.ts index a68fc92..e02eb80 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -932,16 +932,14 @@ interface LedgerSignResponse extends LedgerResponse { */ export declare class LedgerWallet extends Wallet { #private - get listenTimeout (): 30000 - get openTimeout (): 3000 - get status (): DeviceStatus - DynamicTransport: typeof TransportBLE | typeof TransportUSB | typeof TransportHID - private constructor () + static DynamicTransport: typeof TransportBLE | typeof TransportUSB | typeof TransportHID + static get listenTimeout (): 30000 + static get openTimeout (): 3000 /** * Check which transport protocols are supported by the browser and return the * transport type according to the following priorities: Bluetooth, USB, HID. */ - static checkBrowserSupport (): typeof TransportBLE | typeof TransportUSB | typeof TransportHID + static get isUnsupported (): boolean /** * Creates a new Ledger hardware wallet communication layer by dynamically * importing the ledger.js service. @@ -949,6 +947,8 @@ export declare class LedgerWallet extends Wallet { * @returns {LedgerWallet} A wallet containing accounts and a Ledger device communication object */ static create (): Promise + get status (): DeviceStatus + private constructor () /** * Check if the Nano app is currently open and set device status accordingly. * @@ -964,7 +964,6 @@ export declare class LedgerWallet extends Wallet { * allow garbage collection. */ destroy (): Promise - init (): Promise /** * Revokes permission granted by the user to access the Ledger device. * diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index c334655..51b8bb5 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -35,19 +35,13 @@ if (isNode) { const rpc = new Rpc(env.NODE_URL ?? '', env.API_KEY_NAME) -let isUnsupported = true -try { - LedgerWallet.checkBrowserSupport() - isUnsupported = false -} catch {} - /** * HID interactions require user gestures, so to reduce clicks, the variables * shared among tests like wallet and account are declared at the top-level. */ await Promise.all([ /* node:coverage disable */ - suite('Ledger hardware wallet', { skip: false || isNode || isUnsupported }, async () => { + suite('Ledger hardware wallet', { skip: false || isNode || LedgerWallet.isUnsupported }, async () => { let wallet, account, openBlock, sendBlock, receiveBlock -- 2.47.3