-
-
Notifications
You must be signed in to change notification settings - Fork 200
/
safe-event-emitter-provider.ts
150 lines (140 loc) · 4.14 KB
/
safe-event-emitter-provider.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
import type { JsonRpcEngine } from '@metamask/json-rpc-engine';
import { JsonRpcError } from '@metamask/rpc-errors';
import SafeEventEmitter from '@metamask/safe-event-emitter';
import type {
Json,
JsonRpcId,
JsonRpcParams,
JsonRpcRequest,
JsonRpcVersion2,
} from '@metamask/utils';
import { v4 as uuidV4 } from 'uuid';
/**
* A JSON-RPC request conforming to the EIP-1193 specification.
*/
type Eip1193Request<Params extends JsonRpcParams> = {
id?: JsonRpcId;
jsonrpc?: JsonRpcVersion2;
method: string;
params?: Params;
};
/**
* Converts an EIP-1193 request to a JSON-RPC request.
*
* @param eip1193Request - The EIP-1193 request to convert.
* @returns The corresponding JSON-RPC request.
*/
export function convertEip1193RequestToJsonRpcRequest<
Params extends JsonRpcParams,
>(
eip1193Request: Eip1193Request<Params>,
): JsonRpcRequest<Params | Record<never, never>> {
const { id = uuidV4(), jsonrpc = '2.0', method, params } = eip1193Request;
return params
? {
id,
jsonrpc,
method,
params,
}
: {
id,
jsonrpc,
method,
};
}
/**
* An Ethereum provider.
*
* This provider loosely follows conventions that pre-date EIP-1193.
* It is not compliant with any Ethereum provider standard.
*/
export class SafeEventEmitterProvider extends SafeEventEmitter {
#engine: JsonRpcEngine;
/**
* Construct a SafeEventEmitterProvider from a JSON-RPC engine.
*
* @param options - Options.
* @param options.engine - The JSON-RPC engine used to process requests.
*/
constructor({ engine }: { engine: JsonRpcEngine }) {
super();
this.#engine = engine;
if (engine.on) {
engine.on('notification', (message: string) => {
this.emit('data', null, message);
});
}
}
/**
* Send a provider request asynchronously.
*
* @param eip1193Request - The request to send.
* @returns The JSON-RPC response.
*/
async request<Params extends JsonRpcParams, Result extends Json>(
eip1193Request: Eip1193Request<Params>,
): Promise<Result> {
const jsonRpcRequest =
convertEip1193RequestToJsonRpcRequest(eip1193Request);
const response = await this.#engine.handle<
Params | Record<never, never>,
Result
>(jsonRpcRequest);
if ('result' in response) {
return response.result;
}
const error = new JsonRpcError(
response.error.code,
response.error.message,
response.error.data,
);
if ('stack' in response.error) {
error.stack = response.error.stack;
}
throw error;
}
/**
* Send a provider request asynchronously.
*
* This method serves the same purpose as `request`. It only exists for
* legacy reasons.
*
* @param eip1193Request - The request to send.
* @param callback - A function that is called upon the success or failure of the request.
* @deprecated Please use `request` instead.
*/
sendAsync = <Params extends JsonRpcParams>(
eip1193Request: Eip1193Request<Params>,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (error: unknown, providerRes?: any) => void,
) => {
const jsonRpcRequest =
convertEip1193RequestToJsonRpcRequest(eip1193Request);
this.#engine.handle(jsonRpcRequest, callback);
};
/**
* Send a provider request asynchronously.
*
* This method serves the same purpose as `request`. It only exists for
* legacy reasons.
*
* @param eip1193Request - The request to send.
* @param callback - A function that is called upon the success or failure of the request.
* @deprecated Please use `request` instead.
*/
send = <Params extends JsonRpcParams>(
eip1193Request: Eip1193Request<Params>,
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
callback: (error: unknown, providerRes?: any) => void,
) => {
if (typeof callback !== 'function') {
throw new Error('Must provide callback to "send" method.');
}
const jsonRpcRequest =
convertEip1193RequestToJsonRpcRequest(eip1193Request);
this.#engine.handle(jsonRpcRequest, callback);
};
}