From c70de0f5fc4ea355e55c5c12b83224e0e5ffa37c Mon Sep 17 00:00:00 2001 From: Francis Rodriguez <39339295+Freshenext@users.noreply.github.com> Date: Wed, 13 Sep 2023 09:46:25 -0300 Subject: [PATCH] US-1879 Added button in wc screen to paste uri (#731) * US-1879 Added button in wc screen to paste uri * Added uri validation * Changed to form vals * Removed async * refactor: remove unnecessary functions, unify the submition of uri * chore: dapps_wc_label translation * fix: not able to edit uri input --------- Co-authored-by: Alexander Evchenko --- package.json | 2 +- src/lib/i18n.ts | 6 + .../walletConnect/WalletConnect2Context.tsx | 24 +++- src/screens/walletConnect/index.tsx | 59 ++++++++- yarn.lock | 112 +++++++++++------- 5 files changed, 154 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 9b6b1d913..ca640e2cb 100644 --- a/package.json +++ b/package.json @@ -51,7 +51,7 @@ "@walletconnect/jsonrpc-types": "^1.0.3", "@walletconnect/react-native-compat": "^2.9.0", "@walletconnect/utils": "^2.9.0", - "@walletconnect/web3wallet": "^1.8.6", + "@walletconnect/web3wallet": "^1.9.0", "axios": "^0.27.2", "buffer": "^4.9.2", "deprecated-react-native-prop-types": "^2.3.0", diff --git a/src/lib/i18n.ts b/src/lib/i18n.ts index ea266e540..e28deb258 100644 --- a/src/lib/i18n.ts +++ b/src/lib/i18n.ts @@ -364,6 +364,12 @@ const resources = { dapps_session_rejected: 'Session rejected', dapps_error_pairing_title: 'Error when trying to connect to Dapp', dapps_error_pairing_message: 'Please refresh the QR and try again.', + dapps_wc_connect: 'Connect', + dapps_wc_label: 'WC URI', + dapps_insert_wc_uri: 'Insert WC URI', + dapps_uri_not_valid_title: 'Invalid URI', + dapps_uri_not_valid_message: + 'URI is not valid. Please try with a new URI.', }, }, es: { diff --git a/src/screens/walletConnect/WalletConnect2Context.tsx b/src/screens/walletConnect/WalletConnect2Context.tsx index 9993480d0..683244683 100644 --- a/src/screens/walletConnect/WalletConnect2Context.tsx +++ b/src/screens/walletConnect/WalletConnect2Context.tsx @@ -1,5 +1,5 @@ import { createContext, ReactElement, useEffect, useState } from 'react' -import { getSdkError } from '@walletconnect/utils' +import { getSdkError, parseUri } from '@walletconnect/utils' import Web3Wallet, { Web3WalletTypes } from '@walletconnect/web3wallet' import { IWeb3Wallet } from '@walletconnect/web3wallet' import { WalletConnectAdapter } from '@rsksmart/rif-wallet-adapters' @@ -49,6 +49,17 @@ const onSessionReject = async ( } } +const isWcUriValid = (uri: string): boolean => { + const { topic, protocol, version } = parseUri(uri) + if (version !== 2) { + return false + } + if (protocol !== 'wc') { + return false + } + return topic.length !== 0 +} + export type SessionStruct = Awaited> interface PendingSession { @@ -172,7 +183,15 @@ export const WalletConnect2Provider = ({ const web3wallet = await createWeb3Wallet() subscribeToEvents(web3wallet) // Refer to https://docs.walletconnect.com/2.0/reactnative/web3wallet/wallet-usage#session-requests - await web3wallet.core.pairing.pair({ uri }) + + if (!isWcUriValid(uri)) { + setError({ + title: 'dapps_uri_not_valid_title', + message: 'dapps_uri_not_valid_message', + }) + } else { + await web3wallet.core.pairing.pair({ uri }) + } } catch (e) { // This will handle: "Pairing already exists:" if (e instanceof Error || typeof e === 'string') { @@ -200,7 +219,6 @@ export const WalletConnect2Provider = ({ // } } } - console.log(e) } } diff --git a/src/screens/walletConnect/index.tsx b/src/screens/walletConnect/index.tsx index 344ae4e4c..05f09d9a1 100644 --- a/src/screens/walletConnect/index.tsx +++ b/src/screens/walletConnect/index.tsx @@ -1,9 +1,19 @@ import { useIsFocused } from '@react-navigation/native' import { ComponentType, useContext, useEffect, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' -import { Alert, Image, ScrollView, StyleSheet, View } from 'react-native' +import { + Alert, + Image, + KeyboardAvoidingView, + Platform, + ScrollView, + StyleSheet, + View, +} from 'react-native' +import { FormProvider, useForm } from 'react-hook-form' +import Clipboard from '@react-native-community/clipboard' -import { Typography } from 'components/index' +import { AppButton, Input, Typography } from 'components/index' import { ConfirmationModal } from 'components/modal' import { rootTabsRouteNames, @@ -116,8 +126,24 @@ export const WalletConnectScreen = ({ route }: Props) => { } }, [dispatch, isFocused]) + const methods = useForm({ defaultValues: { wcUri: '' } }) + const { watch, setValue } = methods + const wcUri = watch('wcUri') + + const handlePaste = async () => { + const clipboardText = await Clipboard.getString() + setValue('wcUri', clipboardText) + } + + const onUriSubmitted = () => { + onCreateNewSession(wcUri) + } + return ( - + {t('dapps_title')} @@ -176,7 +202,30 @@ export const WalletConnectScreen = ({ route }: Props) => { onCancel={() => setDisconnectingWC(null)} /> ) : null} - + {/* Insert WC URI Manually */} + + + + + ) } @@ -201,7 +250,7 @@ const styles = StyleSheet.create({ flex: 4, alignSelf: 'center', width: '80%', - resizeMode: 'contain', + resizeMode: 'center', }), dappsList: castStyle.view({ flex: 1, diff --git a/yarn.lock b/yarn.lock index 7ece3fe12..a9a486c2b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2327,7 +2327,7 @@ "@stablelib/constant-time" "^1.0.1" "@stablelib/wipe" "^1.0.1" -"@stablelib/random@1.0.2", "@stablelib/random@^1.0.1", "@stablelib/random@^1.0.2": +"@stablelib/random@^1.0.1", "@stablelib/random@^1.0.2": version "1.0.2" resolved "https://registry.yarnpkg.com/@stablelib/random/-/random-1.0.2.tgz#2dece393636489bf7e19c51229dd7900eddf742c" integrity sha512-rIsE83Xpb7clHPVRlBj8qNe5L8ISQOzjghYQm/dZ7VaM2KHYwMW5adjQjrzTZCchFnNCNhkwtnOBa9HTMJCI8w== @@ -2856,43 +2856,43 @@ "@typescript-eslint/types" "5.42.1" eslint-visitor-keys "^3.3.0" -"@walletconnect/auth-client@2.1.0": - version "2.1.0" - resolved "https://registry.yarnpkg.com/@walletconnect/auth-client/-/auth-client-2.1.0.tgz#47b794cf807d6211fe3a87531f7fca7c6838fd3c" - integrity sha512-k6zZLEdlBpYIvbOL5tBWd+3DUJ2R4VFDyHpdp4TuRzC//njRkIzRSksEnsr8gN8P+IKuoJTLPsDy2sWR4qVTNQ== +"@walletconnect/auth-client@2.1.1": + version "2.1.1" + resolved "https://registry.yarnpkg.com/@walletconnect/auth-client/-/auth-client-2.1.1.tgz#45548fc5d5e5ac155503d1b42ac97a96a2cba98d" + integrity sha512-rFGBG3pLkmwCc5DcL9JRCsvOAmPjUcHGxm+KlX31yXNOT1QACT8Gyd8ODSOmtvz5CXZS5dPWBuvO03LUSRbPkw== dependencies: "@ethersproject/hash" "^5.7.0" "@ethersproject/transactions" "^5.7.0" - "@stablelib/random" "1.0.2" + "@stablelib/random" "^1.0.2" "@stablelib/sha256" "^1.0.1" - "@walletconnect/core" "^2.7.2" + "@walletconnect/core" "^2.9.0" "@walletconnect/events" "^1.0.1" - "@walletconnect/heartbeat" "^1.2.0" - "@walletconnect/jsonrpc-utils" "^1.0.7" + "@walletconnect/heartbeat" "^1.2.1" + "@walletconnect/jsonrpc-utils" "^1.0.8" "@walletconnect/logger" "^2.0.1" "@walletconnect/time" "^1.0.2" - "@walletconnect/utils" "^2.7.2" + "@walletconnect/utils" "^2.9.0" events "^3.3.0" isomorphic-unfetch "^3.1.0" -"@walletconnect/core@2.9.0", "@walletconnect/core@^2.7.2": - version "2.9.0" - resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.9.0.tgz#7837a5d015a22b48d35b987bcde2aa9ccdf300d8" - integrity sha512-MZYJghS9YCvGe32UOgDj0mCasaOoGHQaYXWeQblXE/xb8HuaM6kAWhjIQN9P+MNp5QP134BHP5olQostcCotXQ== +"@walletconnect/core@2.10.0", "@walletconnect/core@^2.9.0": + version "2.10.0" + resolved "https://registry.yarnpkg.com/@walletconnect/core/-/core-2.10.0.tgz#b659de4dfb374becd938964abd4f2150d410e617" + integrity sha512-Z8pdorfIMueuiBXLdnf7yloiO9JIiobuxN3j0OTal+MYc4q5/2O7d+jdD1DAXbLi1taJx3x60UXT/FPVkjIqIQ== dependencies: "@walletconnect/heartbeat" "1.2.1" "@walletconnect/jsonrpc-provider" "1.0.13" "@walletconnect/jsonrpc-types" "1.0.3" "@walletconnect/jsonrpc-utils" "1.0.8" - "@walletconnect/jsonrpc-ws-connection" "1.0.12" + "@walletconnect/jsonrpc-ws-connection" "1.0.13" "@walletconnect/keyvaluestorage" "^1.0.2" "@walletconnect/logger" "^2.0.1" "@walletconnect/relay-api" "^1.0.9" "@walletconnect/relay-auth" "^1.0.4" "@walletconnect/safe-json" "^1.0.2" "@walletconnect/time" "^1.0.2" - "@walletconnect/types" "2.9.0" - "@walletconnect/utils" "2.9.0" + "@walletconnect/types" "2.10.0" + "@walletconnect/utils" "2.10.0" events "^3.3.0" lodash.isequal "4.5.0" uint8arrays "^3.1.0" @@ -2912,7 +2912,7 @@ keyvaluestorage-interface "^1.0.0" tslib "1.14.1" -"@walletconnect/heartbeat@1.2.1", "@walletconnect/heartbeat@^1.2.0": +"@walletconnect/heartbeat@1.2.1", "@walletconnect/heartbeat@^1.2.1": version "1.2.1" resolved "https://registry.yarnpkg.com/@walletconnect/heartbeat/-/heartbeat-1.2.1.tgz#afaa3a53232ae182d7c9cff41c1084472d8f32e9" integrity sha512-yVzws616xsDLJxuG/28FqtZ5rzrTA4gUjdEMTbWB5Y8V1XHRmqq4efAxCw5ie7WjbXFSUyBHaWlMR+2/CpQC5Q== @@ -2938,7 +2938,7 @@ keyvaluestorage-interface "^1.0.0" tslib "1.14.1" -"@walletconnect/jsonrpc-utils@1.0.8", "@walletconnect/jsonrpc-utils@^1.0.6", "@walletconnect/jsonrpc-utils@^1.0.7", "@walletconnect/jsonrpc-utils@^1.0.8": +"@walletconnect/jsonrpc-utils@1.0.8", "@walletconnect/jsonrpc-utils@^1.0.6", "@walletconnect/jsonrpc-utils@^1.0.8": version "1.0.8" resolved "https://registry.yarnpkg.com/@walletconnect/jsonrpc-utils/-/jsonrpc-utils-1.0.8.tgz#82d0cc6a5d6ff0ecc277cb35f71402c91ad48d72" integrity sha512-vdeb03bD8VzJUL6ZtzRYsFMq1eZQcM3EAzT0a3st59dyLfJ0wq+tKMpmGH7HlB7waD858UWgfIcudbPFsbzVdw== @@ -2947,10 +2947,10 @@ "@walletconnect/jsonrpc-types" "^1.0.3" tslib "1.14.1" -"@walletconnect/jsonrpc-ws-connection@1.0.12": - version "1.0.12" - resolved "https://registry.yarnpkg.com/@walletconnect/jsonrpc-ws-connection/-/jsonrpc-ws-connection-1.0.12.tgz#2192314884fabdda6d0a9d22e157e5b352025ed8" - integrity sha512-HAcadga3Qjt1Cqy+qXEW6zjaCs8uJGdGQrqltzl3OjiK4epGZRdvSzTe63P+t/3z+D2wG+ffEPn0GVcDozmN1w== +"@walletconnect/jsonrpc-ws-connection@1.0.13": + version "1.0.13" + resolved "https://registry.yarnpkg.com/@walletconnect/jsonrpc-ws-connection/-/jsonrpc-ws-connection-1.0.13.tgz#23b0cdd899801bfbb44a6556936ec2b93ef2adf4" + integrity sha512-mfOM7uFH4lGtQxG+XklYuFBj6dwVvseTt5/ahOkkmpcAEgz2umuzu7fTR+h5EmjQBdrmYyEBOWADbeaFNxdySg== dependencies: "@walletconnect/jsonrpc-utils" "^1.0.6" "@walletconnect/safe-json" "^1.0.2" @@ -3009,19 +3009,19 @@ dependencies: tslib "1.14.1" -"@walletconnect/sign-client@2.9.0": - version "2.9.0" - resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.9.0.tgz#fd3b0acb68bc8d56350f01ed70f8c6326e6e89fa" - integrity sha512-mEKc4LlLMebCe45qzqh+MX4ilQK4kOEBzLY6YJpG8EhyT45eX4JMNA7qQoYa9MRMaaVb/7USJcc4e3ZrjZvQmA== +"@walletconnect/sign-client@2.10.0": + version "2.10.0" + resolved "https://registry.yarnpkg.com/@walletconnect/sign-client/-/sign-client-2.10.0.tgz#0fee8f12821e37783099f0c7bd64e6efdfbd9d86" + integrity sha512-hbDljDS53kR/It3oXD91UkcOsT6diNnW5+Zzksm0YEfwww5dop/YfNlcdnc8+jKUhWOL/YDPNQCjzsCSNlVzbw== dependencies: - "@walletconnect/core" "2.9.0" + "@walletconnect/core" "2.10.0" "@walletconnect/events" "^1.0.1" "@walletconnect/heartbeat" "1.2.1" "@walletconnect/jsonrpc-utils" "1.0.8" "@walletconnect/logger" "^2.0.1" "@walletconnect/time" "^1.0.2" - "@walletconnect/types" "2.9.0" - "@walletconnect/utils" "2.9.0" + "@walletconnect/types" "2.10.0" + "@walletconnect/utils" "2.10.0" events "^3.3.0" "@walletconnect/time@^1.0.2": @@ -3031,6 +3031,18 @@ dependencies: tslib "1.14.1" +"@walletconnect/types@2.10.0": + version "2.10.0" + resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.10.0.tgz#5d63235b49e03d609521402a4b49627dbc4ed514" + integrity sha512-kSTA/WZnbKdEbvbXSW16Ty6dOSzOZCHnGg6JH7q1MuraalD2HuNg00lVVu7QAZ/Rj1Gn9DAkrgP5Wd5a8Xq//Q== + dependencies: + "@walletconnect/events" "^1.0.1" + "@walletconnect/heartbeat" "1.2.1" + "@walletconnect/jsonrpc-types" "1.0.3" + "@walletconnect/keyvaluestorage" "^1.0.2" + "@walletconnect/logger" "^2.0.1" + events "^3.3.0" + "@walletconnect/types@2.9.0": version "2.9.0" resolved "https://registry.yarnpkg.com/@walletconnect/types/-/types-2.9.0.tgz#6e5dfdc7212c1ec4ab49a1ec409c743e16093f72" @@ -3043,7 +3055,27 @@ "@walletconnect/logger" "^2.0.1" events "^3.3.0" -"@walletconnect/utils@2.9.0", "@walletconnect/utils@^2.7.2", "@walletconnect/utils@^2.9.0": +"@walletconnect/utils@2.10.0": + version "2.10.0" + resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.10.0.tgz#6918d12180d797b8bd4a19fb2ff128e394e181d6" + integrity sha512-9GRyEz/7CJW+G04RvrjPET5k7hOEsB9b3fF9cWDk/iDCxSWpbkU/hv/urRB36C+gvQMAZgIZYX3dHfzJWkY/2g== + dependencies: + "@stablelib/chacha20poly1305" "1.0.1" + "@stablelib/hkdf" "1.0.1" + "@stablelib/random" "^1.0.2" + "@stablelib/sha256" "1.0.1" + "@stablelib/x25519" "^1.0.3" + "@walletconnect/relay-api" "^1.0.9" + "@walletconnect/safe-json" "^1.0.2" + "@walletconnect/time" "^1.0.2" + "@walletconnect/types" "2.10.0" + "@walletconnect/window-getters" "^1.0.1" + "@walletconnect/window-metadata" "^1.0.1" + detect-browser "5.3.0" + query-string "7.1.3" + uint8arrays "^3.1.0" + +"@walletconnect/utils@^2.9.0": version "2.9.0" resolved "https://registry.yarnpkg.com/@walletconnect/utils/-/utils-2.9.0.tgz#c73925edb9fefe79021bcf028e957028f986b728" integrity sha512-7Tu3m6dZL84KofrNBcblsgpSqU2vdo9ImLD7zWimLXERVGNQ8smXG+gmhQYblebIBhsPzjy9N38YMC3nPlfQNw== @@ -3063,19 +3095,19 @@ query-string "7.1.3" uint8arrays "^3.1.0" -"@walletconnect/web3wallet@^1.8.6": - version "1.8.6" - resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.8.6.tgz#445f547111dafb1b673d71f6fef849580a14439b" - integrity sha512-HxE3Jtaxs5cKhZNULEwApeMnsQsh9SEyw4FO+lafoe9KKdc2neQlY/CnPz/S4i345/Dg+bz6BcUNXouimgz3EQ== +"@walletconnect/web3wallet@^1.9.0": + version "1.9.0" + resolved "https://registry.yarnpkg.com/@walletconnect/web3wallet/-/web3wallet-1.9.0.tgz#ad4094e1e2ed757bc75efa961121b66b2eeb4306" + integrity sha512-3uu6GbOz2uwcmVaIpijkPlReywC1GsFtwJOB1bJZOkc8wjtNmR3jUMwqxWUv8ojbmDVVWQl1HN7Sptkrmq9Xyw== dependencies: - "@walletconnect/auth-client" "2.1.0" - "@walletconnect/core" "2.9.0" + "@walletconnect/auth-client" "2.1.1" + "@walletconnect/core" "2.10.0" "@walletconnect/jsonrpc-provider" "1.0.13" "@walletconnect/jsonrpc-utils" "1.0.8" "@walletconnect/logger" "2.0.1" - "@walletconnect/sign-client" "2.9.0" - "@walletconnect/types" "2.9.0" - "@walletconnect/utils" "2.9.0" + "@walletconnect/sign-client" "2.10.0" + "@walletconnect/types" "2.10.0" + "@walletconnect/utils" "2.10.0" "@walletconnect/window-getters@^1.0.1": version "1.0.1"