Merge pull request #7519 from BlueWallet/ent

REF: ProvideEntropy to use routeparams instead of a function
This commit is contained in:
GLaDOS 2025-01-23 06:46:45 +00:00 committed by GitHub
commit 7eb1828150
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 29 additions and 47 deletions

View file

@ -21,7 +21,10 @@ import { ScanQRCodeComponent } from './LazyLoadScanQRCodeStack';
import { ScanQRCodeParamList } from './DetailViewStackParamList'; import { ScanQRCodeParamList } from './DetailViewStackParamList';
export type AddWalletStackParamList = { export type AddWalletStackParamList = {
AddWallet: undefined; AddWallet: {
entropy?: string;
words?: number;
};
ImportWallet?: { ImportWallet?: {
label?: string; label?: string;
triggerImport?: boolean; triggerImport?: boolean;
@ -44,8 +47,8 @@ export type AddWalletStackParamList = {
walletID: string; walletID: string;
}; };
ProvideEntropy: { ProvideEntropy: {
onGenerated: (entropy: Buffer) => void;
words: number; words: number;
entropy?: string;
}; };
WalletsAddMultisig: { WalletsAddMultisig: {
walletLabel: string; walletLabel: string;

View file

@ -31,6 +31,7 @@ import HeaderMenuButton from '../../components/HeaderMenuButton';
import { useExtendedNavigation } from '../../hooks/useExtendedNavigation'; import { useExtendedNavigation } from '../../hooks/useExtendedNavigation';
import { NativeStackNavigationProp } from '@react-navigation/native-stack'; import { NativeStackNavigationProp } from '@react-navigation/native-stack';
import { AddWalletStackParamList } from '../../navigation/AddWalletStack'; import { AddWalletStackParamList } from '../../navigation/AddWalletStack';
import { RouteProp, useRoute } from '@react-navigation/native';
enum ButtonSelected { enum ButtonSelected {
// @ts-ignore: Return later to update // @ts-ignore: Return later to update
@ -46,8 +47,6 @@ interface State {
selectedIndex: number; selectedIndex: number;
label: string; label: string;
selectedWalletType: ButtonSelected; selectedWalletType: ButtonSelected;
entropy: Buffer | undefined;
entropyButtonText: string;
} }
const ActionTypes = { const ActionTypes = {
@ -56,8 +55,6 @@ const ActionTypes = {
SET_SELECTED_INDEX: 'SET_SELECTED_INDEX', SET_SELECTED_INDEX: 'SET_SELECTED_INDEX',
SET_LABEL: 'SET_LABEL', SET_LABEL: 'SET_LABEL',
SET_SELECTED_WALLET_TYPE: 'SET_SELECTED_WALLET_TYPE', SET_SELECTED_WALLET_TYPE: 'SET_SELECTED_WALLET_TYPE',
SET_ENTROPY: 'SET_ENTROPY',
SET_ENTROPY_BUTTON_TEXT: 'SET_ENTROPY_BUTTON_TEXT',
} as const; } as const;
type ActionTypes = (typeof ActionTypes)[keyof typeof ActionTypes]; type ActionTypes = (typeof ActionTypes)[keyof typeof ActionTypes];
@ -72,8 +69,6 @@ const initialState: State = {
selectedIndex: 0, selectedIndex: 0,
label: '', label: '',
selectedWalletType: ButtonSelected.ONCHAIN, selectedWalletType: ButtonSelected.ONCHAIN,
entropy: undefined,
entropyButtonText: loc.wallets.add_entropy_provide,
}; };
const walletReducer = (state: State, action: TAction): State => { const walletReducer = (state: State, action: TAction): State => {
@ -88,19 +83,15 @@ const walletReducer = (state: State, action: TAction): State => {
return { ...state, label: action.payload }; return { ...state, label: action.payload };
case ActionTypes.SET_SELECTED_WALLET_TYPE: case ActionTypes.SET_SELECTED_WALLET_TYPE:
return { ...state, selectedWalletType: action.payload }; 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: default:
return state; return state;
} }
}; };
type NavigationProps = NativeStackNavigationProp<AddWalletStackParamList, 'AddWallet'>; type NavigationProps = NativeStackNavigationProp<AddWalletStackParamList, 'AddWallet'>;
type RouteProps = RouteProp<AddWalletStackParamList, 'AddWallet'>;
const WalletsAdd: React.FC = () => { const WalletsAdd: React.FC = () => {
const { colors } = useTheme(); const { colors } = useTheme();
@ -111,12 +102,12 @@ const WalletsAdd: React.FC = () => {
const selectedIndex = state.selectedIndex; const selectedIndex = state.selectedIndex;
const label = state.label; const label = state.label;
const selectedWalletType = state.selectedWalletType; const selectedWalletType = state.selectedWalletType;
const entropy = state.entropy;
const entropyButtonText = state.entropyButtonText;
const colorScheme = useColorScheme(); const colorScheme = useColorScheme();
// //
const { addWallet, saveToDisk } = useStorage(); const { addWallet, saveToDisk } = useStorage();
const { navigate, goBack, setOptions } = useExtendedNavigation<NavigationProps>(); const { entropy: entropyHex } = useRoute<RouteProps>().params || {};
const entropy = entropyHex ? Buffer.from(entropyHex, 'hex') : undefined;
const { navigate, goBack, setOptions, setParams } = useExtendedNavigation<NavigationProps>();
const stylesHook = { const stylesHook = {
advancedText: { advancedText: {
color: colors.feeText, color: colors.feeText,
@ -139,19 +130,14 @@ const WalletsAdd: React.FC = () => {
}, },
}; };
const entropyGenerated = useCallback((newEntropy: Buffer) => { const entropyButtonText = useMemo(() => {
let entropyTitle; if (!entropy) {
if (!newEntropy) { return loc.wallets.add_entropy_provide;
entropyTitle = loc.wallets.add_entropy_provide;
} else {
entropyTitle = loc.formatString(loc.wallets.add_entropy_bytes, {
bytes: newEntropy.length,
});
} }
return loc.formatString(loc.wallets.add_entropy_bytes, {
setEntropy(newEntropy); bytes: entropy?.length,
setEntropyButtonText(entropyTitle); });
}, []); }, [entropy]);
const confirmResetEntropy = useCallback( const confirmResetEntropy = useCallback(
(newWalletType: ButtonSelected) => { (newWalletType: ButtonSelected) => {
@ -168,8 +154,7 @@ const WalletsAdd: React.FC = () => {
text: loc._.ok, text: loc._.ok,
style: 'destructive', style: 'destructive',
onPress: () => { onPress: () => {
setEntropy(undefined); setParams({ entropy: undefined });
setEntropyButtonText(loc.wallets.add_entropy_provide);
setSelectedWalletType(newWalletType); setSelectedWalletType(newWalletType);
}, },
}, },
@ -181,7 +166,7 @@ const WalletsAdd: React.FC = () => {
setSelectedWalletType(newWalletType); setSelectedWalletType(newWalletType);
} }
}, },
[entropy], [entropy, setParams],
); );
const navigateToEntropy = useCallback(() => { const navigateToEntropy = useCallback(() => {
@ -204,20 +189,20 @@ const WalletsAdd: React.FC = () => {
{ {
text: loc.wallets.add_wallet_seed_length_12, text: loc.wallets.add_wallet_seed_length_12,
onPress: () => { onPress: () => {
navigate('ProvideEntropy', { onGenerated: entropyGenerated, words: 12 }); navigate('ProvideEntropy', { words: 12, entropy: entropy?.toString('hex') });
}, },
style: 'default', style: 'default',
}, },
{ {
text: loc.wallets.add_wallet_seed_length_24, text: loc.wallets.add_wallet_seed_length_24,
onPress: () => { onPress: () => {
navigate('ProvideEntropy', { onGenerated: entropyGenerated, words: 24 }); navigate('ProvideEntropy', { words: 24, entropy: entropy?.toString('hex') });
}, },
}, },
], ],
{ cancelable: true }, { cancelable: true },
); );
}, [confirmResetEntropy, entropyGenerated, navigate]); }, [confirmResetEntropy, entropy, navigate]);
const toolTipActions = useMemo(() => { const toolTipActions = useMemo(() => {
const walletSubactions: Action[] = [ const walletSubactions: Action[] = [
@ -331,14 +316,6 @@ const WalletsAdd: React.FC = () => {
dispatch({ type: 'SET_SELECTED_WALLET_TYPE', payload: value }); 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 () => { const createWallet = async () => {
setIsLoading(true); setIsLoading(true);

View file

@ -259,7 +259,7 @@ const D20Tab = ({ active }: { active: boolean }) => {
const ProvideEntropy = () => { const ProvideEntropy = () => {
const [entropy, dispatch] = useReducer(eReducer, initialState); const [entropy, dispatch] = useReducer(eReducer, initialState);
const { onGenerated, words } = useRoute<RouteProps>().params; const { words } = useRoute<RouteProps>().params;
const navigation = useNavigation<NavigationProp>(); const navigation = useNavigation<NavigationProp>();
const [tab, setTab] = useState(1); const [tab, setTab] = useState(1);
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
@ -323,8 +323,10 @@ const ProvideEntropy = () => {
buf = Buffer.concat([buf, random], bufLength); buf = Buffer.concat([buf, random], bufLength);
} }
navigation.pop(); /* Convert Buffer to hex string before navigating as React Navigation
onGenerated(buf); does not support passing Buffer objects between screens
*/
navigation.navigate('AddWallet', { entropy: buf.toString('hex') });
}, },
style: 'default', style: 'default',
}, },