BlueWallet/screen/wallets/export.js

152 lines
4.8 KiB
JavaScript
Raw Normal View History

import React, { useState, useCallback, useContext } from 'react';
2020-08-04 20:03:00 -04:00
import { useWindowDimensions, InteractionManager, ScrollView, ActivityIndicator, StatusBar, View, StyleSheet } from 'react-native';
2019-02-14 00:15:56 -05:00
import QRCode from 'react-native-qrcode-svg';
2020-12-25 19:09:53 +03:00
import { useTheme, useNavigation, useFocusEffect, useRoute } from '@react-navigation/native';
import { BlueSpacing20, SafeBlueArea, BlueText, BlueCopyTextToClipboard, BlueCard } from '../../BlueComponents';
import navigationStyle from '../../components/navigationStyle';
2021-01-18 22:44:55 -05:00
import Privacy from '../../blue_modules/Privacy';
import Biometric from '../../class/biometrics';
import { LegacyWallet, LightningCustodianWallet, SegwitBech32Wallet, SegwitP2SHWallet, WatchOnlyWallet } from '../../class';
2020-07-20 16:38:46 +03:00
import loc from '../../loc';
import { BlueStorageContext } from '../../blue_modules/storage-context';
2018-01-30 22:42:38 +00:00
const styles = StyleSheet.create({
loading: {
flex: 1,
justifyContent: 'center',
},
scrollViewContent: {
alignItems: 'center',
justifyContent: 'center',
flexGrow: 1,
},
2020-08-03 13:26:16 -04:00
activeQrcode: { borderWidth: 6, borderRadius: 8, borderColor: '#FFFFFF' },
type: {
fontSize: 17,
fontWeight: '700',
},
secret: {
alignItems: 'center',
paddingHorizontal: 16,
fontSize: 16,
lineHeight: 24,
},
});
2020-08-04 20:03:00 -04:00
const WalletExport = () => {
const { wallets, saveToDisk } = useContext(BlueStorageContext);
const { walletID } = useRoute().params;
2020-08-04 20:03:00 -04:00
const [isLoading, setIsLoading] = useState(true);
const { goBack } = useNavigation();
const { colors } = useTheme();
const { width, height } = useWindowDimensions();
const wallet = wallets.find(w => w.getID() === walletID);
2020-08-04 20:03:00 -04:00
const stylesHook = {
...styles,
loading: {
...styles.loading,
backgroundColor: colors.elevated,
},
root: {
backgroundColor: colors.elevated,
},
type: { ...styles.type, color: colors.foregroundColor },
secret: { ...styles.secret, color: colors.foregroundColor },
2020-12-03 15:15:05 +00:00
warning: { ...styles.secret, color: colors.failedColor },
2020-08-04 20:03:00 -04:00
};
2018-01-30 22:42:38 +00:00
2020-08-04 20:03:00 -04:00
useFocusEffect(
useCallback(() => {
Privacy.enableBlur();
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
const isBiometricsEnabled = await Biometric.isBiometricUseCapableAndEnabled();
2020-08-04 20:03:00 -04:00
if (isBiometricsEnabled) {
if (!(await Biometric.unlockWithBiometrics())) {
return goBack();
}
}
if (!wallet.getUserHasSavedExport()) {
wallet.setUserHasSavedExport(true);
saveToDisk();
}
2020-08-04 20:03:00 -04:00
setIsLoading(false);
}
});
return () => {
task.cancel();
Privacy.disableBlur();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [goBack, wallet]),
2020-08-04 20:03:00 -04:00
);
if (isLoading || !wallet)
return (
<View style={stylesHook.loading}>
<ActivityIndicator />
</View>
);
// for SLIP39 we need to show all shares
let secrets = wallet.getSecret();
if (typeof secrets === 'string') {
secrets = [secrets];
}
return (
2020-08-04 20:03:00 -04:00
<SafeBlueArea style={stylesHook.root}>
<StatusBar barStyle="light-content" />
2021-03-02 16:38:02 +03:00
<ScrollView contentContainerStyle={styles.scrollViewContent} testID="WalletExportScroll">
2020-08-04 20:03:00 -04:00
<View>
<BlueText style={stylesHook.type}>{wallet.typeReadable}</BlueText>
2018-01-30 22:42:38 +00:00
</View>
{[LegacyWallet.type, SegwitBech32Wallet.type, SegwitP2SHWallet.type].includes(wallet.type) && (
<BlueCard>
<BlueText>{wallet.getAddress()}</BlueText>
</BlueCard>
2020-08-04 20:03:00 -04:00
)}
<BlueSpacing20 />
{secrets.map(s => (
<React.Fragment key={s}>
<View style={styles.activeQrcode}>
<QRCode
value={wallet.getSecret()}
logo={require('../../img/qr-code.png')}
size={height > width ? width - 40 : width / 2}
logoSize={70}
color="#000000"
logoBackgroundColor={colors.brandingColor}
backgroundColor="#FFFFFF"
ecl="H"
/>
</View>
{wallet.type !== WatchOnlyWallet.type && <BlueText style={stylesHook.warning}>{loc.wallets.warning_do_not_disclose}</BlueText>}
<BlueSpacing20 />
{wallet.type === LightningCustodianWallet.type || wallet.type === WatchOnlyWallet.type ? (
<BlueCopyTextToClipboard text={wallet.getSecret()} />
) : (
<BlueText style={stylesHook.secret} testID="Secret">
{wallet.getSecret()}
</BlueText>
)}
</React.Fragment>
))}
2020-08-04 20:03:00 -04:00
</ScrollView>
</SafeBlueArea>
);
2018-03-18 02:48:23 +00:00
};
2020-08-04 20:03:00 -04:00
2021-02-15 11:03:54 +03:00
WalletExport.navigationOptions = navigationStyle(
{
closeButton: true,
headerLeft: null,
},
opts => ({ ...opts, title: loc.wallets.export_title }),
);
2020-08-04 20:03:00 -04:00
export default WalletExport;