PR #4: Wallet Connect V2 tracking branch. #28

Closed
anon wants to merge 10 commits from (deleted):wallet-connect-v2 into master
4 changed files with 856 additions and 1111 deletions
Showing only changes of commit 2054cc53c3 - Show all commits

@ -23,13 +23,17 @@
"@apollo/client": "^3.3.20", "@apollo/client": "^3.3.20",
"@metamask/onboarding": "^1.0.0", "@metamask/onboarding": "^1.0.0",
"@nuxtjs/moment": "^1.6.0", "@nuxtjs/moment": "^1.6.0",
"@walletconnect/web3-provider": "1.7.8", "@tornado/snarkjs": "0.1.20-p2",
"@tornado/websnark": "0.0.4-p1",
"@walletconnect/ethereum-provider": "^2.8.6",
"@walletconnect/modal": "^2.5.9",
"ajv": "^6.10.2", "ajv": "^6.10.2",
"arraybuffer-loader": "^1.0.8", "arraybuffer-loader": "^1.0.8",
"base-path-converter": "^1.0.2", "base-path-converter": "^1.0.2",
"bignumber.js": "^9.0.0", "bignumber.js": "^9.0.0",
"bloomfilter.js": "^1.0.2", "bloomfilter.js": "^1.0.2",
"circomlibjs": "0.1.2", "circomlibjs": "0.1.2",
"cross-env": "7.0.3",
"crypto": "^1.0.1", "crypto": "^1.0.1",
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"eth-ens-namehash": "^2.0.8", "eth-ens-namehash": "^2.0.8",
@ -53,10 +57,7 @@
"vue-clipboard2": "^0.3.1", "vue-clipboard2": "^0.3.1",
"vue-i18n": "^8.15.4", "vue-i18n": "^8.15.4",
"vuex-persistedstate": "^2.7.0", "vuex-persistedstate": "^2.7.0",
"web3": "1.5.2", "web3": "1.5.2"
"cross-env": "7.0.3",
"@tornado/snarkjs": "0.1.20-p2",
"@tornado/websnark": "0.0.4-p1"
}, },
"devDependencies": { "devDependencies": {
"@nuxtjs/eslint-config": "^1.1.2", "@nuxtjs/eslint-config": "^1.1.2",

@ -1,50 +1,69 @@
import BN from 'bignumber.js' // Imports
import WalletConnectProvider from '@walletconnect/web3-provider'
import { NETWORKS } from '@/constants' import BN from 'bignumber.js'
import { EthereumProvider } from '@walletconnect/ethereum-provider'
import networkConfig from '@/networkConfig' import networkConfig from '@/networkConfig'
const { WC_BRIDGE } = process.env // Build an array of the first RPC URL for each network in a reduce
const createRpcMapIterator = (accumulator, chainId) => {
// Get RPCs urls
const { rpcUrls } = networkConfig[`netId${chainId}`]
const WALLET_CONNECT_INTERVAL = 1000 // Choose for each network the first rpc's url
const SUPPORTED_WALLETS = ['metamask', 'trust', 'imtoken', 'genericWeb3']
const RECONNECT_TIME = 3600000 // 1 hour
const getFirstRpcUrls = (acc, netId) => {
const { rpcUrls } = networkConfig[`netId${netId}`]
const [{ url }] = Object.values(rpcUrls) const [{ url }] = Object.values(rpcUrls)
return { ...acc, [netId]: url } // Append and continue
return { ...accumulator, [chainId]: url }
} }
const RPC = NETWORKS.reduce(getFirstRpcUrls, {}) // const walletConnectInterval = 1000 // TODO: Check if we still need
const reconnectInterval = 3600000 // 1 hour
const supportedWallets = ['metamask', 'trust', 'imtoken', 'genericWeb3']
const rpcMap = networkConfig.enabledChains.reduce(createRpcMapIterator, {})
const walletConnectConnector = (chainId) => { const walletConnectConnector = async (chainId) => {
try { try {
const prevConnection = localStorage.getItem('walletconnectTimeStamp') const prevConnection = localStorage.getItem('walletconnectTimeStamp')
if (new BN(Date.now()).minus(prevConnection).isGreaterThanOrEqualTo(RECONNECT_TIME)) { if (new BN(Date.now()).minus(prevConnection).isGreaterThanOrEqualTo(reconnectInterval)) {
localStorage.removeItem('walletconnect') localStorage.removeItem('walletconnect')
} }
const provider = new WalletConnectProvider({ const optionalChains = networkConfig.enabledChains.filter((chain) => chain !== chainId)
chainId,
rpc: RPC, const provider = await EthereumProvider.init({
pollingInterval: WALLET_CONNECT_INTERVAL, projectId: process.env.WC_PROJECT_ID,
qrcodeModalOptions: { relayUrl: process.env.WC_BRIDGE,
mobileLinks: SUPPORTED_WALLETS chains: [chainId],
}, optionalChains,
bridge: WC_BRIDGE rpcMap,
methods: [
'eth_sendTransaction',
'personal_sign',
'eth_signTypedData_v4',
'eth_getEncryptionPublicKey',
'eth_decrypt',
'eth_getBalance',
'eth_getTransactionReceipt',
'eth_accounts',
'eth_chainId',
'wallet_addEthereumChain',
'wallet_switchEthereumChain'
],
showQrModal: true,
qrModalOptions: {
mobileWallets: supportedWallets
}
}) })
provider.injectedRequest = provider.enable provider.injectedRequest = provider.enable
localStorage.setItem('walletconnectTimeStamp', Date.now()) localStorage.setItem('walletconnectTimeStamp', Date.now())
return provider return provider
} catch (err) { } catch (err) {
console.log(err) console.error(err)
throw new Error('WalletConnect error: ', err) throw new Error('WalletConnectConnector error: ', err)
} }
} }

@ -402,6 +402,7 @@ const actions = {
if (providerName === 'walletConnect') { if (providerName === 'walletConnect') {
await dispatch(listener, { provider }) await dispatch(listener, { provider })
} }
const address = await this.$provider.initProvider(provider, {}) const address = await this.$provider.initProvider(provider, {})
if (!address) { if (!address) {
@ -421,8 +422,8 @@ const actions = {
await dispatch('updateAccountBalance', address) await dispatch('updateAccountBalance', address)
if (getters.isWalletConnect) { if (getters.isWalletConnect) {
if (provider.wc.peerMeta) { if (provider.signer.client.name) {
commit('SET_WALLET_NAME', provider.wc.peerMeta.name) commit('SET_WALLET_NAME', provider.signer.client.name)
} }
} }
@ -457,17 +458,16 @@ const actions = {
const { enabled } = rootState.loading const { enabled } = rootState.loading
try { try {
provider.wc.on('disconnect', (error, payload) => { provider.on('disconnect', (error, payload) => {
if (state.isReconnecting) { if (state.isReconnecting) {
console.warn('Provider reconnect payload', { payload, error, isReconnecting: state.isReconnecting }) console.warn('Provider reconnect payload', { payload, error, isReconnecting: state.isReconnecting })
if (enabled) { if (enabled) {
dispatch('loading/disable', {}, { root: true }) dispatch('loading/disable', {}, { root: true })
} }
commit('SET_RECONNECTING', false)
return
}
commit('SET_RECONNECTING', false)
} else {
const prevConnection = localStorage.getItem('walletconnectTimeStamp') const prevConnection = localStorage.getItem('walletconnectTimeStamp')
const isPrevConnection = new BN(Date.now()).minus(prevConnection).isGreaterThanOrEqualTo(5000) const isPrevConnection = new BN(Date.now()).minus(prevConnection).isGreaterThanOrEqualTo(5000)
@ -485,6 +485,7 @@ const actions = {
if (enabled) { if (enabled) {
dispatch('loading/disable', {}, { root: true }) dispatch('loading/disable', {}, { root: true })
} }
}
}) })
} catch (err) { } catch (err) {
console.error('WalletConnect listeners error: ', err) console.error('WalletConnect listeners error: ', err)

1850
yarn.lock

File diff suppressed because it is too large Load Diff