From 788ba200218642cd7714be8c6c19e0a6e69f4923 Mon Sep 17 00:00:00 2001 From: kyranjamie Date: Thu, 12 Dec 2024 15:15:06 +0100 Subject: [PATCH] fix(ledger): legacy auth flow --- src/app/common/publish-subscribe.ts | 1 + .../legacy-account-auth.tsx | 46 +++++++++++++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/app/common/publish-subscribe.ts b/src/app/common/publish-subscribe.ts index 4285815f714..e01d0c04c33 100644 --- a/src/app/common/publish-subscribe.ts +++ b/src/app/common/publish-subscribe.ts @@ -69,6 +69,7 @@ export interface GlobalAppEvents { ledgerStacksMessageSigningCancelled: { unsignedMessage: UnsignedMessage; }; + ledgerJwtMessageSigningComplete: unknown; } export const appEvents = createPublishSubscribe(); diff --git a/src/app/pages/legacy-account-auth/legacy-account-auth.tsx b/src/app/pages/legacy-account-auth/legacy-account-auth.tsx index e3a84105f7b..6108cad18a2 100644 --- a/src/app/pages/legacy-account-auth/legacy-account-auth.tsx +++ b/src/app/pages/legacy-account-auth/legacy-account-auth.tsx @@ -1,10 +1,15 @@ +import { Outlet, useNavigate } from 'react-router-dom'; + +import { RouteUrls } from '@shared/route-urls'; import { closeWindow } from '@shared/utils'; import { useCancelAuthRequest } from '@app/common/authentication/use-cancel-auth-request'; import { useFinishAuthRequest } from '@app/common/authentication/use-finish-auth-request'; import { useAppDetails } from '@app/common/hooks/auth/use-app-details'; import { useOnMount } from '@app/common/hooks/use-on-mount'; +import { appEvents } from '@app/common/publish-subscribe'; import { useSwitchAccountSheet } from '@app/common/switch-account/use-switch-account-sheet-context'; +import { useWalletType } from '@app/common/use-wallet-type'; import { openInNewTab } from '@app/common/utils/open-in-new-tab'; import { CurrentAccountDisplayer } from '@app/features/current-account/current-account-displayer'; import { useOnOriginTabClose } from '@app/routes/hooks/use-on-tab-closed'; @@ -12,11 +17,19 @@ import { useCurrentAccountIndex } from '@app/store/accounts/account'; import { ConnectAccountLayout } from '../../components/connect-account/connect-account.layout'; +function listenForJwtSigningComplete() { + return new Promise(resolve => + appEvents.subscribe('ledgerJwtMessageSigningComplete', () => resolve(true)) + ); +} + export function LegacyAccountAuth() { const { url } = useAppDetails(); const accountIndex = useCurrentAccountIndex(); const finishSignIn = useFinishAuthRequest(); const { toggleSwitchAccount } = useSwitchAccountSheet(); + const { whenWallet } = useWalletType(); + const navigate = useNavigate(); useOnOriginTabClose(() => closeWindow()); @@ -25,17 +38,32 @@ export function LegacyAccountAuth() { const handleUnmount = async () => cancelAuthentication(); useOnMount(() => window.addEventListener('beforeunload', handleUnmount)); + async function signIntoAccount(index: number) { + await whenWallet({ + async software() { + await finishSignIn(index); + }, + async ledger() { + navigate(RouteUrls.ConnectLedger, { state: { index } }); + await listenForJwtSigningComplete(); + }, + })(); + } + if (!url) throw new Error('No app details found'); return ( - finishSignIn(accountIndex)} - // Here we should refocus the tab that initiated the request, however - // because the old auth code doesn't have the tab id and should be - // eventually removed, we just open in a new tab - onClickRequestedByLink={() => openInNewTab(url.origin)} - switchAccount={} - /> + <> + signIntoAccount(accountIndex)} + // Here we should refocus the tab that initiated the request, however + // because the old auth code doesn't have the tab id and should be + // eventually removed, we just open in a new tab + onClickRequestedByLink={() => openInNewTab(url.origin)} + switchAccount={} + /> + + ); }