From 05f471deca3dfe60f5e6bb5f2bc8893281579474 Mon Sep 17 00:00:00 2001 From: Jean Ribeiro Date: Wed, 18 Sep 2024 10:15:59 -0300 Subject: [PATCH] refactor+fix: ledger app name check (#2797) * fix: InstallLedgerView icons * refactor: ledger app name check * fix: pr comments Co-authored-by: Jean Ribeiro --------- Co-authored-by: Nicole O'Brien --- .../popup/popups/SyncAccountsPopup.svelte | 3 +- .../views/FinishOnboardingView.svelte | 10 ++---- .../views/ConnectLedgerView.svelte | 13 ++----- .../views/InstallLedgerView.svelte | 35 ++++++++++++------- .../components/LedgerAppOpen.svelte | 12 ++----- .../views/BalanceFinderView.svelte | 7 ++-- ...ame-to-stardust-network-id-map.constant.ts | 1 + .../ledger/actions/checkOrConnectLedger.ts | 9 ++--- .../core/ledger/utils/handleLedgerError.ts | 8 ++--- .../isLedgerDeviceMatchingActiveProfile.ts | 8 ++--- .../getNetworkIdFromOnboardingNetworkType.ts | 1 + .../handleTransactionProgressEvent.ts | 8 ++--- .../active-profile/checkActiveProfileAuth.ts | 10 ++---- .../active-profile/getProfileLedgerAppName.ts | 23 ++++++++++++ .../profile/actions/active-profile/index.ts | 1 + 15 files changed, 75 insertions(+), 74 deletions(-) create mode 100644 packages/shared/src/lib/core/profile/actions/active-profile/getProfileLedgerAppName.ts diff --git a/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte b/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte index c62141e6c9..7a6a40b9bd 100644 --- a/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte +++ b/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte @@ -38,6 +38,7 @@ const networkSearchMethod: { [key in StardustNetworkId]?: () => Promise } = { [SupportedStardustNetworkId.Iota]: multiAddressSearch, + [SupportedStardustNetworkId.IotaTestnet]: multiAddressSearch, [SupportedStardustNetworkId.Shimmer]: singleAddressSearch, [SupportedStardustNetworkId.Testnet]: singleAddressSearch, } @@ -163,7 +164,7 @@ }} >
- {#if network?.id === SupportedStardustNetworkId.Iota} + {#if network?.id === SupportedStardustNetworkId.Iota || network?.id === SupportedStardustNetworkId.IotaTestnet}
import { Button, IconName, Text } from '@bloomwalletio/ui' import { Animation, Illustration } from '@ui' - import { completeOnboardingProcess, isOnboardingLedgerProfile, onboardingProfile } from '@contexts/onboarding' + import { completeOnboardingProcess, isOnboardingLedgerProfile } from '@contexts/onboarding' import { localize } from '@core/i18n' - import { LedgerAppName, checkOrConnectLedger } from '@core/ledger' + import { checkOrConnectLedger } from '@core/ledger' import { profiles } from '@core/profile/stores' import { OnboardingLayout } from '@views/components' import SuccessSvg from '@views/onboarding/components/SuccessSvg.svelte' @@ -11,7 +11,6 @@ import LoggedOutLayout from '@views/components/LoggedOutLayout.svelte' import features from '@features/features' import { login } from '@core/profile/actions' - import { SupportedStardustNetworkId } from '@core/network' import { handleError } from '@core/error/handlers' import { onMount } from 'svelte' @@ -19,13 +18,10 @@ let isAppSetup = false - $: appName = - $onboardingProfile?.network?.id === SupportedStardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer - async function onContinueClick(): Promise { try { if ($isOnboardingLedgerProfile) { - await checkOrConnectLedger(appName) + await checkOrConnectLedger() } await completeOnboardingProcess() void login({ isFromOnboardingFlow: true }) diff --git a/packages/desktop/views/onboarding/views/create-from-ledger/views/ConnectLedgerView.svelte b/packages/desktop/views/onboarding/views/create-from-ledger/views/ConnectLedgerView.svelte index ca9a572383..17e98d9319 100644 --- a/packages/desktop/views/onboarding/views/create-from-ledger/views/ConnectLedgerView.svelte +++ b/packages/desktop/views/onboarding/views/create-from-ledger/views/ConnectLedgerView.svelte @@ -1,25 +1,18 @@ diff --git a/packages/desktop/views/onboarding/views/restore-profile/views/BalanceFinderView.svelte b/packages/desktop/views/onboarding/views/restore-profile/views/BalanceFinderView.svelte index fdc6b09d92..4379b6f0fe 100644 --- a/packages/desktop/views/onboarding/views/restore-profile/views/BalanceFinderView.svelte +++ b/packages/desktop/views/onboarding/views/restore-profile/views/BalanceFinderView.svelte @@ -4,7 +4,7 @@ import { IAccount } from '@core/account' import { DEFAULT_SYNC_OPTIONS } from '@core/account/constants' import { localize } from '@core/i18n' - import { LedgerAppName, checkOrConnectLedger, ledgerRaceConditionProtectionWrapper } from '@core/ledger' + import { checkOrConnectLedger, ledgerRaceConditionProtectionWrapper } from '@core/ledger' import { SupportedStardustNetworkId } from '@core/network/constants' import { NetworkId } from '@core/network/types' import { ProfileType } from '@core/profile' @@ -37,6 +37,7 @@ const networkSearchMethod: { [key in NetworkId]?: () => Promise } = { [SupportedStardustNetworkId.Iota]: multiAddressSearch, + [SupportedStardustNetworkId.IotaTestnet]: multiAddressSearch, [SupportedStardustNetworkId.Shimmer]: singleAddressSearch, [SupportedStardustNetworkId.Testnet]: singleAddressSearch, } @@ -133,9 +134,7 @@ async function onFindBalancesClick(): Promise { if (type === ProfileType.Ledger) { - await checkOrConnectLedger( - network?.id === SupportedStardustNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer - ) + await checkOrConnectLedger() } await findBalances() } diff --git a/packages/shared/src/lib/auxiliary/third-party/constants/network-name-to-stardust-network-id-map.constant.ts b/packages/shared/src/lib/auxiliary/third-party/constants/network-name-to-stardust-network-id-map.constant.ts index 1d9e914530..3c15942169 100644 --- a/packages/shared/src/lib/auxiliary/third-party/constants/network-name-to-stardust-network-id-map.constant.ts +++ b/packages/shared/src/lib/auxiliary/third-party/constants/network-name-to-stardust-network-id-map.constant.ts @@ -2,6 +2,7 @@ import { SupportedStardustNetworkId, StardustNetworkName, StardustNetworkId } fr export const NETWORK_NAME_TO_STARDUST_NETWORK_ID_MAP: Record = { [SupportedStardustNetworkId.Iota]: SupportedStardustNetworkId.Iota, + [SupportedStardustNetworkId.IotaTestnet]: SupportedStardustNetworkId.IotaTestnet, iota: SupportedStardustNetworkId.Iota, [StardustNetworkName.Shimmer]: SupportedStardustNetworkId.Shimmer, [StardustNetworkName.Testnet]: SupportedStardustNetworkId.Testnet, diff --git a/packages/shared/src/lib/core/ledger/actions/checkOrConnectLedger.ts b/packages/shared/src/lib/core/ledger/actions/checkOrConnectLedger.ts index bf9641448e..597f023971 100644 --- a/packages/shared/src/lib/core/ledger/actions/checkOrConnectLedger.ts +++ b/packages/shared/src/lib/core/ledger/actions/checkOrConnectLedger.ts @@ -1,14 +1,9 @@ import { openProfileAuthPopup, ProfileAuthPopupId } from '../../../../../../desktop/lib/auxiliary/popup' import { get } from 'svelte/store' import { LedgerAppName, LedgerConnectionState, ledgerConnectionAppState } from '..' -import { activeProfile } from '@core/profile/stores' -import { SupportedNetworkId } from '@core/network/constants' +import { getProfileLedgerAppName } from '@core/profile/actions/active-profile' -export function checkOrConnectLedger( - ledgerAppName: LedgerAppName = get(activeProfile)?.network?.id === SupportedNetworkId.Iota - ? LedgerAppName.Iota - : LedgerAppName.Shimmer -): Promise { +export function checkOrConnectLedger(ledgerAppName: LedgerAppName = getProfileLedgerAppName()): Promise { const isCorrectApp = get(ledgerConnectionAppState)?.app === ledgerAppName const isOpen = get(ledgerConnectionAppState)?.state === LedgerConnectionState.AppOpen return new Promise((resolve, reject) => { diff --git a/packages/shared/src/lib/core/ledger/utils/handleLedgerError.ts b/packages/shared/src/lib/core/ledger/utils/handleLedgerError.ts index 405c9c65d2..626baef04a 100644 --- a/packages/shared/src/lib/core/ledger/utils/handleLedgerError.ts +++ b/packages/shared/src/lib/core/ledger/utils/handleLedgerError.ts @@ -13,12 +13,11 @@ import { } from '../../../../../../desktop/lib/auxiliary/popup' import { LEDGER_ERROR_LOCALES } from '../constants' -import { LedgerAppName, LedgerError } from '../enums' +import { LedgerError } from '../enums' import { deriveLedgerError } from '../helpers' import { checkOrConnectLedger, ledgerPreparedOutput, resetLedgerPreparedOutput } from '@core/ledger' import { sendOutput } from '@core/wallet' -import { activeProfile } from '@core/profile/stores' -import { SupportedNetworkId } from '@core/network/constants' +import { getProfileLedgerAppName } from '@core/profile/actions/active-profile' export function handleLedgerError(err: unknown, resetConfirmationPropsOnDenial = true): void { const error = (err as IError)?.error ?? (err as string) @@ -51,8 +50,7 @@ export function handleLedgerError(err: unknown, resetConfirmationPropsOnDenial = */ const hadToEnableBlindSinging = popupType === ProfileAuthPopupId.EnableLedgerBlindSigning && wasDeniedByUser if (hadToEnableBlindSinging) { - const appName = - get(activeProfile)?.network?.id === SupportedNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer + const appName = getProfileLedgerAppName() openProfileAuthPopup({ id: ProfileAuthPopupId.EnableLedgerBlindSigning, props: { diff --git a/packages/shared/src/lib/core/ledger/utils/isLedgerDeviceMatchingActiveProfile.ts b/packages/shared/src/lib/core/ledger/utils/isLedgerDeviceMatchingActiveProfile.ts index 446f2cecbd..efd51f4ee8 100644 --- a/packages/shared/src/lib/core/ledger/utils/isLedgerDeviceMatchingActiveProfile.ts +++ b/packages/shared/src/lib/core/ledger/utils/isLedgerDeviceMatchingActiveProfile.ts @@ -1,15 +1,11 @@ import { getSelectedAccount } from '@core/account/stores' import { getProfileManager } from '@core/profile-manager/stores' -import { LedgerAppName } from '../enums' import { isLedgerAppOpen } from './isLedgerAppOpen' -import { activeProfile } from '@core/profile/stores' -import { get } from 'svelte/store' -import { SupportedNetworkId } from '@core/network' +import { getProfileLedgerAppName } from '@core/profile/actions/active-profile' export async function isLedgerDeviceMatchingActiveProfile(): Promise { - const appName = - get(activeProfile)?.network?.id === SupportedNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer + const appName = getProfileLedgerAppName() if (isLedgerAppOpen(appName)) { try { const account = getSelectedAccount() diff --git a/packages/shared/src/lib/core/network/utils/getNetworkIdFromOnboardingNetworkType.ts b/packages/shared/src/lib/core/network/utils/getNetworkIdFromOnboardingNetworkType.ts index 60fb5ca90f..8182cdae4d 100644 --- a/packages/shared/src/lib/core/network/utils/getNetworkIdFromOnboardingNetworkType.ts +++ b/packages/shared/src/lib/core/network/utils/getNetworkIdFromOnboardingNetworkType.ts @@ -11,6 +11,7 @@ export function getNetworkIdFromOnboardingNetworkType( const nameMap: { [key in OnboardingNetworkType]?: StardustNetworkId } = { [OnboardingNetworkType.Iota]: SupportedStardustNetworkId.Iota, + [OnboardingNetworkType.IotaTestnet]: SupportedStardustNetworkId.IotaTestnet, [OnboardingNetworkType.Shimmer]: SupportedStardustNetworkId.Shimmer, [OnboardingNetworkType.Testnet]: SupportedStardustNetworkId.Testnet, } diff --git a/packages/shared/src/lib/core/profile-manager/actions/events-handlers/handleTransactionProgressEvent.ts b/packages/shared/src/lib/core/profile-manager/actions/events-handlers/handleTransactionProgressEvent.ts index fefabef378..52d9b4ed80 100644 --- a/packages/shared/src/lib/core/profile-manager/actions/events-handlers/handleTransactionProgressEvent.ts +++ b/packages/shared/src/lib/core/profile-manager/actions/events-handlers/handleTransactionProgressEvent.ts @@ -1,9 +1,8 @@ import { isOnboardingLedgerProfile } from '@contexts/onboarding' import { selectedAccountIndex } from '@core/account/stores' -import { LedgerAppName } from '@core/ledger/enums' import { ledgerDeviceState, ledgerPreparedOutput, resetLedgerPreparedOutput } from '@core/ledger/stores' import { deconstructLedgerVerificationProps } from '@core/ledger/helpers' -import { activeProfile, isActiveLedgerProfile } from '@core/profile/stores' +import { isActiveLedgerProfile } from '@core/profile/stores' import { Event, PreparedTransactionEssenceHashProgress, @@ -23,7 +22,7 @@ import { validateWalletApiEvent } from '../../utils' import { checkOrConnectLedger } from '@core/ledger/actions' import { handleError } from '@core/error/handlers' import { sendOutput } from '@core/wallet/actions' -import { SupportedNetworkId } from '@core/network/constants' +import { getProfileLedgerAppName } from '@core/profile/actions/active-profile' export function handleTransactionProgressEvent(error: Error, event: Event): void { const walletEvent = validateWalletApiEvent( @@ -66,8 +65,7 @@ function openPopupIfVerificationNeeded(progress: TransactionProgress): void { }, }) } else if (type === TransactionProgressType.PreparedTransactionEssenceHash) { - const appName = - get(activeProfile)?.network?.id === SupportedNetworkId.Iota ? LedgerAppName.Iota : LedgerAppName.Shimmer + const appName = getProfileLedgerAppName() if (get(ledgerDeviceState)?.settings?.[appName]?.blindSigningEnabled) { openProfileAuthPopup({ id: ProfileAuthPopupId.VerifyLedgerTransaction, diff --git a/packages/shared/src/lib/core/profile/actions/active-profile/checkActiveProfileAuth.ts b/packages/shared/src/lib/core/profile/actions/active-profile/checkActiveProfileAuth.ts index 2f4c6f98c0..693133c8cf 100644 --- a/packages/shared/src/lib/core/profile/actions/active-profile/checkActiveProfileAuth.ts +++ b/packages/shared/src/lib/core/profile/actions/active-profile/checkActiveProfileAuth.ts @@ -1,15 +1,11 @@ import { checkOrConnectLedger } from '@core/ledger/actions' import { LedgerAppName } from '@core/ledger/enums' -import { SupportedNetworkId } from '@core/network' -import { activeProfile, isActiveLedgerProfile, isSoftwareProfile } from '@core/profile/stores' +import { isActiveLedgerProfile, isSoftwareProfile } from '@core/profile/stores' +import { getProfileLedgerAppName } from '@core/profile/actions/active-profile' import { checkOrUnlockStronghold } from '@core/stronghold/actions' import { get } from 'svelte/store' -export async function checkActiveProfileAuth( - ledgerAppName: LedgerAppName = get(activeProfile)?.network?.id === SupportedNetworkId.Iota - ? LedgerAppName.Iota - : LedgerAppName.Shimmer -): Promise { +export async function checkActiveProfileAuth(ledgerAppName: LedgerAppName = getProfileLedgerAppName()): Promise { if (get(isSoftwareProfile)) { await checkOrUnlockStronghold() } else if (get(isActiveLedgerProfile)) { diff --git a/packages/shared/src/lib/core/profile/actions/active-profile/getProfileLedgerAppName.ts b/packages/shared/src/lib/core/profile/actions/active-profile/getProfileLedgerAppName.ts new file mode 100644 index 0000000000..c178d9fe1c --- /dev/null +++ b/packages/shared/src/lib/core/profile/actions/active-profile/getProfileLedgerAppName.ts @@ -0,0 +1,23 @@ +import { IOnboardingProfile, onboardingProfile } from '@contexts/onboarding' +import { LedgerAppName } from '@core/ledger' +import { get } from 'svelte/store' +import { SupportedStardustNetworkId } from '@core/network' +import { IProfile } from '@core/profile/interfaces' +import { activeProfile } from '@core/profile/stores' + +export function getProfileLedgerAppName( + profile: IProfile | Partial = get(onboardingProfile) ?? get(activeProfile) +): LedgerAppName { + const networkId = profile?.network?.id + + switch (networkId) { + case SupportedStardustNetworkId.Iota: + case SupportedStardustNetworkId.IotaTestnet: + return LedgerAppName.Iota + case SupportedStardustNetworkId.Shimmer: + case SupportedStardustNetworkId.Testnet: + return LedgerAppName.Shimmer + default: + throw new Error(`Unsupported network id: ${networkId}`) + } +} diff --git a/packages/shared/src/lib/core/profile/actions/active-profile/index.ts b/packages/shared/src/lib/core/profile/actions/active-profile/index.ts index 9751385cc1..744b763d00 100644 --- a/packages/shared/src/lib/core/profile/actions/active-profile/index.ts +++ b/packages/shared/src/lib/core/profile/actions/active-profile/index.ts @@ -3,6 +3,7 @@ export * from './checkAndRemoveProfilePicture' export * from './checkAndInitializeActiveProfileNetwork' export * from './getBaseToken' export * from './getNetworkHrp' +export * from './getProfileLedgerAppName' export * from './loadAccounts' export * from './loadPersistedProfileIntoActiveProfile' export * from './lockStronghold'