From 463c6c0a902616f49b09e83747adda20da3fc6ad Mon Sep 17 00:00:00 2001 From: Chris Duncan Date: Thu, 10 Jul 2025 13:39:25 -0700 Subject: [PATCH] Quit app ADPU is not working, so sever the connection by losing browser privileges instead. --- src/lib/wallets/ledger-wallet.ts | 25 +++++++++++++++++++------ test/test.ledger.mjs | 3 ++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/lib/wallets/ledger-wallet.ts b/src/lib/wallets/ledger-wallet.ts index bc192c0..492a4ad 100644 --- a/src/lib/wallets/ledger-wallet.ts +++ b/src/lib/wallets/ledger-wallet.ts @@ -136,9 +136,8 @@ export class LedgerWallet extends Wallet { */ async destroy (): Promise { await super.destroy() - const { status } = await this.#close() - if (status !== 'OK') { - throw new Error('Failed to lock Ledger wallet', { cause: status }) + if (!(await this.lock())) { + throw new Error('Failed to lock Ledger wallet') } } @@ -152,7 +151,11 @@ export class LedgerWallet extends Wallet { } /** - * Attempts to close the current process on the Ledger device. + * Revokes permission granted by the user to access the Ledger device. + * + * The 'quit app' ADPU command has not passed testing, so this is the only way + * to ensure the connection is severed at this time. `setTimeout` is used to + * expire any lingering transient user activation. * * Overrides the default wallet `lock()` method since as a hardware wallet it * does not need to be encrypted by software. @@ -160,8 +163,18 @@ export class LedgerWallet extends Wallet { * @returns True if successfully locked */ async lock (): Promise { - const { status } = await this.#close() - return status === 'OK' + try { + const devices = await globalThis.navigator.usb.getDevices() + for (const device of devices) { + if (device.vendorId === ledgerUSBVendorId) { + await device.forget() + } + } + return new Promise(r => setTimeout(r, 0, true)) + } catch (err) { + console.log(err) + return new Promise(r => setTimeout(r, 0, false)) + } } onConnectUsb = async (e: USBConnectionEvent): Promise => { diff --git a/test/test.ledger.mjs b/test/test.ledger.mjs index 09022b9..134fe7c 100644 --- a/test/test.ledger.mjs +++ b/test/test.ledger.mjs @@ -176,7 +176,8 @@ await suite('Ledger hardware wallet', { skip: false || isNode }, async () => { }) await test('destroy wallet', async () => { - await assert.resolves(wallet.destroy()) + await wallet.destroy() + await assert.rejects(wallet.version()) }) await test('fail when using new', async () => { -- 2.47.3