Skip to content

Commit

Permalink
refactor: useLedgerSignTx should be chain agnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Feb 2, 2024
1 parent 8096b8a commit 908a978
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from 'react';
import { Route, useLocation } from 'react-router-dom';
import { Route, useLocation, useNavigate } from 'react-router-dom';

import { deserializeTransaction } from '@stacks/transactions';
import StacksApp, { LedgerError } from '@zondax/ledger-stacks';
Expand All @@ -15,6 +15,7 @@ import {
connectLedgerStacksApp,
getStacksAppVersion,
isStacksAppOpen,
isVersionOfLedgerStacksAppWithContractPrincipalBug,
signLedgerStacksTransaction,
signStacksTransactionWithSignature,
} from '@app/features/ledger/utils/stacks-ledger-utils';
Expand Down Expand Up @@ -43,6 +44,7 @@ function LedgerSignStacksTxContainer() {
const account = useCurrentStacksAccount();
const verifyLedgerPublicKey = useVerifyMatchingLedgerStacksPublicKey();
const [unsignedTx, setUnsignedTx] = useState<null | string>(null);
const navigate = useNavigate();

const chain = 'stacks' as const;

Expand All @@ -63,6 +65,22 @@ function LedgerSignStacksTxContainer() {
isAppOpen: isStacksAppOpen,
getAppVersion: getStacksAppVersion,
connectApp: connectLedgerStacksApp,
async passesAdditionalVersionCheck(appVersion) {
if (appVersion.chain !== 'stacks') {
return true;
}

if (isVersionOfLedgerStacksAppWithContractPrincipalBug(appVersion)) {
navigate(RouteUrls.LedgerOutdatedAppWarning);
const response = await hasUserSkippedBuggyAppWarning.wait();

if (response === 'cancelled-operation') {
ledgerNavigate.cancelLedgerAction();
}
return false;
}
return true;
},
async signTransactionWithDevice(stacksApp) {
// TODO: need better handling
if (!account) return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useMemo, useState } from 'react';
import { useNavigate } from 'react-router-dom';

import StacksApp from '@zondax/ledger-stacks';
import BitcoinApp from 'ledger-bitcoin';

import { SupportedBlockchains } from '@shared/constants';
import { RouteUrls } from '@shared/route-urls';

import { delay } from '@app/common/utils';

Expand All @@ -16,17 +14,15 @@ import {
checkLockedDeviceError,
useLedgerResponseState,
} from '../../utils/generic-ledger-utils';
import {
StacksAppVersion,
isVersionOfLedgerStacksAppWithContractPrincipalBug,
} from '../../utils/stacks-ledger-utils';
import { StacksAppVersion } from '../../utils/stacks-ledger-utils';
import { createWaitForUserToSeeWarningScreen } from './ledger-sign-tx.context';

interface UseLedgerSignTxArgs<App extends BitcoinApp | StacksApp> {
chain: SupportedBlockchains;
isAppOpen({ name }: { name: string }): boolean;
getAppVersion(app: App): Promise<StacksAppVersion> | Promise<BitcoinAppVersion>;
connectApp(): Promise<App>;
passesAdditionalVersionCheck?(appVersion: StacksAppVersion | BitcoinAppVersion): Promise<boolean>;
onSuccess?(): void;
signTransactionWithDevice(app: App): Promise<void>;
}
Expand All @@ -38,31 +34,24 @@ export function useLedgerSignTx<App extends StacksApp | BitcoinApp>({
connectApp,
onSuccess,
signTransactionWithDevice,
passesAdditionalVersionCheck,
}: UseLedgerSignTxArgs<App>) {
const [outdatedAppVersionWarning, setAppVersionOutdatedWarning] = useState(false);
const [latestDeviceResponse, setLatestDeviceResponse] = useLedgerResponseState();
const [awaitingDeviceConnection, setAwaitingDeviceConnection] = useState(false);
const ledgerNavigate = useLedgerNavigate();
const hasUserSkippedBuggyAppWarning = useMemo(() => createWaitForUserToSeeWarningScreen(), []);
const navigate = useNavigate();
async function checkCorrectAppIsOpenWithFailState(app: App) {
const response = await getAppVersion(app);
if (!isAppOpen({ name: response.name })) {
setAwaitingDeviceConnection(false);
throw new Error(LedgerConnectionErrors.AppNotOpen);
}
if (response.chain === 'stacks') {
if (isVersionOfLedgerStacksAppWithContractPrincipalBug(response)) {
navigate(RouteUrls.LedgerOutdatedAppWarning);
const response = await hasUserSkippedBuggyAppWarning.wait();

if (response === 'cancelled-operation') {
ledgerNavigate.cancelLedgerAction();
return;
}
}
const passedAdditionalVersionCheck = await passesAdditionalVersionCheck?.(response);
if (passedAdditionalVersionCheck) {
return response;
}
return response;
return;
}

async function signTransactionImpl() {
Expand All @@ -85,7 +74,8 @@ export function useLedgerSignTx<App extends StacksApp | BitcoinApp>({
}

ledgerNavigate.toErrorStep(chain);
return app?.transport.close();
} finally {
await app?.transport.close();
}
}

Expand Down

0 comments on commit 908a978

Please sign in to comment.