-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathBaseProvider.ts
467 lines (413 loc) · 13.6 KB
/
BaseProvider.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
import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine';
import { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { rpcErrors, JsonRpcError } from '@metamask/rpc-errors';
import SafeEventEmitter from '@metamask/safe-event-emitter';
import type {
JsonRpcRequest,
JsonRpcId,
JsonRpcVersion2,
JsonRpcSuccess,
JsonRpcParams,
Json,
} from '@metamask/utils';
import dequal from 'fast-deep-equal';
import messages from './messages';
import type { ConsoleLike, Maybe } from './utils';
import { getRpcPromiseCallback, isValidChainId } from './utils';
export type UnvalidatedJsonRpcRequest = {
id?: JsonRpcId;
jsonrpc?: JsonRpcVersion2;
method: string;
params?: unknown;
};
export type BaseProviderOptions = {
/**
* The logging API to use.
*/
logger?: ConsoleLike;
/**
* The maximum number of event listeners.
*/
maxEventListeners?: number;
/**
* `@metamask/json-rpc-engine` middleware. The middleware will be inserted in the given
* order immediately after engine initialization.
*/
rpcMiddleware?: JsonRpcMiddleware<JsonRpcParams, Json>[];
};
export type RequestArguments = {
/** The RPC method to request. */
method: string;
/** The params of the RPC method, if any. */
params?: unknown[] | Record<string, unknown>;
};
export type BaseProviderState = {
accounts: null | string[];
isConnected: boolean;
initialized: boolean;
isPermanentlyDisconnected: boolean;
};
/**
* An abstract class implementing the EIP-1193 interface. Implementers must:
*
* 1. At initialization, push a middleware to the internal `_rpcEngine` that
* hands off requests to the server and receives responses in return.
* 2. At initialization, retrieve initial state and call
* {@link BaseProvider._initializeState} **once**.
* 3. Ensure that the provider's state is synchronized with the wallet.
* 4. Ensure that notifications are received and emitted as appropriate.
*/
export abstract class BaseProvider extends SafeEventEmitter {
protected readonly _log: ConsoleLike;
protected _state: BaseProviderState;
protected _rpcEngine: JsonRpcEngine;
protected static _defaultState: BaseProviderState = {
accounts: null,
isConnected: false,
initialized: false,
isPermanentlyDisconnected: false,
};
/**
* The chain ID of the currently connected Ethereum chain.
* See [chainId.network]{@link https://chainid.network} for more information.
*/
#chainId: string | null;
/**
* The user's currently selected Ethereum address.
* If null, MetaMask is either locked or the user has not permitted any
* addresses to be viewed.
*/
#selectedAddress: string | null;
/**
* Create a new instance of the provider.
*
* @param options - An options bag.
* @param options.logger - The logging API to use. Default: `console`.
* @param options.maxEventListeners - The maximum number of event
* listeners. Default: 100.
* @param options.rpcMiddleware - The RPC middleware stack. Default: [].
*/
constructor({
logger = console,
maxEventListeners = 100,
rpcMiddleware = [],
}: BaseProviderOptions = {}) {
super();
this._log = logger;
this.setMaxListeners(maxEventListeners);
// Private state
this._state = {
...BaseProvider._defaultState,
};
// Public state
this.#selectedAddress = null;
this.#chainId = null;
// Bind functions to prevent consumers from making unbound calls
this._handleAccountsChanged = this._handleAccountsChanged.bind(this);
this._handleConnect = this._handleConnect.bind(this);
this._handleChainChanged = this._handleChainChanged.bind(this);
this._handleDisconnect = this._handleDisconnect.bind(this);
this._rpcRequest = this._rpcRequest.bind(this);
this.request = this.request.bind(this);
// Handle RPC requests via dapp-side RPC engine.
//
// ATTN: Implementers must push a middleware that hands off requests to
// the server.
const rpcEngine = new JsonRpcEngine();
rpcMiddleware.forEach((middleware) => rpcEngine.push(middleware));
this._rpcEngine = rpcEngine;
}
//====================
// Public Properties
//====================
get chainId(): string | null {
return this.#chainId;
}
get selectedAddress(): string | null {
return this.#selectedAddress;
}
//====================
// Public Methods
//====================
/**
* Returns whether the provider can process RPC requests.
*
* @returns Whether the provider can process RPC requests.
*/
isConnected(): boolean {
return this._state.isConnected;
}
/**
* Submits an RPC request for the given method, with the given params.
* Resolves with the result of the method call, or rejects on error.
*
* @param args - The RPC request arguments.
* @param args.method - The RPC method name.
* @param args.params - The parameters for the RPC method.
* @returns A Promise that resolves with the result of the RPC method,
* or rejects if an error is encountered.
*/
async request<Type>(args: RequestArguments): Promise<Maybe<Type>> {
if (!args || typeof args !== 'object' || Array.isArray(args)) {
throw rpcErrors.invalidRequest({
message: messages.errors.invalidRequestArgs(),
data: args,
});
}
const { method, params } = args;
if (typeof method !== 'string' || method.length === 0) {
throw rpcErrors.invalidRequest({
message: messages.errors.invalidRequestMethod(),
data: args,
});
}
if (
params !== undefined &&
!Array.isArray(params) &&
(typeof params !== 'object' || params === null)
) {
throw rpcErrors.invalidRequest({
message: messages.errors.invalidRequestParams(),
data: args,
});
}
const payload =
params === undefined || params === null
? {
method,
}
: {
method,
params,
};
return new Promise<Type>((resolve, reject) => {
this._rpcRequest(payload, getRpcPromiseCallback(resolve, reject));
});
}
//====================
// Private Methods
//====================
/**
* MUST be called by child classes.
*
* Sets initial state if provided and marks this provider as initialized.
* Throws if called more than once.
*
* Permits the `networkVersion` field in the parameter object for
* compatibility with child classes that use this value.
*
* @param initialState - The provider's initial state.
* @param initialState.accounts - The user's accounts.
* @param initialState.chainId - The chain ID.
* @param initialState.networkVersion - The network version.
* @param initialState.isConnected - Whether the network is available.
* @fires BaseProvider#_initialized - If `initialState` is defined.
* @fires BaseProvider#connect - If `initialState` is defined.
*/
protected _initializeState(initialState?: {
accounts: string[];
chainId: string;
networkVersion?: string;
isConnected?: boolean;
}) {
if (this._state.initialized) {
throw new Error('Provider already initialized.');
}
if (initialState) {
const { accounts, chainId, networkVersion, isConnected } = initialState;
// EIP-1193 connect
this._handleConnect({ chainId, isConnected });
this._handleChainChanged({ chainId, networkVersion, isConnected });
this._handleAccountsChanged(accounts);
}
// Mark provider as initialized regardless of whether initial state was
// retrieved.
this._state.initialized = true;
this.emit('_initialized');
}
/**
* Internal RPC method. Forwards requests to background via the RPC engine.
* Also remap ids inbound and outbound.
*
* @param payload - The RPC request object.
* @param callback - The consumer's callback.
* @returns The result of the RPC request.
*/
protected _rpcRequest(
payload: UnvalidatedJsonRpcRequest | UnvalidatedJsonRpcRequest[],
callback: (...args: any[]) => void,
) {
let callbackWrapper = callback;
if (!Array.isArray(payload)) {
if (!payload.jsonrpc) {
payload.jsonrpc = '2.0';
}
if (
payload.method === 'eth_accounts' ||
payload.method === 'eth_requestAccounts'
) {
// handle accounts changing
callbackWrapper = (
error: Error,
response: JsonRpcSuccess<string[]>,
) => {
this._handleAccountsChanged(
response.result ?? [],
payload.method === 'eth_accounts',
);
callback(error, response);
};
}
return this._rpcEngine.handle(payload as JsonRpcRequest, callbackWrapper);
}
return this._rpcEngine.handle(payload as JsonRpcRequest[], callbackWrapper);
}
/**
* When the provider becomes connected, updates internal state and emits
* required events. Idempotent.
*
* @param networkInfo - The options object.
* @param networkInfo.chainId - The ID of the newly connected chain.
* @param networkInfo.isConnected - Whether the network is available.
* @fires MetaMaskInpageProvider#connect
*/
protected _handleConnect({
chainId,
isConnected,
}: {
chainId: string;
isConnected?: boolean | undefined;
}) {
if (!this._state.isConnected && isConnected) {
this._state.isConnected = true;
this.emit('connect', { chainId });
this._log.debug(messages.info.connected(chainId));
}
}
/**
* When the provider becomes disconnected, updates internal state and emits
* required events. Idempotent with respect to the isRecoverable parameter.
*
* Error codes per the CloseEvent status codes as required by EIP-1193:
* https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes.
*
* @param isRecoverable - Whether the disconnection is recoverable.
* @param errorMessage - A custom error message.
* @fires BaseProvider#disconnect - If the disconnection is not recoverable.
*/
protected _handleDisconnect(isRecoverable: boolean, errorMessage?: string) {
if (
this._state.isConnected ||
(!this._state.isPermanentlyDisconnected && !isRecoverable)
) {
this._state.isConnected = false;
let error;
if (isRecoverable) {
error = new JsonRpcError(
1013, // Try again later
errorMessage ?? messages.errors.disconnected(),
);
this._log.debug(error);
} else {
error = new JsonRpcError(
1011, // Internal error
errorMessage ?? messages.errors.permanentlyDisconnected(),
);
this._log.error(error);
this.#chainId = null;
this._state.accounts = null;
this.#selectedAddress = null;
this._state.isPermanentlyDisconnected = true;
}
this.emit('disconnect', error);
}
}
/**
* Upon receipt of a new `chainId`, emits the corresponding event and sets
* and sets relevant public state. Does nothing if the given `chainId` is
* equivalent to the existing value.
*
* Permits the `networkVersion` field in the parameter object for
* compatibility with child classes that use this value.
*
* @fires BaseProvider#chainChanged
* @param networkInfo - An object with network info.
* @param networkInfo.chainId - The latest chain ID.
* @param networkInfo.isConnected - Whether the network is available.
*/
protected _handleChainChanged({
chainId,
isConnected,
}:
| {
chainId?: string;
networkVersion?: string | undefined;
isConnected?: boolean | undefined;
}
| undefined = {}) {
if (!isValidChainId(chainId)) {
this._log.error(messages.errors.invalidNetworkParams(), { chainId });
return;
}
this._handleConnect({ chainId, isConnected });
if (chainId !== this.#chainId) {
this.#chainId = chainId;
if (this._state.initialized) {
this.emit('chainChanged', this.#chainId);
}
}
}
/**
* Called when accounts may have changed. Diffs the new accounts value with
* the current one, updates all state as necessary, and emits the
* accountsChanged event.
*
* @param accounts - The new accounts value.
* @param isEthAccounts - Whether the accounts value was returned by
* a call to eth_accounts.
*/
protected _handleAccountsChanged(
accounts: unknown[],
isEthAccounts = false,
): void {
let _accounts = accounts;
if (!Array.isArray(accounts)) {
this._log.error(
'MetaMask: Received invalid accounts parameter. Please report this bug.',
accounts,
);
_accounts = [];
}
for (const account of accounts) {
if (typeof account !== 'string') {
this._log.error(
'MetaMask: Received non-string account. Please report this bug.',
accounts,
);
_accounts = [];
break;
}
}
// emit accountsChanged if anything about the accounts array has changed
if (!dequal(this._state.accounts, _accounts)) {
// we should always have the correct accounts even before eth_accounts
// returns
if (isEthAccounts && this._state.accounts !== null) {
this._log.error(
`MetaMask: 'eth_accounts' unexpectedly updated accounts. Please report this bug.`,
_accounts,
);
}
this._state.accounts = _accounts as string[];
// handle selectedAddress
if (this.#selectedAddress !== _accounts[0]) {
this.#selectedAddress = (_accounts[0] as string) || null;
}
// finally, after all state has been updated, emit the event
if (this._state.initialized) {
const _nextAccounts = [..._accounts];
this.emit('accountsChanged', _nextAccounts);
}
}
}
}