From 18735040b820aebbacd0e141b50ceafb3224e641 Mon Sep 17 00:00:00 2001 From: katspaugh Date: Wed, 8 Nov 2023 15:28:25 +0100 Subject: [PATCH] Rm raw query parsing --- src/pages/wc.tsx | 9 ++++--- .../useWalletConnectSearchParamUri.test.ts | 24 ------------------- .../useWalletConnectSearchParamUri.ts | 17 ++++--------- 3 files changed, 8 insertions(+), 42 deletions(-) diff --git a/src/pages/wc.tsx b/src/pages/wc.tsx index 5beaaea17b..02c41159db 100644 --- a/src/pages/wc.tsx +++ b/src/pages/wc.tsx @@ -1,9 +1,9 @@ import { useEffect } from 'react' import type { NextPage } from 'next' import { useRouter } from 'next/router' -import { parse } from 'querystring' import useLastSafe from '@/hooks/useLastSafe' import { AppRoutes } from '@/config/routes' +import { WC_URI_SEARCH_PARAM } from '@/services/walletconnect/useWalletConnectSearchParamUri' const WcPage: NextPage = () => { const router = useRouter() @@ -14,8 +14,7 @@ const WcPage: NextPage = () => { return } - // Don't use router.query because it cuts off internal paramters of the WC URI (e.g. symKey) - const { uri } = parse(window.location.search.slice(1)) + const { uri } = router.query router.replace( lastSafe @@ -23,13 +22,13 @@ const WcPage: NextPage = () => { pathname: AppRoutes.home, query: { safe: lastSafe, - wc: uri, + [WC_URI_SEARCH_PARAM]: uri, }, } : { pathname: AppRoutes.welcome, query: { - wc: uri, + [WC_URI_SEARCH_PARAM]: uri, }, }, ) diff --git a/src/services/walletconnect/__tests__/useWalletConnectSearchParamUri.test.ts b/src/services/walletconnect/__tests__/useWalletConnectSearchParamUri.test.ts index f9ca59c688..584515b4cf 100644 --- a/src/services/walletconnect/__tests__/useWalletConnectSearchParamUri.test.ts +++ b/src/services/walletconnect/__tests__/useWalletConnectSearchParamUri.test.ts @@ -31,14 +31,6 @@ describe('useWalletConnectSearchParamUri', () => { it('should return the wc uri search param value when present', () => { mockRouter.query = { wc: 'wc:123' } - Object.defineProperty(window, 'location', { - writable: true, - value: { - pathname: window.location.pathname, - search: '?wc=wc:123', - }, - }) - const { result } = renderHook(() => useWalletConnectSearchParamUri()) const [wcUri] = result.current @@ -49,14 +41,6 @@ describe('useWalletConnectSearchParamUri', () => { mockRouter.pathname = '/test' mockRouter.query = { test: 'example', wc: 'wc:123' } - Object.defineProperty(window, 'location', { - writable: true, - value: { - pathname: window.location.pathname, - search: '?wc=wc:123&test=example', - }, - }) - const { result } = renderHook(() => useWalletConnectSearchParamUri()) const [wcUri, setWcUri] = result.current @@ -77,14 +61,6 @@ describe('useWalletConnectSearchParamUri', () => { mockRouter.pathname = '/test' mockRouter.query = { test: 'example', wc: 'wc:123' } - Object.defineProperty(window, 'location', { - writable: true, - value: { - pathname: window.location.pathname, - search: '?wc=wc:123&test=example', - }, - }) - const { result } = renderHook(() => useWalletConnectSearchParamUri()) const [wcUri, setWcUri] = result.current diff --git a/src/services/walletconnect/useWalletConnectSearchParamUri.ts b/src/services/walletconnect/useWalletConnectSearchParamUri.ts index 555502ca87..41ed350120 100644 --- a/src/services/walletconnect/useWalletConnectSearchParamUri.ts +++ b/src/services/walletconnect/useWalletConnectSearchParamUri.ts @@ -1,20 +1,11 @@ import { useRouter } from 'next/router' -import { parse } from 'querystring' -import { useCallback, useEffect, useState } from 'react' +import { useCallback } from 'react' -const WC_URI_SEARCH_PARAM = 'wc' +export const WC_URI_SEARCH_PARAM = 'wc' export function useWalletConnectSearchParamUri(): [string | null, (wcUri: string | null) => void] { const router = useRouter() - const wcQuery = router.query[WC_URI_SEARCH_PARAM] - const [rawUrlParam, setRawUrlParam] = useState('') - - useEffect(() => { - // Don't use router.query because it cuts off internal paramters of the WC URI (e.g. symKey) - const query = parse(window.location.search.slice(1)) - const wcUri = query[WC_URI_SEARCH_PARAM] || '' - setRawUrlParam(wcUri.toString()) - }, [wcQuery]) + const wcUri = (router.query[WC_URI_SEARCH_PARAM] || '').toString() || null const setWcUri = useCallback( (wcUri: string | null) => { @@ -34,5 +25,5 @@ export function useWalletConnectSearchParamUri(): [string | null, (wcUri: string [router], ) - return [rawUrlParam || null, setWcUri] + return [wcUri, setWcUri] }