Add bundled method to create account

This commit is contained in:
Adw8 2024-11-07 13:40:12 +05:30
parent b5005b8e47
commit 0cba7ab55d

View File

@ -1,64 +1,77 @@
import React, { useEffect } from 'react' import React, { useEffect } from 'react';
import { createWallet } from '../utils/accounts';
import { useNetworks } from '../context/NetworksContext';
export const WalletEmbed = () => { export const WalletEmbed = () => {
useEffect(() => { const { networksData } = useNetworks();
const handleMessage = (event: MessageEvent) => {
// Not checking for event origin as only account addresses are returned const getAccountsData = (chainId: string): string => {
const accountCount = localStorage.getItem(`addAccountCounter/cosmos:${chainId}`);
if (!accountCount) return '';
if (event.data.type === 'REQUEST_WALLET_ACCOUNTS') {
try {
let accountsData = ''; let accountsData = '';
const indices = localStorage.getItem(`addAccountCounter/cosmos:${event.data.chainId}`); for (let i = 0; i < Number(accountCount); i++) {
for (let i = 0; i < Number(indices); i++) { const account = localStorage.getItem(`accounts/cosmos:${chainId}/${i}`);
const account = localStorage.getItem(`accounts/cosmos:${event.data.chainId}/${i}`); if (account) accountsData += `${account},`;
if (account) {
accountsData += `${account},`;
}
} }
// Remove trailing comma return accountsData.slice(0, -1); // Remove trailing comma
accountsData = accountsData.slice(0, -1); };
const getAddressesFromData = (accountsData: string): string[] => {
return accountsData
? accountsData.split(',').filter((_, index) => (index + 1) % 4 === 0)
: [];
};
const sendMessage = (
source: Window | null,
type: string,
data: any,
origin: string
): void => {
source?.postMessage({ type, data }, origin);
};
useEffect(() => {
const handleGetAccounts = (event: MessageEvent) => {
if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return;
const accountsData = getAccountsData(event.data.chainId);
if (!accountsData) { if (!accountsData) {
event.source?.postMessage({ sendMessage(event.source as Window, 'ERROR', 'Wallet accounts not found in local storage', event.origin);
type: 'ERROR',
message: 'Wallet accounts not found in local storage',
});
return; return;
} }
const elements = accountsData.split(','); const addresses = getAddressesFromData(accountsData);
const addresses = elements.filter((_, index) => (index + 1) % 4 === 0); sendMessage(event.source as Window, 'WALLET_ACCOUNTS_DATA', addresses, event.origin);
(event.source as Window)?.postMessage(
{
type: 'WALLET_ACCOUNTS_DATA',
data: addresses,
},
event.origin
);
} catch (error) {
(event.source as Window)?.postMessage({
type: 'ERROR',
message: 'Error accessing wallet accounts data'
}, event.origin);
}
}
}; };
// Set up the message event listener window.addEventListener('message', handleGetAccounts);
window.addEventListener('message', handleMessage); return () => window.removeEventListener('message', handleGetAccounts);
// Clean up the event listener on component unmount
return () => {
window.removeEventListener('message', handleMessage);
};
}, []); }, []);
return ( useEffect(() => {
<div> const handleCreateAccounts = async (event: MessageEvent) => {
<p style={{ color: 'white' }}>Text</p> if (event.data.type !== 'REQUEST_CREATE_OR_GET_ACCOUNTS') return;
</div>
); let accountsData = getAccountsData(event.data.chainId);
if (!accountsData) {
console.log("Accounts not found, creating wallet...");
await createWallet(networksData);
// Re-fetch newly created accounts
accountsData = getAccountsData(event.data.chainId);
}
const addresses = getAddressesFromData(accountsData);
sendMessage(event.source as Window, 'WALLET_ACCOUNTS_DATA', addresses, event.origin);
};
window.addEventListener('message', handleCreateAccounts);
return () => window.removeEventListener('message', handleCreateAccounts);
}, [networksData]);
return <p style={{ color: 'white' }}>Text</p>;
}; };