Add network if present after creating wallet

This commit is contained in:
IshaVenikar 2025-06-05 10:36:59 +05:30
parent 8d60fd9e4b
commit a0df2fd853
4 changed files with 31 additions and 44 deletions

View File

@ -45,7 +45,6 @@ import SignRequestEmbed from "./screens/SignRequestEmbed";
import useAddAccountEmbed from "./hooks/useAddAccountEmbed";
import useExportPKEmbed from "./hooks/useExportPrivateKeyEmbed";
import { SignTxEmbed } from "./screens/SignTxEmbed";
import useAddNetworkEmbed from "./hooks/useAddNetworkEmbed";
const Stack = createStackNavigator<StackParamsList>();
@ -288,7 +287,6 @@ const App = (): React.JSX.Element => {
useWebViewHandler();
useAddAccountEmbed();
useExportPKEmbed();
useAddNetworkEmbed();
return (
<Surface style={styles.appSurface}>

View File

@ -1,39 +0,0 @@
import { useEffect } from 'react';
import { addNewNetwork } from '../utils/accounts';
import { ADD_NETWORK } from '../utils/constants';
const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS;
const useAddNetworkEmbed = () => {
useEffect(() => {
const handleAddNetwork = async (event: MessageEvent) => {
if (event.data.type !== ADD_NETWORK) return;
if (!REACT_APP_ALLOWED_URLS) {
console.log('Allowed URLs are not set');
return;
}
const allowedUrls = REACT_APP_ALLOWED_URLS.split(',').map(url => url.trim());
if (!allowedUrls.includes(event.origin)) {
console.log('Unauthorized app.');
return;
}
try {
const { networkData } = event.data;
await addNewNetwork(networkData);
} catch (err) {
console.error('Error preparing sign request:', err);
}
};
window.addEventListener('message', handleAddNetwork);
return () => window.removeEventListener('message', handleAddNetwork);
}, []);
}
export default useAddNetworkEmbed;

View File

@ -1,11 +1,12 @@
import { useEffect, useCallback } from "react";
import { createWallet } from "../utils/accounts";
import { addNewNetwork, createWallet, isChainIdPresent } from "../utils/accounts";
import { sendMessage } from "../utils/misc";
import useAccountsData from "./useAccountsData";
import { useNetworks } from "../context/NetworksContext";
import { useAccounts } from "../context/AccountsContext";
import { REQUEST_CREATE_OR_GET_ACCOUNTS, WALLET_ACCOUNTS_DATA } from "../utils/constants";
import { NetworksFormData } from "../types";
const REACT_APP_ALLOWED_URLS = import.meta.env.REACT_APP_ALLOWED_URLS;
@ -15,12 +16,24 @@ const useGetOrCreateAccounts = () => {
const { setAccounts } = useAccounts();
// Wrap the function in useCallback to prevent recreation on each render
const getOrCreateAccountsForChain = useCallback(async (chainId: string) => {
const getOrCreateAccountsForChain = useCallback(async (chainId: string, networkData?: NetworksFormData) => {
let accountsData = await getAccountsData(chainId);
if (accountsData.length === 0) {
console.log("Accounts not found, creating wallet...");
await createWallet(networksData);
const isNetworkPresent = await isChainIdPresent(chainId);
if (!isNetworkPresent) {
if (!networkData || chainId !== networkData.chainId) {
console.log('Network data not available.');
return [];
}
console.log('ChainId not found. Adding network')
await addNewNetwork(networkData);
}
accountsData = await getAccountsData(chainId);
}

View File

@ -434,6 +434,20 @@ const getCosmosAccountByHDPath = async (
return { cosmosWallet, data };
};
const isChainIdPresent = async (
chainId: string,
): Promise<boolean> => {
const networks = await getInternetCredentials('networks');
if(!networks){
return false;
}
const networksData: NetworksFormData[] = JSON.parse(networks);
return networksData.some((network) => network.chainId === chainId);
}
export {
createWallet,
addAccount,
@ -448,5 +462,6 @@ export {
getNextAccountId,
updateAccountCounter,
getCosmosAccountByHDPath,
addNewNetwork
addNewNetwork,
isChainIdPresent
};