From b23ac84a4ca3a76cdf41d1beccc6139d90d7725c Mon Sep 17 00:00:00 2001 From: RakeshUP Date: Mon, 23 Oct 2023 14:33:17 +0530 Subject: [PATCH 1/5] wc uri validation wip --- .../dAppValidationModal/index.tsx | 45 ++++++++++++------- .../useWalletConnectInterceptor.ts | 5 ++- src/services/walletConnectInterceptor.ts | 6 +++ src/utils/constants/regex.ts | 2 + 4 files changed, 40 insertions(+), 18 deletions(-) diff --git a/src/containers/walletConnect/dAppValidationModal/index.tsx b/src/containers/walletConnect/dAppValidationModal/index.tsx index 30e9613e4..4ee941f4a 100644 --- a/src/containers/walletConnect/dAppValidationModal/index.tsx +++ b/src/containers/walletConnect/dAppValidationModal/index.tsx @@ -60,7 +60,7 @@ const WCdAppValidation: React.FC = props => { ConnectionState.WAITING ); - const {wcConnect, sessions} = useWalletConnectContext(); + const {wcConnect, validateURI, sessions} = useWalletConnectContext(); const {control} = useFormContext(); const {errors} = useFormState({control}); @@ -209,27 +209,38 @@ const WCdAppValidation: React.FC = props => { ( - handleAdornmentClick(value, onChange)} - /> + <> + handleAdornmentClick(value, onChange)} + /> +
+ {error?.message && ( + + )} +
+ )} /> diff --git a/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts b/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts index e14565261..5d0a4e13a 100644 --- a/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts +++ b/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts @@ -26,6 +26,7 @@ export type VerifyConnectionOptions = { export type WcInterceptorValues = { wcConnect: (options: WcConnectOptions) => Promise; wcDisconnect: (topic: string) => Promise; + validateURI: (uri: string) => boolean; sessions: WcSession[]; actions: WcActionRequest[]; }; @@ -45,6 +46,8 @@ export function useWalletConnectInterceptor(): WcInterceptorValues { const [actions, setActions] = useState([]); + const validateURI = walletConnectInterceptor.validateURI; + const updateActiveSessions = useCallback(() => { const newSessions = walletConnectInterceptor.getActiveSessions( daoDetails?.address @@ -181,5 +184,5 @@ export function useWalletConnectInterceptor(): WcInterceptorValues { }; }, [handleRequest]); - return {wcConnect, wcDisconnect, sessions, actions}; + return {wcConnect, wcDisconnect, sessions, actions, validateURI}; } diff --git a/src/services/walletConnectInterceptor.ts b/src/services/walletConnectInterceptor.ts index 358e0516b..297a0edff 100644 --- a/src/services/walletConnectInterceptor.ts +++ b/src/services/walletConnectInterceptor.ts @@ -4,7 +4,9 @@ import Web3WalletClient, {Web3Wallet} from '@walletconnect/web3wallet'; import {AuthClientTypes} from '@walletconnect/auth-client'; import {Web3WalletTypes} from '@walletconnect/web3wallet'; import {PairingTypes, SessionTypes} from '@walletconnect/types'; +import {WC_URI_PATTERN} from 'utils/constants'; +const URI_REGEX = new RegExp(WC_URI_PATTERN); class WalletConnectInterceptor { clientMetadata: AuthClientTypes.Metadata = { name: 'Aragon DAO', @@ -21,6 +23,10 @@ class WalletConnectInterceptor { this.initClient(); } + validateURI(uri: string) { + return URI_REGEX.test(uri); + } + subscribeConnectProposal( cb: (event: Web3WalletTypes.SessionProposal) => void ) { diff --git a/src/utils/constants/regex.ts b/src/utils/constants/regex.ts index 17ba16c5d..9377a5d75 100644 --- a/src/utils/constants/regex.ts +++ b/src/utils/constants/regex.ts @@ -16,3 +16,5 @@ export const ISO_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*))(?:Z|(\+|-)([\d|:]*))?$/; export const BIGINT_PATTERN = /^\d+n$/; + +export const WC_URI_PATTERN = /^$/; From cc40d000f44fdd10b9576ea83a3ba5d6de11eb2d Mon Sep 17 00:00:00 2001 From: RakeshUP Date: Mon, 23 Oct 2023 16:15:16 +0530 Subject: [PATCH 2/5] wc uri validation done --- src/containers/walletConnect/dAppValidationModal/index.tsx | 1 - src/services/walletConnectInterceptor.ts | 3 ++- src/utils/constants/regex.ts | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/containers/walletConnect/dAppValidationModal/index.tsx b/src/containers/walletConnect/dAppValidationModal/index.tsx index 4ee941f4a..89aa1a470 100644 --- a/src/containers/walletConnect/dAppValidationModal/index.tsx +++ b/src/containers/walletConnect/dAppValidationModal/index.tsx @@ -210,7 +210,6 @@ const WCdAppValidation: React.FC = props => { name={WC_URI_INPUT_NAME} control={control} rules={{ - required: 'Invalid code', validate: validateURI, }} defaultValue="" diff --git a/src/services/walletConnectInterceptor.ts b/src/services/walletConnectInterceptor.ts index 297a0edff..ac0879997 100644 --- a/src/services/walletConnectInterceptor.ts +++ b/src/services/walletConnectInterceptor.ts @@ -24,7 +24,8 @@ class WalletConnectInterceptor { } validateURI(uri: string) { - return URI_REGEX.test(uri); + // TODO: Get crowdin key for this + return URI_REGEX.test(uri) || 'Invalid code'; } subscribeConnectProposal( diff --git a/src/utils/constants/regex.ts b/src/utils/constants/regex.ts index 9377a5d75..769037e9f 100644 --- a/src/utils/constants/regex.ts +++ b/src/utils/constants/regex.ts @@ -17,4 +17,6 @@ export const ISO_DATE_PATTERN = export const BIGINT_PATTERN = /^\d+n$/; -export const WC_URI_PATTERN = /^$/; +export const WC_URI_PATTERN = /^wc:[\w\d-]+@[12]\?([^=&\s]+=[^=&\s]+&?)+$/; + +export const SIMPLIFIED_WC_URI_PATTERN = /^wc:[\w\d-]+@[12]\?[^\s]+$/; From cecec80130c6b1105807637199a03d552e00c193 Mon Sep 17 00:00:00 2001 From: RakeshUP Date: Mon, 23 Oct 2023 18:15:08 +0530 Subject: [PATCH 3/5] lint fic --- .../walletConnectProvider/useWalletConnectInterceptor.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts b/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts index 5d0a4e13a..5a052fa1f 100644 --- a/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts +++ b/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts @@ -26,7 +26,7 @@ export type VerifyConnectionOptions = { export type WcInterceptorValues = { wcConnect: (options: WcConnectOptions) => Promise; wcDisconnect: (topic: string) => Promise; - validateURI: (uri: string) => boolean; + validateURI: (uri: string) => boolean | string; sessions: WcSession[]; actions: WcActionRequest[]; }; From 22fcfd51fde32d5d47638b540ba8f93117dc4ddf Mon Sep 17 00:00:00 2001 From: RakeshUP Date: Tue, 24 Oct 2023 18:46:28 +0530 Subject: [PATCH 4/5] review changes --- .../walletConnectProvider/useWalletConnectInterceptor.ts | 2 +- src/services/walletConnectInterceptor.ts | 2 +- src/utils/constants/regex.ts | 2 -- 3 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts b/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts index 5a052fa1f..c4dfe6e51 100644 --- a/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts +++ b/src/containers/walletConnect/walletConnectProvider/useWalletConnectInterceptor.ts @@ -26,7 +26,7 @@ export type VerifyConnectionOptions = { export type WcInterceptorValues = { wcConnect: (options: WcConnectOptions) => Promise; wcDisconnect: (topic: string) => Promise; - validateURI: (uri: string) => boolean | string; + validateURI: (uri: string) => string | undefined; sessions: WcSession[]; actions: WcActionRequest[]; }; diff --git a/src/services/walletConnectInterceptor.ts b/src/services/walletConnectInterceptor.ts index ac0879997..e02602b78 100644 --- a/src/services/walletConnectInterceptor.ts +++ b/src/services/walletConnectInterceptor.ts @@ -25,7 +25,7 @@ class WalletConnectInterceptor { validateURI(uri: string) { // TODO: Get crowdin key for this - return URI_REGEX.test(uri) || 'Invalid code'; + return URI_REGEX.test(uri) ? undefined : 'Invalid code'; } subscribeConnectProposal( diff --git a/src/utils/constants/regex.ts b/src/utils/constants/regex.ts index 769037e9f..4eb978bb2 100644 --- a/src/utils/constants/regex.ts +++ b/src/utils/constants/regex.ts @@ -18,5 +18,3 @@ export const ISO_DATE_PATTERN = export const BIGINT_PATTERN = /^\d+n$/; export const WC_URI_PATTERN = /^wc:[\w\d-]+@[12]\?([^=&\s]+=[^=&\s]+&?)+$/; - -export const SIMPLIFIED_WC_URI_PATTERN = /^wc:[\w\d-]+@[12]\?[^\s]+$/; From 9d08be83c5257c54a1f2668dad931975fc2f9160 Mon Sep 17 00:00:00 2001 From: RakeshUP Date: Tue, 31 Oct 2023 12:23:47 +0530 Subject: [PATCH 5/5] crowdin key --- src/locales/en/common.json | 3 ++- src/services/walletConnectInterceptor.ts | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/locales/en/common.json b/src/locales/en/common.json index 334102889..3d05e6d57 100644 --- a/src/locales/en/common.json +++ b/src/locales/en/common.json @@ -1320,7 +1320,8 @@ "alertCriticalDapp": "The QR code provided is not from {{dappName}}.", "alertSuccess": "{{dappName}} connected", "codeInputPlaceholder": "wc: …", - "alertCriticalQRcode": "The QR code provided is not from {{dappName}}." + "alertCriticalQRcode": "The QR code provided is not from {{dappName}}.", + "alertInvalid": "Invalid code" }, "detaildApp": { "spinnerLabel": "Listening for actions…", diff --git a/src/services/walletConnectInterceptor.ts b/src/services/walletConnectInterceptor.ts index e02602b78..297e7a204 100644 --- a/src/services/walletConnectInterceptor.ts +++ b/src/services/walletConnectInterceptor.ts @@ -5,6 +5,7 @@ import {AuthClientTypes} from '@walletconnect/auth-client'; import {Web3WalletTypes} from '@walletconnect/web3wallet'; import {PairingTypes, SessionTypes} from '@walletconnect/types'; import {WC_URI_PATTERN} from 'utils/constants'; +import {i18n} from '../../i18n.config'; const URI_REGEX = new RegExp(WC_URI_PATTERN); class WalletConnectInterceptor { @@ -25,7 +26,9 @@ class WalletConnectInterceptor { validateURI(uri: string) { // TODO: Get crowdin key for this - return URI_REGEX.test(uri) ? undefined : 'Invalid code'; + return URI_REGEX.test(uri) + ? undefined + : i18n.t('modal.dappConnect.validation.alertInvalid'); } subscribeConnectProposal(