Co-authored-by: Shreerang Kale <shreerangkale@gmail.com> Co-authored-by: IshaVenikar <ishavenikar7@gmail.com> Co-authored-by: AdityaSalunkhe21 <adityasalunkhe2204@gmail.com> Reviewed-on: LaconicNetwork/laconic-wallet-web#26 Co-authored-by: ishavenikar <ishavenikar@noreply.git.vdb.to> Co-committed-by: ishavenikar <ishavenikar@noreply.git.vdb.to>
43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { useEffect } from 'react';
|
|
|
|
import { useAccounts } from '../context/AccountsContext';
|
|
import { getPathKey, sendMessage } from '../utils/misc';
|
|
|
|
const useExportPKEmbed = () => {
|
|
const { accounts } = useAccounts();
|
|
|
|
useEffect(() => {
|
|
const handleMessage = async (event: MessageEvent) => {
|
|
const { type, chainId, address } = event.data;
|
|
|
|
if (type !== 'REQUEST_ACCOUNT_PK') return;
|
|
|
|
try {
|
|
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(
|
|
event.source as Window,
|
|
'ACCOUNT_PK_DATA',
|
|
{ privateKey },
|
|
event.origin,
|
|
);
|
|
} catch (error) {
|
|
console.error('Error fetching private key:', error);
|
|
}
|
|
};
|
|
|
|
window.addEventListener('message', handleMessage);
|
|
return () => {
|
|
window.removeEventListener('message', handleMessage);
|
|
};
|
|
}, [accounts]);
|
|
};
|
|
|
|
export default useExportPKEmbed;
|