diff --git a/types.ts b/types.ts index 2d66734..5a3135d 100644 --- a/types.ts +++ b/types.ts @@ -41,7 +41,7 @@ export type AccountsState = { export type SignMessageParams = { message: string; network: string; - accountId: number; + accountId: string; }; export type CreateWalletProps = { diff --git a/utils.ts b/utils.ts index 21d0ed9..5ea15e9 100644 --- a/utils.ts +++ b/utils.ts @@ -26,12 +26,12 @@ const createWallet = async (): Promise => { const cosmosNode = hdNode.derivePath("m/44'/118'/0'/0/0"); const ethAddress = ethNode.address; - const cosmosAddress = (await getCosmosAccounts(mnemonic, 0)).data.address; + const cosmosAddress = (await getCosmosAccounts(mnemonic, `/0'/0/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:keyPath:0', @@ -42,11 +42,10 @@ const createWallet = async (): Promise => { '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'); @@ -104,7 +103,8 @@ const addAccount = async (network: string): Promise => { address = node.address; break; case 'cosmos': - address = (await getCosmosAccounts(mnemonic, id)).data.address; + address = (await getCosmosAccounts(mnemonic, `0'/0/${id}`)).data + .address; break; default: throw new Error('Invalid wallet type'); @@ -128,8 +128,8 @@ const addAccount = async (network: string): Promise => { } let accountCounter = counterStore.password; - const cIds = accountCounter.split(',').map(Number); - const counterId = cIds[cIds.length - 1] + 1; + const counterIds = accountCounter.split(',').map(Number); + const counterId = counterIds[counterIds.length - 1] + 1; accountCounter += `,${counterId.toString()}`; await resetInternetCredentials(`${network}:globalCounter`); @@ -140,7 +140,6 @@ const addAccount = async (network: string): Promise => { accountCounter, ); - // 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}`, @@ -153,7 +152,7 @@ const addAccount = async (network: string): Promise => { } }; -const createAccountFromHDPath = async ( +const addAccountFromHDPath = async ( hdPath: string, ): Promise<{ pubKey: string; address: string } | undefined> => { try { @@ -165,29 +164,9 @@ const createAccountFromHDPath = async ( const { privKey, pubKey, address, network } = account; const parts = hdPath.split('/'); - const id = parts[5]; const coinType = parts[2]; const path = parts.slice(-3).join('/'); - const accountInfo = `${path},${privKey}`; - - switch (coinType) { - case "60'": - await setInternetCredentials( - `eth:keyServer:${id}`, - `eth:key:${id}`, - accountInfo, - ); - break; - case "118'": - await setInternetCredentials( - `cosmos:keyServer${id}`, - `cosmos:key:${id}`, - accountInfo, - ); - break; - } - const counterStore = await getInternetCredentials( `${network}:globalCounter`, ); @@ -197,8 +176,8 @@ const createAccountFromHDPath = async ( } let accountCounter = counterStore.password; - const cIds = accountCounter.split(',').map(Number); - const counterId = cIds[cIds.length - 1] + 1; + const counterIds = accountCounter.split(',').map(Number); + const counterId = counterIds[counterIds.length - 1] + 1; accountCounter += `,${counterId.toString()}`; await resetInternetCredentials(`${network}:globalCounter`); @@ -209,6 +188,25 @@ const createAccountFromHDPath = async ( accountCounter, ); + const accountInfo = `${path},${privKey}`; + + switch (coinType) { + case "60'": + await setInternetCredentials( + `eth:keyServer:${counterId}`, + `eth:key:${counterId}`, + accountInfo, + ); + break; + case "118'": + await setInternetCredentials( + `cosmos:keyServer${counterId}`, + `cosmos:key:${counterId}`, + accountInfo, + ); + break; + } + return { pubKey, address }; } catch (error) { console.error(error); @@ -234,8 +232,9 @@ const accountInfoFromHDPath = async ( const pubKey = node.publicKey; const parts = hdPath.split('/'); - const id = parseInt(parts[5]); + const path = parts.slice(-3).join('/'); const coinType = parts[2]; + console.log(path); let network: string; let address: string; @@ -247,7 +246,7 @@ const accountInfoFromHDPath = async ( break; case "118'": network = 'cosmos'; - address = (await getCosmosAccounts(mnemonic, id)).data.address; + address = (await getCosmosAccounts(mnemonic, path)).data.address; break; default: throw new Error('Invalid wallet type'); @@ -272,7 +271,7 @@ const signMessage = async ({ const signEthMessage = async ( message: string, - id: number, + id: string, ): Promise => { try { const keyCred = await getInternetCredentials(`eth:keyServer:${id}`); @@ -293,7 +292,7 @@ const signEthMessage = async ( const signCosmosMessage = async ( message: string, - id: number, + id: string, ): Promise => { try { const mnemonicStore = await getInternetCredentials('mnemonicServer'); @@ -336,10 +335,10 @@ const signCosmosMessage = async ( const getCosmosAccounts = async ( mnemonic: string, - id: number, + id: string, ): Promise<{ cosmosWallet: Secp256k1HdWallet; data: AccountData }> => { const cosmosWallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, { - hdPaths: [stringToPath(`m/44'/118'/0'/0/${id}`)], + hdPaths: [stringToPath(`m/44'/118'/${id}`)], }); const accountsData = await cosmosWallet.getAccounts(); @@ -372,6 +371,9 @@ const resetWallet = async () => { await resetInternetCredentials('eth:accountIndices'); await resetInternetCredentials('cosmos:accountIndices'); + + await resetInternetCredentials('eth:globalCounter'); + await resetInternetCredentials('cosmos:globalCounter'); } catch (error) { console.error('Error resetting wallet:', error); throw error; @@ -383,5 +385,5 @@ export { addAccount, signMessage, resetWallet, - createAccountFromHDPath, + addAccountFromHDPath, };