This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
zero.js
118 lines (97 loc) · 3.56 KB
/
zero.js
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
const ProviderEngine = require('./index.js')
const DefaultFixture = require('./subproviders/default-fixture.js')
const NonceTrackerSubprovider = require('./subproviders/nonce-tracker.js')
const CacheSubprovider = require('./subproviders/cache.js')
const FilterSubprovider = require('./subproviders/filters')
const SubscriptionSubprovider = require('./subproviders/subscriptions')
const InflightCacheSubprovider = require('./subproviders/inflight-cache')
const HookedWalletSubprovider = require('./subproviders/hooked-wallet.js')
const SanitizingSubprovider = require('./subproviders/sanitizer.js')
const InfuraSubprovider = require('./subproviders/infura.js')
const FetchSubprovider = require('./subproviders/fetch.js')
const WebSocketSubprovider = require('./subproviders/websocket.js')
module.exports = ZeroClientProvider
function ZeroClientProvider(opts = {}){
const connectionType = getConnectionType(opts)
const engine = new ProviderEngine(opts.engineParams)
// static
const staticSubprovider = new DefaultFixture(opts.static)
engine.addProvider(staticSubprovider)
// nonce tracker
engine.addProvider(new NonceTrackerSubprovider())
// sanitization
const sanitizer = new SanitizingSubprovider()
engine.addProvider(sanitizer)
// cache layer
const cacheSubprovider = new CacheSubprovider()
engine.addProvider(cacheSubprovider)
// filters + subscriptions
// only polyfill if not websockets
if (connectionType !== 'ws') {
engine.addProvider(new SubscriptionSubprovider())
engine.addProvider(new FilterSubprovider())
}
// inflight cache
const inflightCache = new InflightCacheSubprovider()
engine.addProvider(inflightCache)
// id mgmt
const idmgmtSubprovider = new HookedWalletSubprovider({
// accounts
getAccounts: opts.getAccounts,
// transactions
processTransaction: opts.processTransaction,
approveTransaction: opts.approveTransaction,
signTransaction: opts.signTransaction,
publishTransaction: opts.publishTransaction,
// messages
// old eth_sign
processMessage: opts.processMessage,
approveMessage: opts.approveMessage,
signMessage: opts.signMessage,
// new personal_sign
processPersonalMessage: opts.processPersonalMessage,
processTypedMessage: opts.processTypedMessage,
approvePersonalMessage: opts.approvePersonalMessage,
approveTypedMessage: opts.approveTypedMessage,
signPersonalMessage: opts.signPersonalMessage,
signTypedMessage: opts.signTypedMessage,
personalRecoverSigner: opts.personalRecoverSigner,
})
engine.addProvider(idmgmtSubprovider)
// data source
const dataSubprovider = opts.dataSubprovider || createDataSubprovider(connectionType, opts)
engine.addProvider(dataSubprovider)
// start polling
if (!opts.stopped) {
engine.start()
}
return engine
}
function createDataSubprovider(connectionType, opts) {
const { rpcUrl, debug } = opts
// default to infura
if (!connectionType) {
return new InfuraSubprovider()
}
if (connectionType === 'http') {
return new FetchSubprovider({ rpcUrl, debug })
}
if (connectionType === 'ws') {
return new WebSocketSubprovider({ rpcUrl, debug })
}
throw new Error(`ProviderEngine - unrecognized connectionType "${connectionType}"`)
}
function getConnectionType({ rpcUrl }) {
if (!rpcUrl) return undefined
const protocol = rpcUrl.split(':')[0].toLowerCase()
switch (protocol) {
case 'http':
case 'https':
return 'http'
case 'ws':
case 'wss':
return 'ws'
default:
throw new Error(`ProviderEngine - unrecognized protocol in "${rpcUrl}"`)
}
}