Skip to content

Commit 21593f8

Browse files
fix: eth app changed to single instance in ledger keyring (#2736)
Even though only one instance of the ledger keyring can exist in the background, sometimes this.app is still null, I don't know why.
1 parent ef251ad commit 21593f8

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

src/background/service/keyring/eth-ledger-keyring.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as ethUtil from 'ethereumjs-util';
22
import * as sigUtil from 'eth-sig-util';
33
import TransportWebHID from '@ledgerhq/hw-transport-webhid';
44
import Transport from '@ledgerhq/hw-transport';
5-
import LedgerEth, { ledgerService } from '@ledgerhq/hw-app-eth';
5+
import LedgerEth from '@ledgerhq/hw-app-eth';
66
import { is1559Tx } from '@/utils/transaction';
77
import {
88
TransactionFactory,
@@ -34,6 +34,8 @@ const HD_PATH_TYPE = {
3434
[HD_PATH_BASE['LedgerLive']]: HDPathType.LedgerLive,
3535
};
3636

37+
let ethApp: LedgerEth | null = null;
38+
3739
interface Account {
3840
address: string;
3941
balance: number | null;
@@ -57,7 +59,6 @@ class LedgerBridgeKeyring {
5759
hdPath: any;
5860
accounts: any;
5961
transport: null | Transport;
60-
app: null | LedgerEth;
6162
hasHIDPermission: null | boolean;
6263
usedHDPathTypeList: Record<string, HDPathType> = {};
6364

@@ -69,7 +70,6 @@ class LedgerBridgeKeyring {
6970
this.paths = {};
7071
this.hasHIDPermission = null;
7172
this.transport = null;
72-
this.app = null;
7373
this.usedHDPathTypeList = {};
7474
this.deserialize(opts);
7575

@@ -132,7 +132,7 @@ class LedgerBridgeKeyring {
132132
}
133133

134134
isUnlocked() {
135-
return !!this.app;
135+
return !!ethApp;
136136
}
137137

138138
setAccountToUnlock(index) {
@@ -144,10 +144,10 @@ class LedgerBridgeKeyring {
144144
}
145145

146146
async makeApp(signing = false) {
147-
if (!this.app) {
147+
if (!ethApp) {
148148
try {
149149
this.transport = await TransportWebHID.create();
150-
this.app = new LedgerEth(this.transport);
150+
ethApp = new LedgerEth(this.transport);
151151
} catch (e: any) {
152152
if (!e.message?.includes('The device is already open')) {
153153
console.error(e);
@@ -157,7 +157,7 @@ class LedgerBridgeKeyring {
157157
}
158158

159159
async cleanUp() {
160-
this.app = null;
160+
ethApp = null;
161161
if (this.transport) {
162162
await this.transport.close();
163163
}
@@ -176,7 +176,7 @@ class LedgerBridgeKeyring {
176176
await this.makeApp();
177177
let res: { address: string };
178178
try {
179-
res = await this.app!.getAddress(path, false, true);
179+
res = await ethApp!.getAddress(path, false, true);
180180
} catch (e) {
181181
if (e.name === 'DisconnectedDeviceDuringOperation') {
182182
await this.cleanUp();
@@ -317,7 +317,7 @@ class LedgerBridgeKeyring {
317317
const hdPath = await this.unlockAccountByAddress(address);
318318
await this.makeApp(true);
319319
try {
320-
const res = await this.app!.signTransaction(hdPath, rawTxHex);
320+
const res = await ethApp!.signTransaction(hdPath, rawTxHex);
321321
const newOrMutatedTx = handleSigning(res);
322322
const valid = newOrMutatedTx.verifySignature();
323323
if (valid) {
@@ -341,7 +341,7 @@ class LedgerBridgeKeyring {
341341
try {
342342
await this.makeApp(true);
343343
const hdPath = await this.unlockAccountByAddress(withAccount);
344-
const res = await this.app!.signPersonalMessage(
344+
const res = await ethApp!.signPersonalMessage(
345345
hdPath,
346346
ethUtil.stripHexPrefix(message)
347347
);
@@ -422,7 +422,7 @@ class LedgerBridgeKeyring {
422422
isV4
423423
).toString('hex');
424424

425-
const res = await this.app!.signEIP712HashedMessage(
425+
const res = await ethApp!.signEIP712HashedMessage(
426426
hdPath,
427427
domainSeparatorHex,
428428
hashStructMessageHex
@@ -555,7 +555,7 @@ class LedgerBridgeKeyring {
555555
}
556556
private async getPathBasePublicKey(hdPathType: HDPathType) {
557557
const pathBase = this.getHDPathBase(hdPathType);
558-
const res = await this.app!.getAddress(pathBase, false, true);
558+
const res = await ethApp!.getAddress(pathBase, false, true);
559559

560560
return res.publicKey;
561561
}
@@ -581,7 +581,7 @@ class LedgerBridgeKeyring {
581581
const hdPathType = this.getHDPathType(detail.hdPath);
582582

583583
// Account
584-
const res = await this.app!.getAddress(detail.hdPath, false, true);
584+
const res = await ethApp!.getAddress(detail.hdPath, false, true);
585585
const addressInDevice = res.address;
586586

587587
// The address is not the same, so we don't need to fix
@@ -617,7 +617,7 @@ class LedgerBridgeKeyring {
617617
await this.unlock();
618618
const addresses = await this.getAccounts();
619619
const pathBase = this.hdPath;
620-
const { publicKey: currentPublicKey } = await this.app!.getAddress(
620+
const { publicKey: currentPublicKey } = await ethApp!.getAddress(
621621
pathBase,
622622
false,
623623
true
@@ -647,7 +647,7 @@ class LedgerBridgeKeyring {
647647
) {
648648
const info = this.getAccountInfo(address);
649649
if (info?.index === 1) {
650-
const res = await this.app!.getAddress(detail.hdPath, false, true);
650+
const res = await ethApp!.getAddress(detail.hdPath, false, true);
651651
if (isSameAddress(res.address, address)) {
652652
accounts.push(info);
653653
}

0 commit comments

Comments
 (0)