Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove ledger reconnect #2613

Merged
merged 2 commits into from
Nov 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading