2021-09-23 16:05:10 +03:00
|
|
|
import React, { useEffect, useState, useContext } from 'react';
|
2021-09-27 12:40:58 -04:00
|
|
|
import { Platform, View, Keyboard, StyleSheet, Switch, TouchableWithoutFeedback } from 'react-native';
|
2020-12-25 19:09:53 +03:00
|
|
|
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
|
2021-09-23 16:05:10 +03:00
|
|
|
|
2018-07-22 15:49:59 +01:00
|
|
|
import {
|
2021-09-23 16:05:10 +03:00
|
|
|
BlueButton,
|
2018-07-22 15:49:59 +01:00
|
|
|
BlueButtonLink,
|
2019-08-29 00:18:32 -04:00
|
|
|
BlueDoneAndDismissKeyboardInputAccessory,
|
2021-09-23 16:05:10 +03:00
|
|
|
BlueFormLabel,
|
|
|
|
BlueFormMultiInput,
|
2018-10-22 18:51:30 -04:00
|
|
|
BlueSpacing20,
|
2021-09-23 16:05:10 +03:00
|
|
|
BlueText,
|
|
|
|
SafeBlueArea,
|
2018-07-22 15:49:59 +01:00
|
|
|
} from '../../BlueComponents';
|
2020-12-25 19:09:53 +03:00
|
|
|
import navigationStyle from '../../components/navigationStyle';
|
2021-01-18 22:44:55 -05:00
|
|
|
import Privacy from '../../blue_modules/Privacy';
|
2020-07-20 16:38:46 +03:00
|
|
|
import loc from '../../loc';
|
2021-07-18 11:54:43 -04:00
|
|
|
import { BlueStorageContext } from '../../blue_modules/storage-context';
|
2018-07-22 15:49:59 +01:00
|
|
|
|
2019-12-26 20:21:07 -06:00
|
|
|
const WalletsImport = () => {
|
2020-05-27 14:12:17 +03:00
|
|
|
const navigation = useNavigation();
|
2020-07-15 13:32:59 -04:00
|
|
|
const { colors } = useTheme();
|
2021-09-23 16:05:10 +03:00
|
|
|
const route = useRoute();
|
|
|
|
const label = route?.params?.label ?? '';
|
|
|
|
const triggerImport = route?.params?.triggerImport ?? false;
|
2023-03-04 11:30:51 -04:00
|
|
|
const { isAdvancedModeEnabled } = useContext(BlueStorageContext);
|
2021-09-23 16:05:10 +03:00
|
|
|
const [importText, setImportText] = useState(label);
|
|
|
|
const [isToolbarVisibleForAndroid, setIsToolbarVisibleForAndroid] = useState(false);
|
|
|
|
const [, setSpeedBackdoor] = useState(0);
|
|
|
|
const [isAdvancedModeEnabledRender, setIsAdvancedModeEnabledRender] = useState(false);
|
|
|
|
const [searchAccounts, setSearchAccounts] = useState(false);
|
|
|
|
const [askPassphrase, setAskPassphrase] = useState(false);
|
|
|
|
|
2020-07-15 13:32:59 -04:00
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
2021-09-23 16:05:10 +03:00
|
|
|
paddingTop: 10,
|
2020-07-15 13:32:59 -04:00
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
center: {
|
|
|
|
flex: 1,
|
2020-10-06 06:28:06 -04:00
|
|
|
marginHorizontal: 16,
|
2020-07-15 13:32:59 -04:00
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
2021-09-23 16:05:10 +03:00
|
|
|
row: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginHorizontal: 16,
|
|
|
|
marginTop: 10,
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
},
|
2020-07-15 13:32:59 -04:00
|
|
|
});
|
2018-07-22 15:49:59 +01:00
|
|
|
|
2021-11-14 10:51:39 -05:00
|
|
|
const onBlur = () => {
|
2021-11-16 11:18:58 -05:00
|
|
|
const valueWithSingleWhitespace = importText.replace(/^\s+|\s+$|\s+(?=\s)/g, '');
|
2021-11-10 15:17:43 -05:00
|
|
|
setImportText(valueWithSingleWhitespace);
|
2021-11-14 10:57:48 -05:00
|
|
|
return valueWithSingleWhitespace;
|
2021-11-10 15:17:43 -05:00
|
|
|
};
|
|
|
|
|
2019-12-26 20:21:07 -06:00
|
|
|
useEffect(() => {
|
2019-02-27 20:29:13 -05:00
|
|
|
Privacy.enableBlur();
|
2020-04-27 03:11:33 -04:00
|
|
|
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow', () => setIsToolbarVisibleForAndroid(true));
|
|
|
|
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide', () => setIsToolbarVisibleForAndroid(false));
|
|
|
|
return () => {
|
2022-02-11 06:47:06 -05:00
|
|
|
Keyboard.removeAllListeners(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow');
|
|
|
|
Keyboard.removeAllListeners(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide');
|
2020-04-27 03:11:33 -04:00
|
|
|
Privacy.disableBlur();
|
|
|
|
};
|
|
|
|
}, []);
|
2019-02-27 20:29:13 -05:00
|
|
|
|
2021-03-20 13:00:01 +00:00
|
|
|
useEffect(() => {
|
2023-03-04 11:30:51 -04:00
|
|
|
isAdvancedModeEnabled().then(setIsAdvancedModeEnabledRender);
|
2021-03-20 13:00:01 +00:00
|
|
|
if (triggerImport) importButtonPressed();
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, []);
|
|
|
|
|
2019-12-26 20:21:07 -06:00
|
|
|
const importButtonPressed = () => {
|
2021-11-14 10:57:48 -05:00
|
|
|
const textToImport = onBlur();
|
|
|
|
if (textToImport.trim().length === 0) {
|
2019-12-26 20:21:07 -06:00
|
|
|
return;
|
2018-12-27 18:33:39 -05:00
|
|
|
}
|
2021-11-14 10:57:48 -05:00
|
|
|
importMnemonic(textToImport);
|
2019-12-26 20:21:07 -06:00
|
|
|
};
|
2018-07-22 15:49:59 +01:00
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
const importMnemonic = importText => {
|
|
|
|
navigation.navigate('ImportWalletDiscovery', { importText, askPassphrase, searchAccounts });
|
2019-12-26 20:21:07 -06:00
|
|
|
};
|
2018-07-22 15:49:59 +01:00
|
|
|
|
2021-07-16 13:18:56 +02:00
|
|
|
const onBarScanned = value => {
|
2020-10-08 16:39:31 +01:00
|
|
|
if (value && value.data) value = value.data + ''; // no objects here, only strings
|
2021-11-14 10:51:39 -05:00
|
|
|
setImportText(value);
|
2021-07-16 13:18:56 +02:00
|
|
|
setTimeout(() => importMnemonic(value), 500);
|
2019-12-26 20:21:07 -06:00
|
|
|
};
|
2019-02-01 23:00:44 +00:00
|
|
|
|
2020-05-31 18:27:26 -04:00
|
|
|
const importScan = () => {
|
2023-02-25 12:20:46 -04:00
|
|
|
navigation.navigate('ScanQRCodeRoot', {
|
|
|
|
screen: 'ScanQRCode',
|
|
|
|
params: {
|
|
|
|
launchedBy: route.name,
|
|
|
|
onBarScanned,
|
|
|
|
showFileImportButton: true,
|
|
|
|
},
|
|
|
|
});
|
2020-08-23 01:03:44 -04:00
|
|
|
};
|
|
|
|
|
2021-09-23 16:05:10 +03:00
|
|
|
const speedBackdoorTap = () => {
|
|
|
|
setSpeedBackdoor(v => {
|
|
|
|
v += 1;
|
|
|
|
if (v < 5) return v;
|
|
|
|
navigation.navigate('ImportSpeed');
|
|
|
|
return 0;
|
|
|
|
});
|
2021-08-25 16:35:25 -04:00
|
|
|
};
|
|
|
|
|
2021-10-01 14:04:40 -04:00
|
|
|
const renderOptionsAndImportButton = (
|
|
|
|
<>
|
2021-09-23 16:05:10 +03:00
|
|
|
{isAdvancedModeEnabledRender && (
|
|
|
|
<>
|
|
|
|
<View style={styles.row}>
|
|
|
|
<BlueText>{loc.wallets.import_passphrase}</BlueText>
|
|
|
|
<Switch testID="AskPassphrase" value={askPassphrase} onValueChange={setAskPassphrase} />
|
|
|
|
</View>
|
|
|
|
<View style={styles.row}>
|
|
|
|
<BlueText>{loc.wallets.import_search_accounts}</BlueText>
|
|
|
|
<Switch testID="SearchAccounts" value={searchAccounts} onValueChange={setSearchAccounts} />
|
|
|
|
</View>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
|
2020-04-27 03:11:33 -04:00
|
|
|
<BlueSpacing20 />
|
2020-05-24 12:17:26 +03:00
|
|
|
<View style={styles.center}>
|
2020-10-15 12:14:55 +01:00
|
|
|
<>
|
2021-09-23 16:05:10 +03:00
|
|
|
<BlueButton
|
|
|
|
disabled={importText.trim().length === 0}
|
|
|
|
title={loc.wallets.import_do_import}
|
|
|
|
testID="DoImport"
|
|
|
|
onPress={importButtonPressed}
|
|
|
|
/>
|
2020-10-15 12:14:55 +01:00
|
|
|
<BlueSpacing20 />
|
2020-11-04 19:41:40 +00:00
|
|
|
<BlueButtonLink title={loc.wallets.import_scan_qr} onPress={importScan} testID="ScanImport" />
|
2020-10-15 12:14:55 +01:00
|
|
|
</>
|
2020-04-27 03:11:33 -04:00
|
|
|
</View>
|
2021-10-01 14:04:40 -04:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<SafeBlueArea style={styles.root}>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<TouchableWithoutFeedback onPress={speedBackdoorTap} testID="SpeedBackdoor">
|
|
|
|
<BlueFormLabel>{loc.wallets.import_explanation}</BlueFormLabel>
|
|
|
|
</TouchableWithoutFeedback>
|
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueFormMultiInput
|
|
|
|
value={importText}
|
2021-11-14 10:51:39 -05:00
|
|
|
onBlur={onBlur}
|
|
|
|
onChangeText={setImportText}
|
2021-10-01 14:04:40 -04:00
|
|
|
testID="MnemonicInput"
|
|
|
|
inputAccessoryViewID={BlueDoneAndDismissKeyboardInputAccessory.InputAccessoryViewID}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{Platform.select({ android: !isToolbarVisibleForAndroid && renderOptionsAndImportButton, default: renderOptionsAndImportButton })}
|
2020-04-27 03:11:33 -04:00
|
|
|
{Platform.select({
|
|
|
|
ios: (
|
|
|
|
<BlueDoneAndDismissKeyboardInputAccessory
|
|
|
|
onClearTapped={() => {
|
2021-11-14 10:51:39 -05:00
|
|
|
setImportText('');
|
2020-04-27 03:11:33 -04:00
|
|
|
}}
|
|
|
|
onPasteTapped={text => {
|
|
|
|
setImportText(text);
|
|
|
|
Keyboard.dismiss();
|
2020-04-24 11:20:49 +02:00
|
|
|
}}
|
|
|
|
/>
|
2020-04-27 03:11:33 -04:00
|
|
|
),
|
|
|
|
android: isToolbarVisibleForAndroid && (
|
|
|
|
<BlueDoneAndDismissKeyboardInputAccessory
|
|
|
|
onClearTapped={() => {
|
2021-11-14 10:51:39 -05:00
|
|
|
setImportText('');
|
2020-04-27 03:11:33 -04:00
|
|
|
Keyboard.dismiss();
|
|
|
|
}}
|
|
|
|
onPasteTapped={text => {
|
2021-11-14 10:51:39 -05:00
|
|
|
setImportText(text);
|
2020-04-27 03:11:33 -04:00
|
|
|
Keyboard.dismiss();
|
2020-04-24 11:20:49 +02:00
|
|
|
}}
|
2019-02-01 23:00:44 +00:00
|
|
|
/>
|
2020-04-27 03:11:33 -04:00
|
|
|
),
|
|
|
|
})}
|
2019-12-26 20:21:07 -06:00
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
};
|
2018-07-22 15:49:59 +01:00
|
|
|
|
2021-02-15 11:03:54 +03:00
|
|
|
WalletsImport.navigationOptions = navigationStyle({}, opts => ({ ...opts, title: loc.wallets.import_title }));
|
|
|
|
|
2019-12-26 20:21:07 -06:00
|
|
|
export default WalletsImport;
|