forked from wevm/wagmi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinjected.ts
685 lines (632 loc) · 23.3 KB
/
injected.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
import {
type AddEthereumChainParameter,
type Address,
type EIP1193Provider,
type ProviderConnectInfo,
type ProviderRpcError,
ResourceUnavailableRpcError,
type RpcError,
SwitchChainError,
UserRejectedRequestError,
getAddress,
numberToHex,
withRetry,
withTimeout,
} from 'viem'
import type { Connector } from '../createConfig.js'
import { ChainNotConfiguredError } from '../errors/config.js'
import { ProviderNotFoundError } from '../errors/connector.js'
import type { Compute } from '../types/utils.js'
import { createConnector } from './createConnector.js'
export type InjectedParameters = {
/**
* Some injected providers do not support programmatic disconnect.
* This flag simulates the disconnect behavior by keeping track of connection status in storage.
* @default true
*/
shimDisconnect?: boolean | undefined
/**
* [EIP-1193](https://eips.ethereum.org/EIPS/eip-1193) Ethereum Provider to target
*/
target?: TargetId | Target | (() => Target | undefined) | undefined
unstable_shimAsyncInject?: boolean | number | undefined
}
// Regex of wallets/providers that can accurately simulate contract calls & display contract revert reasons.
const supportsSimulationIdRegex = /(rabby|trustwallet)/
const targetMap = {
coinbaseWallet: {
id: 'coinbaseWallet',
name: 'Coinbase Wallet',
provider(window) {
if (window?.coinbaseWalletExtension) return window.coinbaseWalletExtension
return findProvider(window, 'isCoinbaseWallet')
},
},
metaMask: {
id: 'metaMask',
name: 'MetaMask',
provider(window) {
return findProvider(window, (provider) => {
if (!provider.isMetaMask) return false
// Brave tries to make itself look like MetaMask
// Could also try RPC `web3_clientVersion` if following is unreliable
if (provider.isBraveWallet && !provider._events && !provider._state)
return false
// Other wallets that try to look like MetaMask
const flags: WalletProviderFlags[] = [
'isApexWallet',
'isAvalanche',
'isBitKeep',
'isBlockWallet',
'isKuCoinWallet',
'isMathWallet',
'isOkxWallet',
'isOKExWallet',
'isOneInchIOSWallet',
'isOneInchAndroidWallet',
'isOpera',
'isPortal',
'isRabby',
'isTokenPocket',
'isTokenary',
'isZerion',
]
for (const flag of flags) if (provider[flag]) return false
return true
})
},
},
phantom: {
id: 'phantom',
name: 'Phantom',
provider(window) {
if (window?.phantom?.ethereum) return window.phantom?.ethereum
return findProvider(window, 'isPhantom')
},
},
} as const satisfies TargetMap
injected.type = 'injected' as const
export function injected(parameters: InjectedParameters = {}) {
const { shimDisconnect = true, unstable_shimAsyncInject } = parameters
function getTarget(): Compute<Target & { id: string }> {
const target = parameters.target
if (typeof target === 'function') {
const result = target()
if (result) return result
}
if (typeof target === 'object') return target
if (typeof target === 'string')
return {
...(targetMap[target as keyof typeof targetMap] ?? {
id: target,
name: `${target[0]!.toUpperCase()}${target.slice(1)}`,
provider: `is${target[0]!.toUpperCase()}${target.slice(1)}`,
}),
}
return {
id: 'injected',
name: 'Injected',
provider(window) {
return window?.ethereum
},
}
}
type Provider = WalletProvider | undefined
type Properties = {
onConnect(connectInfo: ProviderConnectInfo): void
}
type StorageItem = {
[_ in 'injected.connected' | `${string}.disconnected`]: true
}
let accountsChanged: Connector['onAccountsChanged'] | undefined
let chainChanged: Connector['onChainChanged'] | undefined
let connect: Connector['onConnect'] | undefined
let disconnect: Connector['onDisconnect'] | undefined
return createConnector<Provider, Properties, StorageItem>((config) => ({
get icon() {
return getTarget().icon
},
get id() {
return getTarget().id
},
get name() {
return getTarget().name
},
get supportsSimulation() {
return supportsSimulationIdRegex.test(this.id.toLowerCase())
},
type: injected.type,
async setup() {
const provider = await this.getProvider()
// Only start listening for events if `target` is set, otherwise `injected()` will also receive events
if (provider?.on && parameters.target) {
if (!connect) {
connect = this.onConnect.bind(this)
provider.on('connect', connect)
}
// We shouldn't need to listen for `'accountsChanged'` here since the `'connect'` event should suffice (and wallet shouldn't be connected yet).
// Some wallets, like MetaMask, do not implement the `'connect'` event and overload `'accountsChanged'` instead.
if (!accountsChanged) {
accountsChanged = this.onAccountsChanged.bind(this)
provider.on('accountsChanged', accountsChanged)
}
}
},
async connect({ chainId, isReconnecting } = {}) {
const provider = await this.getProvider()
if (!provider) throw new ProviderNotFoundError()
let accounts: readonly Address[] = []
if (isReconnecting) accounts = await this.getAccounts().catch(() => [])
else if (shimDisconnect) {
// Attempt to show another prompt for selecting account if `shimDisconnect` flag is enabled
try {
const permissions = await provider.request({
method: 'wallet_requestPermissions',
params: [{ eth_accounts: {} }],
})
accounts = (permissions[0]?.caveats?.[0]?.value as string[])?.map(
(x) => getAddress(x),
)
// `'wallet_requestPermissions'` can return a different order of accounts than `'eth_accounts'`
// switch to `'eth_accounts'` ordering if more than one account is connected
// https://github.com/wevm/wagmi/issues/4140
if (accounts.length > 0) {
const sortedAccounts = await this.getAccounts()
accounts = sortedAccounts
}
} catch (err) {
const error = err as RpcError
// Not all injected providers support `wallet_requestPermissions` (e.g. MetaMask iOS).
// Only bubble up error if user rejects request
if (error.code === UserRejectedRequestError.code)
throw new UserRejectedRequestError(error)
// Or prompt is already open
if (error.code === ResourceUnavailableRpcError.code) throw error
}
}
try {
if (!accounts?.length && !isReconnecting) {
const requestedAccounts = await provider.request({
method: 'eth_requestAccounts',
})
accounts = requestedAccounts.map((x) => getAddress(x))
}
// Manage EIP-1193 event listeners
// https://eips.ethereum.org/EIPS/eip-1193#events
if (connect) {
provider.removeListener('connect', connect)
connect = undefined
}
if (!accountsChanged) {
accountsChanged = this.onAccountsChanged.bind(this)
provider.on('accountsChanged', accountsChanged)
}
if (!chainChanged) {
chainChanged = this.onChainChanged.bind(this)
provider.on('chainChanged', chainChanged)
}
if (!disconnect) {
disconnect = this.onDisconnect.bind(this)
provider.on('disconnect', disconnect)
}
// Switch to chain if provided
let currentChainId = await this.getChainId()
if (chainId && currentChainId !== chainId) {
const chain = await this.switchChain!({ chainId }).catch((error) => {
if (error.code === UserRejectedRequestError.code) throw error
return { id: currentChainId }
})
currentChainId = chain?.id ?? currentChainId
}
// Remove disconnected shim if it exists
if (shimDisconnect)
await config.storage?.removeItem(`${this.id}.disconnected`)
// Add connected shim if no target exists
if (!parameters.target)
await config.storage?.setItem('injected.connected', true)
return { accounts, chainId: currentChainId }
} catch (err) {
const error = err as RpcError
if (error.code === UserRejectedRequestError.code)
throw new UserRejectedRequestError(error)
if (error.code === ResourceUnavailableRpcError.code)
throw new ResourceUnavailableRpcError(error)
throw error
}
},
async disconnect() {
const provider = await this.getProvider()
if (!provider) throw new ProviderNotFoundError()
// Manage EIP-1193 event listeners
if (chainChanged) {
provider.removeListener('chainChanged', chainChanged)
chainChanged = undefined
}
if (disconnect) {
provider.removeListener('disconnect', disconnect)
disconnect = undefined
}
if (!connect) {
connect = this.onConnect.bind(this)
provider.on('connect', connect)
}
// Experimental support for MetaMask disconnect
// https://github.com/MetaMask/metamask-improvement-proposals/blob/main/MIPs/mip-2.md
try {
// Adding timeout as not all wallets support this method and can hang
// https://github.com/wevm/wagmi/issues/4064
await withTimeout(
() =>
// TODO: Remove explicit type for viem@3
provider.request<{
Method: 'wallet_revokePermissions'
Parameters: [permissions: { eth_accounts: Record<string, any> }]
ReturnType: null
}>({
// `'wallet_revokePermissions'` added in `viem@2.10.3`
method: 'wallet_revokePermissions',
params: [{ eth_accounts: {} }],
}),
{ timeout: 100 },
)
} catch {}
// Add shim signalling connector is disconnected
if (shimDisconnect) {
await config.storage?.setItem(`${this.id}.disconnected`, true)
}
if (!parameters.target)
await config.storage?.removeItem('injected.connected')
},
async getAccounts() {
const provider = await this.getProvider()
if (!provider) throw new ProviderNotFoundError()
const accounts = await provider.request({ method: 'eth_accounts' })
return accounts.map((x) => getAddress(x))
},
async getChainId() {
const provider = await this.getProvider()
if (!provider) throw new ProviderNotFoundError()
const hexChainId = await provider.request({ method: 'eth_chainId' })
return Number(hexChainId)
},
async getProvider() {
if (typeof window === 'undefined') return undefined
let provider: Provider
const target = getTarget()
if (typeof target.provider === 'function')
provider = target.provider(window as Window | undefined)
else if (typeof target.provider === 'string')
provider = findProvider(window, target.provider)
else provider = target.provider
// Some wallets do not conform to EIP-1193 (e.g. Trust Wallet)
// https://github.com/wevm/wagmi/issues/3526#issuecomment-1912683002
if (provider && !provider.removeListener) {
// Try using `off` handler if it exists, otherwise noop
if ('off' in provider && typeof provider.off === 'function')
provider.removeListener =
provider.off as typeof provider.removeListener
else provider.removeListener = () => {}
}
return provider
},
async isAuthorized() {
try {
const isDisconnected =
shimDisconnect &&
// If shim exists in storage, connector is disconnected
(await config.storage?.getItem(`${this.id}.disconnected`))
if (isDisconnected) return false
// Don't allow injected connector to connect if no target is set and it hasn't already connected
// (e.g. flag in storage is not set). This prevents a targetless injected connector from connecting
// automatically whenever there is a targeted connector configured.
if (!parameters.target) {
const connected = await config.storage?.getItem('injected.connected')
if (!connected) return false
}
const provider = await this.getProvider()
if (!provider) {
if (
unstable_shimAsyncInject !== undefined &&
unstable_shimAsyncInject !== false
) {
// If no provider is found, check for async injection
// https://github.com/wevm/references/issues/167
// https://github.com/MetaMask/detect-provider
const handleEthereum = async () => {
if (typeof window !== 'undefined')
window.removeEventListener(
'ethereum#initialized',
handleEthereum,
)
const provider = await this.getProvider()
return !!provider
}
const timeout =
typeof unstable_shimAsyncInject === 'number'
? unstable_shimAsyncInject
: 1_000
const res = await Promise.race([
...(typeof window !== 'undefined'
? [
new Promise<boolean>((resolve) =>
window.addEventListener(
'ethereum#initialized',
() => resolve(handleEthereum()),
{ once: true },
),
),
]
: []),
new Promise<boolean>((resolve) =>
setTimeout(() => resolve(handleEthereum()), timeout),
),
])
if (res) return true
}
throw new ProviderNotFoundError()
}
// Use retry strategy as some injected wallets (e.g. MetaMask) fail to
// immediately resolve JSON-RPC requests on page load.
const accounts = await withRetry(() => this.getAccounts())
return !!accounts.length
} catch {
return false
}
},
async switchChain({ addEthereumChainParameter, chainId }) {
const provider = await this.getProvider()
if (!provider) throw new ProviderNotFoundError()
const chain = config.chains.find((x) => x.id === chainId)
if (!chain) throw new SwitchChainError(new ChainNotConfiguredError())
try {
await Promise.all([
provider
.request({
method: 'wallet_switchEthereumChain',
params: [{ chainId: numberToHex(chainId) }],
})
// During `'wallet_switchEthereumChain'`, MetaMask makes a `'net_version'` RPC call to the target chain.
// If this request fails, MetaMask does not emit the `'chainChanged'` event, but will still switch the chain.
// To counter this behavior, we request and emit the current chain ID to confirm the chain switch either via
// this callback or an externally emitted `'chainChanged'` event.
// https://github.com/MetaMask/metamask-extension/issues/24247
.then(async () => {
const currentChainId = await this.getChainId()
if (currentChainId === chainId)
config.emitter.emit('change', { chainId })
}),
new Promise<void>((resolve) => {
const listener = ((data) => {
if ('chainId' in data && data.chainId === chainId) {
config.emitter.off('change', listener)
resolve()
}
}) satisfies Parameters<typeof config.emitter.on>[1]
config.emitter.on('change', listener)
}),
])
return chain
} catch (err) {
const error = err as RpcError
// Indicates chain is not added to provider
if (
error.code === 4902 ||
// Unwrapping for MetaMask Mobile
// https://github.com/MetaMask/metamask-mobile/issues/2944#issuecomment-976988719
(error as ProviderRpcError<{ originalError?: { code: number } }>)
?.data?.originalError?.code === 4902
) {
try {
const { default: blockExplorer, ...blockExplorers } =
chain.blockExplorers ?? {}
let blockExplorerUrls: string[] | undefined
if (addEthereumChainParameter?.blockExplorerUrls)
blockExplorerUrls = addEthereumChainParameter.blockExplorerUrls
else if (blockExplorer)
blockExplorerUrls = [
blockExplorer.url,
...Object.values(blockExplorers).map((x) => x.url),
]
let rpcUrls: readonly string[]
if (addEthereumChainParameter?.rpcUrls?.length)
rpcUrls = addEthereumChainParameter.rpcUrls
else rpcUrls = [chain.rpcUrls.default?.http[0] ?? '']
const addEthereumChain = {
blockExplorerUrls,
chainId: numberToHex(chainId),
chainName: addEthereumChainParameter?.chainName ?? chain.name,
iconUrls: addEthereumChainParameter?.iconUrls,
nativeCurrency:
addEthereumChainParameter?.nativeCurrency ??
chain.nativeCurrency,
rpcUrls,
} satisfies AddEthereumChainParameter
await provider.request({
method: 'wallet_addEthereumChain',
params: [addEthereumChain],
})
const currentChainId = await this.getChainId()
if (currentChainId !== chainId)
throw new UserRejectedRequestError(
new Error('User rejected switch after adding network.'),
)
return chain
} catch (error) {
throw new UserRejectedRequestError(error as Error)
}
}
if (error.code === UserRejectedRequestError.code)
throw new UserRejectedRequestError(error)
throw new SwitchChainError(error)
}
},
async onAccountsChanged(accounts) {
// Disconnect if there are no accounts
if (accounts.length === 0) this.onDisconnect()
// Connect if emitter is listening for connect event (e.g. is disconnected and connects through wallet interface)
else if (config.emitter.listenerCount('connect')) {
const chainId = (await this.getChainId()).toString()
this.onConnect({ chainId })
// Remove disconnected shim if it exists
if (shimDisconnect)
await config.storage?.removeItem(`${this.id}.disconnected`)
}
// Regular change event
else
config.emitter.emit('change', {
accounts: accounts.map((x) => getAddress(x)),
})
},
onChainChanged(chain) {
const chainId = Number(chain)
config.emitter.emit('change', { chainId })
},
async onConnect(connectInfo) {
const accounts = await this.getAccounts()
if (accounts.length === 0) return
const chainId = Number(connectInfo.chainId)
config.emitter.emit('connect', { accounts, chainId })
// Manage EIP-1193 event listeners
const provider = await this.getProvider()
if (provider) {
if (connect) {
provider.removeListener('connect', connect)
connect = undefined
}
if (!accountsChanged) {
accountsChanged = this.onAccountsChanged.bind(this)
provider.on('accountsChanged', accountsChanged)
}
if (!chainChanged) {
chainChanged = this.onChainChanged.bind(this)
provider.on('chainChanged', chainChanged)
}
if (!disconnect) {
disconnect = this.onDisconnect.bind(this)
provider.on('disconnect', disconnect)
}
}
},
async onDisconnect(error) {
const provider = await this.getProvider()
// If MetaMask emits a `code: 1013` error, wait for reconnection before disconnecting
// https://github.com/MetaMask/providers/pull/120
if (error && (error as RpcError<1013>).code === 1013) {
if (provider && !!(await this.getAccounts()).length) return
}
// No need to remove `${this.id}.disconnected` from storage because `onDisconnect` is typically
// only called when the wallet is disconnected through the wallet's interface, meaning the wallet
// actually disconnected and we don't need to simulate it.
config.emitter.emit('disconnect')
// Manage EIP-1193 event listeners
if (provider) {
if (chainChanged) {
provider.removeListener('chainChanged', chainChanged)
chainChanged = undefined
}
if (disconnect) {
provider.removeListener('disconnect', disconnect)
disconnect = undefined
}
if (!connect) {
connect = this.onConnect.bind(this)
provider.on('connect', connect)
}
}
},
}))
}
type Target = {
icon?: string | undefined
id: string
name: string
provider:
| WalletProviderFlags
| WalletProvider
| ((window?: Window | undefined) => WalletProvider | undefined)
}
/** @deprecated */
type TargetId = Compute<WalletProviderFlags> extends `is${infer name}`
? name extends `${infer char}${infer rest}`
? `${Lowercase<char>}${rest}`
: never
: never
type TargetMap = { [_ in TargetId]?: Target | undefined }
/** @deprecated */
type WalletProviderFlags =
| 'isApexWallet'
| 'isAvalanche'
| 'isBackpack'
| 'isBifrost'
| 'isBitKeep'
| 'isBitski'
| 'isBlockWallet'
| 'isBraveWallet'
| 'isCoinbaseWallet'
| 'isDawn'
| 'isEnkrypt'
| 'isExodus'
| 'isFrame'
| 'isFrontier'
| 'isGamestop'
| 'isHyperPay'
| 'isImToken'
| 'isKuCoinWallet'
| 'isMathWallet'
| 'isMetaMask'
| 'isOkxWallet'
| 'isOKExWallet'
| 'isOneInchAndroidWallet'
| 'isOneInchIOSWallet'
| 'isOpera'
| 'isPhantom'
| 'isPortal'
| 'isRabby'
| 'isRainbow'
| 'isStatus'
| 'isTally'
| 'isTokenPocket'
| 'isTokenary'
| 'isTrust'
| 'isTrustWallet'
| 'isXDEFI'
| 'isZerion'
type WalletProvider = Compute<
EIP1193Provider & {
[key in WalletProviderFlags]?: true | undefined
} & {
providers?: WalletProvider[] | undefined
/** Only exists in MetaMask as of 2022/04/03 */
_events?: { connect?: (() => void) | undefined } | undefined
/** Only exists in MetaMask as of 2022/04/03 */
_state?:
| {
accounts?: string[]
initialized?: boolean
isConnected?: boolean
isPermanentlyDisconnected?: boolean
isUnlocked?: boolean
}
| undefined
}
>
type Window = {
coinbaseWalletExtension?: WalletProvider | undefined
ethereum?: WalletProvider | undefined
phantom?: { ethereum: WalletProvider } | undefined
}
function findProvider(
window: globalThis.Window | Window | undefined,
select?: WalletProviderFlags | ((provider: WalletProvider) => boolean),
) {
function isProvider(provider: WalletProvider) {
if (typeof select === 'function') return select(provider)
if (typeof select === 'string') return provider[select]
return true
}
const ethereum = (window as Window).ethereum
if (ethereum?.providers)
return ethereum.providers.find((provider) => isProvider(provider))
if (ethereum && isProvider(ethereum)) return ethereum
return undefined
}