-
Notifications
You must be signed in to change notification settings - Fork 70
/
BaseClient.ts
executable file
·442 lines (405 loc) · 14.3 KB
/
BaseClient.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
import { IProvider, ProviderType } from '../interfaces/IProvider';
import { IClientConfig } from '../interfaces/IClientConfig';
import { Buffer } from 'buffer';
import { base58Decode, varintEncode } from '../utils/Xbqcrypto';
import { IContractData } from '../interfaces/IContractData';
import { JsonRpcResponseData } from '../interfaces/JsonRpcResponseData';
import axios, { AxiosResponse, AxiosRequestHeaders } from 'axios';
import { JSON_RPC_REQUEST_METHOD } from '../interfaces/JsonRpcMethods';
import { ITransactionData } from '../interfaces/ITransactionData';
import { OperationTypeId } from '../interfaces/OperationTypes';
import { IRollsData } from '../interfaces/IRollsData';
import { ICallData } from '../interfaces/ICallData';
// encode a string address to bytes.
const encodeAddressToBytes = (
address: string,
isSmartContract = false,
): Buffer => {
let targetAddressEncoded = base58Decode(address.slice(2));
targetAddressEncoded = Buffer.concat([
isSmartContract ? Buffer.from([1]) : Buffer.from([0]),
targetAddressEncoded,
]);
return targetAddressEncoded;
};
export type DataType =
| IContractData
| ITransactionData
| IRollsData
| ICallData;
export const requestHeaders = {
Accept:
'application/json,text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE,PATCH,OPTIONS',
} as AxiosRequestHeaders;
export const PERIOD_OFFSET = 5;
/**
* The Base Client object is the main entry point for interacting with the massa blockchain.
*
* @remarks
* The `BaseClient` class should not be instantiated directly; instead, it should
* be extended by other client classes to provide additional functionality on top of the core
* methods provided by this class.
*
* @throws Will throw an error if no public providers are included in client configuration.
* @throws Will throw an error if no private providers are included in client configuration.
*/
export class BaseClient {
public clientConfig: IClientConfig;
/**
* Constructor of the BaseClient class
*
* @param clientConfig - The client configuration object as defined in {@link IClientConfig}
*/
public constructor(clientConfig: IClientConfig) {
this.clientConfig = clientConfig;
this.clientConfig.periodOffset =
this.clientConfig.periodOffset | PERIOD_OFFSET;
if (this.getPublicProviders().length === 0) {
throw new Error(
'Cannot initialize web3 with no public providers. Need at least one',
);
}
// bind class methods
this.getPrivateProviders = this.getPrivateProviders.bind(this);
this.getProviderForRpcMethod = this.getProviderForRpcMethod.bind(this);
this.getPublicProviders = this.getPublicProviders.bind(this);
this.sendJsonRPCRequest = this.sendJsonRPCRequest.bind(this);
this.compactBytesForOperation = this.compactBytesForOperation.bind(this);
this.setProviders = this.setProviders.bind(this);
this.promisifyJsonRpcCall = this.promisifyJsonRpcCall.bind(this);
}
/**
* Set new providers as {@link IProvider}.
*
* @privateRemarks
* This methods add the providers to the existing ones in the clientConfig object.
*
* @param providers - The new providers to set as an array of IProvider.
*
* @throws Will throw an error if no public providers are included in the given array of providers.
* @throws Will throw an error if no private providers are included in the given array of providers.
*/
public setProviders(providers: Array<IProvider>): void {
const hasPublicProvider = providers.some(
(provider) => provider.type === ProviderType.PUBLIC,
);
if (!hasPublicProvider) {
throw new Error(
'Cannot set providers with no public providers. Need at least one',
);
}
this.clientConfig.providers = providers;
}
/**
* Returns all the private providers.
*
* @returns An array of IProvider containing all the private providers.
*/
protected getPrivateProviders(): Array<IProvider> {
return this.clientConfig.providers.filter(
(provider) => provider.type === ProviderType.PRIVATE,
);
}
/**
* Returns all the public providers.
*
* @returns An array of IProvider containing all the public providers.
*/
protected getPublicProviders(): Array<IProvider> {
return this.clientConfig.providers.filter(
(provider) => provider.type === ProviderType.PUBLIC,
);
}
/**
* Find provider for a concrete rpc method
*
* @remarks
* This method chooses the provider to use for a given rpc method.
* - If the rpc method is about getting or sending data to the blockchain,
* it will choose a public provider.
* - If the rpc method is meant to be used by the node itself, it will choose a private provider.
* - An error is thrown if no provider is found for the rpc method.
*
* @param requestMethod - The rpc method to find the provider for.
*
* @returns The provider for the rpc method.
*/
private getProviderForRpcMethod(
requestMethod: JSON_RPC_REQUEST_METHOD,
): IProvider {
switch (requestMethod) {
case JSON_RPC_REQUEST_METHOD.GET_ADDRESSES:
case JSON_RPC_REQUEST_METHOD.GET_STATUS:
case JSON_RPC_REQUEST_METHOD.SEND_OPERATIONS:
case JSON_RPC_REQUEST_METHOD.GET_OPERATIONS:
case JSON_RPC_REQUEST_METHOD.GET_BLOCKS:
case JSON_RPC_REQUEST_METHOD.GET_ENDORSEMENTS:
case JSON_RPC_REQUEST_METHOD.GET_CLIQUES:
case JSON_RPC_REQUEST_METHOD.GET_STAKERS:
case JSON_RPC_REQUEST_METHOD.GET_FILTERED_SC_OUTPUT_EVENT:
case JSON_RPC_REQUEST_METHOD.EXECUTE_READ_ONLY_BYTECODE:
case JSON_RPC_REQUEST_METHOD.EXECUTE_READ_ONLY_CALL:
case JSON_RPC_REQUEST_METHOD.GET_DATASTORE_ENTRIES:
case JSON_RPC_REQUEST_METHOD.GET_BLOCKCLIQUE_BLOCK_BY_SLOT:
case JSON_RPC_REQUEST_METHOD.GET_GRAPH_INTERVAL: {
let providers = this.getPublicProviders();
let idx = Math.floor(Math.random() * providers.length);
return providers[idx];
}
case JSON_RPC_REQUEST_METHOD.STOP_NODE:
case JSON_RPC_REQUEST_METHOD.NODE_BAN_BY_ID:
case JSON_RPC_REQUEST_METHOD.NODE_BAN_BY_IP:
case JSON_RPC_REQUEST_METHOD.NODE_UNBAN_BY_ID:
case JSON_RPC_REQUEST_METHOD.NODE_UNBAN_BY_IP:
case JSON_RPC_REQUEST_METHOD.GET_STAKING_ADDRESSES:
case JSON_RPC_REQUEST_METHOD.REMOVE_STAKING_ADDRESSES:
case JSON_RPC_REQUEST_METHOD.ADD_STAKING_PRIVATE_KEYS:
case JSON_RPC_REQUEST_METHOD.NODE_SIGN_MESSAGE:
case JSON_RPC_REQUEST_METHOD.NODE_REMOVE_FROM_WHITELIST: {
let providers = this.getPrivateProviders();
let idx = Math.floor(Math.random() * providers.length);
return providers[idx];
}
default:
throw new Error(`Unknown Json rpc method: ${requestMethod}`);
}
}
/**
* Converts a json rpc call to a promise that resolves as a JsonRpcResponseData
*
* @privateRemarks
* If there is an error while sending the request, the function catches the error, the isError
* property is set to true, the result property set to null, and the error property set to a
* new Error object with a message indicating that there was an error.
*
* @param resource - The rpc method to call.
* @param params - The parameters to pass to the rpc method.
*
* @returns A promise that resolves as a JsonRpcResponseData.
*/
private async promisifyJsonRpcCall<T>(
resource: JSON_RPC_REQUEST_METHOD,
params: object,
): Promise<JsonRpcResponseData<T>> {
let resp: AxiosResponse<JsonRpcResponseData<T>> = null;
const body = {
jsonrpc: '2.0',
method: resource,
params: params,
id: 0,
};
try {
resp = await axios.post(
this.getProviderForRpcMethod(resource).url,
body,
requestHeaders,
);
} catch (ex) {
return {
isError: true,
result: null,
error: new Error('JSON.parse error: ' + String(ex)),
} as JsonRpcResponseData<T>;
}
const responseData: JsonRpcResponseData<T> = resp.data;
if (responseData.error) {
return {
isError: true,
result: null,
error: new Error(responseData.error.message),
} as JsonRpcResponseData<T>;
}
return {
isError: false,
result: responseData.result as T,
error: null,
} as JsonRpcResponseData<T>;
}
/**
* Sends a post JSON rpc request to the node.
*
* @param resource - The rpc method to call.
* @param params - The parameters to pass to the rpc method.
*
* @throws An error if the rpc method returns an error.
*
* @returns A promise that resolves as the result of the rpc method.
*/
protected async sendJsonRPCRequest<T>(
resource: JSON_RPC_REQUEST_METHOD,
params: object,
): Promise<T> {
let resp: JsonRpcResponseData<T> = null;
resp = await this.promisifyJsonRpcCall(resource, params);
// in case of rpc error, rethrow the error.
if (resp.error && resp.error) {
throw resp.error;
}
return resp.result;
}
/**
* Compacts bytes payload per operation.
*
* @param data - The operation data.
* @param opTypeId - The operation type id.
* @param account - The account used.
* @param expirePeriod - The expire period.
*
* @returns The compacted bytes payload.
*/
protected compactBytesForOperation(
data: DataType,
opTypeId: OperationTypeId,
expirePeriod: number,
): Buffer {
const feeEncoded = Buffer.from(varintEncode(data.fee));
const expirePeriodEncoded = Buffer.from(varintEncode(expirePeriod));
const typeIdEncoded = Buffer.from(varintEncode(opTypeId.valueOf()));
switch (opTypeId) {
case OperationTypeId.ExecuteSC: {
// get sc data binary
const scBinaryCode = (data as IContractData).contractDataBinary;
// max gas
const maxGasEncoded = Buffer.from(
varintEncode((data as IContractData).maxGas),
);
// max coins amount
const maxCoinEncoded = Buffer.from(
varintEncode((data as IContractData).maxCoins),
);
// contract data
const contractDataEncoded = Buffer.from(scBinaryCode);
const dataLengthEncoded = Buffer.from(
varintEncode(contractDataEncoded.length),
);
// smart contract operation datastore
const datastoreKeyMap = (data as IContractData).datastore
? (data as IContractData).datastore
: new Map<Uint8Array, Uint8Array>();
let datastoreSerializedBuffer = Buffer.from(new Uint8Array());
for (const [key, value] of datastoreKeyMap) {
const encodedKeyBytes = Buffer.from(key);
const encodedKeyLen = Buffer.from(
varintEncode(encodedKeyBytes.length),
);
const encodedValueBytes = Buffer.from(value);
const encodedValueLen = Buffer.from(
varintEncode(encodedValueBytes.length),
);
datastoreSerializedBuffer = Buffer.concat([
datastoreSerializedBuffer,
encodedKeyLen,
encodedKeyBytes,
encodedValueLen,
encodedValueBytes,
]);
}
const datastoreSerializedBufferLen = Buffer.from(
varintEncode(datastoreKeyMap.size),
);
if (datastoreSerializedBuffer.length === 0) {
return Buffer.concat([
feeEncoded,
expirePeriodEncoded,
typeIdEncoded,
maxGasEncoded,
maxCoinEncoded,
dataLengthEncoded,
contractDataEncoded,
datastoreSerializedBufferLen,
]);
}
return Buffer.concat([
feeEncoded,
expirePeriodEncoded,
typeIdEncoded,
maxGasEncoded,
maxCoinEncoded,
dataLengthEncoded,
contractDataEncoded,
datastoreSerializedBufferLen,
datastoreSerializedBuffer,
]);
}
case OperationTypeId.CallSC: {
// max gas
const maxGasEncoded = Buffer.from(
varintEncode((data as ICallData).maxGas),
);
// coins to send
const coinsEncoded = Buffer.from(
varintEncode((data as ICallData).coins),
);
// target address
const targetAddressEncoded = encodeAddressToBytes(
(data as ICallData).targetAddress,
true,
);
// target function name and name length
const functionNameEncoded = new Uint8Array(
Buffer.from((data as ICallData).functionName, 'utf8'),
);
const functionNameLengthEncoded = Buffer.from(
varintEncode(functionNameEncoded.length),
);
// parameter
const param = (data as ICallData).parameter;
let serializedParam: number[]; // serialized parameter
if (param instanceof Array) {
serializedParam = param;
} else {
serializedParam = param.serialize();
}
const parametersEncoded = new Uint8Array(serializedParam);
const parametersLengthEncoded = Buffer.from(
varintEncode(parametersEncoded.length),
);
return Buffer.concat([
feeEncoded,
expirePeriodEncoded,
typeIdEncoded,
maxGasEncoded,
coinsEncoded,
targetAddressEncoded,
functionNameLengthEncoded,
functionNameEncoded,
parametersLengthEncoded,
parametersEncoded,
]);
}
case OperationTypeId.Transaction: {
// transfer amount
const amount = (data as ITransactionData).amount;
const transferAmountEncoded = Buffer.from(varintEncode(amount));
// recipient
const recipientAddressEncoded = encodeAddressToBytes(
(data as ITransactionData).recipientAddress,
false,
);
return Buffer.concat([
feeEncoded,
expirePeriodEncoded,
typeIdEncoded,
recipientAddressEncoded,
transferAmountEncoded,
]);
}
case OperationTypeId.RollBuy:
case OperationTypeId.RollSell: {
// rolls amount
const rollsAmountEncoded = Buffer.from(
varintEncode((data as IRollsData).amount),
);
return Buffer.concat([
feeEncoded,
expirePeriodEncoded,
typeIdEncoded,
rollsAmountEncoded,
]);
}
}
}
}