diff --git a/package.json b/package.json index e88f07d..533a02e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "web-wallet", - "version": "0.1.7-zenith-0.2.4", + "version": "0.1.7-zenith-0.2.5", "private": true, "dependencies": { "@laconic-network/cosmjs-util": "^0.1.0", diff --git a/src/hooks/useExportPrivateKeyEmbed.ts b/src/hooks/useExportPrivateKeyEmbed.ts index ec0e1cb..83b033e 100644 --- a/src/hooks/useExportPrivateKeyEmbed.ts +++ b/src/hooks/useExportPrivateKeyEmbed.ts @@ -1,12 +1,10 @@ import { useEffect } from 'react'; -import { useAccounts } from '../context/AccountsContext'; +import { retrieveNetworksData, retrieveAccounts } from '../utils/accounts'; import { getPathKey, sendMessage } from '../utils/misc'; import { ACCOUNT_PK_RESPONSE, REQUEST_ACCOUNT_PK } from '../utils/constants'; const useExportPKEmbed = () => { - const { accounts } = useAccounts(); - useEffect(() => { const handleMessage = async (event: MessageEvent) => { const { type, chainId, address } = event.data; @@ -14,9 +12,23 @@ const useExportPKEmbed = () => { if (type !== REQUEST_ACCOUNT_PK) return; try { - const selectedAccount = accounts.find(account => account.address === address); + // Look up the network and accounts directly from storage + // rather than relying on the accounts context, which can be + // overwritten by HomeScreen's fetchAccounts for a different network. + const networksData = await retrieveNetworksData(); + const targetNetwork = networksData.find( + net => `${net.namespace}:${net.chainId}` === chainId, + ); + + if (!targetNetwork) { + throw new Error("Network not found"); + } + + const networkAccounts = await retrieveAccounts(targetNetwork); + const selectedAccount = networkAccounts?.find(account => account.address === address); + if (!selectedAccount) { - throw new Error("Account not found") + throw new Error("Account not found"); } const pathKey = await getPathKey(chainId, selectedAccount.index); @@ -37,7 +49,7 @@ const useExportPKEmbed = () => { return () => { window.removeEventListener('message', handleMessage); }; - }, [accounts]); + }, []); }; export default useExportPKEmbed;