diff --git a/apps/laboratory/src/components/Wagmi/WagmiModalInfo.tsx b/apps/laboratory/src/components/Wagmi/WagmiModalInfo.tsx index fd37aadfdf..227db333c8 100644 --- a/apps/laboratory/src/components/Wagmi/WagmiModalInfo.tsx +++ b/apps/laboratory/src/components/Wagmi/WagmiModalInfo.tsx @@ -13,9 +13,9 @@ export function WagmiModalInfo() { async function getClientId() { if (connector?.type === 'walletConnect') { const provider = await connector?.getProvider?.() - const ethereumProvider = provider as UniversalProvider + const universalProvider = provider as UniversalProvider - return ethereumProvider?.client?.core?.crypto?.getClientId() + return universalProvider?.client?.core?.crypto?.getClientId() } return null diff --git a/packages/appkit/src/client.ts b/packages/appkit/src/client.ts index 25e51b84b5..c2fa3a01ef 100644 --- a/packages/appkit/src/client.ts +++ b/packages/appkit/src/client.ts @@ -480,28 +480,21 @@ export class AppKit { } private setMetadata(options: AppKitOptions) { - options.metadata ||= { - name: - typeof document === 'undefined' - ? '' - : document.getElementsByTagName('title')[0]?.textContent || '', + if (typeof window === 'undefined' || !window.document) { + return + } + + options.metadata = { + name: document.getElementsByTagName('title')[0]?.textContent || '', description: - typeof document === 'undefined' - ? '' - : document.querySelector('meta[property="og:description"]')?.content || - '', - url: typeof window === 'undefined' ? '' : window.location.origin, - icons: [ - typeof document === 'undefined' - ? '' - : document.querySelector('link[rel~="icon"]')?.href || '' - ] + document.querySelector('meta[property="og:description"]')?.content || '', + url: window.location.origin, + icons: [document.querySelector('link[rel~="icon"]')?.href || ''] } } private extendCaipNetworks(options: AppKitOptions) { - options.networks = CaipNetworksUtil.extendCaipNetworks({ - caipNetworks: options.networks, + options.networks = CaipNetworksUtil.extendCaipNetworks(options.networks, { networkImageIds: PresetsUtil.NetworkImageIds, customNetworkImageUrls: options.chainImages, projectId: options.projectId @@ -524,26 +517,20 @@ export class AppKit { } private setDefaultNetwork(options: AppKitOptions) { - const [extendedDefaultNetwork] = options.defaultNetwork - ? CaipNetworksUtil.extendCaipNetworks({ - caipNetworks: [options.defaultNetwork], + const extendedDefaultNetwork = options.defaultNetwork + ? CaipNetworksUtil.extendCaipNetwork(options.defaultNetwork, { networkImageIds: PresetsUtil.NetworkImageIds, customNetworkImageUrls: options.chainImages, projectId: options.projectId }) - : [undefined] + : undefined const previousNetwork = SafeLocalStorage.getItem(SafeLocalStorageKeys.ACTIVE_CAIP_NETWORK_ID) const caipNetwork = previousNetwork ? options.networks.find(n => n.id === previousNetwork) : undefined - if (caipNetwork) { - ChainController.setActiveCaipNetwork(caipNetwork) - } else if (extendedDefaultNetwork) { - ChainController.setActiveCaipNetwork(extendedDefaultNetwork) - } else { - ChainController.setActiveCaipNetwork(options.networks[0]) - } + const network = caipNetwork ?? extendedDefaultNetwork ?? options.networks[0] + ChainController.setActiveCaipNetwork(network) } private async initOrContinue() { diff --git a/packages/common/src/utils/CaipNetworksUtil.ts b/packages/common/src/utils/CaipNetworksUtil.ts index 163b541ca7..7c968554f7 100644 --- a/packages/common/src/utils/CaipNetworksUtil.ts +++ b/packages/common/src/utils/CaipNetworksUtil.ts @@ -2,6 +2,12 @@ import type { CaipNetwork } from './TypeUtil.js' const RPC_URL_HOST = 'rpc.walletconnect.org' +type ExtendCaipNetworkParams = { + networkImageIds: Record + customNetworkImageUrls: Record | undefined + projectId: string +} + export const CaipNetworksUtil = { /** * Extends the RPC URL with the project ID if the RPC URL is a Reown URL @@ -25,30 +31,45 @@ export const CaipNetworksUtil = { }, /** - * Extends the CaipNetwork objects with the image ID and image URL if the image ID is not provided + * Extends the CaipNetwork object with the image ID and image URL if the image ID is not provided * @param params - The parameters object - * @param params.caipNetworks - The array of CaipNetwork objects to extend + * @param params.caipNetwork - The CaipNetwork object to extend * @param params.networkImageIds - The network image IDs * @param params.customNetworkImageUrls - The custom network image URLs * @param params.projectId - The project ID * @returns The extended array of CaipNetwork objects */ - extendCaipNetworks({ - caipNetworks, - networkImageIds, - customNetworkImageUrls, - projectId - }: { - caipNetworks: CaipNetwork[] - networkImageIds: Record - customNetworkImageUrls: Record | undefined - projectId: string - }): CaipNetwork[] { - return caipNetworks.map(caipNetwork => ({ + extendCaipNetwork( + caipNetwork: CaipNetwork, + { networkImageIds, customNetworkImageUrls, projectId }: ExtendCaipNetworkParams + ): CaipNetwork { + return { ...caipNetwork, imageId: networkImageIds[caipNetwork.chainId], imageUrl: customNetworkImageUrls?.[caipNetwork.chainId], rpcUrl: CaipNetworksUtil.extendRpcUrlWithProjectId(caipNetwork.rpcUrl, projectId) - })) + } + }, + + /** + * Extends the array of CaipNetwork objects with the image ID and image URL if the image ID is not provided + * @param caipNetworks - The array of CaipNetwork objects to extend + * @param params - The parameters object + * @param params.networkImageIds - The network image IDs + * @param params.customNetworkImageUrls - The custom network image URLs + * @param params.projectId - The project ID + * @returns The extended array of CaipNetwork objects + */ + extendCaipNetworks( + caipNetworks: CaipNetwork[], + { networkImageIds, customNetworkImageUrls, projectId }: ExtendCaipNetworkParams + ): CaipNetwork[] { + return caipNetworks.map(caipNetwork => + CaipNetworksUtil.extendCaipNetwork(caipNetwork, { + networkImageIds, + customNetworkImageUrls, + projectId + }) + ) } }