From 322d2ec9775a82de628de45df7988f3b9e281b61 Mon Sep 17 00:00:00 2001 From: Ben Kremer Date: Wed, 21 Jun 2023 12:12:51 +0200 Subject: [PATCH] fix(react-dapp-v2): ensure clientId is logged and stored --- .../src/contexts/ClientContext.tsx | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/dapps/react-dapp-v2/src/contexts/ClientContext.tsx b/dapps/react-dapp-v2/src/contexts/ClientContext.tsx index 00d0313..0fbdf0a 100644 --- a/dapps/react-dapp-v2/src/contexts/ClientContext.tsx +++ b/dapps/react-dapp-v2/src/contexts/ClientContext.tsx @@ -254,6 +254,22 @@ export function ClientContextProvider({ [session, onSessionConnected] ); + const _logClientId = useCallback(async (_client: Client) => { + if (typeof _client === "undefined") { + throw new Error("WalletConnect is not initialized"); + } + try { + const clientId = await _client.core.crypto.getClientId(); + console.log("WalletConnect ClientID: ", clientId); + localStorage.setItem("WALLETCONNECT_CLIENT_ID", clientId); + } catch (error) { + console.error( + "Failed to set WalletConnect clientId in localStorage: ", + error + ); + } + }, []); + const createClient = useCallback(async () => { try { setIsInitializing(true); @@ -269,20 +285,20 @@ export function ClientContextProvider({ prevRelayerValue.current = relayerRegion; await _subscribeToEvents(_client); await _checkPersistedState(_client); + await _logClientId(_client); } catch (err) { throw err; } finally { setIsInitializing(false); } - }, [_checkPersistedState, _subscribeToEvents, relayerRegion]); + }, [_checkPersistedState, _subscribeToEvents, _logClientId, relayerRegion]); useEffect(() => { if (!client) { - createClient() - } - else if (prevRelayerValue.current !== relayerRegion) { - client.core.relayer.restartTransport(relayerRegion) - prevRelayerValue.current = relayerRegion + createClient(); + } else if (prevRelayerValue.current !== relayerRegion) { + client.core.relayer.restartTransport(relayerRegion); + prevRelayerValue.current = relayerRegion; } }, [createClient, relayerRegion, client]);