diff --git a/src/hooks/useExportPrivateKeyEmbed.ts b/src/hooks/useExportPrivateKeyEmbed.ts index f474266..0ca2265 100644 --- a/src/hooks/useExportPrivateKeyEmbed.ts +++ b/src/hooks/useExportPrivateKeyEmbed.ts @@ -3,16 +3,21 @@ import { useAccounts } from '../context/AccountsContext'; import { getPathKey, sendMessage } from '../utils/misc'; const useExportPKEmbed = () => { - const { currentIndex } = useAccounts(); + const { accounts } = useAccounts(); useEffect(() => { const handleMessage = async (event: MessageEvent) => { - const { type, chainId } = event.data; + const { type, chainId, address } = event.data; if (type !== 'REQUEST_ACCOUNT_PK') return; try { - const pathKey = await getPathKey(chainId, currentIndex); + const selectedAccount = accounts.find(account => account.address === address); + if (!selectedAccount) { + throw new Error("Account not found") + } + + const pathKey = await getPathKey(chainId, selectedAccount.index); const privateKey = pathKey.privKey; sendMessage( @@ -30,7 +35,7 @@ const useExportPKEmbed = () => { return () => { window.removeEventListener('message', handleMessage); }; - }, [currentIndex]); + }, [accounts]); }; export default useExportPKEmbed;