Skip to content

Commit

Permalink
KEEP-819 Don't try to sign without ledger instance
Browse files Browse the repository at this point in the history
Fixes EXTENSION-94
  • Loading branch information
faergeek committed Dec 5, 2022
1 parent 9d95dd9 commit 1f886fe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 42 deletions.
5 changes: 3 additions & 2 deletions src/accounts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import i18next from 'i18next';
import { StrictMode } from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import invariant from 'tiny-invariant';
import Browser from 'webextension-polyfill';

import { createAccountsStore } from './accounts/store/create';
Expand Down Expand Up @@ -89,8 +90,8 @@ Promise.all([
},
ledgerSignRequest: async (request: LedgerSignRequest) => {
const { selectedAccount } = store.getState();

return ledgerService.queueSignRequest(selectedAccount, request);
invariant(selectedAccount);
await ledgerService.queueSignRequest(selectedAccount, request);
},
};

Expand Down
64 changes: 29 additions & 35 deletions src/ledger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import TransportWebUSB from '@ledgerhq/hw-transport-webusb';
import { captureException } from '@sentry/react';
import WavesLedger from '@waves/ledger';
import { PreferencesAccount } from 'preferences/types';
import invariant from 'tiny-invariant';
import Background from 'ui/services/Background';

import { LedgerSignRequest } from './types';
Expand Down Expand Up @@ -99,18 +100,21 @@ class LedgerService {
}

private async sendSignRequest(
selectedAccount: PreferencesAccount | undefined,
selectedAccount: PreferencesAccount,
request: LedgerSignRequest
) {
try {
if (selectedAccount?.type !== 'ledger') {
throw new Error('Active account is not a ledger account');
invariant(
selectedAccount.type === 'ledger',
'Active account is not a ledger account'
);

if (!ledgerService.ledger) {
return;
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const userData = await ledgerService.ledger!.getUserDataById(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
selectedAccount.id!
const userData = await ledgerService.ledger.getUserDataById(
selectedAccount.id
);

if (userData.address !== selectedAccount.address) {
Expand All @@ -123,59 +127,49 @@ class LedgerService {

switch (request.type) {
case 'order':
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signature = await ledgerService.ledger!.signOrder(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
selectedAccount.id!,
{
...request.data,
dataBuffer: new Uint8Array(request.data.dataBuffer),
}
);
signature = await ledgerService.ledger.signOrder(selectedAccount.id, {
...request.data,
dataBuffer: new Uint8Array(request.data.dataBuffer),
});
break;
case 'request':
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signature = await ledgerService.ledger!.signRequest(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
selectedAccount.id!,
signature = await ledgerService.ledger.signRequest(
selectedAccount.id,
{
...request.data,
dataBuffer: new Uint8Array(request.data.dataBuffer),
}
);
break;
case 'someData':
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signature = await ledgerService.ledger!.signSomeData(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
selectedAccount.id!,
signature = await ledgerService.ledger.signSomeData(
selectedAccount.id,
{
...request.data,
dataBuffer: new Uint8Array(request.data.dataBuffer),
}
);
break;
case 'transaction':
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
signature = await ledgerService.ledger!.signTransaction(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
selectedAccount.id!,
signature = await ledgerService.ledger.signTransaction(
selectedAccount.id,
{
...request.data,
dataBuffer: new Uint8Array(request.data.dataBuffer),
}
);
break;
default:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
throw new Error(`Unknown request type: "${(request as any).type}"`);
}

await Background.ledgerSignResponse(request.id, null, signature);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (err: any) {
} catch (err) {
if (err) {
if (err.name === 'TransportStatusError' && err.statusCode === 37120) {
if (
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(err as any).name === 'TransportStatusError' &&
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(err as any).statusCode === 37120
) {
await Background.ledgerSignResponse(
request.id,
new Error('Request is rejected on ledger')
Expand All @@ -189,7 +183,7 @@ class LedgerService {
}

async queueSignRequest(
selectedAccount: PreferencesAccount | undefined,
selectedAccount: PreferencesAccount,
request: LedgerSignRequest
) {
try {
Expand Down
5 changes: 3 additions & 2 deletions src/popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import i18next from 'i18next';
import { StrictMode } from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import invariant from 'tiny-invariant';
import Browser from 'webextension-polyfill';

import type { UiApi } from './background';
Expand Down Expand Up @@ -90,8 +91,8 @@ Promise.all([
},
ledgerSignRequest: async (request: LedgerSignRequest) => {
const { selectedAccount } = store.getState();

return ledgerService.queueSignRequest(selectedAccount, request);
invariant(selectedAccount);
await ledgerService.queueSignRequest(selectedAccount, request);
},
};

Expand Down
7 changes: 4 additions & 3 deletions src/ui/services/Background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -721,15 +721,15 @@ class Background {
}
}

async ledgerSignResponse(requestId: string, error: Error): Promise<void>;
async ledgerSignResponse(requestId: string, error: unknown): Promise<void>;
async ledgerSignResponse(
requestId: string,
error: null,
signature: string
): Promise<void>;
async ledgerSignResponse(
requestId: string,
error: Error | null,
error: unknown,
signature?: string
) {
try {
Expand All @@ -738,7 +738,8 @@ class Background {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return await this.background!.ledgerSignResponse(
requestId,
error && error.message ? error.message : null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
error && (error as any).message ? (error as any).message : null,
signature
);
} catch (err) {
Expand Down

0 comments on commit 1f886fe

Please sign in to comment.