Skip to content

Commit

Permalink
chore(suite-native): validate invalid xpub
Browse files Browse the repository at this point in the history
  • Loading branch information
juriczech committed Feb 26, 2024
1 parent 74aaf0c commit 4bc1385
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 23 deletions.
4 changes: 4 additions & 0 deletions suite-native/intl/src/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ export const en = {
description: 'To check the balance of your coin, scan your public key (XPUB).',
hintButton: 'Where to find it?',
},
xpub: {
title: 'Incompatible XPUB detected',
description: "Provided XPUB doesn't correspond with selected network.",
},
},
input: {
label: {
Expand Down
67 changes: 44 additions & 23 deletions suite-native/module-accounts-import/src/screens/XpubScanScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { prepareNativeStyle, useNativeStyles } from '@trezor/styles';
import { getNetworkType } from '@suite-common/wallet-config';
import { isAddressValid, isAddressBasedNetwork } from '@suite-common/wallet-utils';
import { useAlert } from '@suite-native/alerts';
import { Alert, useAlert } from '@suite-native/alerts';
import { useTranslate } from '@suite-native/intl';
import {
XpubFormContext,
Expand Down Expand Up @@ -44,6 +44,12 @@ const cameraStyle = prepareNativeStyle(utils => ({
marginBottom: utils.spacings.medium,
}));

const isBtcTestnetXpub = (xpubAddress: string) => {
const btcTestnetPrefixes = ['tpub', 'upub', 'vpub', 'Upub', 'Vpub'];

return btcTestnetPrefixes.some(prefix => prefix === xpubAddress.slice(0, 4));
};

export const XpubScanScreen = ({
navigation,
route,
Expand Down Expand Up @@ -78,34 +84,49 @@ export const XpubScanScreen = ({
: 'moduleAccountImport.xpubScanScreen.input.label.xpub',
);

const showDelayedAlert = (alertProps: Alert) => {
// we need to set timeout to avoid showing alert during screen transition, otherwise it will freeze the app
setTimeout(() => {
showAlert(alertProps);
}, 1000);
};

const goToAccountImportScreen = ({ xpubAddress }: XpubFormValues) => {
if (networkSymbol === 'btc' && isBtcTestnetXpub(xpubAddress)) {
showDelayedAlert({
title: translate('moduleAccountImport.xpubScanScreen.alert.xpub.title'),
description: translate('moduleAccountImport.xpubScanScreen.alert.xpub.description'),
icon: 'warningCircle',
pictogramVariant: 'red',
primaryButtonTitle: translate('moduleAccountImport.xpubScanScreen.confirmButton'),
onPressPrimaryButton: () => null,
});

return;
}

if (
xpubAddress &&
!isAddressBasedNetwork(networkType) &&
isAddressValid(xpubAddress, networkSymbol)
) {
// we need to set timeout to avoid showing alert during screen transition, otherwise it will freeze the app
setTimeout(() => {
showAlert({
title: translate('moduleAccountImport.xpubScanScreen.alert.address.title'),
description: translate(
'moduleAccountImport.xpubScanScreen.alert.address.description',
),
icon: 'warningCircle',
pictogramVariant: 'red',
primaryButtonTitle: translate(
'moduleAccountImport.xpubScanScreen.confirmButton',
),
onPressPrimaryButton: () => null,
secondaryButtonTitle: translate(
'moduleAccountImport.xpubScanScreen.alert.address.hintButton',
),
onPressSecondaryButton: () => {
hideAlert();
setIsHintSheetVisible(true);
},
});
}, 1000);
showDelayedAlert({
title: translate('moduleAccountImport.xpubScanScreen.alert.address.title'),
description: translate(
'moduleAccountImport.xpubScanScreen.alert.address.description',
),
icon: 'warningCircle',
pictogramVariant: 'red',
primaryButtonTitle: translate('moduleAccountImport.xpubScanScreen.confirmButton'),
onPressPrimaryButton: () => null,
secondaryButtonTitle: translate(
'moduleAccountImport.xpubScanScreen.alert.address.hintButton',
),
onPressSecondaryButton: () => {
hideAlert();
setIsHintSheetVisible(true);
},
});

return;
}
Expand Down

0 comments on commit 4bc1385

Please sign in to comment.