forked from cerc-io/laconic-wallet
* Display HD path on sign message page * Create component for displaying account details * Add retrieve accounts function * Load accounts after closing app * Fix the retrieve accounts function * Use hdpath instead of id * Check if keystore is empty while retrieving accounts * Add spinner when accounts are being fetched * Display complete hd paths after reloading the app * Remove any return type * Store public key and address * Modify sign message function to use path * Fix the add accounts functionality
94 lines
2.6 KiB
TypeScript
94 lines
2.6 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { ScrollView, View, Text } from 'react-native';
|
|
import { Button, TextInput } from 'react-native-paper';
|
|
|
|
import { addAccountFromHDPath } from '../utils/accounts';
|
|
import { Account, PathState } from '../types';
|
|
import styles from '../styles/stylesheet';
|
|
|
|
const HDPath = ({
|
|
pathCode,
|
|
updateAccounts,
|
|
updateIndex,
|
|
hideDialog,
|
|
}: {
|
|
pathCode: string;
|
|
updateIndex: (index: number) => void;
|
|
updateAccounts: (account: Account) => void;
|
|
hideDialog: () => void;
|
|
}) => {
|
|
const [isAccountCreating, setIsAccountCreating] = useState(false);
|
|
const [path, setPath] = useState<PathState>({
|
|
firstNumber: '',
|
|
secondNumber: '',
|
|
thirdNumber: '',
|
|
});
|
|
|
|
const handleChange = (key: keyof PathState, value: string) => {
|
|
setPath({
|
|
...path,
|
|
[key]: value.replace(/[^0-9]/g, ''),
|
|
});
|
|
};
|
|
|
|
const createFromHDPathHandler = async () => {
|
|
setIsAccountCreating(true);
|
|
const hdPath =
|
|
pathCode +
|
|
`${path.firstNumber}'/${path.secondNumber}/${path.thirdNumber}`;
|
|
try {
|
|
const newAccount = await addAccountFromHDPath(hdPath);
|
|
if (newAccount) {
|
|
updateAccounts(newAccount);
|
|
updateIndex(newAccount.counterId);
|
|
hideDialog();
|
|
}
|
|
} catch (error) {
|
|
console.error('Error creating account:', error);
|
|
} finally {
|
|
setIsAccountCreating(false);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<ScrollView style={styles.HDcontainer}>
|
|
<View style={styles.HDrowContainer}>
|
|
<Text style={styles.HDtext}>{pathCode}</Text>
|
|
<TextInput
|
|
keyboardType="numeric"
|
|
mode="outlined"
|
|
onChangeText={text => handleChange('firstNumber', text)}
|
|
value={path.firstNumber}
|
|
style={styles.HDtextInput}
|
|
/>
|
|
<Text style={styles.HDtext}>'/</Text>
|
|
<TextInput
|
|
keyboardType="numeric"
|
|
mode="outlined"
|
|
onChangeText={text => handleChange('secondNumber', text)}
|
|
value={path.secondNumber}
|
|
style={styles.HDtextInput}
|
|
/>
|
|
<Text style={styles.HDtext}>/</Text>
|
|
<TextInput
|
|
keyboardType="numeric"
|
|
mode="outlined"
|
|
onChangeText={text => handleChange('thirdNumber', text)}
|
|
value={path.thirdNumber}
|
|
style={styles.HDtextInput}
|
|
/>
|
|
</View>
|
|
<View style={styles.HDbuttonContainer}>
|
|
<Button
|
|
mode="contained"
|
|
onPress={createFromHDPathHandler}
|
|
loading={isAccountCreating}>
|
|
{isAccountCreating ? 'Adding' : 'Add Account'}
|
|
</Button>
|
|
</View>
|
|
</ScrollView>
|
|
);
|
|
};
|
|
|
|
export default HDPath;
|