From e63d3bbc2ad60411920cc4872d36fc11d1ae73d7 Mon Sep 17 00:00:00 2001 From: Kant Date: Thu, 21 Sep 2023 18:19:37 +0200 Subject: [PATCH] fix: platform-sdk & wallet-api onClose response in `account.request` & `message.sign` [LIVE-9487] --- .changeset/thick-icons-pull.md | 7 ++ .../Web3AppWebview/PlatformAPIWebview.tsx | 2 +- .../Web3AppWebview/WalletAPIWebview.tsx | 4 +- .../RootNavigator/BaseNavigator.tsx | 23 +++--- .../types/AddAccountsNavigator.ts | 2 - .../RootNavigator/types/BaseNavigator.ts | 3 +- .../types/RequestAccountNavigator.ts | 3 - .../Web3AppWebview/PlatformAPIWebview.tsx | 77 +++++++++---------- .../src/components/Web3AppWebview/helpers.ts | 6 +- .../RequestAccount/02-SelectAccount.tsx | 11 +-- .../src/wallet-api/react.ts | 4 +- 11 files changed, 68 insertions(+), 74 deletions(-) create mode 100644 .changeset/thick-icons-pull.md diff --git a/.changeset/thick-icons-pull.md b/.changeset/thick-icons-pull.md new file mode 100644 index 000000000000..abd28d8b0c29 --- /dev/null +++ b/.changeset/thick-icons-pull.md @@ -0,0 +1,7 @@ +--- +"ledger-live-desktop": patch +"live-mobile": patch +"@ledgerhq/live-common": patch +--- + +fix: platform-sdk & wallet-api onClose response in `account.request` & `message.sign` diff --git a/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/PlatformAPIWebview.tsx b/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/PlatformAPIWebview.tsx index cfa1a072594c..8e1930578690 100644 --- a/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/PlatformAPIWebview.tsx +++ b/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/PlatformAPIWebview.tsx @@ -247,7 +247,7 @@ export const PlatformAPIWebview = forwardRef( }, onClose: () => { tracking.platformSignMessageUserRefused(manifest); - reject(UserRefusedOnDevice()); + reject(new UserRefusedOnDevice()); }, }), ); diff --git a/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/WalletAPIWebview.tsx b/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/WalletAPIWebview.tsx index c14b6c0e72d1..b66eb5cdeb45 100644 --- a/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/WalletAPIWebview.tsx +++ b/apps/ledger-live-desktop/src/renderer/components/Web3AppWebview/WalletAPIWebview.tsx @@ -41,7 +41,7 @@ function useUiHook(manifest: AppManifest): Partial { return useMemo( () => ({ - "account.request": ({ accounts$, currencies, onSuccess, onError }) => { + "account.request": ({ accounts$, currencies, onSuccess, onCancel }) => { setDrawer( SelectAccountAndCurrencyDrawer, { @@ -55,7 +55,7 @@ function useUiHook(manifest: AppManifest): Partial { { onRequestClose: () => { setDrawer(); - onError(); + onCancel(); }, }, ); diff --git a/apps/ledger-live-mobile/src/components/RootNavigator/BaseNavigator.tsx b/apps/ledger-live-mobile/src/components/RootNavigator/BaseNavigator.tsx index 20efaaf0dbec..e6a1601729a8 100644 --- a/apps/ledger-live-mobile/src/components/RootNavigator/BaseNavigator.tsx +++ b/apps/ledger-live-mobile/src/components/RootNavigator/BaseNavigator.tsx @@ -216,6 +216,14 @@ export default function BaseNavigator() { name={NavigatorName.SignMessage} component={SignMessageNavigator} options={{ headerShown: false }} + listeners={({ route }) => ({ + beforeRemove: () => { + const onClose = route.params?.onClose; + if (onClose && typeof onClose === "function") { + onClose(); + } + }, + })} /> ({ beforeRemove: () => { - /** - react-navigation workaround try to fetch params from current route params - or fallback to child navigator route params - since this listener is on top of another navigator - */ - const onError = - route.params?.onError || (route.params as unknown as typeof route)?.params?.onError; - // @TODO This route.params.error mechanism is deprecated, no part of the code is currently using it - // WalletAPI are suggesting they will rework that at some point - if (onError && typeof onError === "function" && route.params.error) - onError(route.params.error); + const onClose = route.params?.onClose; + if (onClose && typeof onClose === "function") { + onClose(); + } }, })} /> diff --git a/apps/ledger-live-mobile/src/components/RootNavigator/types/AddAccountsNavigator.ts b/apps/ledger-live-mobile/src/components/RootNavigator/types/AddAccountsNavigator.ts index 995431a80a07..f2775b7682f9 100644 --- a/apps/ledger-live-mobile/src/components/RootNavigator/types/AddAccountsNavigator.ts +++ b/apps/ledger-live-mobile/src/components/RootNavigator/types/AddAccountsNavigator.ts @@ -16,7 +16,6 @@ export type AddAccountsNavigatorParamList = { returnToSwap?: boolean; analyticsPropertyFlow?: string; onSuccess?: () => void; - onError?: (_: Error) => void; }; [ScreenName.AddAccountsAccounts]: { currency: CryptoOrTokenCurrency; @@ -24,7 +23,6 @@ export type AddAccountsNavigatorParamList = { inline?: boolean; returnToSwap?: boolean; onSuccess?: (_?: unknown) => void; - onError?: (_: Error) => void; }; [ScreenName.AddAccountsSuccess]?: { currency: CryptoOrTokenCurrency; diff --git a/apps/ledger-live-mobile/src/components/RootNavigator/types/BaseNavigator.ts b/apps/ledger-live-mobile/src/components/RootNavigator/types/BaseNavigator.ts index 2f574b9c4313..c5ca43cf4373 100644 --- a/apps/ledger-live-mobile/src/components/RootNavigator/types/BaseNavigator.ts +++ b/apps/ledger-live-mobile/src/components/RootNavigator/types/BaseNavigator.ts @@ -227,8 +227,7 @@ export type BaseNavigatorStackParamList = { }) | undefined; [NavigatorName.RequestAccount]: NavigatorScreenParams & { - onError?: (_: Error) => void; - error?: Error; + onClose?: () => void; }; [NavigatorName.Exchange]: NavigatorScreenParams | undefined; [NavigatorName.ExchangeStack]: NavigatorScreenParams & { diff --git a/apps/ledger-live-mobile/src/components/RootNavigator/types/RequestAccountNavigator.ts b/apps/ledger-live-mobile/src/components/RootNavigator/types/RequestAccountNavigator.ts index 5b1c22b0189d..b13462976fee 100644 --- a/apps/ledger-live-mobile/src/components/RootNavigator/types/RequestAccountNavigator.ts +++ b/apps/ledger-live-mobile/src/components/RootNavigator/types/RequestAccountNavigator.ts @@ -13,7 +13,6 @@ export type RequestAccountNavigatorParamList = { currencies: CryptoOrTokenCurrency[]; allowAddAccount?: boolean; onSuccess?: (account: AccountLike, parentAccount?: Account) => void; - onError?: (_: Error) => void; }; [ScreenName.RequestAccountsSelectAccount]: { accounts$?: Observable; @@ -21,7 +20,6 @@ export type RequestAccountNavigatorParamList = { currency: CryptoOrTokenCurrency; allowAddAccount?: boolean; onSuccess?: (account: AccountLike, parentAccount?: Account) => void; - onError?: (_: Error) => void; }; [NavigatorName.RequestAccountsAddAccounts]: NavigatorScreenParams & Partial<{ @@ -31,6 +29,5 @@ export type RequestAccountNavigatorParamList = { returnToSwap?: boolean; analyticsPropertyFlow?: string; onSuccess?: (account: AccountLike, parentAccount?: Account) => void; - onError?: (_: Error) => void; }>; }; diff --git a/apps/ledger-live-mobile/src/components/Web3AppWebview/PlatformAPIWebview.tsx b/apps/ledger-live-mobile/src/components/Web3AppWebview/PlatformAPIWebview.tsx index 72e3653f7853..330f7bc21f46 100644 --- a/apps/ledger-live-mobile/src/components/Web3AppWebview/PlatformAPIWebview.tsx +++ b/apps/ledger-live-mobile/src/components/Web3AppWebview/PlatformAPIWebview.tsx @@ -134,9 +134,9 @@ export const PlatformAPIWebview = forwardRef( resolve(serializePlatformAccount(accountToPlatformAccount(account, parentAccount))); }; - const onError = (error: Error) => { + const onClose = () => { tracking.platformRequestAccountFail(manifest); - reject(error); + reject(new Error("User cancelled")); }; // if single currency available redirect to select account directly @@ -157,8 +157,8 @@ export const PlatformAPIWebview = forwardRef( currency, allowAddAccount, onSuccess, - onError, }, + onClose, }); } else { navigation.navigate(NavigatorName.RequestAccount, { @@ -167,8 +167,8 @@ export const PlatformAPIWebview = forwardRef( currencies: allCurrencies, allowAddAccount, onSuccess, - onError, }, + onClose, }); } }), @@ -233,44 +233,41 @@ export const PlatformAPIWebview = forwardRef( ); return new Promise((resolve, reject) => { - (navigation as StackNavigatorNavigation).navigate( - NavigatorName.SignTransaction, - { - screen: ScreenName.SignTransactionSummary, - params: { - currentNavigation: ScreenName.SignTransactionSummary, - nextNavigation: ScreenName.SignTransactionSelectDevice, - transaction: tx as Transaction, - accountId, - parentId: parentAccount?.id, - appName: params?.useApp, - onSuccess: ({ - signedOperation, - transactionSignError, - }: { - signedOperation: SignedOperation; - transactionSignError: Error; - }) => { - if (transactionSignError) { - tracking.platformSignTransactionFail(manifest); - reject(transactionSignError); - } else { - tracking.platformSignTransactionSuccess(manifest); - resolve(serializePlatformSignedTransaction(signedOperation)); - const n = - navigation.getParent< - StackNavigatorNavigation - >() || navigation; - n.pop(); - } - }, - onError: (error: Error) => { + navigation.navigate(NavigatorName.SignTransaction, { + screen: ScreenName.SignTransactionSummary, + params: { + currentNavigation: ScreenName.SignTransactionSummary, + nextNavigation: ScreenName.SignTransactionSelectDevice, + transaction: tx as Transaction, + accountId, + parentId: parentAccount?.id, + appName: params?.useApp, + onSuccess: ({ + signedOperation, + transactionSignError, + }: { + signedOperation: SignedOperation; + transactionSignError: Error; + }) => { + if (transactionSignError) { tracking.platformSignTransactionFail(manifest); - reject(error); - }, + reject(transactionSignError); + } else { + tracking.platformSignTransactionSuccess(manifest); + resolve(serializePlatformSignedTransaction(signedOperation)); + const n = + navigation.getParent< + StackNavigatorNavigation + >() || navigation; + n.pop(); + } + }, + onError: (error: Error) => { + tracking.platformSignTransactionFail(manifest); + reject(error); }, }, - ); + }); }); }, ), @@ -435,7 +432,7 @@ export const PlatformAPIWebview = forwardRef( }, onClose: () => { tracking.platformSignMessageUserRefused(manifest); - reject(UserRefusedOnDevice()); + reject(new UserRefusedOnDevice()); }, }); }), diff --git a/apps/ledger-live-mobile/src/components/Web3AppWebview/helpers.ts b/apps/ledger-live-mobile/src/components/Web3AppWebview/helpers.ts index c73852f4891c..4f191343bb5e 100644 --- a/apps/ledger-live-mobile/src/components/Web3AppWebview/helpers.ts +++ b/apps/ledger-live-mobile/src/components/Web3AppWebview/helpers.ts @@ -251,7 +251,7 @@ function useUiHook(): Partial { return useMemo( () => ({ - "account.request": ({ accounts$, currencies, onSuccess, onError }) => { + "account.request": ({ accounts$, currencies, onSuccess, onCancel }) => { if (currencies.length === 1) { navigation.navigate(NavigatorName.RequestAccount, { screen: ScreenName.RequestAccountsSelectAccount, @@ -260,8 +260,8 @@ function useUiHook(): Partial { currency: currencies[0], allowAddAccount: true, onSuccess, - onError, }, + onClose: onCancel, }); } else { navigation.navigate(NavigatorName.RequestAccount, { @@ -271,8 +271,8 @@ function useUiHook(): Partial { currencies, allowAddAccount: true, onSuccess, - onError, }, + onClose: onCancel, }); } }, diff --git a/apps/ledger-live-mobile/src/screens/RequestAccount/02-SelectAccount.tsx b/apps/ledger-live-mobile/src/screens/RequestAccount/02-SelectAccount.tsx index d48e093f7428..cdb0a0e6a8d7 100644 --- a/apps/ledger-live-mobile/src/screens/RequestAccount/02-SelectAccount.tsx +++ b/apps/ledger-live-mobile/src/screens/RequestAccount/02-SelectAccount.tsx @@ -96,7 +96,7 @@ const List = ({ function SelectAccount({ navigation, route }: Props) { const { colors } = useTheme(); - const { accounts$, currency, allowAddAccount, onSuccess, onError } = route.params; + const { accounts$, currency, allowAddAccount, onSuccess } = route.params; const accountIds = useGetAccountIds(accounts$); const accounts = useSelector(accountsByCryptoCurrencyScreenSelector(currency, accountIds)) as { account: AccountLike; @@ -122,15 +122,10 @@ function SelectAccount({ navigation, route }: Props) { screen: ScreenName.AddAccountsSelectDevice, params: { currency: currency as CryptoOrTokenCurrency, - onSuccess: () => - navigation.navigate(NavigatorName.RequestAccount, { - screen: ScreenName.RequestAccountsSelectAccount, - params: route.params, - }), - onError, + onSuccess: () => navigation.navigate(ScreenName.RequestAccountsSelectAccount, route.params), }, }); - }, [currency, navigation, onError, route.params]); + }, [currency, navigation, route.params]); const renderFooter = useCallback( () => diff --git a/libs/ledger-live-common/src/wallet-api/react.ts b/libs/ledger-live-common/src/wallet-api/react.ts index 19feff887262..4ae3a7c70b6d 100644 --- a/libs/ledger-live-common/src/wallet-api/react.ts +++ b/libs/ledger-live-common/src/wallet-api/react.ts @@ -113,7 +113,7 @@ export interface UiHook { accounts$: Observable; currencies: CryptoOrTokenCurrency[]; onSuccess: (account: AccountLike, parentAccount: Account | undefined) => void; - onError: () => void; + onCancel: () => void; }) => void; "account.receive": (params: { account: AccountLike; @@ -355,7 +355,7 @@ export function useWalletAPIServer({ tracking.requestAccountSuccess(manifest); resolve(accountToWalletAPIAccount(account, parentAccount)); }, - onError: () => { + onCancel: () => { tracking.requestAccountFail(manifest); reject(new Error("Canceled by user")); },