Skip to content

Commit

Permalink
refactor: quit app ourselves instead of prompting user to do it
Browse files Browse the repository at this point in the history
  • Loading branch information
edgarkhanzadian committed Jan 10, 2024
1 parent e894808 commit 35c2ff1
Show file tree
Hide file tree
Showing 15 changed files with 107 additions and 166 deletions.
19 changes: 4 additions & 15 deletions src/app/features/ledger/components/ledger-inline-warnings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ interface RequiresChainProp {
interface CommonLedgerInlineWarningsProps extends RequiresChainProp {
latestDeviceResponse: LatestDeviceResponse;
outdatedLedgerAppWarning?: boolean;
incorrectAppOpened: boolean;
}

function OutdatedLedgerAppWarning({ chain }: RequiresChainProp) {
Expand All @@ -39,16 +38,6 @@ function LedgerDeviceLockedWarning({ chain }: RequiresChainProp) {
);
}

function LedgerIncorrectAppWarning({ chain }: RequiresChainProp) {
return (
<WarningLabel textAlign="left">
Incorrect app is opened. Close it and open the {''}
<Capitalize>{chain}</Capitalize>
{''} app to continue.
</WarningLabel>
);
}

function LedgerAppClosedWarning({ chain }: RequiresChainProp) {
return (
<WarningLabel textAlign="left">
Expand All @@ -61,14 +50,14 @@ export function CommonLedgerDeviceInlineWarnings({
chain,
latestDeviceResponse,
outdatedLedgerAppWarning = false,
incorrectAppOpened,
}: CommonLedgerInlineWarningsProps) {
if (!latestDeviceResponse) return null;

if (outdatedLedgerAppWarning) {
return <OutdatedLedgerAppWarning chain={chain} />;
}
if (latestDeviceResponse?.deviceLocked) return <LedgerDeviceLockedWarning chain={chain} />;
if (incorrectAppOpened) return <LedgerIncorrectAppWarning chain={chain} />;
if (latestDeviceResponse && isStacksLedgerAppClosed(latestDeviceResponse))
if (latestDeviceResponse.deviceLocked) return <LedgerDeviceLockedWarning chain={chain} />;
if (isStacksLedgerAppClosed(latestDeviceResponse))
return <LedgerAppClosedWarning chain={chain} />;
return null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import { ledgerSignTxRoutes } from '../../generic-flows/tx-signing/ledger-sign-t
import { useLedgerAnalytics } from '../../hooks/use-ledger-analytics.hook';
import { useLedgerNavigate } from '../../hooks/use-ledger-navigate';
import { connectLedgerBitcoinApp, getBitcoinAppVersion } from '../../utils/bitcoin-ledger-utils';
import {
checkIncorrectAppOpenedError,
checkLockedDeviceError,
useLedgerResponseState,
} from '../../utils/generic-ledger-utils';
import { checkLockedDeviceError, useLedgerResponseState } from '../../utils/generic-ledger-utils';
import { ApproveSignLedgerBitcoinTx } from './steps/approve-bitcoin-sign-ledger-tx';

export const ledgerBitcoinTxSigningRoutes = ledgerSignTxRoutes({
Expand Down Expand Up @@ -68,7 +64,6 @@ function LedgerSignBitcoinTxContainer() {
useEffect(() => () => setUnsignedTransaction(null), []);

const [latestDeviceResponse, setLatestDeviceResponse] = useLedgerResponseState();
const [incorrectAppOpened, setIncorrectAppOpened] = useState(false);

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

Expand Down Expand Up @@ -120,10 +115,6 @@ function LedgerSignBitcoinTxContainer() {
void bitcoinApp.transport.close();
}
} catch (e) {
if (checkIncorrectAppOpenedError(e)) {
setIncorrectAppOpened(true);
return;
}
if (checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
Expand All @@ -137,7 +128,6 @@ function LedgerSignBitcoinTxContainer() {
signTransaction,
latestDeviceResponse,
awaitingDeviceConnection,
incorrectAppOpened,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import {
} from '@app/store/accounts/blockchain/stacks/stacks-account.hooks';

import { useLedgerNavigate } from '../../hooks/use-ledger-navigate';
import {
checkIncorrectAppOpenedError,
checkLockedDeviceError,
useLedgerResponseState,
} from '../../utils/generic-ledger-utils';
import { checkLockedDeviceError, useLedgerResponseState } from '../../utils/generic-ledger-utils';
import {
addSignatureToAuthResponseJwt,
getSha256HashOfJwtAuthPayload,
Expand Down Expand Up @@ -61,7 +57,6 @@ export function LedgerSignJwtContainer() {
}, [location.state]);

const [latestDeviceResponse, setLatestDeviceResponse] = useLedgerResponseState();
const [incorrectAppOpened, setIncorrectAppOpened] = useState(false);

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

Expand Down Expand Up @@ -97,10 +92,6 @@ export function LedgerSignJwtContainer() {
const stacks = await prepareLedgerDeviceStacksAppConnection({
setLoadingState: setAwaitingDeviceConnection,
onError(e) {
if (checkIncorrectAppOpenedError(e)) {
setIncorrectAppOpened(true);
return;
}
if (checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
Expand Down Expand Up @@ -182,7 +173,6 @@ export function LedgerSignJwtContainer() {
jwtPayloadHash,
latestDeviceResponse,
awaitingDeviceConnection,
incorrectAppOpened,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export const ledgerJwtSigningContext = createContext<LedgerJwtSigningContext>({
awaitingDeviceConnection: false,
signJwtPayload: noop,
jwtPayloadHash: null,
incorrectAppOpened: false,
});

export const LedgerJwtSigningProvider = ledgerJwtSigningContext.Provider;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ledgerJwtSigningContext } from '@app/features/ledger/flows/jwt-signing/
import { ConnectLedger } from '@app/features/ledger/generic-steps';

export function ConnectLedgerSignJwt() {
const { signJwtPayload, latestDeviceResponse, awaitingDeviceConnection, incorrectAppOpened } =
const { signJwtPayload, latestDeviceResponse, awaitingDeviceConnection } =
useContext(ledgerJwtSigningContext);

return (
Expand All @@ -16,7 +16,6 @@ export function ConnectLedgerSignJwt() {
<CommonLedgerDeviceInlineWarnings
chain="stacks"
latestDeviceResponse={latestDeviceResponse}
incorrectAppOpened={incorrectAppOpened}
/>
}
onConnectLedger={signJwtPayload}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,37 @@ function LedgerRequestBitcoinKeys() {
const ledgerNavigate = useLedgerNavigate();
const network = useCurrentNetwork();

const { requestKeys, latestDeviceResponse, awaitingDeviceConnection, incorrectAppOpened } =
useRequestLedgerKeys({
chain: 'bitcoin',
connectApp: connectLedgerBitcoinApp(network.chain.bitcoin.bitcoinNetwork),
getAppVersion: getBitcoinAppVersion,
isAppOpen({ name }: { name: string }) {
return name === 'Bitcoin' || name === 'Bitcoin Test';
},
onSuccess() {
navigate('/', { replace: true });
},
async pullKeysFromDevice(app) {
const { keys } = await pullBitcoinKeysFromLedgerDevice(app)({
network: bitcoinNetworkModeToCoreNetworkMode(network.chain.bitcoin.bitcoinNetwork),
onRequestKey(index) {
if (index <= 4) {
ledgerNavigate.toDeviceBusyStep(
`Requesting Bitcoin Native Segwit address (${index + 1}…5)`
);
return;
}
ledgerNavigate.toDeviceBusyStep(`Requesting Bitcoin Taproot address (${index - 4}…5)`);
},
});
dispatch(bitcoinKeysSlice.actions.addKeys(keys));
},
});
const { requestKeys, latestDeviceResponse, awaitingDeviceConnection } = useRequestLedgerKeys({
chain: 'bitcoin',
connectApp: connectLedgerBitcoinApp(network.chain.bitcoin.bitcoinNetwork),
getAppVersion: getBitcoinAppVersion,
isAppOpen({ name }: { name: string }) {
return name === 'Bitcoin' || name === 'Bitcoin Test';
},
onSuccess() {
navigate('/', { replace: true });
},
async pullKeysFromDevice(app) {
const { keys } = await pullBitcoinKeysFromLedgerDevice(app)({
network: bitcoinNetworkModeToCoreNetworkMode(network.chain.bitcoin.bitcoinNetwork),
onRequestKey(index) {
if (index <= 4) {
ledgerNavigate.toDeviceBusyStep(
`Requesting Bitcoin Native Segwit address (${index + 1}…5)`
);
return;
}
ledgerNavigate.toDeviceBusyStep(`Requesting Bitcoin Taproot address (${index - 4}…5)`);
},
});
dispatch(bitcoinKeysSlice.actions.addKeys(keys));
},
});

const ledgerContextValue: LedgerRequestKeysContext = {
chain: 'bitcoin',
pullPublicKeysFromDevice: requestKeys,
latestDeviceResponse,
incorrectAppOpened,
awaitingDeviceConnection,
outdatedAppVersionWarning: false,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,47 @@ function LedgerRequestStacksKeys() {

const dispatch = useDispatch();

const {
requestKeys,
latestDeviceResponse,
awaitingDeviceConnection,
outdatedAppVersionWarning,
incorrectAppOpened,
} = useRequestLedgerKeys({
chain: 'stacks',
connectApp: connectLedgerStacksApp,
getAppVersion: getStacksAppVersion,
isAppOpen(resp) {
return !isStacksLedgerAppClosed(resp);
},
onSuccess() {
navigate('/', { replace: true });
},
async pullKeysFromDevice(app) {
const resp = await pullStacksKeysFromLedgerDevice(app)({
onRequestKey(accountIndex) {
ledgerNavigate.toDeviceBusyStep(`Requesting STX addresses (${accountIndex + 1}…5)`);
},
});
if (resp.status === 'failure') {
toast.error(resp.errorMessage);
ledgerNavigate.toErrorStep(resp.errorMessage);
return;
}
ledgerNavigate.toDeviceBusyStep();
dispatch(
stacksKeysSlice.actions.addKeys(
resp.publicKeys.map(keys => ({
...keys,
id: keys.path.replace('m', defaultWalletKeyId),
targetId: latestDeviceResponse?.targetId || '',
}))
)
);
},
});
const { requestKeys, latestDeviceResponse, awaitingDeviceConnection, outdatedAppVersionWarning } =
useRequestLedgerKeys({
chain: 'stacks',
connectApp: connectLedgerStacksApp,
getAppVersion: getStacksAppVersion,
isAppOpen(resp) {
return !isStacksLedgerAppClosed(resp);
},
onSuccess() {
navigate('/', { replace: true });
},
async pullKeysFromDevice(app) {
const resp = await pullStacksKeysFromLedgerDevice(app)({
onRequestKey(accountIndex) {
ledgerNavigate.toDeviceBusyStep(`Requesting STX addresses (${accountIndex + 1}…5)`);
},
});
if (resp.status === 'failure') {
toast.error(resp.errorMessage);
ledgerNavigate.toErrorStep(resp.errorMessage);
return;
}
ledgerNavigate.toDeviceBusyStep();
dispatch(
stacksKeysSlice.actions.addKeys(
resp.publicKeys.map(keys => ({
...keys,
id: keys.path.replace('m', defaultWalletKeyId),
targetId: latestDeviceResponse?.targetId || '',
}))
)
);
},
});

const ledgerContextValue: LedgerRequestKeysContext = {
chain: 'stacks',
pullPublicKeysFromDevice: requestKeys,
latestDeviceResponse,
awaitingDeviceConnection,
outdatedAppVersionWarning,
incorrectAppOpened,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@ import { useSignatureRequestSearchParams } from '@app/store/signatures/requests.
import { useLedgerAnalytics } from '../../hooks/use-ledger-analytics.hook';
import { useLedgerNavigate } from '../../hooks/use-ledger-navigate';
import { useVerifyMatchingLedgerStacksPublicKey } from '../../hooks/use-verify-matching-stacks-public-key';
import {
checkIncorrectAppOpenedError,
checkLockedDeviceError,
useLedgerResponseState,
} from '../../utils/generic-ledger-utils';
import { checkLockedDeviceError, useLedgerResponseState } from '../../utils/generic-ledger-utils';
import {
LedgerMessageSigningContext,
LedgerMsgSigningProvider,
Expand Down Expand Up @@ -63,7 +59,6 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
const { tabId, requestToken } = useSignatureRequestSearchParams();

const [latestDeviceResponse, setLatestDeviceResponse] = useLedgerResponseState();
const [incorrectAppOpened, setIncorrectAppOpened] = useState(false);
const canUserCancelAction = useActionCancellableByUser();

const [awaitingDeviceConnection, setAwaitingDeviceConnection] = useState(false);
Expand All @@ -72,10 +67,6 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
const stacksApp = await prepareLedgerDeviceStacksAppConnection({
setLoadingState: setAwaitingDeviceConnection,
onError(e) {
if (checkIncorrectAppOpenedError(e)) {
setIncorrectAppOpened(true);
return;
}
if (checkLockedDeviceError(e)) {
setLatestDeviceResponse({ deviceLocked: true } as any);
return;
Expand Down Expand Up @@ -161,7 +152,6 @@ function LedgerSignStacksMsg({ account, unsignedMessage }: LedgerSignMsgProps) {
signMessage,
latestDeviceResponse,
awaitingDeviceConnection,
incorrectAppOpened,
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const ledgerMsgSigningContext = createContext<LedgerMessageSigningContext
latestDeviceResponse: null,
awaitingDeviceConnection: false,
signMessage: noop,
incorrectAppOpened: false,
});

export const LedgerMsgSigningProvider = ledgerMsgSigningContext.Provider;
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useWhenReattemptingLedgerConnection } from '@app/features/ledger/hooks/
import { ledgerMsgSigningContext } from '../ledger-stacks-sign-msg.context';

export function ConnectLedgerSignMsg() {
const { signMessage, latestDeviceResponse, awaitingDeviceConnection, incorrectAppOpened } =
const { signMessage, latestDeviceResponse, awaitingDeviceConnection } =
useContext(ledgerMsgSigningContext);

useWhenReattemptingLedgerConnection(() => signMessage());
Expand All @@ -20,7 +20,6 @@ export function ConnectLedgerSignMsg() {
<CommonLedgerDeviceInlineWarnings
chain="stacks"
latestDeviceResponse={latestDeviceResponse}
incorrectAppOpened={incorrectAppOpened}
/>
}
onConnectLedger={signMessage}
Expand Down
Loading

0 comments on commit 35c2ff1

Please sign in to comment.