From 49613ef52fdbc3323d5fa0696134e5a20e523113 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Fri, 16 Feb 2024 14:37:21 +0530 Subject: [PATCH] Add new account using hd path given by user --- components/Accounts.tsx | 6 ++-- components/HDPath.tsx | 63 +++++++++++++++++++++++++++++++++++++++++ types.ts | 1 + utils.ts | 38 ++++++++++++++++++++++++- 4 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 components/HDPath.tsx diff --git a/components/Accounts.tsx b/components/Accounts.tsx index 3d787dd..e3b0aa7 100644 --- a/components/Accounts.tsx +++ b/components/Accounts.tsx @@ -14,7 +14,7 @@ const Accounts: React.FC = ({ accounts, updateAccounts, currentIndex, - updateIndex, + updateIndex: updateId, }) => { const navigation = useNavigation>(); @@ -29,7 +29,7 @@ const Accounts: React.FC = ({ setIsAccountCreating(false); if (newAccount) { updateAccounts(newAccount); - updateIndex(newAccount.id); + updateId(newAccount.id); } }; @@ -50,7 +50,7 @@ const Accounts: React.FC = ({ key={account.id} title={`Account ${account.id + 1}`} onPress={() => { - updateIndex(account.id); + updateId(account.id); setExpanded(false); }} /> diff --git a/components/HDPath.tsx b/components/HDPath.tsx new file mode 100644 index 0000000..64ed2d7 --- /dev/null +++ b/components/HDPath.tsx @@ -0,0 +1,63 @@ +import React, { useState } from 'react'; +import { ScrollView, View, Alert } from 'react-native'; +import { Button, Text, TextInput } from 'react-native-paper'; + +import { NativeStackScreenProps } from '@react-navigation/native-stack'; + +import { StackParamsList } from '../types'; +import { accountFromHDPath } from '../utils'; + +type HDPathProps = NativeStackScreenProps; + +const HDPath: React.FC = ({}) => { + const [path, setPath] = useState(''); + const [account, setAccount] = useState(null); + const [isAccountCreating, setIsAccountCreating] = useState(false); + + const createFromHDPathHandler = async () => { + setIsAccountCreating(true); + const newAccount = await accountFromHDPath(path); + setIsAccountCreating(false); + setAccount(newAccount); + Alert.alert('Account Created'); + }; + + return ( + + + + Enter the HD Path: + + + setPath(text)} + value={path} + /> + + + + + {account && ( + <> + + Address: + {account.address} + + + Public Key: + {account.pubKey} + + + )} + + + ); +}; + +export default HDPath; diff --git a/types.ts b/types.ts index ac0e9b1..2d66734 100644 --- a/types.ts +++ b/types.ts @@ -1,6 +1,7 @@ export type StackParamsList = { Laconic: undefined; SignMessage: { selectedNetwork: string; accountInfo: Account } | undefined; + HDPath: undefined; }; export type Account = { diff --git a/utils.ts b/utils.ts index 54b93de..83e7822 100644 --- a/utils.ts +++ b/utils.ts @@ -130,6 +130,36 @@ const addAccount = async (network: string): Promise => { } }; +const accountFromHDPath = async ( + hdPath: string, +): Promise< + | { + pubKey: string; + address: string; + } + | undefined +> => { + try { + const mnemonicStore = await getInternetCredentials('mnemonicServer'); + if (!mnemonicStore) { + throw new Error('Mnemonic not found!'); + } + + const mnemonic = mnemonicStore.password; + const hdNode = HDNode.fromMnemonic(mnemonic); + + const node = hdNode.derivePath(hdPath); + + const pubKey = node.publicKey; + const address = node.address; + + return { pubKey, address }; + } catch (error) { + console.error('Error creating account:', error); + return undefined; + } +}; + const signMessage = async ({ message, network, @@ -253,4 +283,10 @@ const resetWallet = async () => { } }; -export { createWallet, addAccount, signMessage, resetWallet }; +export { + createWallet, + addAccount, + signMessage, + resetWallet, + accountFromHDPath, +};