Skip to content

Commit

Permalink
fix: clear the ledger's transport when disconnected (#2635)
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 authored Nov 22, 2024
1 parent 56d0164 commit f4d1af5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/background/service/keyring/eth-ledger-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { SignHelper, LedgerHDPathType } from './helper';
const type = 'Ledger Hardware';

import HDPathType = LedgerHDPathType;
import Browser from 'webextension-polyfill';
import {
LedgerAction,
OffscreenCommunicationTarget,
} from '@/constant/offscreen-communication';
import { isManifestV3 } from '@/utils/env';

const HD_PATH_BASE = {
[HDPathType.BIP44]: "m/44'/60'/0'/0",
Expand Down Expand Up @@ -70,6 +76,17 @@ class LedgerBridgeKeyring {
this.app = null;
this.usedHDPathTypeList = {};
this.deserialize(opts);

if (isManifestV3) {
Browser.runtime.onMessage.addListener((request) => {
if (
request.target === OffscreenCommunicationTarget.extension &&
request.event === LedgerAction.ledgerDeviceDisconnect
) {
this.cleanUp();
}
});
}
}

serialize() {
Expand Down Expand Up @@ -145,7 +162,9 @@ class LedgerBridgeKeyring {

async cleanUp() {
this.app = null;
if (this.transport) this.transport.close();
if (this.transport) {
await this.transport.close();
}
this.transport = null;
}

Expand Down
4 changes: 4 additions & 0 deletions src/constant/offscreen-communication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,7 @@ export enum LatticeAction {
export enum KnownOrigins {
lattice = 'https://lattice.gridplus.io',
}

export enum LedgerAction {
ledgerDeviceDisconnect = 'ledger-device-disconnect',
}
17 changes: 17 additions & 0 deletions src/offscreen/scripts/ledger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import {
LedgerAction,
OffscreenCommunicationTarget,
} from '@/constant/offscreen-communication';
import { ledgerUSBVendorId } from '@ledgerhq/devices';
import Browser from 'webextension-polyfill';

export function initLedger() {
navigator.hid.addEventListener('disconnect', ({ device }) => {
if (device.vendorId === ledgerUSBVendorId) {
Browser.runtime.sendMessage({
target: OffscreenCommunicationTarget.extension,
event: LedgerAction.ledgerDeviceDisconnect,
});
}
});
}
2 changes: 2 additions & 0 deletions src/offscreen/scripts/offscreen.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { initBitBox02 } from './bitbox02';
import { initImKey } from './imkey';
import initLattice from './lattice';
import { initLedger } from './ledger';
import { initOneKey } from './onekey';

initImKey();
initOneKey();
initBitBox02();
initLattice();
initLedger();

// keep alive when ui page is open
let pageCount = 0;
Expand Down

0 comments on commit f4d1af5

Please sign in to comment.