diff --git a/navigation/AddWalletStack.tsx b/navigation/AddWalletStack.tsx index 8c52c448a..605692cfe 100644 --- a/navigation/AddWalletStack.tsx +++ b/navigation/AddWalletStack.tsx @@ -21,7 +21,10 @@ import { ScanQRCodeComponent } from './LazyLoadScanQRCodeStack'; import { ScanQRCodeParamList } from './DetailViewStackParamList'; export type AddWalletStackParamList = { - AddWallet: undefined; + AddWallet: { + entropy?: string; + words?: number; + }; ImportWallet?: { label?: string; triggerImport?: boolean; @@ -44,8 +47,8 @@ export type AddWalletStackParamList = { walletID: string; }; ProvideEntropy: { - onGenerated: (entropy: Buffer) => void; words: number; + entropy?: string; }; WalletsAddMultisig: { walletLabel: string; diff --git a/screen/wallets/Add.tsx b/screen/wallets/Add.tsx index 7e6ce322c..493f39dc3 100644 --- a/screen/wallets/Add.tsx +++ b/screen/wallets/Add.tsx @@ -31,6 +31,7 @@ import HeaderMenuButton from '../../components/HeaderMenuButton'; import { useExtendedNavigation } from '../../hooks/useExtendedNavigation'; import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { AddWalletStackParamList } from '../../navigation/AddWalletStack'; +import { RouteProp, useRoute } from '@react-navigation/native'; enum ButtonSelected { // @ts-ignore: Return later to update @@ -46,8 +47,6 @@ interface State { selectedIndex: number; label: string; selectedWalletType: ButtonSelected; - entropy: Buffer | undefined; - entropyButtonText: string; } const ActionTypes = { @@ -56,8 +55,6 @@ const ActionTypes = { SET_SELECTED_INDEX: 'SET_SELECTED_INDEX', SET_LABEL: 'SET_LABEL', SET_SELECTED_WALLET_TYPE: 'SET_SELECTED_WALLET_TYPE', - SET_ENTROPY: 'SET_ENTROPY', - SET_ENTROPY_BUTTON_TEXT: 'SET_ENTROPY_BUTTON_TEXT', } as const; type ActionTypes = (typeof ActionTypes)[keyof typeof ActionTypes]; @@ -72,8 +69,6 @@ const initialState: State = { selectedIndex: 0, label: '', selectedWalletType: ButtonSelected.ONCHAIN, - entropy: undefined, - entropyButtonText: loc.wallets.add_entropy_provide, }; const walletReducer = (state: State, action: TAction): State => { @@ -88,19 +83,15 @@ const walletReducer = (state: State, action: TAction): State => { return { ...state, label: action.payload }; case ActionTypes.SET_SELECTED_WALLET_TYPE: return { ...state, selectedWalletType: action.payload }; - case ActionTypes.SET_ENTROPY: - if (!action.payload) { - return { ...state, entropy: action.payload, entropyButtonText: loc.wallets.add_entropy_provide }; - } - return { ...state, entropy: action.payload }; - case ActionTypes.SET_ENTROPY_BUTTON_TEXT: - return { ...state, entropyButtonText: action.payload }; default: return state; } }; type NavigationProps = NativeStackNavigationProp; + +type RouteProps = RouteProp; + const WalletsAdd: React.FC = () => { const { colors } = useTheme(); @@ -111,12 +102,12 @@ const WalletsAdd: React.FC = () => { const selectedIndex = state.selectedIndex; const label = state.label; const selectedWalletType = state.selectedWalletType; - const entropy = state.entropy; - const entropyButtonText = state.entropyButtonText; const colorScheme = useColorScheme(); // const { addWallet, saveToDisk } = useStorage(); - const { navigate, goBack, setOptions } = useExtendedNavigation(); + const { entropy: entropyHex } = useRoute().params || {}; + const entropy = entropyHex ? Buffer.from(entropyHex, 'hex') : undefined; + const { navigate, goBack, setOptions, setParams } = useExtendedNavigation(); const stylesHook = { advancedText: { color: colors.feeText, @@ -139,19 +130,14 @@ const WalletsAdd: React.FC = () => { }, }; - const entropyGenerated = useCallback((newEntropy: Buffer) => { - let entropyTitle; - if (!newEntropy) { - entropyTitle = loc.wallets.add_entropy_provide; - } else { - entropyTitle = loc.formatString(loc.wallets.add_entropy_bytes, { - bytes: newEntropy.length, - }); + const entropyButtonText = useMemo(() => { + if (!entropy) { + return loc.wallets.add_entropy_provide; } - - setEntropy(newEntropy); - setEntropyButtonText(entropyTitle); - }, []); + return loc.formatString(loc.wallets.add_entropy_bytes, { + bytes: entropy?.length, + }); + }, [entropy]); const confirmResetEntropy = useCallback( (newWalletType: ButtonSelected) => { @@ -168,8 +154,7 @@ const WalletsAdd: React.FC = () => { text: loc._.ok, style: 'destructive', onPress: () => { - setEntropy(undefined); - setEntropyButtonText(loc.wallets.add_entropy_provide); + setParams({ entropy: undefined }); setSelectedWalletType(newWalletType); }, }, @@ -181,7 +166,7 @@ const WalletsAdd: React.FC = () => { setSelectedWalletType(newWalletType); } }, - [entropy], + [entropy, setParams], ); const navigateToEntropy = useCallback(() => { @@ -204,20 +189,20 @@ const WalletsAdd: React.FC = () => { { text: loc.wallets.add_wallet_seed_length_12, onPress: () => { - navigate('ProvideEntropy', { onGenerated: entropyGenerated, words: 12 }); + navigate('ProvideEntropy', { words: 12, entropy: entropy?.toString('hex') }); }, style: 'default', }, { text: loc.wallets.add_wallet_seed_length_24, onPress: () => { - navigate('ProvideEntropy', { onGenerated: entropyGenerated, words: 24 }); + navigate('ProvideEntropy', { words: 24, entropy: entropy?.toString('hex') }); }, }, ], { cancelable: true }, ); - }, [confirmResetEntropy, entropyGenerated, navigate]); + }, [confirmResetEntropy, entropy, navigate]); const toolTipActions = useMemo(() => { const walletSubactions: Action[] = [ @@ -331,14 +316,6 @@ const WalletsAdd: React.FC = () => { dispatch({ type: 'SET_SELECTED_WALLET_TYPE', payload: value }); }; - const setEntropy = (value: Buffer | undefined) => { - dispatch({ type: 'SET_ENTROPY', payload: value }); - }; - - const setEntropyButtonText = (value: string | undefined) => { - dispatch({ type: 'SET_ENTROPY_BUTTON_TEXT', payload: value ?? loc.wallets.add_entropy_provide }); - }; - const createWallet = async () => { setIsLoading(true); diff --git a/screen/wallets/ProvideEntropy.tsx b/screen/wallets/ProvideEntropy.tsx index 1f2db356c..2af58cb85 100644 --- a/screen/wallets/ProvideEntropy.tsx +++ b/screen/wallets/ProvideEntropy.tsx @@ -259,7 +259,7 @@ const D20Tab = ({ active }: { active: boolean }) => { const ProvideEntropy = () => { const [entropy, dispatch] = useReducer(eReducer, initialState); - const { onGenerated, words } = useRoute().params; + const { words } = useRoute().params; const navigation = useNavigation(); const [tab, setTab] = useState(1); const [show, setShow] = useState(false); @@ -323,8 +323,10 @@ const ProvideEntropy = () => { buf = Buffer.concat([buf, random], bufLength); } - navigation.pop(); - onGenerated(buf); + /* Convert Buffer to hex string before navigating as React Navigation + does not support passing Buffer objects between screens + */ + navigation.navigate('AddWallet', { entropy: buf.toString('hex') }); }, style: 'default', },