diff --git a/src/screens/AddNetwork.tsx b/src/screens/AddNetwork.tsx index ee0cd68..d57597c 100644 --- a/src/screens/AddNetwork.tsx +++ b/src/screens/AddNetwork.tsx @@ -23,7 +23,7 @@ import { INVALID_URL_ERROR, IS_NUMBER_REGEX, } from "../utils/constants"; -import { getCosmosAccounts } from "../utils/accounts"; +import { getCosmosAccountByHDPath } from "../utils/accounts"; import ETH_CHAINS from "../assets/ethereum-chains.json"; import { getInternetCredentials, @@ -163,7 +163,7 @@ const AddNetwork = () => { case COSMOS: address = ( - await getCosmosAccounts( + await getCosmosAccountByHDPath( mnemonic, hdPath, (newNetworkData as z.infer) diff --git a/src/screens/SignRequestEmbed.tsx b/src/screens/SignRequestEmbed.tsx index 3cafa44..a364a2e 100644 --- a/src/screens/SignRequestEmbed.tsx +++ b/src/screens/SignRequestEmbed.tsx @@ -12,7 +12,7 @@ import { getHeaderTitle } from '@react-navigation/elements'; import { Account, StackParamsList } from '../types'; import AccountDetails from '../components/AccountDetails'; import styles from '../styles/stylesheet'; -import { getCosmosAccounts, retrieveSingleAccount } from '../utils/accounts'; +import { getCosmosAccountByHDPath, retrieveSingleAccount } from '../utils/accounts'; import { getMnemonic, getPathKey, sendMessage } from '../utils/misc'; import { COSMOS, SIGN_MESSAGE, SIGNED_MESSAGE } from '../utils/constants'; import { useNetworks } from '../context/NetworksContext'; @@ -50,7 +50,7 @@ const SignRequestEmbed = ({ route }: SignRequestProps) => { throw new Error("Requested network not found") } - const cosmosAccount = await getCosmosAccounts(mnemonic, path, requestedNetworkData?.addressPrefix); + const cosmosAccount = await getCosmosAccountByHDPath(mnemonic, path, requestedNetworkData?.addressPrefix); const cosmosAminoSignature = await cosmosAccount.cosmosWallet.signAmino( signerAddress, diff --git a/src/screens/SignTxEmbed.tsx b/src/screens/SignTxEmbed.tsx index dcd7c07..a4f2a84 100644 --- a/src/screens/SignTxEmbed.tsx +++ b/src/screens/SignTxEmbed.tsx @@ -11,11 +11,11 @@ import { DirectSecp256k1Wallet, Algo, TxBodyEncodeObject, decodeOptionalPubkey } import { SigningStargateClient } from '@cosmjs/stargate'; import { toHex } from '@cosmjs/encoding'; -import { getCosmosAccount, retrieveAccounts, retrieveSingleAccount } from '../utils/accounts'; +import { getCosmosAccountByHDPath, retrieveAccounts, retrieveSingleAccount } from '../utils/accounts'; import AccountDetails from '../components/AccountDetails'; import styles from '../styles/stylesheet'; import DataBox from '../components/DataBox'; -import { getPathKey, sendMessage } from '../utils/misc'; +import { getMnemonic, getPathKey, sendMessage } from '../utils/misc'; import { useNetworks } from '../context/NetworksContext'; import TxErrorDialog from '../components/TxErrorDialog'; import { Account, NetworksDataState } from '../types'; @@ -26,13 +26,13 @@ interface GetAccountsRequestData { chainId: string, } -interface SignOnboardTxRequestData { +interface SignTxRequestData { address: string; signDoc: SignDoc; txBody: TxBodyEncodeObject; } -type IncomingMessageData = SignOnboardTxRequestData | GetAccountsRequestData; +type IncomingMessageData = SignTxRequestData | GetAccountsRequestData; interface IncomingMessageEventData { id: string; @@ -76,6 +76,7 @@ export const SignTxEmbed = () => { const source = event.source as Window; const origin = event.origin; const requestData = data as GetAccountsRequestData; + const mnemonic = await getMnemonic(); console.log(`Received ${REQUEST_COSMOS_ACCOUNTS_DATA}`, id); try { @@ -91,10 +92,9 @@ export const SignTxEmbed = () => { throw new Error("Accounts not found for network") } - // TODO: Refactor getCosmosAccounts functions to return all accounts const responseAccounts = await Promise.all( allAccounts.map(async (acc) => { - const cosmosAccount = await getCosmosAccount(acc.hdPath, requestedNetworkData.addressPrefix!); + const cosmosAccount = (await getCosmosAccountByHDPath(mnemonic, acc.hdPath, requestedNetworkData.addressPrefix)).data; return { ...cosmosAccount, pubkey: toHex(cosmosAccount.pubkey), @@ -112,11 +112,11 @@ export const SignTxEmbed = () => { } }, [networksData]); - const handleSignOnboardTxRequest = useCallback(async (event: MessageEvent) => { + const handleSignTxRequest = useCallback(async (event: MessageEvent) => { const { id, data } = event.data; const source = event.source as Window; const origin = event.origin; - const requestData = data as SignOnboardTxRequestData; + const requestData = data as SignTxRequestData; console.log(`Received ${REQUEST_SIGN_TX}`, id); setIsTxApprovalVisible(false); @@ -185,12 +185,12 @@ export const SignTxEmbed = () => { handleGetCosmosAccountsRequest(event as MessageEvent); break; case REQUEST_SIGN_TX: - handleSignOnboardTxRequest(event as MessageEvent); + handleSignTxRequest(event as MessageEvent); break; default: console.warn(`Received unknown message type: ${messageData.type}`); } - }, [handleGetCosmosAccountsRequest, handleSignOnboardTxRequest]); + }, [handleGetCosmosAccountsRequest, handleSignTxRequest]); useEffect(() => { window.addEventListener('message', handleIncomingMessage); diff --git a/src/utils/accounts.ts b/src/utils/accounts.ts index cc891dd..3b010d5 100644 --- a/src/utils/accounts.ts +++ b/src/utils/accounts.ts @@ -56,7 +56,7 @@ const createWalletFromMnemonic = async ( case COSMOS: address = ( - await getCosmosAccounts(mnemonic, hdPath, network.addressPrefix) + await getCosmosAccountByHDPath(mnemonic, hdPath, network.addressPrefix) ).data.address; break; @@ -281,7 +281,7 @@ const accountInfoFromHDPath = async ( break; case COSMOS: address = ( - await getCosmosAccounts(mnemonic, hdPath, networkData.addressPrefix) + await getCosmosAccountByHDPath(mnemonic, hdPath, networkData.addressPrefix) ).data.address; break; default: @@ -290,17 +290,6 @@ const accountInfoFromHDPath = async ( return { privKey, pubKey, address }; }; -const getCosmosAccount = async (hdPath: string, addressPrefix: string) => { - const mnemonicStore = getInternetCredentials('mnemonicServer'); - if (!mnemonicStore) { - throw new Error('Mnemonic not found!'); - } - - const mnemonic = mnemonicStore; - - return (await getCosmosAccounts(mnemonic, hdPath, addressPrefix)).data -} - const getNextAccountId = async (namespaceChainId: string): Promise => { const idStore = await getInternetCredentials( `addAccountCounter/${namespaceChainId}`, @@ -334,7 +323,7 @@ const updateAccountCounter = async ( ); }; -const getCosmosAccounts = async ( +const getCosmosAccountByHDPath = async ( mnemonic: string, path: string, prefix: string = COSMOS, @@ -362,6 +351,5 @@ export { accountInfoFromHDPath, getNextAccountId, updateAccountCounter, - getCosmosAccounts, - getCosmosAccount + getCosmosAccountByHDPath, }; diff --git a/src/utils/sign-message.ts b/src/utils/sign-message.ts index bb3904c..ef5ec06 100644 --- a/src/utils/sign-message.ts +++ b/src/utils/sign-message.ts @@ -10,7 +10,7 @@ import { SignDoc } from 'cosmjs-types/cosmos/tx/v1beta1/tx'; import { SignMessageParams } from '../types'; import { getDirectWallet, getMnemonic, getPathKey } from './misc'; -import { getCosmosAccounts } from './accounts'; +import { getCosmosAccountByHDPath } from './accounts'; import { COSMOS, EIP155 } from './constants'; const signMessage = async ({ @@ -58,7 +58,7 @@ const signCosmosMessage = async ( const mnemonic = await getMnemonic(); const addressPrefix = fromBech32(cosmosAddress).prefix - const cosmosAccount = await getCosmosAccounts(mnemonic, path, addressPrefix); + const cosmosAccount = await getCosmosAccountByHDPath(mnemonic, path, addressPrefix); const address = cosmosAccount.data.address; const cosmosSignature = await cosmosAccount.cosmosWallet.signAmino( address, diff --git a/src/utils/wallet-connect/wallet-connect-requests.ts b/src/utils/wallet-connect/wallet-connect-requests.ts index db3d8f4..d00fef2 100644 --- a/src/utils/wallet-connect/wallet-connect-requests.ts +++ b/src/utils/wallet-connect/wallet-connect-requests.ts @@ -18,7 +18,7 @@ import { EIP155_SIGNING_METHODS } from './EIP155Data'; import { signDirectMessage, signEthMessage } from '../sign-message'; import { Account } from '../../types'; import { getMnemonic, getPathKey } from '../misc'; -import { getCosmosAccounts } from '../accounts'; +import { getCosmosAccountByHDPath } from '../accounts'; import { COSMOS_METHODS } from './COSMOSData'; import { COSMOS } from '../constants'; @@ -88,7 +88,7 @@ export async function approveWalletConnectRequest( addressPrefix = fromBech32(account.address).prefix } - const cosmosAccount = await getCosmosAccounts(mnemonic, path, addressPrefix); + const cosmosAccount = await getCosmosAccountByHDPath(mnemonic, path, addressPrefix); const address = account.address; switch (request.method) {