mirror of
https://github.com/BlueWallet/BlueWallet.git
synced 2025-02-27 01:21:19 +01:00
Merge pull request #7519 from BlueWallet/ent
REF: ProvideEntropy to use routeparams instead of a function
This commit is contained in:
commit
7eb1828150
3 changed files with 29 additions and 47 deletions
|
@ -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;
|
||||
|
|
|
@ -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<AddWalletStackParamList, 'AddWallet'>;
|
||||
|
||||
type RouteProps = RouteProp<AddWalletStackParamList, 'AddWallet'>;
|
||||
|
||||
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<NavigationProps>();
|
||||
const { entropy: entropyHex } = useRoute<RouteProps>().params || {};
|
||||
const entropy = entropyHex ? Buffer.from(entropyHex, 'hex') : undefined;
|
||||
const { navigate, goBack, setOptions, setParams } = useExtendedNavigation<NavigationProps>();
|
||||
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);
|
||||
|
||||
|
|
|
@ -259,7 +259,7 @@ const D20Tab = ({ active }: { active: boolean }) => {
|
|||
|
||||
const ProvideEntropy = () => {
|
||||
const [entropy, dispatch] = useReducer(eReducer, initialState);
|
||||
const { onGenerated, words } = useRoute<RouteProps>().params;
|
||||
const { words } = useRoute<RouteProps>().params;
|
||||
const navigation = useNavigation<NavigationProp>();
|
||||
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',
|
||||
},
|
||||
|
|
Loading…
Add table
Reference in a new issue