Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Fix walletconnect double qr code #2547

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"@web3-react/fortmatic-connector": "^6.1.6",
"@web3-react/injected-connector": "^6.0.7",
"@web3-react/portis-connector": "^6.1.9",
"@web3-react/walletconnect-connector": "6.2.4",
"@web3-react/walletconnect-connector": "^7.0.2-alpha.0",
"@web3-react/walletlink-connector": "^6.2.12",
"ajv": "^6.12.3",
"array.prototype.flat": "^1.2.4",
Expand Down
13 changes: 12 additions & 1 deletion src/custom/components/WalletModal/WalletModalMod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import Option from 'components/WalletModal/Option'
import PendingView from 'components/WalletModal/PendingView'
import { LightCard } from 'components/Card'

import { SupportedChainId } from 'constants/chains'

export const CloseIcon = styled.div`
position: absolute;
right: 1rem;
Expand Down Expand Up @@ -209,7 +211,7 @@ export default function WalletModal({
setWalletView(WALLET_VIEWS.PENDING)

// if the connector is walletconnect and the user has already tried to connect, manually reset the connector
if (connector instanceof WalletConnectConnector && connector.walletConnectProvider?.wc?.uri) {
if (connector instanceof WalletConnectConnector) {
connector.walletConnectProvider = undefined
}

Expand All @@ -219,6 +221,15 @@ export default function WalletModal({
// const walletAddress = await connector.getAccount()
// logMonitoringEvent({ walletAddress })
// })
.then(() => {
// fix for this https://github.com/gnosis/cowswap/issues/1930
// manually set the WalletConnectConnector http.connection.url to currently connected network url
nenadV91 marked this conversation as resolved.
Show resolved Hide resolved
if (connector instanceof WalletConnectConnector) {
const { http, rpc, signer } = connector.walletConnectProvider
const chainId = signer.connection.chainId || SupportedChainId.MAINNET
http.connection.url = rpc.custom[chainId]
}
})
.catch((error) => {
if (error instanceof UnsupportedChainIdError) {
activate(connector) // a little janky...can't use setError because the connector isn't set
Expand Down
3 changes: 2 additions & 1 deletion src/custom/hooks/useWalletInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ function checkIsSupportedWallet(params: {
async function getWcPeerMetadata(connector: WalletConnectConnector): Promise<{ walletName?: string; icon?: string }> {
const provider = (await connector.getProvider()) as WalletConnectProvider

const meta = provider.walletMeta
// fix for this https://github.com/gnosis/cowswap/issues/1929
const meta = provider.walletMeta || provider.signer.connection.wc.peerMeta
if (meta) {
return {
walletName: meta.name,
Expand Down
20 changes: 20 additions & 0 deletions src/custom/hooks/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { injected, gnosisSafe, walletconnect, getProviderType, WalletProvider, f
import { isMobile } from 'utils/userAgent'

import { STORAGE_KEY_LAST_PROVIDER, WAITING_TIME_RECONNECT_LAST_PROVIDER } from 'constants/index'
import { WalletConnectConnector } from '@web3-react/walletconnect-connector'

// exports from the original file
export { useInactiveListener, useActiveWeb3React } from '@src/hooks/web3'
Expand Down Expand Up @@ -125,6 +126,25 @@ export function useEagerConnect() {
return () => timeout && clearTimeout(timeout)
}, [active])

useEffect(() => {
// fix for this https://github.com/gnosis/cowswap/issues/1923
// check if current connector is of type WalletConnect
nenadV91 marked this conversation as resolved.
Show resolved Hide resolved
if (connector instanceof WalletConnectConnector) {
const walletConnect = connector.walletConnectProvider.signer.connection.wc

// listen on disconnect events directly on WalletConnect client and close connection
// important if the connection is closed from the wallet side after page refresh
walletConnect.on('disconnect', (error: any) => {
connector.close()
localStorage.removeItem(STORAGE_KEY_LAST_PROVIDER)

if (error) {
throw error
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if the on function receives an error? u are bubling up, is this intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure where I was going with this one but I've changed it to just console log if there is an error otherwise execute rest of the that block code.

}
})
}
}, [connector])

useEffect(() => {
// add beforeunload event listener on initial component mount
window.addEventListener('beforeunload', handleBeforeUnload)
Expand Down
10 changes: 9 additions & 1 deletion src/custom/utils/signatures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import { registerOnWindow } from 'utils/misc'
// - https://www.jsonrpc.org/specification#error_object
const METAMASK_SIGNATURE_ERROR_CODE = -32603
const METHOD_NOT_FOUND_ERROR_CODE = -32601
// Added the following because of 1Inch walet who doesn't send the error code
// So we will check the actual error text
const METHOD_NOT_FOUND_ERROR_MSG_REGEX = /Method not found/i
const V4_ERROR_MSG_REGEX = /eth_signTypedData_v4 does not exist/i
const V3_ERROR_MSG_REGEX = /eth_signTypedData_v3 does not exist/i
const RPC_REQUEST_FAILED_REGEX = /RPC request failed/i
Expand Down Expand Up @@ -159,7 +162,12 @@ async function _signPayload(
try {
signature = (await signFn({ ...payload, signer: _signer, signingScheme })) as EcdsaSignature // Only ECDSA signing supported for now
} catch (e) {
if (e.code === METHOD_NOT_FOUND_ERROR_CODE || RPC_REQUEST_FAILED_REGEX.test(e.message)) {
const regexErrorCheck = [METHOD_NOT_FOUND_ERROR_MSG_REGEX, RPC_REQUEST_FAILED_REGEX].some((regex) =>
// for example 1Inch error doesn't have e.message so we will check the output of toString()
[e.message, e.toString()].some((msg) => regex.test(msg))
)

if (e.code === METHOD_NOT_FOUND_ERROR_CODE || regexErrorCheck) {
// Maybe the wallet returns the proper error code? We can only hope 🤞
// OR it failed with a generic message, there's no error code set, and we also hope it'll work
// with other methods...
Expand Down
14 changes: 7 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5205,7 +5205,7 @@
resolved "https://registry.yarnpkg.com/@walletconnect/environment/-/environment-1.0.0.tgz#c4545869fa9c389ec88c364e1a5f8178e8ab5034"
integrity sha512-4BwqyWy6KpSvkocSaV7WR3BlZfrxLbJSLkg+j7Gl6pTDE+U55lLhJvQaMuDVazXYxcjBsG09k7UlH7cGiUI5vQ==

"@walletconnect/[email protected]":
"@walletconnect/[email protected]", "@walletconnect/ethereum-provider@^1.5.4":
version "1.6.4"
resolved "https://registry.yarnpkg.com/@walletconnect/ethereum-provider/-/ethereum-provider-1.6.4.tgz#ffe3fee5aab7d0d7f02da2d31e7125e82de1ab53"
integrity sha512-QhrBcAv/XJKz+UGRChjOl/SSJfbf0+QL86KjHzPufSTIGazusjNRtXc60QdFMy3Nh5u3xLODUbjXeSYIUR9UHQ==
Expand Down Expand Up @@ -5340,7 +5340,7 @@
js-sha3 "0.8.0"
query-string "6.13.5"

"@walletconnect/web3-provider@^1.5.0", "@walletconnect/web3-provider@^1.6.6":
"@walletconnect/web3-provider@^1.6.6":
version "1.7.0"
resolved "https://registry.yarnpkg.com/@walletconnect/web3-provider/-/web3-provider-1.7.0.tgz#14d67a75555547205e9fd916a7142b1f951a143e"
integrity sha512-tX0nKVJAs1jrRmFAP8nv1h27ZttWQ/4pOW8hkH32JIB76zbsncdUOjHpnzpzgXrzMyrCzfeXeZ8dbdW4jS1KDw==
Expand Down Expand Up @@ -5416,12 +5416,12 @@
resolved "https://registry.yarnpkg.com/@web3-react/types/-/types-6.0.7.tgz#34a6204224467eedc6123abaf55fbb6baeb2809f"
integrity sha512-ofGmfDhxmNT1/P/MgVa8IKSkCStFiyvXe+U5tyZurKdrtTDFU+wJ/LxClPDtFerWpczNFPUSrKcuhfPX1sI6+A==

"@web3-react/walletconnect-connector@6.2.4":
version "6.2.4"
resolved "https://registry.yarnpkg.com/@web3-react/walletconnect-connector/-/walletconnect-connector-6.2.4.tgz#0a128699fc93ddac885935f4aeca32925f6285f0"
integrity sha512-IEVjCXrlcfVa6ggUBEyKtLRaLQuZGtT2lGuzOFtdbJJkN84u1++pzzeDrcsVhKAoS5wq33zyJT9baEbG1Aed8g==
"@web3-react/walletconnect-connector@^7.0.2-alpha.0":
version "7.0.2-alpha.0"
resolved "https://registry.yarnpkg.com/@web3-react/walletconnect-connector/-/walletconnect-connector-7.0.2-alpha.0.tgz#dacd59db626b42137a1e4f34ea23ef1f04cc8b99"
integrity sha512-Qr+AecDt5/gbAb8sFfW5kbMo0nberCAU/6AB9KmmwCm2YGEEqJrj8fW3Kin7SGxv8pgDxgXwPYsW7qMUzayXEQ==
dependencies:
"@walletconnect/web3-provider" "^1.5.0"
"@walletconnect/ethereum-provider" "^1.5.4"
"@web3-react/abstract-connector" "^6.0.7"
"@web3-react/types" "^6.0.7"
tiny-invariant "^1.0.6"
Expand Down