diff --git a/src/accounts.tsx b/src/accounts.tsx index 20a58698d..13eef4d78 100644 --- a/src/accounts.tsx +++ b/src/accounts.tsx @@ -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'; @@ -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); }, }; diff --git a/src/ledger/service.ts b/src/ledger/service.ts index f1a090cd1..1783164c8 100644 --- a/src/ledger/service.ts +++ b/src/ledger/service.ts @@ -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'; @@ -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) { @@ -123,21 +127,14 @@ 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), @@ -145,10 +142,8 @@ class LedgerService { ); 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), @@ -156,26 +151,25 @@ class LedgerService { ); 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') @@ -189,7 +183,7 @@ class LedgerService { } async queueSignRequest( - selectedAccount: PreferencesAccount | undefined, + selectedAccount: PreferencesAccount, request: LedgerSignRequest ) { try { diff --git a/src/popup.tsx b/src/popup.tsx index 2e006f34a..762ed0b9b 100644 --- a/src/popup.tsx +++ b/src/popup.tsx @@ -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'; @@ -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); }, }; diff --git a/src/ui/services/Background.ts b/src/ui/services/Background.ts index a4f4e49b2..e05fb8a75 100644 --- a/src/ui/services/Background.ts +++ b/src/ui/services/Background.ts @@ -721,7 +721,7 @@ class Background { } } - async ledgerSignResponse(requestId: string, error: Error): Promise; + async ledgerSignResponse(requestId: string, error: unknown): Promise; async ledgerSignResponse( requestId: string, error: null, @@ -729,7 +729,7 @@ class Background { ): Promise; async ledgerSignResponse( requestId: string, - error: Error | null, + error: unknown, signature?: string ) { try { @@ -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) {