Skip to content

Commit

Permalink
chore: refactor canclose
Browse files Browse the repository at this point in the history
  • Loading branch information
pete-watters committed May 15, 2024
1 parent ffd79f4 commit ff5aba0
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
getBitcoinAppVersion,
isBitcoinAppOpen,
} from '@app/features/ledger/utils/bitcoin-ledger-utils';
import { useActionCancellableByUser } from '@app/features/ledger/utils/stacks-ledger-utils';
import { useCancelLedgerAction } from '@app/features/ledger/utils/generic-ledger-utils';
import { useToast } from '@app/features/toasts/use-toast';
import { useSignLedgerBitcoinTx } from '@app/store/accounts/blockchain/bitcoin/bitcoin.hooks';
import { useCurrentNetwork } from '@app/store/networks/networks.selectors';
Expand All @@ -43,7 +43,6 @@ function LedgerSignBitcoinTxContainer() {
const location = useLocation();
const ledgerNavigate = useLedgerNavigate();
const ledgerAnalytics = useLedgerAnalytics();
const canUserCancelAction = useActionCancellableByUser();
useScrollLock(true);

const [unsignedTransactionRaw, setUnsignedTransactionRaw] = useState<null | string>(null);
Expand Down Expand Up @@ -113,12 +112,12 @@ function LedgerSignBitcoinTxContainer() {
latestDeviceResponse,
awaitingDeviceConnection,
};
const canCancelLedgerAction = useCancelLedgerAction(awaitingDeviceConnection);

const canClose = !awaitingDeviceConnection && canUserCancelAction;
return (
<TxSigningFlow
context={ledgerContextValue}
closeAction={canClose ? ledgerNavigate.cancelLedgerAction : undefined}
closeAction={canCancelLedgerAction ? ledgerNavigate.cancelLedgerAction : undefined}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-s
import { useKeyActions } from '@app/common/hooks/use-key-actions';
import { useScrollLock } from '@app/common/hooks/use-scroll-lock';
import { makeLedgerCompatibleUnsignedAuthResponsePayload } from '@app/common/unsafe-auth-response';
import { useCancelLedgerAction } from '@app/features/ledger/utils/generic-ledger-utils';
import {
getStacksAppVersion,
prepareLedgerDeviceStacksAppConnection,
useActionCancellableByUser,
} from '@app/features/ledger/utils/stacks-ledger-utils';
import {
useCurrentStacksAccount,
Expand Down Expand Up @@ -47,7 +47,6 @@ export function LedgerSignJwtContainer() {
const getBitcoinAddressesLegacyFormat = useGetLegacyAuthBitcoinAddresses();

const keyActions = useKeyActions();
const canUserCancelAction = useActionCancellableByUser();
const { decodedAuthRequest, authRequest } = useOnboardingState();

const [accountIndex, setAccountIndex] = useState<null | number>(null);
Expand Down Expand Up @@ -178,13 +177,14 @@ export function LedgerSignJwtContainer() {
awaitingDeviceConnection,
};

const canClose = !awaitingDeviceConnection && canUserCancelAction;
const canCancelLedgerAction = useCancelLedgerAction(awaitingDeviceConnection);

return (
<LedgerJwtSigningProvider value={ledgerContextValue}>
<Dialog
isShowing
header={<DialogHeader />}
onClose={canClose ? () => onCancelConnectLedger() : undefined}
onClose={canCancelLedgerAction ? () => onCancelConnectLedger() : undefined}
>
<Outlet />
</Dialog>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ import {
getBitcoinAppVersion,
isBitcoinAppOpen,
} from '@app/features/ledger/utils/bitcoin-ledger-utils';
import { useActionCancellableByUser } from '@app/features/ledger/utils/stacks-ledger-utils';
import { useCancelLedgerAction } from '@app/features/ledger/utils/generic-ledger-utils';
import { bitcoinKeysSlice } from '@app/store/ledger/bitcoin/bitcoin-key.slice';
import { useCurrentNetwork } from '@app/store/networks/networks.selectors';

function LedgerRequestBitcoinKeys() {
const navigate = useNavigate();
const dispatch = useDispatch();

const canUserCancelAction = useActionCancellableByUser();
const ledgerNavigate = useLedgerNavigate();
const network = useCurrentNetwork();

Expand Down Expand Up @@ -69,10 +68,11 @@ function LedgerRequestBitcoinKeys() {
outdatedAppVersionWarning: false,
};

const canCancelLedgerAction = useCancelLedgerAction(awaitingDeviceConnection);
return (
<RequestKeysFlow
context={ledgerContextValue}
isActionCancellableByUser={!awaitingDeviceConnection && canUserCancelAction}
isActionCancellableByUser={canCancelLedgerAction}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {
useRequestLedgerKeys,
} from '@app/features/ledger/generic-flows/request-keys/use-request-ledger-keys';
import { useLedgerNavigate } from '@app/features/ledger/hooks/use-ledger-navigate';
import { useCancelLedgerAction } from '@app/features/ledger/utils/generic-ledger-utils';
import {
connectLedgerStacksApp,
getStacksAppVersion,
isStacksAppOpen,
useActionCancellableByUser,
} from '@app/features/ledger/utils/stacks-ledger-utils';
import { useToast } from '@app/features/toasts/use-toast';
import { stacksKeysSlice } from '@app/store/ledger/stacks/stacks-key.slice';
Expand All @@ -27,7 +27,6 @@ function LedgerRequestStacksKeys() {
const toast = useToast();
const navigate = useNavigate();
const ledgerNavigate = useLedgerNavigate();
const canUserCancelAction = useActionCancellableByUser();

const dispatch = useDispatch();

Expand Down Expand Up @@ -76,10 +75,11 @@ function LedgerRequestStacksKeys() {
outdatedAppVersionWarning,
};

const canCancelLedgerAction = useCancelLedgerAction(awaitingDeviceConnection);
return (
<RequestKeysFlow
context={ledgerContextValue}
isActionCancellableByUser={!awaitingDeviceConnection && canUserCancelAction}
isActionCancellableByUser={canCancelLedgerAction}
/>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { delay, isError } from '@shared/utils';

import { useScrollLock } from '@app/common/hooks/use-scroll-lock';
import { appEvents } from '@app/common/publish-subscribe';
import { useCancelLedgerAction } from '@app/features/ledger/utils/generic-ledger-utils';
import {
getStacksAppVersion,
prepareLedgerDeviceStacksAppConnection,
signLedgerStacksStructuredMessage,
signLedgerStacksUtf8Message,
useActionCancellableByUser,
} from '@app/features/ledger/utils/stacks-ledger-utils';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';
import { StacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.models';
Expand Down Expand Up @@ -54,7 +54,6 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
const verifyLedgerPublicKey = useVerifyMatchingLedgerStacksPublicKey();

const [latestDeviceResponse, setLatestDeviceResponse] = useLedgerResponseState();
const canUserCancelAction = useActionCancellableByUser();

const [awaitingDeviceConnection, setAwaitingDeviceConnection] = useState(false);

Expand Down Expand Up @@ -142,14 +141,14 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
latestDeviceResponse,
awaitingDeviceConnection,
};
const canCancelLedgerAction = useCancelLedgerAction(awaitingDeviceConnection);

const canClose = !awaitingDeviceConnection && canUserCancelAction;
return (
<LedgerMsgSigningProvider value={ledgerContextValue}>
<Dialog
isShowing
header={<DialogHeader />}
onClose={canClose ? () => ledgerNavigate.cancelLedgerAction() : undefined}
onClose={canCancelLedgerAction ? () => ledgerNavigate.cancelLedgerAction() : undefined}
>
<Outlet />
</Dialog>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ import { delay, isError } from '@shared/utils';
import { useScrollLock } from '@app/common/hooks/use-scroll-lock';
import { appEvents } from '@app/common/publish-subscribe';
import { LedgerTxSigningContext } from '@app/features/ledger/generic-flows/tx-signing/ledger-sign-tx.context';
import { useCancelLedgerAction } from '@app/features/ledger/utils/generic-ledger-utils';
import {
connectLedgerStacksApp,
getStacksAppVersion,
isStacksAppOpen,
isVersionOfLedgerStacksAppWithContractPrincipalBug,
signLedgerStacksTransaction,
signStacksTransactionWithSignature,
useActionCancellableByUser,
} from '@app/features/ledger/utils/stacks-ledger-utils';
import { useCurrentStacksAccount } from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';

Expand Down Expand Up @@ -46,7 +46,6 @@ function LedgerSignStacksTxContainer() {
const verifyLedgerPublicKey = useVerifyMatchingLedgerStacksPublicKey();
const [unsignedTx, setUnsignedTx] = useState<null | string>(null);
const navigate = useNavigate();
const canUserCancelAction = useActionCancellableByUser();

const chain = 'stacks' as const;

Expand Down Expand Up @@ -149,10 +148,12 @@ function LedgerSignStacksTxContainer() {
awaitingDeviceConnection,
hasUserSkippedBuggyAppWarning,
};

const canClose = !awaitingDeviceConnection && canUserCancelAction;
const canCancelLedgerAction = useCancelLedgerAction(awaitingDeviceConnection);

return (
<TxSigningFlow context={ledgerContextValue} closeAction={canClose ? closeAction : undefined} />
<TxSigningFlow
context={ledgerContextValue}
closeAction={canCancelLedgerAction ? closeAction : undefined}
/>
);
}
18 changes: 18 additions & 0 deletions src/app/features/ledger/utils/generic-ledger-utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useState } from 'react';
import { useLocation } from 'react-router-dom';

import TransportWebUSB from '@ledgerhq/hw-transport-webusb';
import BitcoinApp from 'ledger-bitcoin';

import { RouteUrls } from '@shared/route-urls';
import { delay } from '@shared/utils';

import { safeAwait } from '@app/common/utils/safe-await';
Expand Down Expand Up @@ -120,3 +122,19 @@ export function checkLockedDeviceError(e: Error) {
e?.message === LedgerConnectionErrors.DeviceLocked
);
}

function useActionCancellableByUser() {
const { pathname } = useLocation();
return (
pathname.includes(RouteUrls.ConnectLedger) ||
pathname.includes(RouteUrls.ConnectLedgerSuccess) ||
pathname.includes(RouteUrls.ConnectLedgerError) ||
pathname.includes(RouteUrls.AwaitingDeviceUserAction)
);
}

export function useCancelLedgerAction(awaitingDeviceConnection: boolean) {
const canUserCancelAction = useActionCancellableByUser();

return !awaitingDeviceConnection && canUserCancelAction;
}
13 changes: 0 additions & 13 deletions src/app/features/ledger/utils/stacks-ledger-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { useLocation } from 'react-router-dom';

import Transport from '@ledgerhq/hw-transport-webusb';
import {
AddressVersion,
Expand All @@ -11,7 +9,6 @@ import StacksApp, { LedgerError, ResponseSign, ResponseVersion } from '@zondax/l
import { compare } from 'compare-versions';

import { getStxDerivationPath, stxDerivationWithAccount } from '@shared/crypto/stacks/stacks.utils';
import { RouteUrls } from '@shared/route-urls';

import {
LEDGER_APPS_MAP,
Expand Down Expand Up @@ -85,16 +82,6 @@ export function signStacksTransactionWithSignature(transaction: string, signatur
return deserializedTx;
}

export function useActionCancellableByUser() {
const { pathname } = useLocation();
return (
pathname.includes(RouteUrls.ConnectLedger) ||
pathname.includes(RouteUrls.ConnectLedgerSuccess) ||
pathname.includes(RouteUrls.ConnectLedgerError) ||
pathname.includes(RouteUrls.AwaitingDeviceUserAction)
);
}

export function isStacksLedgerAppClosed(response: ResponseVersion) {
const anotherUnknownErrorCodeMeaningAppClosed = 28161;
return (
Expand Down

0 comments on commit ff5aba0

Please sign in to comment.