2018-07-22 16:49:59 +02:00
|
|
|
/* global alert */
|
2019-12-27 03:21:07 +01:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-10-06 12:28:06 +02:00
|
|
|
import { Platform, View, Keyboard, StatusBar, StyleSheet } from 'react-native';
|
2020-12-25 17:09:53 +01:00
|
|
|
import ReactNativeHapticFeedback from 'react-native-haptic-feedback';
|
|
|
|
import { useNavigation, useRoute, useTheme } from '@react-navigation/native';
|
|
|
|
import { getSystemName } from 'react-native-device-info';
|
|
|
|
|
2018-07-22 16:49:59 +02:00
|
|
|
import {
|
|
|
|
BlueFormMultiInput,
|
|
|
|
BlueButtonLink,
|
|
|
|
BlueFormLabel,
|
2019-08-29 06:18:32 +02:00
|
|
|
BlueDoneAndDismissKeyboardInputAccessory,
|
2018-07-22 16:49:59 +02:00
|
|
|
BlueButton,
|
|
|
|
SafeBlueArea,
|
2018-10-23 00:51:30 +02:00
|
|
|
BlueSpacing20,
|
2018-07-22 16:49:59 +02:00
|
|
|
} from '../../BlueComponents';
|
2020-12-25 17:09:53 +01:00
|
|
|
import navigationStyle from '../../components/navigationStyle';
|
2019-02-28 02:29:13 +01:00
|
|
|
import Privacy from '../../Privacy';
|
2020-05-24 12:27:08 +02:00
|
|
|
import WalletImport from '../../class/wallet-import';
|
2020-07-20 15:38:46 +02:00
|
|
|
import loc from '../../loc';
|
2020-08-23 07:07:50 +02:00
|
|
|
const isDesktop = getSystemName() === 'Mac OS X';
|
2020-12-15 05:11:05 +01:00
|
|
|
const fs = require('../../blue_modules/fs');
|
2018-07-22 16:49:59 +02:00
|
|
|
|
2019-12-27 03:21:07 +01:00
|
|
|
const WalletsImport = () => {
|
|
|
|
const [isToolbarVisibleForAndroid, setIsToolbarVisibleForAndroid] = useState(false);
|
2020-05-28 11:53:14 +02:00
|
|
|
const route = useRoute();
|
|
|
|
const label = (route.params && route.params.label) || '';
|
|
|
|
const [importText, setImportText] = useState(label);
|
2020-05-27 13:12:17 +02:00
|
|
|
const navigation = useNavigation();
|
2020-07-15 19:32:59 +02:00
|
|
|
const { colors } = useTheme();
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
root: {
|
|
|
|
flex: 1,
|
|
|
|
paddingTop: 40,
|
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
center: {
|
|
|
|
flex: 1,
|
2020-10-06 12:28:06 +02:00
|
|
|
marginHorizontal: 16,
|
2020-07-15 19:32:59 +02:00
|
|
|
backgroundColor: colors.elevated,
|
|
|
|
},
|
|
|
|
});
|
2018-07-22 16:49:59 +02:00
|
|
|
|
2019-12-27 03:21:07 +01:00
|
|
|
useEffect(() => {
|
2019-02-28 02:29:13 +01:00
|
|
|
Privacy.enableBlur();
|
2020-04-27 09:11:33 +02:00
|
|
|
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow', () => setIsToolbarVisibleForAndroid(true));
|
|
|
|
Keyboard.addListener(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide', () => setIsToolbarVisibleForAndroid(false));
|
|
|
|
return () => {
|
|
|
|
Keyboard.removeListener(Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide');
|
|
|
|
Keyboard.removeListener(Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow');
|
|
|
|
Privacy.disableBlur();
|
|
|
|
};
|
|
|
|
}, []);
|
2019-02-28 02:29:13 +01:00
|
|
|
|
2019-12-27 03:21:07 +01:00
|
|
|
const importButtonPressed = () => {
|
|
|
|
if (importText.trim().length === 0) {
|
|
|
|
return;
|
2018-12-28 00:33:39 +01:00
|
|
|
}
|
2019-12-27 03:21:07 +01:00
|
|
|
importMnemonic(importText);
|
|
|
|
};
|
2018-07-22 16:49:59 +02:00
|
|
|
|
2020-02-25 15:42:11 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param importText
|
|
|
|
* @param additionalProperties key-values passed from outside. Used only to set up `masterFingerprint` property for watch-only wallet
|
|
|
|
*/
|
2020-10-15 13:14:55 +02:00
|
|
|
const importMnemonic = async (importText, additionalProperties) => {
|
|
|
|
if (WalletImport.isCurrentlyImportingWallet()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
WalletImport.addPlaceholderWallet(importText);
|
|
|
|
navigation.dangerouslyGetParent().pop();
|
|
|
|
await new Promise(resolve => setTimeout(resolve, 500)); // giving some time to animations
|
2018-07-22 16:49:59 +02:00
|
|
|
try {
|
2020-10-15 13:14:55 +02:00
|
|
|
await WalletImport.processImportText(importText, additionalProperties);
|
|
|
|
WalletImport.removePlaceholderWallet();
|
2019-12-27 03:21:07 +01:00
|
|
|
} catch (error) {
|
2020-10-15 13:14:55 +02:00
|
|
|
WalletImport.removePlaceholderWallet();
|
|
|
|
WalletImport.addPlaceholderWallet(importText, true);
|
2020-10-24 19:20:59 +02:00
|
|
|
console.log(error);
|
2020-07-20 15:38:46 +02:00
|
|
|
alert(loc.wallets.import_error);
|
2019-12-27 03:21:07 +01:00
|
|
|
ReactNativeHapticFeedback.trigger('notificationError', { ignoreAndroidSystemSettings: false });
|
2018-07-22 16:49:59 +02:00
|
|
|
}
|
2019-12-27 03:21:07 +01:00
|
|
|
};
|
2018-07-22 16:49:59 +02:00
|
|
|
|
2020-02-25 15:42:11 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param value
|
|
|
|
* @param additionalProperties key-values passed from outside. Used only to set up `masterFingerprint` property for watch-only wallet
|
|
|
|
*/
|
2020-01-01 04:31:04 +01:00
|
|
|
const onBarScanned = (value, additionalProperties) => {
|
2020-10-08 17:39:31 +02:00
|
|
|
if (value && value.data) value = value.data + ''; // no objects here, only strings
|
2019-12-27 03:21:07 +01:00
|
|
|
setImportText(value);
|
2020-10-15 13:14:55 +02:00
|
|
|
setTimeout(() => importMnemonic(value, additionalProperties), 500);
|
2019-12-27 03:21:07 +01:00
|
|
|
};
|
2019-02-02 00:00:44 +01:00
|
|
|
|
2020-06-01 00:27:26 +02:00
|
|
|
const importScan = () => {
|
2020-08-23 07:07:50 +02:00
|
|
|
if (isDesktop) {
|
2020-12-15 05:11:05 +01:00
|
|
|
fs.showActionSheet().then(onBarScanned);
|
2020-08-23 07:07:50 +02:00
|
|
|
} else {
|
|
|
|
navigation.navigate('ScanQRCodeRoot', {
|
|
|
|
screen: 'ScanQRCode',
|
|
|
|
params: {
|
|
|
|
launchedBy: route.name,
|
|
|
|
onBarScanned: onBarScanned,
|
|
|
|
showFileImportButton: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
2020-08-23 07:03:44 +02:00
|
|
|
};
|
|
|
|
|
2019-12-27 03:21:07 +01:00
|
|
|
return (
|
2020-05-24 11:17:26 +02:00
|
|
|
<SafeBlueArea forceInset={{ horizontal: 'always' }} style={styles.root}>
|
2020-10-29 22:13:54 +01:00
|
|
|
<StatusBar barStyle="light-content" />
|
2020-07-15 19:32:59 +02:00
|
|
|
<BlueSpacing20 />
|
2020-07-20 15:38:46 +02:00
|
|
|
<BlueFormLabel>{loc.wallets.import_explanation}</BlueFormLabel>
|
2020-04-27 09:11:33 +02:00
|
|
|
<BlueSpacing20 />
|
|
|
|
<BlueFormMultiInput
|
2020-04-27 10:23:56 +02:00
|
|
|
testID="MnemonicInput"
|
2020-04-27 09:11:33 +02:00
|
|
|
value={importText}
|
2020-08-05 03:05:42 +02:00
|
|
|
contextMenuHidden={getSystemName() !== 'Mac OS X'}
|
2020-04-27 09:11:33 +02:00
|
|
|
onChangeText={setImportText}
|
|
|
|
inputAccessoryViewID={BlueDoneAndDismissKeyboardInputAccessory.InputAccessoryViewID}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<BlueSpacing20 />
|
2020-05-24 11:17:26 +02:00
|
|
|
<View style={styles.center}>
|
2020-10-15 13:14:55 +02:00
|
|
|
<>
|
|
|
|
<BlueButton
|
|
|
|
testID="DoImport"
|
|
|
|
disabled={importText.trim().length === 0}
|
|
|
|
title={loc.wallets.import_do_import}
|
|
|
|
onPress={importButtonPressed}
|
|
|
|
/>
|
|
|
|
<BlueSpacing20 />
|
2020-11-04 20:41:40 +01:00
|
|
|
<BlueButtonLink title={loc.wallets.import_scan_qr} onPress={importScan} testID="ScanImport" />
|
2020-10-15 13:14:55 +02:00
|
|
|
</>
|
2020-04-27 09:11:33 +02:00
|
|
|
</View>
|
|
|
|
{Platform.select({
|
|
|
|
ios: (
|
|
|
|
<BlueDoneAndDismissKeyboardInputAccessory
|
|
|
|
onClearTapped={() => {
|
|
|
|
setImportText('');
|
|
|
|
Keyboard.dismiss();
|
|
|
|
}}
|
|
|
|
onPasteTapped={text => {
|
|
|
|
setImportText(text);
|
|
|
|
Keyboard.dismiss();
|
2020-04-24 11:20:49 +02:00
|
|
|
}}
|
|
|
|
/>
|
2020-04-27 09:11:33 +02:00
|
|
|
),
|
|
|
|
android: isToolbarVisibleForAndroid && (
|
|
|
|
<BlueDoneAndDismissKeyboardInputAccessory
|
|
|
|
onClearTapped={() => {
|
|
|
|
setImportText('');
|
|
|
|
Keyboard.dismiss();
|
|
|
|
}}
|
|
|
|
onPasteTapped={text => {
|
|
|
|
setImportText(text);
|
|
|
|
Keyboard.dismiss();
|
2020-04-24 11:20:49 +02:00
|
|
|
}}
|
2019-02-02 00:00:44 +01:00
|
|
|
/>
|
2020-04-27 09:11:33 +02:00
|
|
|
),
|
|
|
|
})}
|
2019-12-27 03:21:07 +01:00
|
|
|
</SafeBlueArea>
|
|
|
|
);
|
|
|
|
};
|
2018-07-22 16:49:59 +02:00
|
|
|
|
2020-12-25 17:09:53 +01:00
|
|
|
WalletsImport.navigationOptions = navigationStyle({
|
2020-07-20 15:38:46 +02:00
|
|
|
title: loc.wallets.import_title,
|
2020-07-15 19:32:59 +02:00
|
|
|
});
|
2019-12-27 03:21:07 +01:00
|
|
|
export default WalletsImport;
|