From 7a3ad2823d1efcd5065c5efbeeea069af6a45ce9 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Tue, 20 Feb 2024 10:33:49 +0530 Subject: [PATCH] Store the private keys and paths for all the accounts --- utils.ts | 74 ++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 26 deletions(-) diff --git a/utils.ts b/utils.ts index 819bddb..21d0ed9 100644 --- a/utils.ts +++ b/utils.ts @@ -28,19 +28,24 @@ const createWallet = async (): Promise => { const ethAddress = ethNode.address; const cosmosAddress = (await getCosmosAccounts(mnemonic, 0)).data.address; + const ethAccountInfo = `${0},${ethNode.privateKey}`; + const cosmosAccountInfo = `${0},${cosmosNode.privateKey}`; + + // Store HD Id and private key of accounts created using add account await setInternetCredentials( 'eth:keyServer:0', - 'eth:key:0', - ethNode.privateKey, + 'eth:keyPath:0', + ethAccountInfo, ); await setInternetCredentials( 'cosmos:keyServer:0', - 'cosmos:key:0', - cosmosNode.privateKey, + 'cosmos:keyPath:0', + cosmosAccountInfo, ); - + // Counter to keep track of add account index await setInternetCredentials('eth:accountIndices', 'ethAccount', '0'); await setInternetCredentials('cosmos:accountIndices', 'cosmosAccount', '0'); + // Global counter await setInternetCredentials('eth:globalCounter', 'ethCounter', '0'); await setInternetCredentials('cosmos:globalCounter', 'cosmosCounter', '0'); @@ -105,12 +110,6 @@ const addAccount = async (network: string): Promise => { throw new Error('Invalid wallet type'); } - await setInternetCredentials( - `${network}:keyServer:${id}`, - `${network}:key:${id}`, - privKey, - ); - let indices = idStore.password; indices += `,${id.toString()}`; @@ -141,7 +140,14 @@ const addAccount = async (network: string): Promise => { accountCounter, ); - return { pubKey, address, id, hdPath: hdPath }; + // Store the path and private key against the global counter - while fetching accounts in UI, this counter will be used + await setInternetCredentials( + `${network}:keyServer:${counterId}`, + `${network}:key:${counterId}`, + `${id}${privKey}`, + ); + + return { counterId, pubKey, address, hdPath }; } catch (error) { console.error('Error creating account:', error); } @@ -156,7 +162,7 @@ const createAccountFromHDPath = async ( throw new Error('Error while creating account'); } - const { privKey, pubKey, address } = account; + const { privKey, pubKey, address, network } = account; const parts = hdPath.split('/'); const id = parts[5]; @@ -165,22 +171,18 @@ const createAccountFromHDPath = async ( const accountInfo = `${path},${privKey}`; - let network = null; - switch (coinType) { - case '60': - network = 'eth'; + case "60'": await setInternetCredentials( - `Eth:keyServer:${id}`, - `Eth:key:${id}`, + `eth:keyServer:${id}`, + `eth:key:${id}`, accountInfo, ); break; - case '118': - network = 'cosmos'; + case "118'": await setInternetCredentials( - `Cosmos:keyServer${id}`, - `Cosmos:key:${id}`, + `cosmos:keyServer${id}`, + `cosmos:key:${id}`, accountInfo, ); break; @@ -189,6 +191,7 @@ const createAccountFromHDPath = async ( const counterStore = await getInternetCredentials( `${network}:globalCounter`, ); + if (!counterStore) { throw new Error('Error while fetching counter'); } @@ -215,7 +218,8 @@ const createAccountFromHDPath = async ( const accountInfoFromHDPath = async ( hdPath: string, ): Promise< - { privKey: string; pubKey: string; address: string } | undefined + | { privKey: string; pubKey: string; address: string; network: string } + | undefined > => { const mnemonicStore = await getInternetCredentials('mnemonicServer'); if (!mnemonicStore) { @@ -228,9 +232,27 @@ const accountInfoFromHDPath = async ( const privKey = node.privateKey; const pubKey = node.publicKey; - const address = node.address; - return { privKey, pubKey, address }; + const parts = hdPath.split('/'); + const id = parseInt(parts[5]); + const coinType = parts[2]; + + let network: string; + let address: string; + + switch (coinType) { + case "60'": + network = 'eth'; + address = node.address; + break; + case "118'": + network = 'cosmos'; + address = (await getCosmosAccounts(mnemonic, id)).data.address; + break; + default: + throw new Error('Invalid wallet type'); + } + return { privKey, pubKey, address, network }; }; const signMessage = async ({