Show Mnemonic on wallet creation (#25)

* Display mnemonic on wallet creation

* Change srp to mnemonic

* Display mnemonic in a grid

* Remove line

* Make review changes

---------

Co-authored-by: Adw8 <adwait@deepstacksoft.com>
This commit is contained in:
Adwait Gharpure 2024-02-19 17:45:31 +05:30 committed by GitHub
parent 921327850b
commit 96c7fedacf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 77 additions and 6 deletions

View File

@ -1,6 +1,10 @@
import React from 'react'; import React from 'react';
import { View } from 'react-native';
import { Button, Dialog, Portal, Text } from 'react-native-paper'; import { Button, Dialog, Portal, Text } from 'react-native-paper';
import styles from '../styles/stylesheet';
import GridView from './Grid';
type CustomDialogProps = { type CustomDialogProps = {
visible: boolean; visible: boolean;
hideDialog: () => void; hideDialog: () => void;
@ -13,11 +17,23 @@ const DialogComponent: React.FC<CustomDialogProps> = ({
hideDialog, hideDialog,
contentText, contentText,
}) => { }) => {
const words = contentText.split(' ');
return ( return (
<Portal> <Portal>
<Dialog visible={visible} onDismiss={hideDialog}> <Dialog visible={visible} onDismiss={hideDialog}>
<Dialog.Content> <Dialog.Content>
<Text variant="titleMedium">{contentText}</Text> <Text variant="titleLarge">Mnemonic</Text>
<View style={styles.dialogTitle}>
<Text variant="titleMedium">
Your mnemonic provides full access to your wallet and funds. Make
sure to note it down.{' '}
</Text>
<Text variant="titleMedium" style={styles.dialogWarning}>
Do not share your mnemonic with anyone
</Text>
<GridView words={words} />
</View>
</Dialog.Content> </Dialog.Content>
<Dialog.Actions> <Dialog.Actions>
<Button onPress={hideDialog}>Done</Button> <Button onPress={hideDialog}>Done</Button>

21
components/Grid.tsx Normal file
View File

@ -0,0 +1,21 @@
import React from 'react';
import { View } from 'react-native';
import { Text } from 'react-native-paper';
import styles from '../styles/stylesheet';
import { GridViewProps } from '../types';
const GridView: React.FC<GridViewProps> = ({ words }) => {
return (
<View style={styles.gridContainer}>
{words.map((word, index) => (
<View key={index} style={styles.gridItem}>
<Text>{index + 1}. </Text>
<Text variant="bodySmall">{word}</Text>
</View>
))}
</View>
);
};
export default GridView;

View File

@ -18,6 +18,7 @@ const HomeScreen = () => {
const [resetWalletDialog, setResetWalletDialog] = useState<boolean>(false); const [resetWalletDialog, setResetWalletDialog] = useState<boolean>(false);
const [network, setNetwork] = useState<string>('eth'); const [network, setNetwork] = useState<string>('eth');
const [currentIndex, setCurrentIndex] = useState<number>(0); const [currentIndex, setCurrentIndex] = useState<number>(0);
const [phrase, setPhrase] = useState('');
const [accounts, setAccounts] = useState<AccountsState>({ const [accounts, setAccounts] = useState<AccountsState>({
ethAccounts: [], ethAccounts: [],
@ -30,7 +31,7 @@ const HomeScreen = () => {
const createWalletHandler = async () => { const createWalletHandler = async () => {
setIsWalletCreating(true); setIsWalletCreating(true);
await new Promise(resolve => setTimeout(resolve, 2000)); await new Promise(resolve => setTimeout(resolve, 2000));
const { ethAccounts, cosmosAccounts } = await createWallet(); const { mnemonic, ethAccounts, cosmosAccounts } = await createWallet();
ethAccounts && ethAccounts &&
cosmosAccounts && cosmosAccounts &&
setAccounts({ setAccounts({
@ -39,6 +40,7 @@ const HomeScreen = () => {
}); });
setWalletDialog(true); setWalletDialog(true);
setIsWalletCreated(true); setIsWalletCreated(true);
setPhrase(mnemonic);
}; };
const confirmResetWallet = async () => { const confirmResetWallet = async () => {
@ -86,7 +88,7 @@ const HomeScreen = () => {
<DialogComponent <DialogComponent
visible={walletDialog} visible={walletDialog}
hideDialog={hideWalletDialog} hideDialog={hideWalletDialog}
contentText="Wallet created" contentText={phrase}
/> />
<ResetWalletDialog <ResetWalletDialog
visible={resetWalletDialog} visible={resetWalletDialog}

View File

@ -54,6 +54,33 @@ const styles = StyleSheet.create({
networkDropdown: { networkDropdown: {
marginBottom: 20, marginBottom: 20,
}, },
dialogTitle: {
padding: 10,
},
dialogContents: {
marginTop: 24,
padding: 10,
borderWidth: 1,
borderRadius: 10,
},
dialogWarning: {
color: 'red',
},
gridContainer: {
flexDirection: 'row',
flexWrap: 'wrap',
justifyContent: 'center',
},
gridItem: {
width: '25%',
margin: 8,
padding: 6,
borderWidth: 1,
borderColor: '#ccc',
borderRadius: 8,
alignItems: 'center',
justifyContent: 'flex-start',
},
}); });
export default styles; export default styles;

View File

@ -10,6 +10,7 @@ export type Account = {
}; };
export type WalletDetails = { export type WalletDetails = {
mnemonic: string;
ethAccounts: Account | undefined; ethAccounts: Account | undefined;
cosmosAccounts: Account | undefined; cosmosAccounts: Account | undefined;
}; };
@ -51,3 +52,7 @@ export type ResetDialogProps = {
hideDialog: () => void; hideDialog: () => void;
onConfirm: () => void; onConfirm: () => void;
}; };
export type GridViewProps = {
words: string[];
};

View File

@ -18,7 +18,7 @@ import { Account, SignMessageParams, WalletDetails } from './types';
const createWallet = async (): Promise<WalletDetails> => { const createWallet = async (): Promise<WalletDetails> => {
try { try {
const mnemonic = utils.entropyToMnemonic(utils.randomBytes(32)); const mnemonic = utils.entropyToMnemonic(utils.randomBytes(16));
await setInternetCredentials('mnemonicServer', 'mnemonic', mnemonic); await setInternetCredentials('mnemonicServer', 'mnemonic', mnemonic);
const hdNode = HDNode.fromMnemonic(mnemonic); const hdNode = HDNode.fromMnemonic(mnemonic);
@ -54,10 +54,10 @@ const createWallet = async (): Promise<WalletDetails> => {
address: cosmosAddress, address: cosmosAddress,
}; };
return { ethAccounts, cosmosAccounts }; return { mnemonic, ethAccounts, cosmosAccounts };
} catch (error) { } catch (error) {
console.error('Error creating HD wallet:', error); console.error('Error creating HD wallet:', error);
return { ethAccounts: undefined, cosmosAccounts: undefined }; return { mnemonic: '', ethAccounts: undefined, cosmosAccounts: undefined };
} }
}; };