-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
json_rpc.ts
130 lines (113 loc) · 4.87 KB
/
json_rpc.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
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
import { isNullish } from 'web3-validator';
import {
JsonRpcPayload,
JsonRpcResponse,
JsonRpcResponseWithResult,
JsonRpcResponseWithError,
JsonRpcOptionalRequest,
JsonRpcBatchRequest,
JsonRpcNotification,
JsonRpcRequest,
JsonRpcBatchResponse,
JsonRpcSubscriptionResult,
} from 'web3-types';
import { rpcErrorsMap } from 'web3-errors';
import { uuidV4 } from './uuid.js';
// check if code is a valid rpc server error code
export const isResponseRpcError = (rpcError: JsonRpcResponseWithError) => {
const errorCode = rpcError.error.code;
return rpcErrorsMap.has(errorCode) || (errorCode >= -32099 && errorCode <= -32000);
};
export const isResponseWithResult = <Result = unknown, Error = unknown>(
response: JsonRpcResponse<Result, Error>,
): response is JsonRpcResponseWithResult<Result> =>
!Array.isArray(response) &&
!!response &&
response.jsonrpc === '2.0' &&
// JSON RPC consider "null" as valid response
'result' in response &&
isNullish(response.error) &&
(typeof response.id === 'number' || typeof response.id === 'string');
// To avoid circular package dependency, copied to code here. If you update this please update same function in `response_errors.ts`
export const isResponseWithError = <Error = unknown, Result = unknown>(
response: JsonRpcResponse<Result, Error>,
): response is JsonRpcResponseWithError<Error> =>
!Array.isArray(response) &&
response.jsonrpc === '2.0' &&
!!response &&
isNullish(response.result) &&
// JSON RPC consider "null" as valid response
'error' in response &&
(typeof response.id === 'number' || typeof response.id === 'string');
export const isResponseWithNotification = <Result>(
response: JsonRpcNotification<Result> | JsonRpcSubscriptionResult,
): response is JsonRpcNotification<Result> =>
!Array.isArray(response) &&
!!response &&
response.jsonrpc === '2.0' &&
!isNullish(response.params) &&
!isNullish(response.method);
export const isSubscriptionResult = <Result>(
response: JsonRpcNotification<Result> | JsonRpcSubscriptionResult,
): response is JsonRpcSubscriptionResult =>
!Array.isArray(response) &&
!!response &&
response.jsonrpc === '2.0' &&
'id' in response &&
// JSON RPC consider "null" as valid response
'result' in response;
export const validateResponse = <Result = unknown, Error = unknown>(
response: JsonRpcResponse<Result, Error>,
): boolean => isResponseWithResult<Result>(response) || isResponseWithError<Error>(response);
export const isValidResponse = <Result = unknown, Error = unknown>(
response: JsonRpcResponse<Result, Error>,
): boolean =>
Array.isArray(response) ? response.every(validateResponse) : validateResponse(response);
export const isBatchResponse = <Result = unknown, Error = unknown>(
response: JsonRpcResponse<Result, Error>,
): response is JsonRpcBatchResponse<Result, Error> =>
Array.isArray(response) && response.length > 0 && isValidResponse(response);
// internal optional variable to increment and use for the jsonrpc `id`
let requestIdSeed: number | undefined;
/**
* Optionally use to make the jsonrpc `id` start from a specific number.
* Without calling this function, the `id` will be filled with a Uuid.
* But after this being called with a number, the `id` will be a number staring from the provided `start` variable.
* However, if `undefined` was passed to this function, the `id` will be a Uuid again.
* @param start - a number to start incrementing from.
* Or `undefined` to use a new Uuid (this is the default behavior)
*/
export const setRequestIdStart = (start: number | undefined) => {
requestIdSeed = start;
};
export const toPayload = <ParamType = unknown[]>(
request: JsonRpcOptionalRequest<ParamType>,
): JsonRpcPayload<ParamType> => {
if (typeof requestIdSeed !== 'undefined') {
requestIdSeed += 1;
}
return {
jsonrpc: request.jsonrpc ?? '2.0',
id: request.id ?? requestIdSeed ?? uuidV4(),
method: request.method,
params: request.params ?? undefined,
};
};
export const toBatchPayload = (requests: JsonRpcOptionalRequest<unknown>[]): JsonRpcBatchRequest =>
requests.map(request => toPayload<unknown>(request)) as JsonRpcBatchRequest;
export const isBatchRequest = (
request: JsonRpcBatchRequest | JsonRpcRequest<unknown> | JsonRpcOptionalRequest<unknown>,
): request is JsonRpcBatchRequest => Array.isArray(request) && request.length > 0;