From a3c434178bfd42e4d9c486160519f501ce28cca7 Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Mon, 19 Feb 2024 19:10:21 +0530 Subject: [PATCH] Add a global counter for eth and cosmos --- utils.ts | 96 +++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 68 insertions(+), 28 deletions(-) diff --git a/utils.ts b/utils.ts index ad5f4fb..88897cb 100644 --- a/utils.ts +++ b/utils.ts @@ -41,6 +41,9 @@ const createWallet = async (): Promise => { 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'); const ethAccounts = { id: 0, @@ -108,23 +111,37 @@ const addAccount = async (network: string): Promise => { privKey, ); - const accountIndicesKey = `${network}:accountIndices`; - const accountIndices = await getInternetCredentials(accountIndicesKey); - - if (!accountIndices) { - throw new Error('Account not found!'); - } - let indices = accountIndices.password; + let indices = idStore.password; indices += `,${id.toString()}`; - await resetInternetCredentials(accountIndicesKey); + await resetInternetCredentials(`${network}:accountIndices`); await setInternetCredentials( - accountIndicesKey, + `${network}:accountIndices`, `${network}Account`, indices, ); - return { id, pubKey, address, hdPath }; + const counterStore = await getInternetCredentials( + `${network}:globalCounter`, + ); + if (!counterStore) { + throw new Error('Error while fetching counter'); + } + + let accountCounter = counterStore.password; + const cIds = accountCounter.split(',').map(Number); + const counterId = cIds[cIds.length - 1] + 1; + accountCounter += `,${counterId.toString()}`; + + await resetInternetCredentials(`${network}:globalCounter`); + + await setInternetCredentials( + `${network}:globalCounter`, + `${network}Counter`, + accountCounter, + ); + + return { pubKey, address, id, hdPath: hdPath }; } catch (error) { console.error('Error creating account:', error); } @@ -135,7 +152,9 @@ const createAccountFromHDPath = async ( ): Promise<{ pubKey: string; address: string } | undefined> => { try { const account = await accountInfoFromHDPath(hdPath); - if (!account) throw new Error('Error while creating account'); + if (!account) { + throw new Error('Error while creating account'); + } const { privKey, pubKey, address } = account; @@ -146,8 +165,11 @@ const createAccountFromHDPath = async ( const accountInfo = `${path},${privKey}`; + let network = null; + switch (coinType) { case '60': + network = 'eth'; await setInternetCredentials( `Eth:keyServer:${id}`, `Eth:key:${id}`, @@ -155,6 +177,7 @@ const createAccountFromHDPath = async ( ); break; case '118': + network = 'cosmos'; await setInternetCredentials( `Cosmos:keyServer${id}`, `Cosmos:key:${id}`, @@ -163,9 +186,29 @@ const createAccountFromHDPath = async ( break; } + const counterStore = await getInternetCredentials( + `${network}:globalCounter`, + ); + if (!counterStore) { + throw new Error('Error while fetching counter'); + } + + let accountCounter = counterStore.password; + const cIds = accountCounter.split(',').map(Number); + const counterId = cIds[cIds.length - 1] + 1; + accountCounter += `,${counterId.toString()}`; + + await resetInternetCredentials(`${network}:globalCounter`); + + await setInternetCredentials( + `${network}:globalCounter`, + `${network}Counter`, + accountCounter, + ); + return { pubKey, address }; } catch (error) { - console.error('Error creating account:', error); + console.error(error); return undefined; } }; @@ -175,23 +218,20 @@ const accountInfoFromHDPath = async ( ): Promise< { privKey: string; 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 privKey = node.privateKey; - const pubKey = node.publicKey; - const address = node.address; - - return { privKey, pubKey, address }; - } catch (error) { - console.error('Error creating account:', error); - return undefined; + 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 privKey = node.privateKey; + const pubKey = node.publicKey; + const address = node.address; + + return { privKey, pubKey, address }; }; const signMessage = async ({