Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP-2549: Field validation for WC QR code input #1086

Merged
merged 5 commits into from
Oct 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 27 additions & 17 deletions src/containers/walletConnect/dAppValidationModal/index.tsx
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ const WCdAppValidation: React.FC<Props> = props => {
ConnectionState.WAITING
);

const {wcConnect, sessions} = useWalletConnectContext();
const {wcConnect, validateURI, sessions} = useWalletConnectContext();

const {control} = useFormContext();
const {errors} = useFormState({control});
@@ -209,27 +209,37 @@ const WCdAppValidation: React.FC<Props> = props => {
<Controller
name={WC_URI_INPUT_NAME}
control={control}
rules={{
validate: validateURI,
}}
defaultValue=""
render={({
field: {name, onBlur, onChange, value},
fieldState: {error},
}) => (
<WalletInputLegacy
mode={error ? 'critical' : 'default'}
name={name}
disabled={
connectionStatus === ConnectionState.LOADING ||
connectionStatus === ConnectionState.SUCCESS
}
onBlur={onBlur}
onChange={onChange}
value={value ?? ''}
placeholder={t(
'modal.dappConnect.validation.codeInputPlaceholder'
)}
adornmentText={adornmentText}
onAdornmentClick={() => handleAdornmentClick(value, onChange)}
/>
<>
<WalletInputLegacy
mode={error ? 'critical' : 'default'}
name={name}
disabled={
connectionStatus === ConnectionState.LOADING ||
connectionStatus === ConnectionState.SUCCESS
}
onBlur={onBlur}
onChange={onChange}
value={value ?? ''}
placeholder={t(
'modal.dappConnect.validation.codeInputPlaceholder'
)}
adornmentText={adornmentText}
onAdornmentClick={() => handleAdornmentClick(value, onChange)}
/>
<div className="mt-2">
{error?.message && (
<AlertInline label={error.message} mode="critical" />
)}
</div>
</>
)}
/>
</FormGroup>
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ export type VerifyConnectionOptions = {
export type WcInterceptorValues = {
wcConnect: (options: WcConnectOptions) => Promise<WcSession>;
wcDisconnect: (topic: string) => Promise<void>;
validateURI: (uri: string) => string | undefined;
sessions: WcSession[];
actions: WcActionRequest[];
};
@@ -45,6 +46,8 @@ export function useWalletConnectInterceptor(): WcInterceptorValues {

const [actions, setActions] = useState<WcActionRequest[]>([]);

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};
}
3 changes: 2 additions & 1 deletion src/locales/en/common.json
Original file line number Diff line number Diff line change
@@ -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…",
10 changes: 10 additions & 0 deletions src/services/walletConnectInterceptor.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,10 @@ 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';
import {i18n} from '../../i18n.config';

const URI_REGEX = new RegExp(WC_URI_PATTERN);
class WalletConnectInterceptor {
clientMetadata: AuthClientTypes.Metadata = {
name: 'Aragon DAO',
@@ -21,6 +24,13 @@ class WalletConnectInterceptor {
this.initClient();
}

validateURI(uri: string) {
// TODO: Get crowdin key for this
return URI_REGEX.test(uri)
? undefined
: i18n.t('modal.dappConnect.validation.alertInvalid');
}

subscribeConnectProposal(
cb: (event: Web3WalletTypes.SessionProposal) => void
) {
2 changes: 2 additions & 0 deletions src/utils/constants/regex.ts
Original file line number Diff line number Diff line change
@@ -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 = /^wc:[\w\d-]+@[12]\?([^=&\s]+=[^=&\s]+&?)+$/;