Skip to content

Commit 6cfe072

Browse files
authored
[walletConnect v2.2.0-alpha.1]: Fix - walletConnect support for wallet_switchEthereumChain method (#1363)
* Working as expected * Bump version for release * Cleanup * Revert demo app changes, bump version for release * version bump mminor instead of patch * Refactor
1 parent c4dc9c3 commit 6cfe072

File tree

4 files changed

+58
-10
lines changed

4 files changed

+58
-10
lines changed

packages/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"@web3-onboard/torus": "^2.1.3",
3939
"@web3-onboard/trezor": "^2.3.2",
4040
"@web3-onboard/tallyho": "^2.0.1",
41-
"@web3-onboard/walletconnect": "^2.1.3",
41+
"@web3-onboard/walletconnect": "^2.2.0-alpha.1",
4242
"@web3-onboard/web3auth": "^2.1.3",
4343
"vconsole": "^3.9.5"
4444
},

packages/walletconnect/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/walletconnect",
3-
"version": "2.1.3",
3+
"version": "2.2.0-alpha.1",
44
"description": "WalletConnect SDK module for connecting to Web3-Onboard. Web3-Onboard makes it simple to connect Ethereum hardware and software wallets to your dapp. Features standardised spec compliant web3 providers for all supported wallets, framework agnostic modern javascript UI with code splitting, CSS customization, multi-chain and multi-account support, reactive wallet state subscriptions and real-time transaction state change notifications.",
55
"keywords": [
66
"Ethereum",

packages/walletconnect/src/index.ts

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ interface WalletConnectOptions {
1515
connectFirstChainId?: boolean
1616
}
1717

18+
const isHexString = (value: string | number) => {
19+
if (typeof value !== 'string' || !value.match(/^0x[0-9A-Fa-f]*$/)) {
20+
return false
21+
}
22+
23+
return true
24+
}
25+
1826
function walletConnect(options?: WalletConnectOptions): WalletInit {
1927
const {
2028
bridge = 'https://bridge.walletconnect.org',
@@ -97,7 +105,10 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
97105
next: ({ params }) => {
98106
const [{ accounts, chainId }] = params
99107
this.emit('accountsChanged', accounts)
100-
this.emit('chainChanged', `0x${chainId.toString(16)}`)
108+
const hexChainId = isHexString(chainId)
109+
? chainId
110+
: `0x${chainId.toString(16)}`
111+
this.emit('chainChanged', hexChainId)
101112
},
102113
error: console.warn
103114
})
@@ -125,7 +136,9 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
125136

126137
this.request = async ({ method, params }) => {
127138
if (method === 'eth_chainId') {
128-
return `0x${this.connector.chainId.toString(16)}`
139+
return isHexString(this.connector.chainId)
140+
? this.connector.chainId
141+
: `0x${this.connector.chainId.toString(16)}`
129142
}
130143

131144
if (method === 'eth_requestAccounts') {
@@ -154,7 +167,10 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
154167
})
155168
} else {
156169
const { accounts, chainId } = this.connector.session
157-
this.emit('chainChanged', `0x${chainId.toString(16)}`)
170+
const hexChainId = isHexString(chainId)
171+
? chainId
172+
: `0x${chainId.toString(16)}`
173+
this.emit('chainChanged', hexChainId)
158174
return resolve(accounts)
159175
}
160176

@@ -171,7 +187,10 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
171187
next: ({ params }) => {
172188
const [{ accounts, chainId }] = params
173189
this.emit('accountsChanged', accounts)
174-
this.emit('chainChanged', `0x${chainId.toString(16)}`)
190+
const hexChainId = isHexString(chainId)
191+
? chainId
192+
: `0x${chainId.toString(16)}`
193+
this.emit('chainChanged', hexChainId)
175194
QRCodeModal.close()
176195
resolve(accounts)
177196
},
@@ -180,16 +199,36 @@ function walletConnect(options?: WalletConnectOptions): WalletInit {
180199
})
181200
}
182201

183-
if (
184-
method === 'wallet_switchEthereumChain' ||
185-
method === 'eth_selectAccounts'
186-
) {
202+
if (method === 'eth_selectAccounts') {
187203
throw new ProviderRpcError({
188204
code: ProviderRpcErrorCode.UNSUPPORTED_METHOD,
189205
message: `The Provider does not support the requested method: ${method}`
190206
})
191207
}
192208

209+
if (method == 'wallet_switchEthereumChain') {
210+
if (!params) {
211+
throw new ProviderRpcError({
212+
code: ProviderRpcErrorCode.INVALID_PARAMS,
213+
message: `The Provider requires a chainId to be passed in as an argument`
214+
})
215+
}
216+
const chainIdObj = params[0] as { chainId?: number }
217+
if (
218+
!chainIdObj.hasOwnProperty('chainId') ||
219+
typeof chainIdObj['chainId'] === 'undefined'
220+
) {
221+
throw new ProviderRpcError({
222+
code: ProviderRpcErrorCode.INVALID_PARAMS,
223+
message: `The Provider requires a chainId to be passed in as an argument`
224+
})
225+
}
226+
return this.connector.updateSession({
227+
chainId: chainIdObj.chainId,
228+
accounts: this.connector.accounts
229+
})
230+
}
231+
193232
// @ts-ignore
194233
if (method === 'eth_sendTransaction') {
195234
// @ts-ignore

yarn.lock

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,6 +2953,15 @@
29532953
dependencies:
29542954
"@walletconnect/window-getters" "^1.0.0"
29552955

2956+
"@web3-onboard/gnosis@^2.1.3":
2957+
version "2.1.4"
2958+
resolved "https://registry.yarnpkg.com/@web3-onboard/gnosis/-/gnosis-2.1.4.tgz#8f0bfa50bf662084091c61cc7c9248bb44e0ea6d"
2959+
integrity sha512-lTAnfc3FEy6zmc/SIOI9dW0oVH23WM+Nqo8pAfjKOUrGq+ChyyA7ghhP1dUoKfJgAtblUC5MKHzPLdL+K+PYNw==
2960+
dependencies:
2961+
"@gnosis.pm/safe-apps-provider" "^0.9.2"
2962+
"@gnosis.pm/safe-apps-sdk" "^6.1.1"
2963+
"@web3-onboard/common" "^2.2.3"
2964+
29562965
"@web3auth/base-plugin@^1.0.1":
29572966
version "1.0.1"
29582967
resolved "https://registry.yarnpkg.com/@web3auth/base-plugin/-/base-plugin-1.0.1.tgz#1e2a87acf745299fdff6f92e6c46ee9bc38aa670"

0 commit comments

Comments
 (0)