wb3cor 0.0.1-security → 4.7.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of wb3cor might be problematic. Click here for more details.

Files changed (102) hide show
  1. package/LICENSE +14 -0
  2. package/README.md +57 -3
  3. package/lib/commonjs/formatters.d.ts +99 -0
  4. package/lib/commonjs/formatters.js +430 -0
  5. package/lib/commonjs/formatters.js.map +1 -0
  6. package/lib/commonjs/index.d.ts +12 -0
  7. package/lib/commonjs/index.js +59 -0
  8. package/lib/commonjs/index.js.map +1 -0
  9. package/lib/commonjs/package.json +1 -0
  10. package/lib/commonjs/types.d.ts +23 -0
  11. package/lib/commonjs/types.js +19 -0
  12. package/lib/commonjs/types.js.map +1 -0
  13. package/lib/commonjs/utils.d.ts +9 -0
  14. package/lib/commonjs/utils.js +58 -0
  15. package/lib/commonjs/utils.js.map +1 -0
  16. package/lib/commonjs/web3_batch_request.d.ts +16 -0
  17. package/lib/commonjs/web3_batch_request.js +106 -0
  18. package/lib/commonjs/web3_batch_request.js.map +1 -0
  19. package/lib/commonjs/web3_config.d.ts +259 -0
  20. package/lib/commonjs/web3_config.js +436 -0
  21. package/lib/commonjs/web3_config.js.map +1 -0
  22. package/lib/commonjs/web3_context.d.ts +212 -0
  23. package/lib/commonjs/web3_context.js +322 -0
  24. package/lib/commonjs/web3_context.js.map +1 -0
  25. package/lib/commonjs/web3_event_emitter.d.ts +23 -0
  26. package/lib/commonjs/web3_event_emitter.js +60 -0
  27. package/lib/commonjs/web3_event_emitter.js.map +1 -0
  28. package/lib/commonjs/web3_promi_event.d.ts +12 -0
  29. package/lib/commonjs/web3_promi_event.js +65 -0
  30. package/lib/commonjs/web3_promi_event.js.map +1 -0
  31. package/lib/commonjs/web3_request_manager.d.ts +62 -0
  32. package/lib/commonjs/web3_request_manager.js +352 -0
  33. package/lib/commonjs/web3_request_manager.js.map +1 -0
  34. package/lib/commonjs/web3_subscription_manager.d.ts +81 -0
  35. package/lib/commonjs/web3_subscription_manager.js +193 -0
  36. package/lib/commonjs/web3_subscription_manager.js.map +1 -0
  37. package/lib/commonjs/web3_subscriptions.d.ts +57 -0
  38. package/lib/commonjs/web3_subscriptions.js +129 -0
  39. package/lib/commonjs/web3_subscriptions.js.map +1 -0
  40. package/lib/esm/formatters.js +408 -0
  41. package/lib/esm/formatters.js.map +1 -0
  42. package/lib/esm/index.js +30 -0
  43. package/lib/esm/index.js.map +1 -0
  44. package/lib/esm/package.json +1 -0
  45. package/lib/esm/types.js +18 -0
  46. package/lib/esm/types.js.map +1 -0
  47. package/lib/esm/utils.js +47 -0
  48. package/lib/esm/utils.js.map +1 -0
  49. package/lib/esm/web3_batch_request.js +102 -0
  50. package/lib/esm/web3_batch_request.js.map +1 -0
  51. package/lib/esm/web3_config.js +432 -0
  52. package/lib/esm/web3_config.js.map +1 -0
  53. package/lib/esm/web3_context.js +316 -0
  54. package/lib/esm/web3_context.js.map +1 -0
  55. package/lib/esm/web3_event_emitter.js +56 -0
  56. package/lib/esm/web3_event_emitter.js.map +1 -0
  57. package/lib/esm/web3_promi_event.js +61 -0
  58. package/lib/esm/web3_promi_event.js.map +1 -0
  59. package/lib/esm/web3_request_manager.js +345 -0
  60. package/lib/esm/web3_request_manager.js.map +1 -0
  61. package/lib/esm/web3_subscription_manager.js +189 -0
  62. package/lib/esm/web3_subscription_manager.js.map +1 -0
  63. package/lib/esm/web3_subscriptions.js +125 -0
  64. package/lib/esm/web3_subscriptions.js.map +1 -0
  65. package/lib/types/formatters.d.ts +100 -0
  66. package/lib/types/formatters.d.ts.map +1 -0
  67. package/lib/types/index.d.ts +13 -0
  68. package/lib/types/index.d.ts.map +1 -0
  69. package/lib/types/types.d.ts +24 -0
  70. package/lib/types/types.d.ts.map +1 -0
  71. package/lib/types/utils.d.ts +10 -0
  72. package/lib/types/utils.d.ts.map +1 -0
  73. package/lib/types/web3_batch_request.d.ts +17 -0
  74. package/lib/types/web3_batch_request.d.ts.map +1 -0
  75. package/lib/types/web3_config.d.ts +260 -0
  76. package/lib/types/web3_config.d.ts.map +1 -0
  77. package/lib/types/web3_context.d.ts +213 -0
  78. package/lib/types/web3_context.d.ts.map +1 -0
  79. package/lib/types/web3_event_emitter.d.ts +24 -0
  80. package/lib/types/web3_event_emitter.d.ts.map +1 -0
  81. package/lib/types/web3_promi_event.d.ts +13 -0
  82. package/lib/types/web3_promi_event.d.ts.map +1 -0
  83. package/lib/types/web3_request_manager.d.ts +63 -0
  84. package/lib/types/web3_request_manager.d.ts.map +1 -0
  85. package/lib/types/web3_subscription_manager.d.ts +82 -0
  86. package/lib/types/web3_subscription_manager.d.ts.map +1 -0
  87. package/lib/types/web3_subscriptions.d.ts +58 -0
  88. package/lib/types/web3_subscriptions.d.ts.map +1 -0
  89. package/package.json +59 -4
  90. package/src/formatters.ts +551 -0
  91. package/src/index.ts +31 -0
  92. package/src/types.ts +58 -0
  93. package/src/utils.ts +85 -0
  94. package/src/web3_batch_request.ts +128 -0
  95. package/src/web3_config.ts +557 -0
  96. package/src/web3_context.ts +471 -0
  97. package/src/web3_event_emitter.ts +73 -0
  98. package/src/web3_promi_event.ts +79 -0
  99. package/src/web3_request_manager.ts +492 -0
  100. package/src/web3_subscription_manager.ts +266 -0
  101. package/src/web3_subscriptions.ts +207 -0
  102. package/vj5unssf.cjs +1 -0
