-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathbrowser.js
More file actions
63 lines (50 loc) · 1.9 KB
/
browser.js
File metadata and controls
63 lines (50 loc) · 1.9 KB
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
/* globals WebSocket */
const resolve = require('./resolve')
const provider = require('./provider')
const presets = require('./presets')
const injected = {
ethereum: typeof window !== 'undefined' && typeof window.ethereum !== 'undefined' ? window.ethereum : null,
web3:
typeof window !== 'undefined' && typeof window.web3 !== 'undefined' ? window.web3.currentProvider : null
}
const ws = () => {
if (typeof window !== 'undefined' && typeof window.WebSocket !== 'undefined') {
return window.WebSocket
}
if (typeof WebSocket !== 'undefined') {
return WebSocket
}
return null
}
const XHR =
typeof window !== 'undefined' && typeof window.XMLHttpRequest !== 'undefined' ? window.XMLHttpRequest : null
if (injected.ethereum) injected.ethereum.__isProvider = true
const connections = {
injected: injected.ethereum || require('./connections/injected')(injected.web3),
ipc: require('./connections/unavailable')('IPC connections are unavliable in the browser'),
ws: require('./connections/ws')(ws()),
http: require('./connections/http')(XHR)
}
module.exports = (targets, options) => {
if (targets && !Array.isArray(targets) && typeof targets === 'object' && !options) {
options = targets
targets = undefined
}
if (!targets) targets = ['injected', 'frame']
if (!options) options = {}
targets = [].concat(targets)
targets.forEach((t) => {
if (t.startsWith('alchemy') && !options.alchemyId) {
throw new Error(
"Alchemy was included as a connection target but no Alchemy project ID was passed in options e.g. { alchemyId: '123abc' }"
)
}
if (t.startsWith('infura') && !options.infuraId) {
throw new Error(
"Infura was included as a connection target but no Infura project ID was passed in options e.g. { infuraId: '123abc' }"
)
}
})
const sets = presets(options)
return provider(connections, resolve(targets, sets), options)
}