From 6dc4887d5251a78cfc90d5bbf837a2f5f1416229 Mon Sep 17 00:00:00 2001 From: heisenberg Date: Mon, 11 Nov 2024 12:22:53 +0800 Subject: [PATCH 1/2] fix: remove ledger reconnect --- .../service/keyring/eth-ledger-keyring.ts | 23 ------------------- 1 file changed, 23 deletions(-) diff --git a/src/background/service/keyring/eth-ledger-keyring.ts b/src/background/service/keyring/eth-ledger-keyring.ts index a6aa30d6996..b44391ff67f 100644 --- a/src/background/service/keyring/eth-ledger-keyring.ts +++ b/src/background/service/keyring/eth-ledger-keyring.ts @@ -235,7 +235,6 @@ class LedgerBridgeKeyring { signTransaction(address, tx) { return this.signHelper.invoke(async () => { // make sure the previous transaction is cleaned up - await this._reconnect(); // transactions built with older versions of ethereumjs-tx have a // getChainId method that newer versions do not. Older versions are mutable @@ -296,20 +295,6 @@ class LedgerBridgeKeyring { }); } - async _reconnect() { - await this.cleanUp(); - - let count = 0; - // wait connect the WebHID - while (!this.app) { - await this.makeApp(); - await new Promise((resolve) => setTimeout(resolve, 100)); - if (count++ > 50) { - throw new Error('Ledger: Failed to connect to Ledger'); - } - } - } - async _signTransaction(address, rawTxHex, handleSigning) { const hdPath = await this.unlockAccountByAddress(address); await this.makeApp(true); @@ -326,8 +311,6 @@ class LedgerBridgeKeyring { throw new Error( err.toString() || 'Ledger: Unknown error while signing transaction' ); - } finally { - this.cleanUp(); } } @@ -338,7 +321,6 @@ class LedgerBridgeKeyring { // For personal_sign, we need to prefix the message: async signPersonalMessage(withAccount, message) { return this.signHelper.invoke(async () => { - await this._reconnect(); try { await this.makeApp(true); const hdPath = await this.unlockAccountByAddress(withAccount); @@ -369,8 +351,6 @@ class LedgerBridgeKeyring { throw new Error( e.toString() || 'Ledger: Unknown error while signing message' ); - } finally { - this.cleanUp(); } }); } @@ -397,7 +377,6 @@ class LedgerBridgeKeyring { async signTypedData(withAccount, data, options: any = {}) { return this.signHelper.invoke(async () => { - await this._reconnect(); const isV4 = options.version === 'V4'; if (!isV4) { throw new Error( @@ -471,8 +450,6 @@ class LedgerBridgeKeyring { throw new Error( e.toString() || 'Ledger: Unknown error while signing message' ); - } finally { - this.cleanUp(); } }); } From 710912a5a31b45aa91f79e5e39b57c4cffebbf52 Mon Sep 17 00:00:00 2001 From: heisenberg Date: Mon, 11 Nov 2024 12:51:11 +0800 Subject: [PATCH 2/2] fix: reconnect when disconnect --- .../service/keyring/eth-ledger-keyring.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/background/service/keyring/eth-ledger-keyring.ts b/src/background/service/keyring/eth-ledger-keyring.ts index b44391ff67f..c7d4b178e3c 100644 --- a/src/background/service/keyring/eth-ledger-keyring.ts +++ b/src/background/service/keyring/eth-ledger-keyring.ts @@ -159,10 +159,19 @@ class LedgerBridgeKeyring { const path = hdPath ? this._toLedgerPath(hdPath) : this.hdPath; await this.makeApp(); - const res = await this.app!.getAddress(path, false, true); - const { address } = res; + let res: { address: string }; + try { + res = await this.app!.getAddress(path, false, true); + } catch (e) { + if (e.name === 'DisconnectedDeviceDuringOperation') { + await this.cleanUp(); + return this.unlock(hdPath, force); + } else { + throw e; + } + } - return address; + return res?.address; } addAccounts(n = 1) {