From f2727315bde9a52d08bbcf5dd323b5019dee679d Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Tue, 20 Feb 2024 14:39:13 +0530 Subject: [PATCH] Resolve errors due to rebasing --- components/Accounts.tsx | 8 ++--- components/HDPath.tsx | 4 +-- components/SignMessage.tsx | 4 +-- types.ts | 4 +-- utils.ts | 62 +++++++++++++++++++++++--------------- 5 files changed, 48 insertions(+), 34 deletions(-) diff --git a/components/Accounts.tsx b/components/Accounts.tsx index e3b0aa7..3eca801 100644 --- a/components/Accounts.tsx +++ b/components/Accounts.tsx @@ -29,7 +29,7 @@ const Accounts: React.FC = ({ setIsAccountCreating(false); if (newAccount) { updateAccounts(newAccount); - updateId(newAccount.id); + updateId(newAccount.counterId); } }; @@ -47,10 +47,10 @@ const Accounts: React.FC = ({ const renderAccountItems = () => selectedAccounts.map(account => ( { - updateId(account.id); + updateId(account.counterId); setExpanded(false); }} /> diff --git a/components/HDPath.tsx b/components/HDPath.tsx index 64ed2d7..ae2e1ab 100644 --- a/components/HDPath.tsx +++ b/components/HDPath.tsx @@ -5,7 +5,7 @@ import { Button, Text, TextInput } from 'react-native-paper'; import { NativeStackScreenProps } from '@react-navigation/native-stack'; import { StackParamsList } from '../types'; -import { accountFromHDPath } from '../utils'; +import { addAccountFromHDPath } from '../utils'; type HDPathProps = NativeStackScreenProps; @@ -16,7 +16,7 @@ const HDPath: React.FC = ({}) => { const createFromHDPathHandler = async () => { setIsAccountCreating(true); - const newAccount = await accountFromHDPath(path); + const newAccount = await addAccountFromHDPath(path); setIsAccountCreating(false); setAccount(newAccount); Alert.alert('Account Created'); diff --git a/components/SignMessage.tsx b/components/SignMessage.tsx index 64884f7..db62e64 100644 --- a/components/SignMessage.tsx +++ b/components/SignMessage.tsx @@ -24,7 +24,7 @@ const SignMessage = ({ route }: SignProps) => { const signedMessage = await signMessage({ message, network, - accountId: account.id, + accountId: account.counterId, }); Alert.alert('Signature', signedMessage); } @@ -35,7 +35,7 @@ const SignMessage = ({ route }: SignProps) => { - {account && `Account ${account.id + 1}`} + {account && `Account ${account.counterId + 1}`} diff --git a/types.ts b/types.ts index 5a3135d..980b680 100644 --- a/types.ts +++ b/types.ts @@ -5,7 +5,7 @@ export type StackParamsList = { }; export type Account = { - id: number; + counterId: number; pubKey: string; address: string; hdPath: string; @@ -41,7 +41,7 @@ export type AccountsState = { export type SignMessageParams = { message: string; network: string; - accountId: string; + accountId: number; }; export type CreateWalletProps = { diff --git a/utils.ts b/utils.ts index 5ea15e9..31d35f3 100644 --- a/utils.ts +++ b/utils.ts @@ -26,7 +26,7 @@ const createWallet = async (): Promise => { const cosmosNode = hdNode.derivePath("m/44'/118'/0'/0/0"); const ethAddress = ethNode.address; - const cosmosAddress = (await getCosmosAccounts(mnemonic, `/0'/0/0`)).data + const cosmosAddress = (await getCosmosAccounts(mnemonic, `0'/0/0`)).data .address; const ethAccountInfo = `${0},${ethNode.privateKey}`; @@ -34,30 +34,30 @@ const createWallet = async (): Promise => { await setInternetCredentials( 'eth:keyServer:0', - 'eth:keyPath:0', + 'eth:pathKey:0', ethAccountInfo, ); await setInternetCredentials( 'cosmos:keyServer:0', - 'cosmos:keyPath:0', + 'cosmos:pathKey:0', cosmosAccountInfo, ); - await setInternetCredentials('eth:accountIndices', 'ethAccount', '0'); - await setInternetCredentials('cosmos:accountIndices', 'cosmosAccount', '0'); + await setInternetCredentials('eth:accountIndices', 'ethCounter', '0'); + await setInternetCredentials('cosmos:accountIndices', 'cosmosCounter', '0'); - await setInternetCredentials('eth:globalCounter', 'ethCounter', '0'); - await setInternetCredentials('cosmos:globalCounter', 'cosmosCounter', '0'); + await setInternetCredentials('eth:globalCounter', 'ethGlobal', '0'); + await setInternetCredentials('cosmos:globalCounter', 'cosmosGlobal', '0'); const ethAccounts = { - id: 0, + counterId: 0, pubKey: ethNode.publicKey, address: ethAddress, hdPath: "m/44'/60'/0'/0/0", }; const cosmosAccounts = { - id: 0, + counterId: 0, pubKey: cosmosNode.publicKey, address: cosmosAddress, hdPath: "m/44'/118'/0'/0/0", @@ -116,7 +116,7 @@ const addAccount = async (network: string): Promise => { await resetInternetCredentials(`${network}:accountIndices`); await setInternetCredentials( `${network}:accountIndices`, - `${network}Account`, + `${network}Counter`, indices, ); @@ -136,14 +136,14 @@ const addAccount = async (network: string): Promise => { await setInternetCredentials( `${network}:globalCounter`, - `${network}Counter`, + `${network}Global`, accountCounter, ); await setInternetCredentials( `${network}:keyServer:${counterId}`, - `${network}:key:${counterId}`, - `${id}${privKey}`, + `${network}:pathKey:${counterId}`, + `0'/0/${id}${privKey}`, ); return { counterId, pubKey, address, hdPath }; @@ -184,7 +184,7 @@ const addAccountFromHDPath = async ( await setInternetCredentials( `${network}:globalCounter`, - `${network}Counter`, + `${network}Global`, accountCounter, ); @@ -194,19 +194,21 @@ const addAccountFromHDPath = async ( case "60'": await setInternetCredentials( `eth:keyServer:${counterId}`, - `eth:key:${counterId}`, + `eth:pathKey:${counterId}`, accountInfo, ); break; case "118'": await setInternetCredentials( `cosmos:keyServer${counterId}`, - `cosmos:key:${counterId}`, + `cosmos:pathKey:${counterId}`, accountInfo, ); break; } + console.log(pubKey, address); + return { pubKey, address }; } catch (error) { console.error(error); @@ -259,11 +261,23 @@ const signMessage = async ({ network, accountId, }: SignMessageParams): Promise => { + const pathKeyStore = await getInternetCredentials( + `${network}:keyServer:${accountId}`, + ); + + if (!pathKeyStore) { + throw new Error('Error while fetching counter'); + } + + const pathKeyVal = pathKeyStore.password; + const pathkey = pathKeyVal.split(','); + const hdPath = pathkey[pathkey.length - 1]; + switch (network) { case 'eth': - return await signEthMessage(message, accountId); + return await signEthMessage(message, hdPath); case 'cosmos': - return await signCosmosMessage(message, accountId); + return await signCosmosMessage(message, hdPath); default: throw new Error('Invalid wallet type'); } @@ -271,10 +285,10 @@ const signMessage = async ({ const signEthMessage = async ( message: string, - id: string, + hdPath: string, ): Promise => { try { - const keyCred = await getInternetCredentials(`eth:keyServer:${id}`); + const keyCred = await getInternetCredentials(`eth:keyServer:${hdPath}`); if (!keyCred) { throw new Error('Failed to retrieve internet credentials'); @@ -292,7 +306,7 @@ const signEthMessage = async ( const signCosmosMessage = async ( message: string, - id: string, + hdPath: string, ): Promise => { try { const mnemonicStore = await getInternetCredentials('mnemonicServer'); @@ -302,7 +316,7 @@ const signCosmosMessage = async ( } const mnemonic = mnemonicStore.password; - const cosmosAccount = await getCosmosAccounts(mnemonic, id); + const cosmosAccount = await getCosmosAccounts(mnemonic, hdPath); const cosmosSignature = await cosmosAccount.cosmosWallet.signAmino( cosmosAccount.data.address, { @@ -335,10 +349,10 @@ const signCosmosMessage = async ( const getCosmosAccounts = async ( mnemonic: string, - id: string, + hdPath: string, ): Promise<{ cosmosWallet: Secp256k1HdWallet; data: AccountData }> => { const cosmosWallet = await Secp256k1HdWallet.fromMnemonic(mnemonic, { - hdPaths: [stringToPath(`m/44'/118'/${id}`)], + hdPaths: [stringToPath(`m/44'/118'/${hdPath}`)], }); const accountsData = await cosmosWallet.getAccounts();