2024-05-20 10:54:13 +01:00
|
|
|
import { useFocusEffect, useNavigation, useRoute } from '@react-navigation/native';
|
2024-05-31 13:18:01 -04:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
2024-05-20 10:54:13 +01:00
|
|
|
import { ActivityIndicator, AppState, InteractionManager, ScrollView, StyleSheet, View } from 'react-native';
|
|
|
|
import { BlueCard, BlueSpacing20, BlueText } from '../../BlueComponents';
|
|
|
|
import { LegacyWallet, LightningCustodianWallet, SegwitBech32Wallet, SegwitP2SHWallet, WatchOnlyWallet } from '../../class';
|
|
|
|
import CopyTextToClipboard from '../../components/CopyTextToClipboard';
|
2024-04-02 21:50:19 -04:00
|
|
|
import HandOffComponent from '../../components/HandOffComponent';
|
2024-05-20 10:54:13 +01:00
|
|
|
import QRCodeComponent from '../../components/QRCodeComponent';
|
2023-12-27 13:52:11 +07:00
|
|
|
import SafeArea from '../../components/SafeArea';
|
2024-05-20 10:54:13 +01:00
|
|
|
import { useTheme } from '../../components/themes';
|
2024-02-20 13:40:16 -04:00
|
|
|
import usePrivacy from '../../hooks/usePrivacy';
|
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';
|
2018-01-30 22:42:38 +00:00
|
|
|
|
2020-08-04 20:03:00 -04:00
|
|
|
const WalletExport = () => {
|
2024-05-31 11:52:29 -04:00
|
|
|
const { wallets, saveToDisk } = useStorage();
|
2020-10-24 13:20:59 -04:00
|
|
|
const { walletID } = useRoute().params;
|
2020-08-04 20:03:00 -04:00
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
const { goBack } = useNavigation();
|
|
|
|
const { colors } = useTheme();
|
2021-04-15 20:52:48 +03:00
|
|
|
const wallet = wallets.find(w => w.getID() === walletID);
|
2021-09-13 13:43:26 -04:00
|
|
|
const [qrCodeSize, setQRCodeSize] = useState(90);
|
2022-08-13 11:39:00 -04:00
|
|
|
const appState = useRef(AppState.currentState);
|
2024-02-20 13:40:16 -04:00
|
|
|
const { enableBlur, disableBlur } = usePrivacy();
|
2022-08-13 11:39:00 -04:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
2022-08-13 11:39:00 -04:00
|
|
|
type: { color: colors.foregroundColor },
|
2021-10-22 01:54:06 -04:00
|
|
|
secret: { color: colors.foregroundColor },
|
2022-08-13 11:39:00 -04:00
|
|
|
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-02-20 13:40:16 -04:00
|
|
|
enableBlur();
|
2020-08-04 20:03:00 -04:00
|
|
|
const task = InteractionManager.runAfterInteractions(async () => {
|
|
|
|
if (wallet) {
|
2021-04-15 20:52:48 +03:00
|
|
|
if (!wallet.getUserHasSavedExport()) {
|
|
|
|
wallet.setUserHasSavedExport(true);
|
2020-12-14 22:41:19 -05:00
|
|
|
saveToDisk();
|
|
|
|
}
|
2020-08-04 20:03:00 -04:00
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return () => {
|
|
|
|
task.cancel();
|
2024-02-20 13:40:16 -04:00
|
|
|
disableBlur();
|
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
|
|
|
);
|
2019-02-27 20:29:13 -05:00
|
|
|
|
2021-04-15 20:52:48 +03:00
|
|
|
if (isLoading || !wallet)
|
|
|
|
return (
|
2022-08-13 11:39:00 -04:00
|
|
|
<View style={[styles.loading, stylesHook.loading]}>
|
2021-04-15 20:52:48 +03:00
|
|
|
<ActivityIndicator />
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
// for SLIP39 we need to show all shares
|
|
|
|
let secrets = wallet.getSecret();
|
|
|
|
if (typeof secrets === 'string') {
|
|
|
|
secrets = [secrets];
|
|
|
|
}
|
|
|
|
|
2021-09-13 13:43:26 -04:00
|
|
|
const onLayout = e => {
|
|
|
|
const { height, width } = e.nativeEvent.layout;
|
|
|
|
setQRCodeSize(height > width ? width - 40 : e.nativeEvent.layout.width / 1.8);
|
|
|
|
};
|
|
|
|
|
2021-04-15 20:52:48 +03:00
|
|
|
return (
|
2023-12-27 13:52:11 +07:00
|
|
|
<SafeArea style={[styles.root, 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>
|
2022-08-13 11:39:00 -04:00
|
|
|
<BlueText style={[styles.type, stylesHook.type]}>{wallet.typeReadable}</BlueText>
|
2018-01-30 22:42:38 +00:00
|
|
|
</View>
|
2018-12-11 22:52:46 +00:00
|
|
|
|
2021-04-15 20:52:48 +03:00
|
|
|
{[LegacyWallet.type, SegwitBech32Wallet.type, SegwitP2SHWallet.type].includes(wallet.type) && (
|
|
|
|
<BlueCard>
|
|
|
|
<BlueText>{wallet.getAddress()}</BlueText>
|
|
|
|
</BlueCard>
|
2020-08-04 20:03:00 -04:00
|
|
|
)}
|
2021-04-15 20:52:48 +03:00
|
|
|
<BlueSpacing20 />
|
|
|
|
{secrets.map(s => (
|
|
|
|
<React.Fragment key={s}>
|
2021-09-13 13:43:26 -04:00
|
|
|
<QRCodeComponent isMenuAvailable={false} value={wallet.getSecret()} size={qrCodeSize} logoSize={70} />
|
2022-08-13 11:39:00 -04:00
|
|
|
{wallet.type !== WatchOnlyWallet.type && (
|
|
|
|
<BlueText style={[styles.warning, stylesHook.warning]}>{loc.wallets.warning_do_not_disclose}</BlueText>
|
|
|
|
)}
|
2021-04-15 20:52:48 +03:00
|
|
|
<BlueSpacing20 />
|
|
|
|
{wallet.type === LightningCustodianWallet.type || wallet.type === WatchOnlyWallet.type ? (
|
2024-03-27 00:37:43 -04:00
|
|
|
<CopyTextToClipboard text={wallet.getSecret()} />
|
2021-04-15 20:52:48 +03:00
|
|
|
) : (
|
2021-10-22 01:54:06 -04:00
|
|
|
<BlueText style={[styles.secret, styles.secretWritingDirection, stylesHook.secret]} testID="Secret">
|
2021-04-15 20:52:48 +03:00
|
|
|
{wallet.getSecret()}
|
|
|
|
</BlueText>
|
|
|
|
)}
|
2021-09-25 13:20:59 -04:00
|
|
|
{wallet.type === WatchOnlyWallet.type && (
|
2024-06-04 15:02:12 -04:00
|
|
|
<HandOffComponent title={loc.wallets.xpub_title} type={HandOffActivityType.Xpub} userInfo={{ xpub: wallet.getSecret() }} />
|
2021-09-25 13:20:59 -04:00
|
|
|
)}
|
2021-04-15 20:52:48 +03:00
|
|
|
</React.Fragment>
|
|
|
|
))}
|
2020-08-04 20:03:00 -04:00
|
|
|
</ScrollView>
|
2023-12-27 13:52:11 +07:00
|
|
|
</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
|
|
|
|
2022-08-13 11:39: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;
|