Use existing method to retrieve accounts

This commit is contained in:
Adw8 2024-11-07 18:09:45 +05:30
parent 42d0c06b50
commit 92e7d058b0
2 changed files with 37 additions and 22 deletions

View File

@ -34,7 +34,7 @@ import { NETWORK_METHODS } from "./utils/wallet-connect/common-data";
import { COSMOS_METHODS } from "./utils/wallet-connect/COSMOSData"; import { COSMOS_METHODS } from "./utils/wallet-connect/COSMOSData";
import styles from "./styles/stylesheet"; import styles from "./styles/stylesheet";
import { Header } from "./components/Header"; import { Header } from "./components/Header";
import { WalletEmbed } from "./components/WalletEmbed"; import { WalletEmbed } from "./screens/WalletEmbed";
const Stack = createStackNavigator<StackParamsList>(); const Stack = createStackNavigator<StackParamsList>();
@ -317,6 +317,9 @@ const App = (): React.JSX.Element => {
<Stack.Screen <Stack.Screen
name="WalletEmbed" name="WalletEmbed"
component={WalletEmbed} component={WalletEmbed}
options={{
header: () => <></>,
}}
/> />
</Stack.Navigator> </Stack.Navigator>
<PairingModal <PairingModal

View File

@ -1,26 +1,32 @@
import { useEffect } from 'react'; import { useCallback, useEffect } from 'react';
import { createWallet } from '../utils/accounts';
import { createWallet, retrieveAccounts } from '../utils/accounts';
import { useNetworks } from '../context/NetworksContext'; import { useNetworks } from '../context/NetworksContext';
export const WalletEmbed = () => { export const WalletEmbed = () => {
const { networksData } = useNetworks(); const { networksData } = useNetworks();
const getAccountsData = (chainId: string): string => { const getAccountsData = useCallback(async (chainId: string) => {
const accountCount = localStorage.getItem(`addAccountCounter/cosmos:${chainId}`); const targetNetwork = networksData.find(network => network.chainId === "laconic-testnet-2");
if (!accountCount) return '';
let accountsData = ''; if (!targetNetwork) {
for (let i = 0; i < Number(accountCount); i++) { return '';
const account = localStorage.getItem(`accounts/cosmos:${chainId}/${i}`);
if (account) accountsData += `${account},`;
} }
return accountsData.slice(0, -1); // Remove trailing comma const accounts = await retrieveAccounts(targetNetwork);
};
if (!accounts || accounts.length === 0) {
return '';
}
const accountsData = accounts.map(account => account.address).join(',');
return accountsData;
}, [networksData]);
const getAddressesFromData = (accountsData: string): string[] => { const getAddressesFromData = (accountsData: string): string[] => {
return accountsData return accountsData
? accountsData.split(',').filter((_, index) => (index + 1) % 4 === 0) ? accountsData.split(',')
: []; : [];
}; };
@ -34,12 +40,12 @@ export const WalletEmbed = () => {
}; };
useEffect(() => { useEffect(() => {
const handleGetAccounts = (event: MessageEvent) => { const handleGetAccounts = async (event: MessageEvent) => {
if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return; if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return;
const accountsData = getAccountsData(event.data.chainId); const accountsData = await getAccountsData(event.data.chainId);
if (!accountsData) { if (!accountsData) {
sendMessage(event.source as Window, 'ERROR', 'Wallet accounts not found in local storage', event.origin); sendMessage(event.source as Window, 'ERROR', 'Wallet accounts not found', event.origin);
return; return;
} }
@ -48,21 +54,24 @@ export const WalletEmbed = () => {
}; };
window.addEventListener('message', handleGetAccounts); window.addEventListener('message', handleGetAccounts);
return () => window.removeEventListener('message', handleGetAccounts);
}, []); return () => {
window.removeEventListener('message', handleGetAccounts);
};
}, [getAccountsData]);
useEffect(() => { useEffect(() => {
const handleCreateAccounts = async (event: MessageEvent) => { const handleCreateAccounts = async (event: MessageEvent) => {
if (event.data.type !== 'REQUEST_CREATE_OR_GET_ACCOUNTS') return; if (event.data.type !== 'REQUEST_CREATE_OR_GET_ACCOUNTS') return;
let accountsData = getAccountsData(event.data.chainId); let accountsData = await getAccountsData(event.data.chainId);
if (!accountsData) { if (!accountsData) {
console.log("Accounts not found, creating wallet..."); console.log("Accounts not found, creating wallet...");
await createWallet(networksData); await createWallet(networksData);
// Re-fetch newly created accounts // Re-fetch newly created accounts
accountsData = getAccountsData(event.data.chainId); accountsData = await getAccountsData(event.data.chainId);
} }
const addresses = getAddressesFromData(accountsData); const addresses = getAddressesFromData(accountsData);
@ -70,8 +79,11 @@ export const WalletEmbed = () => {
}; };
window.addEventListener('message', handleCreateAccounts); window.addEventListener('message', handleCreateAccounts);
return () => window.removeEventListener('message', handleCreateAccounts);
}, [networksData]); return () => {
window.removeEventListener('message', handleCreateAccounts);
};
}, [networksData, getAccountsData]);
return null; return null;
}; };