Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(suite-native): testnet cannot be imported as bitcoin account #11329

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,15 @@ const cameraStyle = prepareNativeStyle(utils => ({
marginBottom: utils.spacings.medium,
}));

const isBtcTestnetXpub = (xpubAddress: string) => {
// This is to strip the start section of taproot xpubs
const xpub = xpubAddress.slice(xpubAddress.indexOf(']') + 1);

const btcTestnetPrefixes = ['tpub', 'upub', 'vpub', 'Upub', 'Vpub'];

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

export const XpubScanScreen = ({
navigation,
route,
Expand Down Expand Up @@ -78,34 +87,50 @@ 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;
}

// This is only to verify the bitcoin based networks. If you supply address instead of XPUB, it should fail here.
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
Loading