forked from wevm/references
-
Notifications
You must be signed in to change notification settings - Fork 0
/
walletConnect.ts
471 lines (419 loc) · 14.6 KB
/
walletConnect.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
import {
Chain,
ProviderRpcError,
SwitchChainError,
UserRejectedRequestError,
getClient,
normalizeChainId,
} from '@wagmi/core'
import type WalletConnectProvider from '@walletconnect/ethereum-provider'
import type {
UniversalProviderOpts,
UniversalProvider as UniversalProvider_,
} from '@walletconnect/universal-provider'
import type { Web3Modal } from '@web3modal/standalone'
import { providers } from 'ethers'
import { getAddress, hexValue } from 'ethers/lib/utils.js'
import { Connector } from './base'
// Shared config for WalletConnect v2
const defaultV2Config = {
namespace: 'eip155',
methods: [
'eth_sendTransaction',
'eth_sendRawTransaction',
'eth_sign',
'eth_signTransaction',
'eth_signTypedData',
'eth_signTypedData_v3',
'eth_signTypedData_v4',
'personal_sign',
'wallet_switchEthereumChain',
'wallet_addEthereumChain',
],
events: ['accountsChanged', 'chainChanged'],
}
type UniversalProvider = InstanceType<typeof UniversalProvider_>
type WalletConnectOptions = {
/** When `true`, uses default WalletConnect QR Code modal */
qrcode?: boolean
} & (
| ({
version?: '1'
} & ConstructorParameters<typeof WalletConnectProvider>[0])
| ({
/**
* Project ID associated with [WalletConnect account](https://cloud.walletconnect.com)
*/
projectId: NonNullable<UniversalProviderOpts['projectId']>
version: '2'
} & Omit<UniversalProviderOpts, 'projectId'>)
)
type WalletConnectSigner = providers.JsonRpcSigner
export class WalletConnectConnector extends Connector<
WalletConnectProvider | UniversalProvider,
WalletConnectOptions,
WalletConnectSigner
> {
readonly id = 'walletConnect'
readonly name = 'WalletConnect'
readonly ready = true
#provider?: WalletConnectProvider | UniversalProvider
#initUniversalProviderPromise?: Promise<void>
#web3Modal?: Web3Modal
constructor(config: { chains?: Chain[]; options: WalletConnectOptions }) {
super(config)
if (this.version === '2') {
this.#createUniversalProvider()
if (this.isQrCode) this.#createWeb3Modal()
}
}
get isQrCode() {
return this.options.qrcode !== false
}
get namespacedChains() {
return this.chains.map(
(chain) => `${defaultV2Config.namespace}:${chain.id}`,
)
}
get version() {
if ('version' in this.options) return this.options.version
return '1'
}
async connect({ chainId }: { chainId?: number } = {}) {
const isV1 = this.version === '1'
const isV2 = this.version === '2'
try {
let targetChainId = chainId
if (!targetChainId) {
const lastUsedChainId = getClient().lastUsedChainId
if (lastUsedChainId && !this.isChainUnsupported(lastUsedChainId))
targetChainId = lastUsedChainId
}
const provider = await this.getProvider({
chainId: targetChainId,
create: isV1,
})
provider.on('accountsChanged', this.onAccountsChanged)
provider.on('chainChanged', this.onChainChanged)
provider.on('disconnect', this.onDisconnect)
if (isV2) {
provider.on('session_delete', this.onDisconnect)
provider.on('display_uri', this.onDisplayUri)
const isChainsAuthorized = await this.#isChainsAuthorized()
// If there is an active session, and the chains are not authorized,
// disconnect the session.
if ((provider as UniversalProvider).session && !isChainsAuthorized)
await provider.disconnect()
// If there is not an active session, or the chains are not authorized,
// attempt to connect.
if (
!(provider as UniversalProvider).session ||
((provider as UniversalProvider).session && !isChainsAuthorized)
) {
await Promise.race([
provider.connect({
namespaces: {
[defaultV2Config.namespace]: {
methods: defaultV2Config.methods,
events: defaultV2Config.events,
chains: this.namespacedChains,
rpcMap: this.chains.reduce(
(rpc, chain) => ({
...rpc,
[chain.id]: chain.rpcUrls.default.http[0],
}),
{},
),
},
},
}),
...(this.isQrCode
? [
new Promise<void>((_resolve, reject) =>
provider.on('display_uri', async (uri: string) => {
await this.#web3Modal?.openModal({ uri })
// Modal is closed, reject promise so `catch` block for `connect` is called.
this.#web3Modal?.subscribeModal(({ open }) => {
if (!open) reject(new Error('user rejected'))
})
}),
),
]
: []),
])
// If execution reaches here, connection was successful and we can close modal.
if (this.isQrCode) this.#web3Modal?.closeModal()
}
}
// Defer message to the next tick to ensure wallet connect data (provided by `.enable()`) is available
setTimeout(() => this.emit('message', { type: 'connecting' }), 0)
const accounts = (await Promise.race([
provider.enable(),
// When using WalletConnect v1 QR Code Modal, handle user rejection request from wallet
...(isV1 && this.isQrCode
? [
new Promise((_res, reject) =>
(provider as WalletConnectProvider).connector.on(
'disconnect',
() => reject(new Error('user rejected')),
),
),
]
: []),
])) as string[]
const account = getAddress(accounts[0] as string)
const id = await this.getChainId()
const unsupported = this.isChainUnsupported(id)
// Not all WalletConnect v1 options support programmatic chain switching.
// Only enable for wallet options that do.
if (isV1) {
const walletName =
(provider as WalletConnectProvider).connector?.peerMeta?.name ?? ''
/**
* Wallets that support chain switching through WalletConnect
* - imToken (token.im)
* - MetaMask (metamask.io)
* - Omni (omni.app)
* - Rainbow (rainbow.me)
* - Trust Wallet (trustwallet.com)
*/
const switchChainAllowedRegex =
/(imtoken|metamask|omni|rainbow|trust wallet)/i
if (switchChainAllowedRegex.test(walletName))
this.switchChain = this.#switchChain
}
// In v2, chain switching is allowed programatically
// as the user approves the chains when approving the pairing
else this.switchChain = this.#switchChain
return {
account,
chain: { id, unsupported },
provider: new providers.Web3Provider(
provider as providers.ExternalProvider,
),
}
} catch (error) {
if (isV2 && this.isQrCode) this.#web3Modal?.closeModal()
// WalletConnect v1: "user closed modal"
// WalletConnect v2: "user rejected"
if (
/user closed modal|user rejected/i.test(
(error as ProviderRpcError)?.message,
)
) {
throw new UserRejectedRequestError(error)
}
throw error
}
}
async disconnect() {
const provider = await this.getProvider()
try {
await provider.disconnect()
} catch (error) {
// If the session does not exist, we don't want to throw.
if (!/No matching key/i.test((error as Error).message)) throw error
}
provider.removeListener('accountsChanged', this.onAccountsChanged)
provider.removeListener('chainChanged', this.onChainChanged)
provider.removeListener('disconnect', this.onDisconnect)
if (this.version === '1' && typeof localStorage !== 'undefined')
// Remove local storage session so user can connect again
localStorage.removeItem('walletconnect')
else {
provider.removeListener('session_delete', this.onDisconnect)
provider.removeListener('display_uri', this.onDisplayUri)
}
}
async getAccount() {
const provider = await this.getProvider()
let accounts
if (this.version === '1')
accounts = (provider as WalletConnectProvider).accounts
else
accounts = (await provider.request({
method: 'eth_accounts',
})) as string[]
// return checksum address
return getAddress(accounts[0] as string)
}
async getChainId() {
const provider = await this.getProvider()
if (this.version === '1')
return normalizeChainId((provider as WalletConnectProvider).chainId)
// WalletConnect v2 does not internally manage chainIds anymore, so
// we need to retrieve it from the client, or request it from the provider
// if none exists.
return (
getClient().data?.chain?.id ??
normalizeChainId(await provider.request({ method: 'eth_chainId' }))
)
}
async getProvider({
chainId,
create,
}: { chainId?: number; create?: boolean } = {}) {
// WalletConnect v2
if (this.options.version === '2') {
if (!this.#provider) await this.#createUniversalProvider()
if (chainId)
(this.#provider as UniversalProvider).setDefaultChain(
`${defaultV2Config.namespace}:${chainId}`,
)
return this.#provider as UniversalProvider
}
// WalletConnect v1 (Force create new provider)
else if (!this.#provider || chainId || create) {
const rpc = !this.options?.infuraId
? this.chains.reduce(
(rpc, chain) => ({
...rpc,
[chain.id]: chain.rpcUrls.default.http[0],
}),
{},
)
: {}
const WalletConnectProvider = (
await import('@walletconnect/ethereum-provider')
).default
this.#provider = new WalletConnectProvider({
...this.options,
chainId,
rpc: { ...rpc, ...this.options?.rpc },
})
return this.#provider
}
return this.#provider
}
async getSigner({ chainId }: { chainId?: number } = {}) {
const [provider, account] = await Promise.all([
this.getProvider({ chainId }),
this.getAccount(),
])
let provider_ = provider as providers.ExternalProvider
if (this.version === '2') {
const chainId_ = await this.getChainId()
provider_ = {
...provider,
async request(args) {
return await provider.request(
args,
`${defaultV2Config.namespace}:${chainId ?? chainId_}`,
)
},
} as providers.ExternalProvider
}
return new providers.Web3Provider(provider_, chainId).getSigner(account)
}
async isAuthorized() {
try {
const [account, isChainsAuthorized] = await Promise.all([
this.getAccount(),
this.#isChainsAuthorized(),
])
return !!account && isChainsAuthorized
} catch {
return false
}
}
async #createWeb3Modal() {
const { Web3Modal } = await import('@web3modal/standalone')
const { version } = this.options
this.#web3Modal = new Web3Modal({
walletConnectVersion: version === '2' ? 2 : 1,
projectId: version === '2' ? this.options.projectId : '',
standaloneChains: this.namespacedChains,
})
}
async #initUniversalProvider() {
const WalletConnectProvider = (
await import('@walletconnect/universal-provider')
).default
if (typeof WalletConnectProvider?.init === 'function') {
this.#provider = await WalletConnectProvider.init(
this.options as UniversalProviderOpts,
)
}
}
async #createUniversalProvider() {
if (!this.#initUniversalProviderPromise) {
this.#initUniversalProviderPromise = this.#initUniversalProvider()
}
return this.#initUniversalProviderPromise
}
/**
* @description Checks if the connector is authorized to access the requested chains.
*
* There could be a case where requested chains are out of sync with
* the users' approved chains (e.g. the dapp could have added support
* for an additional chain), hence the user will be unauthorized.
*/
async #isChainsAuthorized() {
const provider = await this.getProvider()
if (this.version === '1') return true
const providerChains =
(provider as UniversalProvider).namespaces?.[defaultV2Config.namespace]
?.chains || []
const authorizedChainIds = providerChains.map(
(chain) => parseInt(chain.split(':')[1] || '') as Chain['id'],
)
return !this.chains.some(({ id }) => !authorizedChainIds.includes(id))
}
async #switchChain(chainId: number) {
const provider = await this.getProvider()
const id = hexValue(chainId)
try {
// Set up a race between `wallet_switchEthereumChain` & the `chainChanged` event
// to ensure the chain has been switched. This is because there could be a case
// where a wallet may not resolve the `wallet_switchEthereumChain` method, or
// resolves slower than `chainChanged`.
await Promise.race([
provider.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: id }],
}),
new Promise((res) =>
this.on('change', ({ chain }) => {
if (chain?.id === chainId) res(chainId)
}),
),
])
if (this.version === '2') {
;(provider as UniversalProvider).setDefaultChain(
`${defaultV2Config.namespace}:${chainId}`,
)
this.onChainChanged(chainId)
}
return (
this.chains.find((x) => x.id === chainId) ?? {
id: chainId,
name: `Chain ${id}`,
network: `${id}`,
nativeCurrency: { decimals: 18, name: 'Ether', symbol: 'ETH' },
rpcUrls: { default: { http: [''] }, public: { http: [''] } },
}
)
} catch (error) {
const message =
typeof error === 'string' ? error : (error as ProviderRpcError)?.message
if (/user rejected request/i.test(message))
throw new UserRejectedRequestError(error)
throw new SwitchChainError(error)
}
}
protected onAccountsChanged = (accounts: string[]) => {
if (accounts.length === 0) this.emit('disconnect')
else this.emit('change', { account: getAddress(accounts[0] as string) })
}
protected onChainChanged = (chainId: number | string) => {
const id = normalizeChainId(chainId)
const unsupported = this.isChainUnsupported(id)
this.emit('change', { chain: { id, unsupported } })
}
protected onDisconnect = () => {
this.emit('disconnect')
}
protected onDisplayUri = (uri: string) => {
this.emit('message', { type: 'display_uri', data: uri })
}
}