Skip to content

Commit

Permalink
Merge branch 'fix/ledger-reconnect' into tmp/20241108
Browse files Browse the repository at this point in the history
  • Loading branch information
heisenberg-2077 committed Nov 11, 2024
2 parents fc3ccb1 + 710912a commit 30466c8
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions src/background/service/keyring/eth-ledger-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -235,7 +244,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
Expand Down Expand Up @@ -296,20 +304,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);
Expand All @@ -326,8 +320,6 @@ class LedgerBridgeKeyring {
throw new Error(
err.toString() || 'Ledger: Unknown error while signing transaction'
);
} finally {
this.cleanUp();
}
}

Expand All @@ -338,7 +330,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);
Expand Down Expand Up @@ -369,8 +360,6 @@ class LedgerBridgeKeyring {
throw new Error(
e.toString() || 'Ledger: Unknown error while signing message'
);
} finally {
this.cleanUp();
}
});
}
Expand All @@ -397,7 +386,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(
Expand Down Expand Up @@ -471,8 +459,6 @@ class LedgerBridgeKeyring {
throw new Error(
e.toString() || 'Ledger: Unknown error while signing message'
);
} finally {
this.cleanUp();
}
});
}
Expand Down

0 comments on commit 30466c8

Please sign in to comment.