From 99b5162413d6f0fb5cc7b4e3fa221102c466af39 Mon Sep 17 00:00:00 2001 From: Raul Duarte Pereira Date: Tue, 28 May 2024 00:50:42 -0300 Subject: [PATCH] CU-86dthjvx1 - NEON2 - Investigate the problem on NWD's WC deeplink when using Web3modal --- app/containers/App/App.jsx | 56 ++++++++++++++-------- app/containers/ConnectDapp/ConnectDapp.jsx | 4 +- 2 files changed, 38 insertions(+), 22 deletions(-) diff --git a/app/containers/App/App.jsx b/app/containers/App/App.jsx index d4ed86bd4..6728e2c9e 100644 --- a/app/containers/App/App.jsx +++ b/app/containers/App/App.jsx @@ -1,5 +1,5 @@ // @flow -import React, { useEffect, useState } from 'react' +import React, { useCallback, useEffect, useState } from 'react' import { useWalletConnectWallet } from '@cityofzion/wallet-connect-sdk-wallet-react' import { ROUTES } from '../../core/constants' @@ -12,7 +12,6 @@ import styles from './App.scss' import themes from '../../themes' import ErrorBoundary from '../../components/ErrorBoundaries/Main' import FramelessNavigation from '../../components/FramelessNavigation' -import { parseQuery } from '../../core/formatters' import withSettingsContext from '../../hocs/withSettingsContext' import { getInformationFromSession } from '../../util/walletConnect' @@ -44,6 +43,10 @@ const routesWithSideBar = [ ROUTES.NFT, ] +function isWalletConnectUri(uri) { + return /^wc:.+@\d.*$/g.test(uri) +} + const App = ({ children, address, @@ -58,6 +61,24 @@ const App = ({ const { requests, sessions, disconnect } = useWalletConnectWallet() const [decodedDeeplinkUri, setDecodedDeeplinkUri] = useState(null) + const handleDeeplink = useCallback(async (uri: string) => { + await ipc.invoke('restore') + + const realUri = uri.split('uri=').pop() + if (!realUri) return + + const decodedUri = decodeURIComponent(realUri) + if (isWalletConnectUri(decodedUri)) { + setDecodedDeeplinkUri(decodedUri) + return + } + + const decodedBase64Uri = atob(decodedUri) + if (isWalletConnectUri(decodedBase64Uri)) { + setDecodedDeeplinkUri(decodedBase64Uri) + } + }, []) + useEffect(() => { async function handleUpgrade() { checkVersion() @@ -72,22 +93,22 @@ const App = ({ handleUpgrade() }, []) - useEffect(() => { - const listener = async (_event, uri) => { - await ipc.invoke('restore') - setDecodedDeeplinkUri(uri) - } + useEffect( + () => { + ipc.invoke('getInitialDeepLinkUri').then(handleDeeplink) - ipc.on('link', listener) + const listener = async (_event, uri: string) => { + handleDeeplink(uri) + } - return () => { - ipc.off('link', listener) - } - }, []) + ipc.on('link', listener) - useEffect(() => { - ipc.invoke('getInitialDeepLinkUri').then(setDecodedDeeplinkUri) - }, []) + return () => { + ipc.off('link', listener) + } + }, + [handleDeeplink], + ) useEffect( () => { @@ -100,12 +121,9 @@ const App = ({ return } - const { uri } = parseQuery(decodeURI(decodedDeeplinkUri)) - if (!uri) return - history.push({ pathname: ROUTES.CONNECT_DAPP, - state: { uri }, + state: { uri: decodedDeeplinkUri }, }) setDecodedDeeplinkUri(null) }, diff --git a/app/containers/ConnectDapp/ConnectDapp.jsx b/app/containers/ConnectDapp/ConnectDapp.jsx index 3ce913e2f..2a2931172 100644 --- a/app/containers/ConnectDapp/ConnectDapp.jsx +++ b/app/containers/ConnectDapp/ConnectDapp.jsx @@ -58,9 +58,7 @@ const ConnectDapp = ({ showErrorNotification, history }: Props) => { useEffect( () => { if (!uri) return - - const decoded = atob(uri) - handleOnURI(decoded) + handleOnURI(uri) }, [uri, handleOnURI], )