Skip to content

Commit 804a0e8

Browse files
authored
Fix: WalletConnect v1 Switch Accounts and bump V2 package support (#1644)
* Working for switching chains and accounts * Cleanup on walletconnect v1 and standardize accounts to lowercase to match other wallets * Revert demo changes for testing
1 parent 119e105 commit 804a0e8

File tree

4 files changed

+158
-105
lines changed

4 files changed

+158
-105
lines changed

packages/demo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"@web3-onboard/torus": "^2.2.3",
4646
"@web3-onboard/taho": "^2.0.2",
4747
"@web3-onboard/web3auth": "^2.2.1",
48-
"@web3-onboard/walletconnect": "^2.3.5",
48+
"@web3-onboard/walletconnect": "^2.3.6-alpha.1",
4949
"@web3-onboard/enkrypt": "^2.0.2",
5050
"@web3-onboard/mew-wallet": "^2.0.1",
5151
"@web3-onboard/xdefi": "^2.0.2",

packages/walletconnect/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@web3-onboard/walletconnect",
3-
"version": "2.3.5",
3+
"version": "2.3.6-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",
@@ -61,7 +61,7 @@
6161
},
6262
"dependencies": {
6363
"@ethersproject/providers": "^5.5.0",
64-
"@walletconnect/ethereum-provider": "2.5.2",
64+
"@walletconnect/ethereum-provider": "2.7.0",
6565
"@walletconnect/client": "^1.8.0",
6666
"@walletconnect/qrcode-modal": "^1.8.0",
6767
"@web3modal/standalone":"^2.2.2",

packages/walletconnect/src/v1.ts

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import type {
33
Chain,
44
ProviderAccounts,
55
WalletInit,
6-
EIP1193Provider
6+
EIP1193Provider,
7+
ChainId,
8+
AccountAddress
79
} from '@web3-onboard/common'
810
import { isHexString, WalletConnectOptions } from './index.js'
911

@@ -86,24 +88,31 @@ function walletConnect(
8688
this.chains = chains
8789
this.disconnected$ = new Subject()
8890
this.providers = {}
91+
let activeChain: ChainId
8992

9093
// listen for session updates
9194
fromEvent(this.connector, 'session_update', (error, payload) => {
9295
if (error) {
9396
throw error
9497
}
95-
9698
return payload
9799
})
98100
.pipe(takeUntil(this.disconnected$))
99101
.subscribe({
100102
next: ({ params }) => {
101103
const [{ accounts, chainId }] = params
102-
this.emit('accountsChanged', accounts)
104+
const lowerCaseAccounts = accounts.map(
105+
(accountAddress: AccountAddress) =>
106+
accountAddress.toLowerCase()
107+
)
108+
this.emit('accountsChanged', lowerCaseAccounts)
103109
const hexChainId = isHexString(chainId)
104110
? chainId
105111
: `0x${chainId.toString(16)}`
106-
this.emit('chainChanged', hexChainId)
112+
if (!activeChain || activeChain !== hexChainId) {
113+
this.emit('chainChanged', hexChainId)
114+
activeChain = hexChainId
115+
}
107116
},
108117
error: console.warn
109118
})
@@ -138,6 +147,34 @@ function walletConnect(
138147

139148
if (method === 'eth_requestAccounts') {
140149
return new Promise<ProviderAccounts>((resolve, reject) => {
150+
// Subscribe to connection events
151+
fromEvent(this.connector, 'connect', (error, payload) => {
152+
if (error) {
153+
throw error
154+
}
155+
156+
return payload
157+
})
158+
.pipe(take(1))
159+
.subscribe({
160+
next: ({ params }) => {
161+
const [{ accounts, chainId }] = params
162+
const lowerCaseAccounts = accounts.map(
163+
(accountAddress: AccountAddress) =>
164+
accountAddress.toLowerCase()
165+
)
166+
this.emit('accountsChanged', lowerCaseAccounts)
167+
const hexChainId = isHexString(chainId)
168+
? chainId
169+
: `0x${chainId.toString(16)}`
170+
if (!activeChain) activeChain = hexChainId
171+
this.emit('chainChanged', hexChainId)
172+
QRCodeModal.close()
173+
resolve(lowerCaseAccounts)
174+
},
175+
error: reject
176+
})
177+
141178
// Check if connection is already established
142179
if (!this.connector.connected) {
143180
// create new session
@@ -165,32 +202,15 @@ function walletConnect(
165202
const hexChainId = isHexString(chainId)
166203
? chainId
167204
: `0x${chainId.toString(16)}`
205+
168206
this.emit('chainChanged', hexChainId)
169-
return resolve(accounts)
207+
if (!activeChain) activeChain = hexChainId as ChainId
208+
const lowerCaseAccounts = accounts.map(
209+
(accountAddress: AccountAddress) =>
210+
accountAddress.toLowerCase()
211+
)
212+
return resolve(lowerCaseAccounts)
170213
}
171-
172-
// Subscribe to connection events
173-
fromEvent(this.connector, 'connect', (error, payload) => {
174-
if (error) {
175-
throw error
176-
}
177-
178-
return payload
179-
})
180-
.pipe(take(1))
181-
.subscribe({
182-
next: ({ params }) => {
183-
const [{ accounts, chainId }] = params
184-
this.emit('accountsChanged', accounts)
185-
const hexChainId = isHexString(chainId)
186-
? chainId
187-
: `0x${chainId.toString(16)}`
188-
this.emit('chainChanged', hexChainId)
189-
QRCodeModal.close()
190-
resolve(accounts)
191-
},
192-
error: reject
193-
})
194214
})
195215
}
196216

@@ -283,7 +303,6 @@ function walletConnect(
283303
currentChain.rpcUrl
284304
)
285305
}
286-
287306
return this.providers[chainId].send(
288307
method,
289308
// @ts-ignore

0 commit comments

Comments
 (0)