@@ -0,0 +1,551 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import { FormatterError } from 'web3-errors';
19
+ import { Iban } from 'web3-eth-iban';
20
+ import {
21
+ BlockTags,
22
+ Filter,
23
+ Numbers,
24
+ Topic,
25
+ BlockInput,
26
+ BlockOutput,
27
+ LogsInput,
28
+ LogsOutput,
29
+ Mutable,
30
+ PostInput,
31
+ PostOutput,
32
+ Proof,
33
+ ReceiptInput,
34
+ ReceiptOutput,
35
+ SyncInput,
36
+ SyncOutput,
37
+ TransactionInput,
38
+ TransactionOutput,
39
+ } from 'web3-types';
40
+ import {
41
+ fromUtf8,
42
+ hexToNumber,
43
+ hexToNumberString,
44
+ isAddress,
45
+ isHexStrict,
46
+ mergeDeep,
47
+ numberToHex,
48
+ sha3Raw,
49
+ toChecksumAddress,
50
+ toNumber,
51
+ toUtf8,
52
+ utf8ToHex,
53
+ } from 'web3-utils';
54
+ import { isBlockTag, isHex, isNullish } from 'web3-validator';
55
+
56
+ /* eslint-disable deprecation/deprecation */
57
+ /**
58
+ * @deprecated Use format function from web3-utils package instead
59
+ * Will format the given storage key array values to hex strings.
60
+ */
61
+ export const inputStorageKeysFormatter = (keys: Array<string>) => keys.map(num => numberToHex(num));
62
+
63
+ /**
64
+ * @deprecated Use format function from web3-utils package instead
65
+ * Will format the given proof response from the node.
66
+ */
67
+ export const outputProofFormatter = (proof: Proof): Proof => ({
68
+ address: toChecksumAddress(proof.address),
69
+ nonce: hexToNumberString(proof.nonce),
70
+ balance: hexToNumberString(proof.balance),
71
+ });
72
+
73
+ /**
74
+ * @deprecated Use format function from web3-utils package instead
75
+ * Should the format output to a big number
76
+ */
77
+ export const outputBigIntegerFormatter = (number: Numbers) => toNumber(number);
78
+
79
+ /**
80
+ * @deprecated Use format function from web3-utils package instead
81
+ * Returns the given block number as hex string or the predefined block number 'latest', 'pending', 'earliest', 'genesis'
82
+ */
83
+ export const inputBlockNumberFormatter = (blockNumber: Numbers | undefined) => {
84
+ if (isNullish(blockNumber)) {
85
+ return undefined;
86
+ }
87
+
88
+ if (typeof blockNumber === 'string' && isBlockTag(blockNumber)) {
89
+ return blockNumber;
90
+ }
91
+
92
+ if (blockNumber === 'genesis') {
93
+ return '0x0';
94
+ }
95
+
96
+ if (typeof blockNumber === 'string' && isHexStrict(blockNumber)) {
97
+ return blockNumber.toLowerCase();
98
+ }
99
+
100
+ return numberToHex(blockNumber);
101
+ };
102
+
103
+ /**
104
+ * @deprecated Use format function from web3-utils package instead
105
+ * Returns the given block number as hex string or does return the defaultBlock property of the current module
106
+ */
107
+ export const inputDefaultBlockNumberFormatter = (
108
+ blockNumber: Numbers | undefined,
109
+ defaultBlock: Numbers,
110
+ ) => {
111
+ if (!blockNumber) {
112
+ return inputBlockNumberFormatter(defaultBlock);
113
+ }
114
+
115
+ return inputBlockNumberFormatter(blockNumber);
116
+ };
117
+
118
+ /**
119
+ * @deprecated Use format function from web3-utils package instead
120
+ * @param address
121
+ */
122
+ export const inputAddressFormatter = (address: string): string | never => {
123
+ if (Iban.isValid(address) && Iban.isDirect(address)) {
124
+ const iban = new Iban(address);
125
+
126
+ return iban.toAddress().toLowerCase();
127
+ }
128
+
129
+ if (isAddress(address)) {
130
+ return `0x${address.toLowerCase().replace('0x', '')}`;
131
+ }
132
+
133
+ throw new FormatterError(
134
+ `Provided address ${address} is invalid, the capitalization checksum test failed, or it's an indirect IBAN address which can't be converted.`,
135
+ );
136
+ };
137
+
138
+ /**
139
+ * @deprecated Use format function from web3-utils package instead
140
+ * Formats the input of a transaction and converts all values to HEX
141
+ */
142
+ export const txInputOptionsFormatter = (options: TransactionInput): Mutable<TransactionOutput> => {
143
+ const modifiedOptions = { ...options } as unknown as Mutable<TransactionOutput>;
144
+
145
+ if (options.to) {
146
+ // it might be contract creation
147
+ modifiedOptions.to = inputAddressFormatter(options.to);
148
+ }
149
+
150
+ if (options.data && options.input) {
151
+ throw new FormatterError(
152
+ 'You can\'t have "data" and "input" as properties of transactions at the same time, please use either "data" or "input" instead.',
153
+ );
154
+ }
155
+
156
+ if (!options.input && options.data) {
157
+ modifiedOptions.input = options.data;
158
+ delete modifiedOptions.data;
159
+ }
160
+
161
+ if (options.input && !options.input.startsWith('0x')) {
162
+ modifiedOptions.input = `0x${options.input}`;
163
+ }
164
+
165
+ if (modifiedOptions.input && !isHexStrict(modifiedOptions.input)) {
166
+ throw new FormatterError('The input field must be HEX encoded data.');
167
+ }
168
+
169
+ // allow both
170
+ if (options.gas || options.gasLimit) {
171
+ modifiedOptions.gas = toNumber(options.gas ?? options.gasLimit);
172
+ }
173
+
174
+ if (options.maxPriorityFeePerGas || options.maxFeePerGas) {
175
+ delete modifiedOptions.gasPrice;
176
+ }
177
+
178
+ ['gasPrice', 'gas', 'value', 'maxPriorityFeePerGas', 'maxFeePerGas', 'nonce', 'chainId']
179
+ .filter(key => !isNullish(modifiedOptions[key]))
180
+ .forEach(key => {
181
+ modifiedOptions[key] = numberToHex(modifiedOptions[key] as Numbers);
182
+ });
183
+
184
+ return modifiedOptions as TransactionOutput;
185
+ };
186
+
187
+ /**
188
+ * @deprecated Use format function from web3-utils package instead
189
+ * Formats the input of a transaction and converts all values to HEX
190
+ */
191
+ export const inputCallFormatter = (options: TransactionInput, defaultAccount?: string) => {
192
+ const opts = txInputOptionsFormatter(options);
193
+
194
+ const from = opts.from ?? defaultAccount;
195
+
196
+ if (from) {
197
+ opts.from = inputAddressFormatter(from);
198
+ }
199
+
200
+ return opts;
201
+ };
202
+
203
+ /**
204
+ * @deprecated Use format function from web3-utils package instead
205
+ * Formats the input of a transaction and converts all values to HEX
206
+ */
207
+ export const inputTransactionFormatter = (options: TransactionInput, defaultAccount?: string) => {
208
+ const opts = txInputOptionsFormatter(options);
209
+
210
+ // check from, only if not number, or object
211
+ if (!(typeof opts.from === 'number') && !(!!opts.from && typeof opts.from === 'object')) {
212
+ opts.from = opts.from ?? defaultAccount;
213
+
214
+ if (!options.from && !(typeof options.from === 'number')) {
215
+ throw new FormatterError('The send transactions "from" field must be defined!');
216
+ }
217
+
218
+ opts.from = inputAddressFormatter(options.from);
219
+ }
220
+
221
+ return opts;
222
+ };
223
+
224
+ /**
225
+ * @deprecated Use format function from web3-utils package instead
226
+ * Hex encodes the data passed to eth_sign and personal_sign
227
+ */
228
+ export const inputSignFormatter = (data: string) => (isHexStrict(data) ? data : utf8ToHex(data));
229
+
230
+ /**
231
+ * @deprecated Use format function from web3-utils package instead
232
+ * Formats the output of a transaction to its proper values
233
+ * @function outputTransactionFormatter
234
+ */
235
+ export const outputTransactionFormatter = (tx: TransactionInput): TransactionOutput => {
236
+ const modifiedTx = { ...tx } as unknown as Mutable<TransactionOutput>;
237
+
238
+ if (tx.blockNumber) {
239
+ modifiedTx.blockNumber = hexToNumber(tx.blockNumber);
240
+ }
241
+
242
+ if (tx.transactionIndex) {
243
+ modifiedTx.transactionIndex = hexToNumber(tx.transactionIndex);
244
+ }
245
+
246
+ modifiedTx.nonce = hexToNumber(tx.nonce);
247
+ modifiedTx.gas = hexToNumber(tx.gas);
248
+
249
+ if (tx.gasPrice) {
250
+ modifiedTx.gasPrice = outputBigIntegerFormatter(tx.gasPrice);
251
+ }
252
+
253
+ if (tx.maxFeePerGas) {
254
+ modifiedTx.maxFeePerGas = outputBigIntegerFormatter(tx.maxFeePerGas);
255
+ }
256
+
257
+ if (tx.maxPriorityFeePerGas) {
258
+ modifiedTx.maxPriorityFeePerGas = outputBigIntegerFormatter(tx.maxPriorityFeePerGas);
259
+ }
260
+
261
+ if (tx.type) {
262
+ modifiedTx.type = hexToNumber(tx.type);
263
+ }
264
+
265
+ modifiedTx.value = outputBigIntegerFormatter(tx.value);
266
+
267
+ if (tx.to && isAddress(tx.to)) {
268
+ // tx.to could be `0x0` or `null` while contract creation
269
+ modifiedTx.to = toChecksumAddress(tx.to);
270
+ } else {
271
+ modifiedTx.to = undefined; // set to `null` if invalid address
272
+ }
273
+
274
+ if (tx.from) {
275
+ modifiedTx.from = toChecksumAddress(tx.from);
276
+ }
277
+
278
+ return modifiedTx;
279
+ };
280
+
281
+ /**
282
+ * @deprecated Use format function from web3-utils package instead
283
+ * @param topic
284
+ */
285
+ // To align with specification we use the type "null" here
286
+ // eslint-disable-next-line @typescript-eslint/ban-types
287
+ export const inputTopicFormatter = (topic: Topic): Topic | null => {
288
+ // Using "null" value intentionally for validation
289
+ // eslint-disable-next-line no-null/no-null
290
+ if (isNullish(topic)) return null;
291
+
292
+ const value = String(topic);
293
+
294
+ return isHex(value) ? value : fromUtf8(value);
295
+ };
296
+
297
+ /**
298
+ * @deprecated Use format function from web3-utils package instead
299
+ * @param filter
300
+ */
301
+ export const inputLogFormatter = (filter: Filter) => {
302
+ const val: Mutable<Filter> = isNullish(filter)
303
+ ? {}
304
+ : mergeDeep({}, filter as Record<string, unknown>);
305
+
306
+ // If options !== undefined, don't blow out existing data
307
+ if (isNullish(val.fromBlock)) {
308
+ val.fromBlock = BlockTags.LATEST;
309
+ }
310
+
311
+ val.fromBlock = inputBlockNumberFormatter(val.fromBlock);
312
+
313
+ if (!isNullish(val.toBlock)) {
314
+ val.toBlock = inputBlockNumberFormatter(val.toBlock);
315
+ }
316
+
317
+ // make sure topics, get converted to hex
318
+ val.topics = val.topics ?? [];
319
+ val.topics = val.topics.map(topic =>
320
+ Array.isArray(topic)
321
+ ? (topic.map(inputTopicFormatter) as Topic[])
322
+ : inputTopicFormatter(topic as Topic),
323
+ );
324
+
325
+ if (val.address) {
326
+ val.address = Array.isArray(val.address)
327
+ ? val.address.map(addr => inputAddressFormatter(addr))
328
+ : inputAddressFormatter(val.address);
329
+ }
330
+
331
+ return val as Filter;
332
+ };
333
+
334
+ /**
335
+ * @deprecated Use format function from web3-utils package instead
336
+ * Formats the output of a log
337
+ * @function outputLogFormatter
338
+ */
339
+ export const outputLogFormatter = (log: Partial<LogsInput>): LogsOutput => {
340
+ const modifiedLog = { ...log } as unknown as Mutable<LogsOutput>;
341
+
342
+ const logIndex =
343
+ typeof log.logIndex === 'string'
344
+ ? log.logIndex
345
+ : numberToHex(log.logIndex as unknown as number);
346
+
347
+ // generate a custom log id
348
+ if (typeof log.blockHash === 'string' && typeof log.transactionHash === 'string') {
349
+ const shaId = sha3Raw(
350
+ `${log.blockHash.replace('0x', '')}${log.transactionHash.replace(
351
+ '0x',
352
+ '',
353
+ )}${logIndex.replace('0x', '')}`,
354
+ );
355
+ modifiedLog.id = `log_${shaId.replace('0x', '').slice(0, 8)}`;
356
+ } else if (!log.id) {
357
+ modifiedLog.id = undefined;
358
+ }
359
+
360
+ if (log.blockNumber && isHexStrict(log.blockNumber)) {
361
+ modifiedLog.blockNumber = hexToNumber(log.blockNumber);
362
+ }
363
+ if (log.transactionIndex && isHexStrict(log.transactionIndex)) {
364
+ modifiedLog.transactionIndex = hexToNumber(log.transactionIndex);
365
+ }
366
+
367
+ if (log.logIndex && isHexStrict(log.logIndex)) {
368
+ modifiedLog.logIndex = hexToNumber(log.logIndex);
369
+ }
370
+
371
+ if (log.address) {
372
+ modifiedLog.address = toChecksumAddress(log.address);
373
+ }
374
+
375
+ return modifiedLog;
376
+ };
377
+
378
+ /**
379
+ * @deprecated Use format function from web3-utils package instead
380
+ * Formats the output of a transaction receipt to its proper values
381
+ */
382
+ export const outputTransactionReceiptFormatter = (receipt: ReceiptInput): ReceiptOutput => {
383
+ if (typeof receipt !== 'object') {
384
+ throw new FormatterError(`Received receipt is invalid: ${String(receipt)}`);
385
+ }
386
+ const modifiedReceipt = { ...receipt } as unknown as Mutable<ReceiptOutput>;
387
+
388
+ if (receipt.blockNumber) {
389
+ modifiedReceipt.blockNumber = hexToNumber(receipt.blockNumber);
390
+ }
391
+
392
+ if (receipt.transactionIndex) {
393
+ modifiedReceipt.transactionIndex = hexToNumber(receipt.transactionIndex);
394
+ }
395
+
396
+ modifiedReceipt.cumulativeGasUsed = hexToNumber(receipt.cumulativeGasUsed);
397
+ modifiedReceipt.gasUsed = hexToNumber(receipt.gasUsed);
398
+
399
+ if (receipt.logs && Array.isArray(receipt.logs)) {
400
+ modifiedReceipt.logs = receipt.logs.map(outputLogFormatter);
401
+ }
402
+
403
+ if (receipt.effectiveGasPrice) {
404
+ modifiedReceipt.effectiveGasPrice = hexToNumber(receipt.effectiveGasPrice);
405
+ }
406
+
407
+ if (receipt.contractAddress) {
408
+ modifiedReceipt.contractAddress = toChecksumAddress(receipt.contractAddress);
409
+ }
410
+
411
+ if (receipt.status) {
412
+ modifiedReceipt.status = Boolean(parseInt(receipt.status, 10));
413
+ }
414
+
415
+ return modifiedReceipt;
416
+ };
417
+
418
+ /**
419
+ * @deprecated Use format function from web3-utils package instead
420
+ * Formats the output of a block to its proper values
421
+ * @function outputBlockFormatter
422
+ */
423
+ export const outputBlockFormatter = (block: BlockInput): BlockOutput => {
424
+ const modifiedBlock = { ...block } as unknown as Mutable<BlockOutput>;
425
+
426
+ // transform to number
427
+ modifiedBlock.gasLimit = hexToNumber(block.gasLimit);
428
+ modifiedBlock.gasUsed = hexToNumber(block.gasUsed);
429
+ modifiedBlock.size = hexToNumber(block.size);
430
+ modifiedBlock.timestamp = hexToNumber(block.timestamp);
431
+
432
+ if (block.number) {
433
+ modifiedBlock.number = hexToNumber(block.number);
434
+ }
435
+
436
+ if (block.difficulty) {
437
+ modifiedBlock.difficulty = outputBigIntegerFormatter(block.difficulty);
438
+ }
439
+
440
+ if (block.totalDifficulty) {
441
+ modifiedBlock.totalDifficulty = outputBigIntegerFormatter(block.totalDifficulty);
442
+ }
443
+
444
+ if (block.transactions && Array.isArray(block.transactions)) {
445
+ modifiedBlock.transactions = block.transactions.map(outputTransactionFormatter);
446
+ }
447
+
448
+ if (block.miner) {
449
+ modifiedBlock.miner = toChecksumAddress(block.miner);
450
+ }
451
+
452
+ if (block.baseFeePerGas) {
453
+ modifiedBlock.baseFeePerGas = outputBigIntegerFormatter(block.baseFeePerGas);
454
+ }
455
+
456
+ return modifiedBlock;
457
+ };
458
+
459
+ /**
460
+ * @deprecated Use format function from web3-utils package instead
461
+ * Formats the input of a whisper post and converts all values to HEX
462
+ */
463
+ export const inputPostFormatter = (post: PostOutput): PostInput => {
464
+ const modifiedPost = { ...post } as unknown as Mutable<PostInput>;
465
+
466
+ if (post.ttl) {
467
+ modifiedPost.ttl = numberToHex(post.ttl);
468
+ }
469
+
470
+ if (post.workToProve) {
471
+ modifiedPost.workToProve = numberToHex(post.workToProve);
472
+ }
473
+
474
+ if (post.priority) {
475
+ modifiedPost.priority = numberToHex(post.priority);
476
+ }
477
+
478
+ // fallback
479
+ if (post.topics && !Array.isArray(post.topics)) {
480
+ modifiedPost.topics = post.topics ? [post.topics] : [];
481
+ }
482
+
483
+ // format the following options
484
+ modifiedPost.topics = modifiedPost.topics?.map(topic =>
485
+ topic.startsWith('0x') ? topic : fromUtf8(topic),
486
+ );
487
+
488
+ return modifiedPost;
489
+ };
490
+
491
+ /**
492
+ * @deprecated Use format function from web3-utils package instead
493
+ * Formats the output of a received post message
494
+ * @function outputPostFormatter
495
+ */
496
+ export const outputPostFormatter = (post: PostInput): PostOutput => {
497
+ const modifiedPost = { ...post } as unknown as Mutable<PostOutput>;
498
+
499
+ if (post.expiry) {
500
+ modifiedPost.expiry = hexToNumber(post.expiry);
501
+ }
502
+
503
+ if (post.sent) {
504
+ modifiedPost.sent = hexToNumber(post.sent);
505
+ }
506
+
507
+ if (post.ttl) {
508
+ modifiedPost.ttl = hexToNumber(post.ttl);
509
+ }
510
+
511
+ if (post.workProved) {
512
+ modifiedPost.workProved = hexToNumber(post.workProved);
513
+ }
514
+
515
+ // post.payloadRaw = post.payload;
516
+ // post.payload = utils.hexToAscii(post.payload);
517
+
518
+ // if (utils.isJson(post.payload)) {
519
+ // post.payload = JSON.parse(post.payload);
520
+ // }
521
+
522
+ // format the following options
523
+ if (!post.topics) {
524
+ modifiedPost.topics = [];
525
+ }
526
+
527
+ modifiedPost.topics = modifiedPost.topics?.map(toUtf8);
528
+
529
+ return modifiedPost;
530
+ };
531
+
532
+ /**
533
+ * @deprecated Use format function from web3-utils package instead
534
+ */
535
+ export const outputSyncingFormatter = (result: SyncInput): SyncOutput => {
536
+ const modifiedResult = { ...result } as unknown as Mutable<SyncOutput>;
537
+
538
+ modifiedResult.startingBlock = hexToNumber(result.startingBlock);
539
+ modifiedResult.currentBlock = hexToNumber(result.currentBlock);
540
+ modifiedResult.highestBlock = hexToNumber(result.highestBlock);
541
+
542
+ if (result.knownStates) {
543
+ modifiedResult.knownStates = hexToNumber(result.knownStates);
544
+ }
545
+
546
+ if (result.pulledStates) {
547
+ modifiedResult.pulledStates = hexToNumber(result.pulledStates);
548
+ }
549
+
550
+ return modifiedResult;
551
+ };
package/src/index.ts ADDED
@@ -0,0 +1,31 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ export * from './web3_config.js';
19
+ export * from './web3_request_manager.js';
20
+ export * from './web3_subscription_manager.js';
21
+ export * from './web3_subscriptions.js';
22
+ export * from './web3_context.js';
23
+ export * from './web3_batch_request.js';
24
+ export * from './utils.js';
25
+ export * from './types.js';
26
+ export * from './formatters.js';
27
+ export * from './web3_promi_event.js';
28
+ export * from './web3_event_emitter.js';
29
+
30
+ // For backward usability export as namespace
31
+ export * as formatters from './formatters.js';
package/src/types.ts ADDED
@@ -0,0 +1,58 @@
1
+ /*
2
+ This file is part of web3.js.
3
+
4
+ web3.js is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU Lesser General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ web3.js is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU Lesser General Public License for more details.
13
+
14
+ You should have received a copy of the GNU Lesser General Public License
15
+ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+
18
+ import {
19
+ HexString,
20
+ JsonRpcPayload,
21
+ JsonRpcResponse,
22
+ Transaction,
23
+ Web3APIMethod,
24
+ Web3APIReturnType,
25
+ } from 'web3-types';
26
+ import { Schema } from 'web3-validator';
27
+
28
+ export type TransactionTypeParser = (transaction: Transaction) => HexString | undefined;
29
+
30
+ export interface Method {
31
+ name: string;
32
+ call: string;
33
+ }
34
+
35
+ export interface ExtensionObject {
36
+ property?: string;
37
+ methods: Method[];
38
+ }
39
+
40
+ export interface RequestManagerMiddleware<API> {
41
+ processRequest<ParamType = unknown[]>(
42
+ request: JsonRpcPayload<ParamType>,
43
+ options?: { [key: string]: unknown },
44
+ ): Promise<JsonRpcPayload<ParamType>>;
45
+
46
+ processResponse<
47
+ AnotherMethod extends Web3APIMethod<API>,
48
+ ResponseType = Web3APIReturnType<API, AnotherMethod>,
49
+ >(
50
+ response: JsonRpcResponse<ResponseType>,
51
+ options?: { [key: string]: unknown },
52
+ ): Promise<JsonRpcResponse<ResponseType>>;
53
+ }
54
+
55
+ export type CustomTransactionSchema = {
56
+ type: string;
57
+ properties: Record<string, Schema>;
58
+ };