Use RPC URL from external app if the network already exists
All checks were successful
Lint and Build / Run lint and build checks (pull_request) Successful in 3m34s

This commit is contained in:
Shreerang Kale 2026-02-10 16:46:30 +05:30
parent 490f4ec8a4
commit 2f8174cce3
2 changed files with 37 additions and 2 deletions

View File

@ -1,6 +1,6 @@
import { useEffect, useCallback } from "react";
import { addNewNetwork, createWallet, checkNetworkForChainID, isWalletCreated } from "../utils/accounts";
import { addNewNetwork, createWallet, checkNetworkForChainID, isWalletCreated, updateNetworkRpcUrl } from "../utils/accounts";
import { useNetworks } from "../context/NetworksContext";
import { NETWORK_ADDED_RESPONSE, NETWORK_ADD_FAILED_RESPONSE, NETWORK_ALREADY_EXISTS_RESPONSE, REQUEST_ADD_NETWORK } from "../utils/constants";
import { NetworksFormData } from "../types";
@ -52,6 +52,11 @@ const useCreateNetwork = () => {
chainId
}, sourceOrigin);
} else {
console.log("Network already exists. Updating RPC URL");
const retrievedNetworksData = await updateNetworkRpcUrl(chainId, networkData.rpcUrl);
setNetworksData(retrievedNetworksData);
sendMessage(window.parent, NETWORK_ALREADY_EXISTS_RESPONSE, {
type: NETWORK_ALREADY_EXISTS_RESPONSE,
chainId

View File

@ -459,6 +459,35 @@ const checkNetworkForChainID = async (
return networksData.some((network) => network.chainId === chainId);
}
const updateNetworkRpcUrl = async (
chainId: string,
rpcUrl: string,
): Promise<NetworksDataState[]> => {
const networks = await getInternetCredentials('networks');
if (!networks) {
throw new Error('Networks not found');
}
const networksData: NetworksDataState[] = JSON.parse(networks);
const networkIndex = networksData.findIndex((network) => network.chainId === chainId);
if (networkIndex === -1) {
throw new Error('Network not found');
}
networksData[networkIndex].rpcUrl = rpcUrl;
await setInternetCredentials(
'networks',
'_',
JSON.stringify(networksData),
);
return networksData;
}
const isWalletCreated = async (
): Promise<boolean> => {
const mnemonicServer = await getInternetCredentials("mnemonicServer");
@ -483,5 +512,6 @@ export {
getCosmosAccountByHDPath,
addNewNetwork,
checkNetworkForChainID,
isWalletCreated
isWalletCreated,
updateNetworkRpcUrl
};