BlueWallet/screen/wallets/WalletExport.tsx

152 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-07-23 22:06:05 -04:00
import { useFocusEffect, useNavigation, useRoute, RouteProp } from '@react-navigation/native';
2024-05-31 13:18:01 -04:00
import React, { useCallback, useEffect, useRef, useState } from 'react';
2024-07-23 22:06:05 -04:00
import { ActivityIndicator, AppState, InteractionManager, ScrollView, StyleSheet, View, LayoutChangeEvent } from 'react-native';
2024-05-20 10:54:13 +01:00
import { BlueCard, BlueSpacing20, BlueText } from '../../BlueComponents';
import { LegacyWallet, LightningCustodianWallet, SegwitBech32Wallet, SegwitP2SHWallet, WatchOnlyWallet } from '../../class';
import CopyTextToClipboard from '../../components/CopyTextToClipboard';
import HandOffComponent from '../../components/HandOffComponent';
2024-05-20 10:54:13 +01:00
import QRCodeComponent from '../../components/QRCodeComponent';
import SafeArea from '../../components/SafeArea';
2024-05-20 10:54:13 +01:00
import { useTheme } from '../../components/themes';
2024-10-14 01:46:57 -04:00
import { disallowScreenshot } from 'react-native-screen-capture';
2024-05-20 10:54:13 +01:00
import loc from '../../loc';
2024-05-31 13:18:01 -04:00
import { useStorage } from '../../hooks/context/useStorage';
2024-06-04 15:02:12 -04:00
import { HandOffActivityType } from '../../components/types';
2024-07-23 22:06:05 -04:00
import { WalletExportStackParamList } from '../../navigation/WalletExportStack';
2018-01-30 22:42:38 +00:00
2024-07-23 22:06:05 -04:00
type RouteProps = RouteProp<WalletExportStackParamList, 'WalletExport'>;
const WalletExport: React.FC = () => {
const { wallets, saveToDisk } = useStorage();
2024-07-23 22:06:05 -04:00
const { walletID } = useRoute<RouteProps>().params;
2020-08-04 20:03:00 -04:00
const [isLoading, setIsLoading] = useState(true);
const { goBack } = useNavigation();
const { colors } = useTheme();
const wallet = wallets.find(w => w.getID() === walletID);
2021-09-13 13:43:26 -04:00
const [qrCodeSize, setQRCodeSize] = useState(90);
const appState = useRef(AppState.currentState);
useEffect(() => {
const subscription = AppState.addEventListener('change', nextAppState => {
if (!isLoading && nextAppState === 'background') {
goBack();
}
appState.current = nextAppState;
});
return () => {
subscription.remove();
};
}, [goBack, isLoading]);
2020-08-04 20:03:00 -04:00
const stylesHook = {
loading: {
backgroundColor: colors.elevated,
},
root: {
backgroundColor: colors.elevated,
},
type: { color: colors.foregroundColor },
2021-10-22 01:54:06 -04:00
secret: { color: colors.foregroundColor },
warning: { 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(() => {
2024-10-14 01:46:57 -04:00
disallowScreenshot(true);
2020-08-04 20:03:00 -04:00
const task = InteractionManager.runAfterInteractions(async () => {
if (wallet) {
if (!wallet.getUserHasSavedExport()) {
wallet.setUserHasSavedExport(true);
saveToDisk();
}
2020-08-04 20:03:00 -04:00
setIsLoading(false);
}
});
return () => {
task.cancel();
2024-10-14 01:46:57 -04:00
disallowScreenshot(false);
2020-08-04 20:03:00 -04:00
};
2024-02-20 13:40:16 -04:00
// eslint-disable-next-line react-hooks/exhaustive-deps
2024-03-23 23:48:44 -04:00
}, [wallet]),
2020-08-04 20:03:00 -04:00
);
if (isLoading || !wallet)
return (
<View style={[styles.loading, stylesHook.loading]}>
<ActivityIndicator />
</View>
);
// for SLIP39 we need to show all shares
2024-07-23 22:06:05 -04:00
const secrets: string[] = (typeof wallet.getSecret() === 'string' ? [wallet.getSecret()] : wallet.getSecret()) as string[];
2024-07-23 22:06:05 -04:00
const onLayout = (e: LayoutChangeEvent) => {
2021-09-13 13:43:26 -04:00
const { height, width } = e.nativeEvent.layout;
2024-07-23 22:06:05 -04:00
setQRCodeSize(height > width ? width - 40 : width / 1.8);
2021-09-13 13:43:26 -04:00
};
return (
2024-07-23 22:06:05 -04:00
<SafeArea style={stylesHook.root} onLayout={onLayout}>
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={[styles.type, 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}>
2024-07-23 22:06:05 -04:00
<QRCodeComponent isMenuAvailable={false} value={s} size={qrCodeSize} logoSize={70} />
{wallet.type !== WatchOnlyWallet.type && <BlueText style={stylesHook.warning}>{loc.wallets.warning_do_not_disclose}</BlueText>}
<BlueSpacing20 />
{wallet.type === LightningCustodianWallet.type || wallet.type === WatchOnlyWallet.type ? (
2024-07-23 22:06:05 -04:00
<CopyTextToClipboard text={s} />
) : (
2021-10-22 01:54:06 -04:00
<BlueText style={[styles.secret, styles.secretWritingDirection, stylesHook.secret]} testID="Secret">
2024-07-23 22:06:05 -04:00
{s}
</BlueText>
)}
2021-09-25 13:20:59 -04:00
{wallet.type === WatchOnlyWallet.type && (
2024-07-23 22:06:05 -04:00
<HandOffComponent title={loc.wallets.xpub_title} type={HandOffActivityType.Xpub} userInfo={{ xpub: s }} />
2021-09-25 13:20:59 -04:00
)}
</React.Fragment>
))}
2020-08-04 20:03:00 -04:00
</ScrollView>
</SafeArea>
2020-08-04 20:03:00 -04:00
);
2018-03-18 02:48:23 +00:00
};
2020-08-04 20:03:00 -04:00
const styles = StyleSheet.create({
loading: {
flex: 1,
justifyContent: 'center',
},
scrollViewContent: {
alignItems: 'center',
justifyContent: 'center',
flexGrow: 1,
},
type: {
fontSize: 17,
fontWeight: '700',
},
secret: {
alignSelf: 'stretch',
textAlign: 'center',
paddingHorizontal: 16,
fontSize: 16,
lineHeight: 24,
},
secretWritingDirection: {
writingDirection: 'ltr',
},
});
2020-08-04 20:03:00 -04:00
export default WalletExport;