From dbaf43f98cb2e1f3e704c96e4c3539fbf0b87fe1 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Fri, 16 Feb 2024 11:06:42 +0530 Subject: [PATCH] Display hdpaths for newly created accounts --- components/Accounts.tsx | 12 ++++++++++-- components/HomeScreen.tsx | 1 - types.ts | 1 + utils.ts | 16 +++++++--------- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/components/Accounts.tsx b/components/Accounts.tsx index 31b7339..3d787dd 100644 --- a/components/Accounts.tsx +++ b/components/Accounts.tsx @@ -27,8 +27,10 @@ const Accounts: React.FC = ({ setIsAccountCreating(true); const newAccount = await addAccount(network); setIsAccountCreating(false); - newAccount && updateAccounts(newAccount); - updateIndex(selectedAccounts[selectedAccounts.length - 1].id + 1); + if (newAccount) { + updateAccounts(newAccount); + updateIndex(newAccount.id); + } }; const selectedAccounts: Account[] = (() => { @@ -82,6 +84,12 @@ const Accounts: React.FC = ({ Public Key: {selectedAccounts[currentIndex]?.pubKey} + + HD Path: + {selectedAccounts && + selectedAccounts[currentIndex] && + selectedAccounts[currentIndex].hdPath} + diff --git a/components/HomeScreen.tsx b/components/HomeScreen.tsx index 9acdc9f..bfa62e3 100644 --- a/components/HomeScreen.tsx +++ b/components/HomeScreen.tsx @@ -30,7 +30,6 @@ const HomeScreen = () => { const createWalletHandler = async () => { setIsWalletCreating(true); - await new Promise(resolve => setTimeout(resolve, 2000)); const { mnemonic, ethAccounts, cosmosAccounts } = await createWallet(); ethAccounts && cosmosAccounts && diff --git a/types.ts b/types.ts index ff85673..ac0e9b1 100644 --- a/types.ts +++ b/types.ts @@ -7,6 +7,7 @@ export type Account = { id: number; pubKey: string; address: string; + hdPath: string; }; export type WalletDetails = { diff --git a/utils.ts b/utils.ts index b9a9f7c..54b93de 100644 --- a/utils.ts +++ b/utils.ts @@ -22,12 +22,8 @@ const createWallet = async (): Promise => { await setInternetCredentials('mnemonicServer', 'mnemonic', mnemonic); const hdNode = HDNode.fromMnemonic(mnemonic); - - const ethDerivationPath = "m/44'/60'/0'/0/0"; - const cosmosDerivationPath = "m/44'/118'/0'/0/0"; - - const ethNode = hdNode.derivePath(ethDerivationPath); - const cosmosNode = hdNode.derivePath(cosmosDerivationPath); + const ethNode = hdNode.derivePath("m/44'/60'/0'/0/0"); + const cosmosNode = hdNode.derivePath("m/44'/118'/0'/0/0"); const ethAddress = ethNode.address; const cosmosAddress = (await getCosmosAccounts(mnemonic, 0)).data.address; @@ -50,12 +46,14 @@ const createWallet = async (): Promise => { id: 0, pubKey: ethNode.publicKey, address: ethAddress, + hdPath: "m/44'/60'/0'/0/0", }; const cosmosAccounts = { id: 0, pubKey: cosmosNode.publicKey, address: cosmosAddress, + hdPath: "m/44'/118'/0'/0/0", }; return { mnemonic, ethAccounts, cosmosAccounts }; @@ -84,10 +82,10 @@ const addAccount = async (network: string): Promise => { const ids = accountIds.split(',').map(Number); const id = ids[ids.length - 1] + 1; - const derivationPath = + const hdPath = network === 'eth' ? `m/44'/60'/0'/0/${id}` : `m/44'/118'/0'/0/${id}`; - const node = hdNode.derivePath(derivationPath); + const node = hdNode.derivePath(hdPath); const privKey = node.privateKey; const pubKey = node.publicKey; @@ -126,7 +124,7 @@ const addAccount = async (network: string): Promise => { indices, ); - return { id, pubKey, address }; + return { pubKey, address, id, hdPath: hdPath }; } catch (error) { console.error('Error creating account:', error); }