Skip to content

Commit

Permalink
Check network code on ledger status update (#261)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Slipchenko authored Apr 19, 2022
1 parent 859ab7c commit c581979
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions src/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function delay(ms: number) {
class LedgerService {
private _connectionRetryIsNeeded: boolean;
private _ledger: WavesLedger;
private _networkCode: string = null;
private _signRequestPromise = Promise.resolve();
private _status = LedgerServiceStatus.Disconnected;

Expand All @@ -32,6 +33,8 @@ class LedgerService {
async connectUsb(networkCode: string) {
await this.disconnect();

this._networkCode = networkCode;

this._ledger = new WavesLedger({
debug: true,
openTimeout: 3000,
Expand All @@ -42,7 +45,7 @@ class LedgerService {
});

while (this._ledger && this._status !== LedgerServiceStatus.Ready) {
await this.updateStatus();
await this.updateStatus(networkCode);

if (this._connectionRetryIsNeeded) {
await delay(1000);
Expand All @@ -51,13 +54,18 @@ class LedgerService {
}
}

async updateStatus() {
async updateStatus(networkCode: string) {
this._connectionRetryIsNeeded = false;

if (!this._ledger) {
return;
}

if (this._networkCode !== networkCode) {
this.disconnect();
return;
}

try {
if (await this._ledger.probeDevice()) {
this._status = LedgerServiceStatus.Ready;
Expand Down Expand Up @@ -190,6 +198,7 @@ class LedgerService {
async disconnect(status = LedgerServiceStatus.Disconnected) {
const ledger = this._ledger;
this._ledger = null;
this._networkCode = null;
this._status = status;

if (ledger) {
Expand Down
2 changes: 1 addition & 1 deletion src/ui/components/pages/importEmail/signWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function SignWrapper({ onConfirm, children }: Props) {
};
}

ledgerService.updateStatus().then(() => {
ledgerService.updateStatus(account.networkCode).then(() => {
if (ledgerService.status === LedgerServiceStatus.Ready) {
setPending(false);
onConfirm(...args);
Expand Down

0 comments on commit c581979

Please sign in to comment.