@witnet/sdk 1.2.2 → 1.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/.env_witnet +2 -1
  2. package/dist/package.json +4 -4
  3. package/dist/src/bin/helpers.js +1 -1
  4. package/dist/src/index.js +1 -1
  5. package/dist/src/lib/crypto/account.js +1 -1
  6. package/dist/src/lib/crypto/coinbase.js +1 -1
  7. package/dist/src/lib/crypto/index.js +1 -1
  8. package/dist/src/lib/crypto/interfaces.js +1 -1
  9. package/dist/src/lib/crypto/payloads/DataRequestPayload.js +1 -1
  10. package/dist/src/lib/crypto/payloads/StakePayload.js +1 -1
  11. package/dist/src/lib/crypto/payloads/UnstakePayload.js +1 -1
  12. package/dist/src/lib/crypto/payloads/ValueTransferPayload.js +1 -1
  13. package/dist/src/lib/crypto/payloads.js +1 -1
  14. package/dist/src/lib/crypto/signer.js +1 -1
  15. package/dist/src/lib/crypto/transmitters/DataRequests.js +1 -1
  16. package/dist/src/lib/crypto/transmitters/StakeDeposits.js +1 -1
  17. package/dist/src/lib/crypto/transmitters/StakeWithdrawals.js +1 -1
  18. package/dist/src/lib/crypto/transmitters/ValueTransfers.js +1 -1
  19. package/dist/src/lib/crypto/transmitters.js +1 -1
  20. package/dist/src/lib/crypto/types.js +1 -1
  21. package/dist/src/lib/crypto/utils.js +1 -1
  22. package/dist/src/lib/crypto/wallet.js +1 -1
  23. package/dist/src/lib/index.js +1 -1
  24. package/dist/src/lib/radon/ccdr/eth.js +1 -1
  25. package/dist/src/lib/radon/ccdr/index.js +1 -1
  26. package/dist/src/lib/radon/ccdr/wit.js +1 -1
  27. package/dist/src/lib/radon/filters.js +1 -1
  28. package/dist/src/lib/radon/index.js +1 -1
  29. package/dist/src/lib/radon/reducers.js +1 -1
  30. package/dist/src/lib/radon/types.js +1 -1
  31. package/dist/src/lib/radon/utils.d.ts.map +1 -1
  32. package/dist/src/lib/radon/utils.js +1 -1
  33. package/dist/src/lib/rest/kermit.d.ts.map +1 -1
  34. package/dist/src/lib/rest/kermit.js +2 -4
  35. package/dist/src/lib/rest/types.js +1 -1
  36. package/dist/src/lib/rpc/index.js +1 -1
  37. package/dist/src/lib/rpc/nodes.js +1 -1
  38. package/dist/src/lib/rpc/provider.js +1 -1
  39. package/dist/src/lib/rpc/types.js +1 -1
  40. package/dist/src/lib/types.js +1 -1
  41. package/dist/src/lib/utils.js +1 -1
  42. package/package.json +4 -4
  43. package/scripts/clean.cjs +21 -21
  44. package/scripts/postinstall.cjs +9 -9
  45. package/src/bin/bots/watcher.cjs +365 -354
  46. package/src/bin/cli/history.cjs +31 -31
  47. package/src/bin/cli/inspect.js +581 -581
  48. package/src/bin/cli/network.js +695 -695
  49. package/src/bin/cli/nodes.js +424 -424
  50. package/src/bin/cli/radon.js +1124 -1122
  51. package/src/bin/cli/wallet.js +1362 -1362
  52. package/src/bin/helpers.js +974 -974
  53. package/src/bin/index.js +328 -328
  54. package/witnet/assets/_index.cjs +0 -5
  55. package/witnet/assets/_requests.cjs +0 -25
  56. package/witnet/assets/_sources.cjs +0 -36
  57. package/witnet/assets/_templates.cjs +0 -36
  58. package/witnet/assets/index.cjs +0 -4
  59. package/witnet/assets/modals/index.cjs +0 -7
  60. package/witnet/assets/modals/web3/btc.cjs +0 -0
  61. package/witnet/assets/modals/web3/eth.cjs +0 -27
  62. package/witnet/assets/modals/web3/ipfs.cjs +0 -22
  63. package/witnet/assets/modals/web3/sol.cjs +0 -0
  64. package/witnet/assets/modals/web3/wit.cjs +0 -28
  65. package/witnet/assets/requests.cjs +0 -49
  66. package/witnet/witnet copy.proto +0 -457
  67. package/witnet/witnet.proto +0 -456
  68. package/witnet/witnet.proto.json +0 -1322
  69. package/witnet/witnet_toolkit-1.6.7-x86_64-pc-windows-msvc.exe +0 -0
  70. package/witnet/witnet_toolkit-1.7.1-x86_64-pc-windows-msvc.exe +0 -0
  71. package/witnet/witnet_toolkit-2.0.0-rc.7-x86_64-pc-windows-msvc.exe +0 -0
  72. package/witnet/witnet_toolkit-2.0.2-x86_64-pc-windows-msvc.exe +0 -0
  73. package/witnet/witnet_toolkit-2.0.21-x86_64-pc-windows-msvc.exe +0 -0
  74. package/witnet/witnet_toolkit-2.0.7-x86_64-pc-windows-msvc.exe +0 -0
  75. package/witnet/witnet_toolkit-2.0.8-x86_64-pc-windows-msvc.exe +0 -0
@@ -1,581 +1,581 @@
1
- import moment from "moment";
2
- import { utils, Witnet } from "../../../dist/src/index.js";
3
- import * as helpers from "../helpers.js";
4
-
5
- const {
6
- cyan,
7
- gray,
8
- green,
9
- lyellow,
10
- magenta,
11
- mgreen,
12
- mmagenta,
13
- myellow,
14
- yellow,
15
- } = helpers.colors;
16
-
17
- const _DEFAULT_LIMIT = 100;
18
-
19
- /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
- /// CLI SUBMODULE CONSTANTS ===========================================================================================
21
-
22
- export const envars = {
23
- WITNET_SDK_PROVIDER_URL:
24
- "=> Wit/Oracle RPC provider(s) to connect to, if no otherwise specified.",
25
- };
26
-
27
- export const flags = {
28
- provider: {
29
- hint: "Public Wit/Oracle JSON-RPC provider, other than default.",
30
- param: "URL",
31
- },
32
- reverse: {
33
- hint: "List most recent data requests first (default: true).",
34
- },
35
- verbose: {
36
- hint: "Outputs validators' nonce and last validation epochs.",
37
- },
38
- };
39
-
40
- export const router = {
41
- balance: {
42
- hint: "Show available Wits on given address.",
43
- params: "WIT_ADDRESS",
44
- },
45
- block: {
46
- hint: "Get block data given its block hash.",
47
- params: "BLOCK_HASH",
48
- },
49
- dataRequest: {
50
- hint: "Get query parameters and result to some data request transaction.",
51
- params: "DR_TX_HASH",
52
- options: {
53
- force: {
54
- hint: "Get data even if the WIT/RPC provider is not synced.",
55
- },
56
- mode: {
57
- hint: "Possible report formats (default: `ethereal`).",
58
- param: "`ethereal` | `full``",
59
- },
60
- },
61
- },
62
- dataRequests: {
63
- hint: "Search for in-flight or recently solved data request transactions.",
64
- params: "RAD_BYTECODE | RAD_HASH",
65
- options: {
66
- limit: { hint: "Limit output records (default: 100).", param: "LIMIT" },
67
- offset: {
68
- hint: "Skips first records as found on server side (default: 0).",
69
- param: "SKIP",
70
- },
71
- mode: {
72
- hint: "Possible report formats (default: `ethereal`).",
73
- param: "`ethereal` | `full``",
74
- },
75
- since: {
76
- hint: "Number of past epochs to search for (default: -30240).",
77
- param: "EPOCH|MINUS_EPOCHS",
78
- },
79
- },
80
- },
81
- superblock: {
82
- hint: "Show superblock metadata for given epoch.",
83
- params: "EPOCH",
84
- },
85
- transaction: {
86
- hint: "Report transaction details given its transaction hash.",
87
- params: "TX_HASH",
88
- },
89
- validators: {
90
- hint: "List validators treasuring delegated stake from the specified address.",
91
- params: "WIT_ADDRESS",
92
- },
93
- valueTransfer: {
94
- hint: "Report value transfer details given its transaction hash.",
95
- params: "VT_TX_HASH",
96
- options: {
97
- force: {
98
- hint: "Get data even if the WIT/RPC provider is not synced.",
99
- },
100
- mode: {
101
- hint: "Possible report formats (default: `full`).",
102
- param: "`ethereal` | `full` | `simple`",
103
- },
104
- },
105
- },
106
- withdrawers: {
107
- hint: "List withdrawers currently delegating stake to the specified address.",
108
- params: "WIT_ADDRESS",
109
- },
110
- utxos: {
111
- hint: "List UTXOs available to the specified address.",
112
- params: "WIT_ADDRESS",
113
- options: {
114
- from: {
115
- hint: "Show only UTXOs that previously belonged to this other address.",
116
- param: "WIT_ADDRESS",
117
- },
118
- "min-value": {
119
- hint: "Filter out UTXOs with a value smaller than this amount.",
120
- param: "WITS",
121
- },
122
- strategy: {
123
- hint: "UTXOs listing order: `big-first`, `random`, `small-first` (default: `big-first`).",
124
- param: "STRATEGY",
125
- },
126
- },
127
- },
128
- };
129
-
130
- export const subcommands = {
131
- balance,
132
- block,
133
- dataRequest,
134
- dataRequests,
135
- superblock,
136
- transaction,
137
- validators,
138
- withdrawers,
139
- utxos,
140
- valueTransfer,
141
- };
142
-
143
- /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
144
- /// CLI SUBMODULE COMMANDS ============================================================================================
145
-
146
- async function balance(options = {}, args = []) {
147
- if (args.length === 0) {
148
- throw Error("No WIT_ADDRESS was specified");
149
- }
150
- const pkh = args[0];
151
- const provider = new Witnet.JsonRpcProvider(options?.provider);
152
- const balance = await provider.getBalance(pkh);
153
- const records = [];
154
- records.push([
155
- Witnet.Coins.fromNanowits(balance.locked).wits,
156
- Witnet.Coins.fromNanowits(balance.staked).wits,
157
- Witnet.Coins.fromNanowits(balance.unlocked).wits,
158
- Witnet.Coins.fromNanowits(
159
- balance.locked + balance.staked + balance.unlocked,
160
- ).wits,
161
- ]);
162
- helpers.traceTable(records, {
163
- headlines: [
164
- "Locked ($WIT)",
165
- "Staked ($WIT)",
166
- "Available ($WIT)",
167
- "BALANCE ($WIT)",
168
- ],
169
- humanizers: [
170
- helpers.commas,
171
- helpers.commas,
172
- helpers.commas,
173
- helpers.commas,
174
- ],
175
- colors: [gray, yellow, myellow, lyellow],
176
- });
177
- }
178
-
179
- async function block(options = {}, args = []) {
180
- if (args.length === 0) {
181
- throw Error("No BLOCK_HASH was specified");
182
- }
183
- const blockHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
184
- if (!helpers.isHexString(blockHash)) {
185
- throw Error("Invalid BLOCK_HASH was provided");
186
- }
187
- const provider = await Witnet.JsonRpcProvider.fromEnv(options?.provider);
188
- const block = await provider.getBlock(blockHash);
189
- console.info(
190
- gray(
191
- JSON.stringify(
192
- block,
193
- (key, value) => {
194
- switch (key) {
195
- case "bytes":
196
- case "der":
197
- case "proof":
198
- return Array.isArray(value)
199
- ? helpers.toHexString(value, true)
200
- : value;
201
-
202
- case "public_key":
203
- return Array.isArray(value)
204
- ? helpers.toHexString(value, true)
205
- : typeof value === "object"
206
- ? Witnet.PublicKey.fromProtobuf(value)
207
- .hash()
208
- .toBech32(provider.network)
209
- : value;
210
-
211
- default:
212
- return value;
213
- }
214
- },
215
- 2,
216
- ),
217
- ),
218
- );
219
- }
220
-
221
- async function dataRequests(options = {}, [arg]) {
222
- // if (!args || args.length === 0) {
223
- if (!arg) {
224
- throw new Error("No RAD_HASH or RAD_RAD_BYTECODE was specified.");
225
- }
226
- const provider = new Witnet.JsonRpcProvider(options?.provider);
227
- // const radHashes = args.map(arg => {
228
- if (!helpers.isHexString(arg)) {
229
- throw new Error(`Invalid hex string was provided: '${arg}'`);
230
- }
231
- let radHash;
232
- if (helpers.isHexStringOfLength(arg, 32)) {
233
- radHash = arg;
234
- } else {
235
- const request = Witnet.Radon.RadonRequest.fromBytecode(arg);
236
- radHash = request.radHash;
237
- }
238
- // return radHash
239
- // })
240
- // let results = await helpers.prompter(
241
- // Promise.all(
242
- // [...new Set(radHashes)].map(radHash => provider.searchDataRequests(radHash, {
243
- // limit: options?.limit ? parseInt(options.limit) : undefined,
244
- // offset: options?.offset ? parseInt(options.offset) : undefined,
245
- // reverse: options?.reverse,
246
- // }))
247
- // ).then(results => results
248
- // .flat()
249
- // .sort((a, b) => options?.reverse ? b.block_epoch - a.block_epoch : a.block_epoch - b.block_epoch)
250
- // .slice(options?.offset)
251
- // .slice(0, options?.limit || DEFAULT_LIMIT)
252
- // )
253
- // )
254
- const results = await helpers.prompter(
255
- provider.searchDataRequests(radHash, {
256
- limit: options?.limit ? parseInt(options.limit, 10) : undefined,
257
- offset: options?.offset ? parseInt(options.offset, 10) : undefined,
258
- mode: options?.mode,
259
- reverse: options?.reverse,
260
- }),
261
- );
262
- helpers.traceTable(
263
- results.map((record) => {
264
- let result = record?.result.cbor_bytes
265
- ? utils.cbor.decode(record?.result.cbor_bytes, { encoding: "hex" })
266
- : "";
267
- const request = Witnet.Radon.RadonRequest.fromBytecode(
268
- record.query.rad_bytecode,
269
- );
270
- const dataType =
271
- result.constructor.name === "Tagged" ? "RadonError" : request.dataType;
272
- if (dataType !== "RadonError")
273
- result = Buffer.from(
274
- utils.fromHexString(record?.result.cbor_bytes),
275
- ).toString("base64");
276
- else if (result.constructor.name === "Buffer")
277
- result = result.toString("base64");
278
- return [
279
- record.block_epoch,
280
- record.hash,
281
- record.query.witnesses,
282
- Witnet.Coins.fromPedros(record.query.unitary_reward).toString(2),
283
- dataType === "RadonError"
284
- ? helpers.colors.mred("RadonError")
285
- : helpers.colors.mgreen(dataType),
286
- ...(options?.verbose
287
- ? [
288
- dataType === "RadonError"
289
- ? helpers.colors.red(result)
290
- : record?.result.finalized
291
- ? helpers.colors.mcyan(result)
292
- : helpers.colors.cyan(result),
293
- ]
294
- : [
295
- record?.result
296
- ? `${record.result.cbor_bytes.length / 2} bytes`
297
- : "",
298
- record?.result.timestamp
299
- ? moment.unix(record.result.timestamp).fromNow()
300
- : "",
301
- ]),
302
- ];
303
- }),
304
- {
305
- headlines: [
306
- "EPOCH:",
307
- "DATA REQUEST TRANSACION HASH",
308
- "witnesses",
309
- "total fees",
310
- ":data type",
311
- ...(options?.verbose
312
- ? [":DATA REQUEST RESULT"]
313
- : ["CBOR SIZE:", "DATA FRESHNESS:"]),
314
- ],
315
- humanizers: [helpers.commas],
316
- colors: [
317
- undefined,
318
- helpers.colors.gray,
319
- helpers.colors.green,
320
- helpers.colors.green,
321
- undefined,
322
- helpers.colors.cyan,
323
- helpers.colors.mcyan,
324
- ],
325
- },
326
- );
327
- }
328
-
329
- async function dataRequest(options = {}, args = []) {
330
- if (args.length === 0) {
331
- throw Error("No DR_TX_HASH was specified");
332
- }
333
- const drTxHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
334
- if (!helpers.isHexString(drTxHash)) {
335
- throw Error("Invalid DR_TX_HASH was provided");
336
- }
337
- const provider = new Witnet.JsonRpcProvider(options?.provider);
338
-
339
- const drTxJsonReplacer = (key, value) => {
340
- switch (key) {
341
- case "proof":
342
- case "public_key":
343
- case "signature":
344
- case "signatures":
345
- return undefined;
346
-
347
- case "reveal":
348
- case "tally":
349
- if (Array.isArray(value)) {
350
- const result = utils.cbor.decode(Uint8Array.from(value));
351
- return Buffer.isBuffer(result) ? utils.toHexString(value) : result;
352
- }
353
-
354
- default:
355
- return value;
356
- }
357
- };
358
-
359
- const mode = options?.mode || `ethereal`;
360
- if (!["ethereal", "full"].includes(mode)) {
361
- throw Error(`Invalid mode value: "${options.mode}"`);
362
- }
363
-
364
- const report = await provider.getDataRequest(drTxHash, mode, options?.force);
365
- console.info(JSON.stringify(report, drTxJsonReplacer, 4));
366
- }
367
-
368
- async function superblock(options = {}, args = []) {
369
- if (args.length === 0) {
370
- throw Error("No EPOCH was specified");
371
- }
372
- const provider = new Witnet.JsonRpcProvider(options?.provider);
373
- const superblock = await provider.getSuperblock(args[0]);
374
- console.info(superblock);
375
- }
376
-
377
- async function transaction(options = {}, args = []) {
378
- if (args.length === 0) {
379
- throw Error("No TX_HASH was specified");
380
- }
381
- const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
382
- if (!helpers.isHexString(txHash)) {
383
- throw Error("Invalid TX_HASH was provided");
384
- }
385
- const provider = new Witnet.JsonRpcProvider(options?.provider);
386
- const transaction = await provider.getTransaction(txHash);
387
- console.info(
388
- `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`,
389
- );
390
- }
391
-
392
- async function utxos(options = {}, args = []) {
393
- if (args.length < 1) {
394
- throw Error("No WIT_ADDRESS was specified");
395
- }
396
- const now = Math.floor(Date.now() / 1000);
397
- const provider = new Witnet.JsonRpcProvider(options?.provider);
398
- let utxos = await provider.getUtxos(args[0], {
399
- minValue: options["min-value"]
400
- ? Witnet.Coins.fromWits(Number(options["min-value"])).pedros
401
- : undefined,
402
- fromSigner: options.from,
403
- });
404
- let totalBalance = 0n;
405
- if (!options?.verbose) {
406
- utxos = utils
407
- .selectUtxos({ utxos, strategy: options?.strategy || "big-first" })
408
- .filter((utxo) => utxo.timelock <= now)
409
- .map((utxo) => {
410
- totalBalance += utxo.value;
411
- return [utxo.output_pointer, utxo.value];
412
- });
413
- helpers.traceTable(utxos, {
414
- headlines: [":UTXOs", "Value ($pedros)"],
415
- humanizers: [undefined, helpers.commas],
416
- colors: [undefined, myellow],
417
- });
418
- } else {
419
- utxos = utxos.map((utxo) => {
420
- totalBalance += utxo.value;
421
- return [
422
- utxo.output_pointer,
423
- utxo.timelock > now ? gray(moment.unix(utxo.timelock).fromNow()) : "",
424
- utxo.timelock > now
425
- ? gray(helpers.commas(utxo.value))
426
- : myellow(helpers.commas(utxo.value)),
427
- ];
428
- });
429
- helpers.traceTable(utxos, {
430
- headlines: [":UTXOs", "Timelock", "Value ($pedros)"],
431
- });
432
- }
433
- console.info(
434
- `^ Showing ${utxos.length} UTXOs: ${lyellow(helpers.whole_wits(totalBalance, 2))}.`,
435
- );
436
- }
437
-
438
- async function validators(options = {}, args = []) {
439
- if (args.length === 0) {
440
- throw Error("No WIT_ADDRESS was specified");
441
- }
442
- const provider = new Witnet.JsonRpcProvider(options?.provider);
443
- const query = {
444
- filter: { withdrawer: args[0] },
445
- };
446
- const records = await provider.stakes(query);
447
- let nanowits = 0;
448
- if (records.length > 0) {
449
- helpers.traceTable(
450
- records.map((record, index) => {
451
- nanowits += record.value.coins;
452
- return [
453
- 1 + index,
454
- record.key.validator,
455
- ...(options?.verbose
456
- ? [
457
- record.value.nonce,
458
- record.value.epochs.witnessing,
459
- record.value.epochs.mining,
460
- ]
461
- : []),
462
- Witnet.Coins.fromNanowits(record.value.coins).wits,
463
- ];
464
- }),
465
- {
466
- headlines: [
467
- "RANK",
468
- "VALIDATORS",
469
- ...(options?.verbose ? ["Nonce", "LW_Epoch", "LM_Epoch"] : []),
470
- "STAKED ($WIT)",
471
- ],
472
- humanizers: [
473
- undefined,
474
- undefined,
475
- ...(options?.verbose
476
- ? [helpers.commas, helpers.commas, helpers.commas]
477
- : []),
478
- helpers.commas,
479
- ],
480
- colors: [
481
- undefined,
482
- green,
483
- ...(options?.verbose
484
- ? [undefined, magenta, cyan, myellow]
485
- : [myellow]),
486
- ],
487
- },
488
- );
489
- console.info(
490
- `^ ${records.length} validators for withdrawer ${mgreen(
491
- args[0],
492
- )}: ${lyellow(helpers.whole_wits(nanowits, 2))}`,
493
- );
494
- } else {
495
- console.info(`> No validators found for withdrawer ${mmagenta(args[0])}.`);
496
- }
497
- }
498
-
499
- async function valueTransfer(options = {}, args = []) {
500
- if (args.length === 0) {
501
- throw Error("No VT_TX_HASH was specified");
502
- }
503
- const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
504
- if (!helpers.isHexString(txHash)) {
505
- throw Error("Invalid VT_TX_HASH was provided");
506
- }
507
- const mode = options?.mode || `full`;
508
- if (!["ethereal", "full", "simple"].includes(mode)) {
509
- throw Error(`Invalid mode value: "${options.mode}"`);
510
- }
511
- const provider = new Witnet.JsonRpcProvider(options?.provider);
512
- const transaction = await provider.getValueTransfer(
513
- txHash,
514
- mode,
515
- options?.force,
516
- );
517
- console.info(
518
- `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`,
519
- );
520
- }
521
-
522
- async function withdrawers(options = {}, args = []) {
523
- if (args.length === 0) {
524
- throw Error("No WIT_ADDRESS was specified");
525
- }
526
- const provider = new Witnet.JsonRpcProvider(options?.provider);
527
- const query = {
528
- filter: { validator: args[0] },
529
- };
530
- const records = await provider.stakes(query);
531
- let nanowits = 0;
532
- if (records.length > 0) {
533
- helpers.traceTable(
534
- records.map((record, index) => {
535
- nanowits += record.value.coins;
536
- return [
537
- 1 + index,
538
- record.key.withdrawer,
539
- ...(options?.verbose
540
- ? [
541
- record.value.nonce,
542
- record.value.epochs.witnessing,
543
- record.value.epochs.mining,
544
- ]
545
- : []),
546
- Witnet.Coins.fromNanowits(record.value.coins).wits,
547
- ];
548
- }),
549
- {
550
- headlines: [
551
- "RANK",
552
- "WITHDRAWERS",
553
- ...(options?.verbose ? ["Nonce", "LW_Epoch", "LM_Epoch"] : []),
554
- "STAKED ($WIT)",
555
- ],
556
- humanizers: [
557
- undefined,
558
- undefined,
559
- ...(options?.verbose
560
- ? [helpers.commas, helpers.commas, helpers.commas]
561
- : []),
562
- helpers.commas,
563
- ],
564
- colors: [
565
- undefined,
566
- green,
567
- ...(options?.verbose
568
- ? [undefined, magenta, cyan, myellow]
569
- : [myellow]),
570
- ],
571
- },
572
- );
573
- console.info(
574
- `^ ${records.length} withdrawers for validator ${mgreen(
575
- args[0],
576
- )}: ${lyellow(helpers.whole_wits(nanowits, 2))}`,
577
- );
578
- } else {
579
- console.info(`> No withdrawers found for validator ${mmagenta(args[0])}.`);
580
- }
581
- }
1
+ import moment from "moment";
2
+ import { utils, Witnet } from "../../../dist/src/index.js";
3
+ import * as helpers from "../helpers.js";
4
+
5
+ const {
6
+ cyan,
7
+ gray,
8
+ green,
9
+ lyellow,
10
+ magenta,
11
+ mgreen,
12
+ mmagenta,
13
+ myellow,
14
+ yellow,
15
+ } = helpers.colors;
16
+
17
+ const _DEFAULT_LIMIT = 100;
18
+
19
+ /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
20
+ /// CLI SUBMODULE CONSTANTS ===========================================================================================
21
+
22
+ export const envars = {
23
+ WITNET_SDK_PROVIDER_URL:
24
+ "=> Wit/Oracle RPC provider(s) to connect to, if no otherwise specified.",
25
+ };
26
+
27
+ export const flags = {
28
+ provider: {
29
+ hint: "Public Wit/Oracle JSON-RPC provider, other than default.",
30
+ param: "URL",
31
+ },
32
+ reverse: {
33
+ hint: "List most recent data requests first (default: true).",
34
+ },
35
+ verbose: {
36
+ hint: "Outputs validators' nonce and last validation epochs.",
37
+ },
38
+ };
39
+
40
+ export const router = {
41
+ balance: {
42
+ hint: "Show available Wits on given address.",
43
+ params: "WIT_ADDRESS",
44
+ },
45
+ block: {
46
+ hint: "Get block data given its block hash.",
47
+ params: "BLOCK_HASH",
48
+ },
49
+ dataRequest: {
50
+ hint: "Get query parameters and result to some data request transaction.",
51
+ params: "DR_TX_HASH",
52
+ options: {
53
+ force: {
54
+ hint: "Get data even if the WIT/RPC provider is not synced.",
55
+ },
56
+ mode: {
57
+ hint: "Possible report formats (default: `ethereal`).",
58
+ param: "`ethereal` | `full``",
59
+ },
60
+ },
61
+ },
62
+ dataRequests: {
63
+ hint: "Search for in-flight or recently solved data request transactions.",
64
+ params: "RAD_BYTECODE | RAD_HASH",
65
+ options: {
66
+ limit: { hint: "Limit output records (default: 100).", param: "LIMIT" },
67
+ offset: {
68
+ hint: "Skips first records as found on server side (default: 0).",
69
+ param: "SKIP",
70
+ },
71
+ mode: {
72
+ hint: "Possible report formats (default: `ethereal`).",
73
+ param: "`ethereal` | `full``",
74
+ },
75
+ since: {
76
+ hint: "Number of past epochs to search for (default: -30240).",
77
+ param: "EPOCH|MINUS_EPOCHS",
78
+ },
79
+ },
80
+ },
81
+ superblock: {
82
+ hint: "Show superblock metadata for given epoch.",
83
+ params: "EPOCH",
84
+ },
85
+ transaction: {
86
+ hint: "Report transaction details given its transaction hash.",
87
+ params: "TX_HASH",
88
+ },
89
+ validators: {
90
+ hint: "List validators treasuring delegated stake from the specified address.",
91
+ params: "WIT_ADDRESS",
92
+ },
93
+ valueTransfer: {
94
+ hint: "Report value transfer details given its transaction hash.",
95
+ params: "VT_TX_HASH",
96
+ options: {
97
+ force: {
98
+ hint: "Get data even if the WIT/RPC provider is not synced.",
99
+ },
100
+ mode: {
101
+ hint: "Possible report formats (default: `full`).",
102
+ param: "`ethereal` | `full` | `simple`",
103
+ },
104
+ },
105
+ },
106
+ withdrawers: {
107
+ hint: "List withdrawers currently delegating stake to the specified address.",
108
+ params: "WIT_ADDRESS",
109
+ },
110
+ utxos: {
111
+ hint: "List UTXOs available to the specified address.",
112
+ params: "WIT_ADDRESS",
113
+ options: {
114
+ from: {
115
+ hint: "Show only UTXOs that previously belonged to this other address.",
116
+ param: "WIT_ADDRESS",
117
+ },
118
+ "min-value": {
119
+ hint: "Filter out UTXOs with a value smaller than this amount.",
120
+ param: "WITS",
121
+ },
122
+ strategy: {
123
+ hint: "UTXOs listing order: `big-first`, `random`, `small-first` (default: `big-first`).",
124
+ param: "STRATEGY",
125
+ },
126
+ },
127
+ },
128
+ };
129
+
130
+ export const subcommands = {
131
+ balance,
132
+ block,
133
+ dataRequest,
134
+ dataRequests,
135
+ superblock,
136
+ transaction,
137
+ validators,
138
+ withdrawers,
139
+ utxos,
140
+ valueTransfer,
141
+ };
142
+
143
+ /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
144
+ /// CLI SUBMODULE COMMANDS ============================================================================================
145
+
146
+ async function balance(options = {}, args = []) {
147
+ if (args.length === 0) {
148
+ throw Error("No WIT_ADDRESS was specified");
149
+ }
150
+ const pkh = args[0];
151
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
152
+ const balance = await provider.getBalance(pkh);
153
+ const records = [];
154
+ records.push([
155
+ Witnet.Coins.fromNanowits(balance.locked).wits,
156
+ Witnet.Coins.fromNanowits(balance.staked).wits,
157
+ Witnet.Coins.fromNanowits(balance.unlocked).wits,
158
+ Witnet.Coins.fromNanowits(
159
+ balance.locked + balance.staked + balance.unlocked,
160
+ ).wits,
161
+ ]);
162
+ helpers.traceTable(records, {
163
+ headlines: [
164
+ "Locked ($WIT)",
165
+ "Staked ($WIT)",
166
+ "Available ($WIT)",
167
+ "BALANCE ($WIT)",
168
+ ],
169
+ humanizers: [
170
+ helpers.commas,
171
+ helpers.commas,
172
+ helpers.commas,
173
+ helpers.commas,
174
+ ],
175
+ colors: [gray, yellow, myellow, lyellow],
176
+ });
177
+ }
178
+
179
+ async function block(options = {}, args = []) {
180
+ if (args.length === 0) {
181
+ throw Error("No BLOCK_HASH was specified");
182
+ }
183
+ const blockHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
184
+ if (!helpers.isHexString(blockHash)) {
185
+ throw Error("Invalid BLOCK_HASH was provided");
186
+ }
187
+ const provider = await Witnet.JsonRpcProvider.fromEnv(options?.provider);
188
+ const block = await provider.getBlock(blockHash);
189
+ console.info(
190
+ gray(
191
+ JSON.stringify(
192
+ block,
193
+ (key, value) => {
194
+ switch (key) {
195
+ case "bytes":
196
+ case "der":
197
+ case "proof":
198
+ return Array.isArray(value)
199
+ ? helpers.toHexString(value, true)
200
+ : value;
201
+
202
+ case "public_key":
203
+ return Array.isArray(value)
204
+ ? helpers.toHexString(value, true)
205
+ : typeof value === "object"
206
+ ? Witnet.PublicKey.fromProtobuf(value)
207
+ .hash()
208
+ .toBech32(provider.network)
209
+ : value;
210
+
211
+ default:
212
+ return value;
213
+ }
214
+ },
215
+ 2,
216
+ ),
217
+ ),
218
+ );
219
+ }
220
+
221
+ async function dataRequests(options = {}, [arg]) {
222
+ // if (!args || args.length === 0) {
223
+ if (!arg) {
224
+ throw new Error("No RAD_HASH or RAD_RAD_BYTECODE was specified.");
225
+ }
226
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
227
+ // const radHashes = args.map(arg => {
228
+ if (!helpers.isHexString(arg)) {
229
+ throw new Error(`Invalid hex string was provided: '${arg}'`);
230
+ }
231
+ let radHash;
232
+ if (helpers.isHexStringOfLength(arg, 32)) {
233
+ radHash = arg;
234
+ } else {
235
+ const request = Witnet.Radon.RadonRequest.fromBytecode(arg);
236
+ radHash = request.radHash;
237
+ }
238
+ // return radHash
239
+ // })
240
+ // let results = await helpers.prompter(
241
+ // Promise.all(
242
+ // [...new Set(radHashes)].map(radHash => provider.searchDataRequests(radHash, {
243
+ // limit: options?.limit ? parseInt(options.limit) : undefined,
244
+ // offset: options?.offset ? parseInt(options.offset) : undefined,
245
+ // reverse: options?.reverse,
246
+ // }))
247
+ // ).then(results => results
248
+ // .flat()
249
+ // .sort((a, b) => options?.reverse ? b.block_epoch - a.block_epoch : a.block_epoch - b.block_epoch)
250
+ // .slice(options?.offset)
251
+ // .slice(0, options?.limit || DEFAULT_LIMIT)
252
+ // )
253
+ // )
254
+ const results = await helpers.prompter(
255
+ provider.searchDataRequests(radHash, {
256
+ limit: options?.limit ? parseInt(options.limit, 10) : undefined,
257
+ offset: options?.offset ? parseInt(options.offset, 10) : undefined,
258
+ mode: options?.mode,
259
+ reverse: options?.reverse,
260
+ }),
261
+ );
262
+ helpers.traceTable(
263
+ results.map((record) => {
264
+ let result = record?.result.cbor_bytes
265
+ ? utils.cbor.decode(record?.result.cbor_bytes, { encoding: "hex" })
266
+ : "";
267
+ const request = Witnet.Radon.RadonRequest.fromBytecode(
268
+ record.query.rad_bytecode,
269
+ );
270
+ const dataType =
271
+ result.constructor.name === "Tagged" ? "RadonError" : request.dataType;
272
+ if (dataType !== "RadonError")
273
+ result = Buffer.from(
274
+ utils.fromHexString(record?.result.cbor_bytes),
275
+ ).toString("base64");
276
+ else if (result.constructor.name === "Buffer")
277
+ result = result.toString("base64");
278
+ return [
279
+ record.block_epoch,
280
+ record.hash,
281
+ record.query.witnesses,
282
+ Witnet.Coins.fromPedros(record.query.unitary_reward).toString(2),
283
+ dataType === "RadonError"
284
+ ? helpers.colors.mred("RadonError")
285
+ : helpers.colors.mgreen(dataType),
286
+ ...(options?.verbose
287
+ ? [
288
+ dataType === "RadonError"
289
+ ? helpers.colors.red(result)
290
+ : record?.result.finalized
291
+ ? helpers.colors.mcyan(result)
292
+ : helpers.colors.cyan(result),
293
+ ]
294
+ : [
295
+ record?.result
296
+ ? `${record.result.cbor_bytes.length / 2} bytes`
297
+ : "",
298
+ record?.result.timestamp
299
+ ? moment.unix(record.result.timestamp).fromNow()
300
+ : "",
301
+ ]),
302
+ ];
303
+ }),
304
+ {
305
+ headlines: [
306
+ "EPOCH:",
307
+ "DATA REQUEST TRANSACION HASH",
308
+ "witnesses",
309
+ "total fees",
310
+ ":data type",
311
+ ...(options?.verbose
312
+ ? [":DATA REQUEST RESULT"]
313
+ : ["CBOR SIZE:", "DATA FRESHNESS:"]),
314
+ ],
315
+ humanizers: [helpers.commas],
316
+ colors: [
317
+ undefined,
318
+ helpers.colors.gray,
319
+ helpers.colors.green,
320
+ helpers.colors.green,
321
+ undefined,
322
+ helpers.colors.cyan,
323
+ helpers.colors.mcyan,
324
+ ],
325
+ },
326
+ );
327
+ }
328
+
329
+ async function dataRequest(options = {}, args = []) {
330
+ if (args.length === 0) {
331
+ throw Error("No DR_TX_HASH was specified");
332
+ }
333
+ const drTxHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
334
+ if (!helpers.isHexString(drTxHash)) {
335
+ throw Error("Invalid DR_TX_HASH was provided");
336
+ }
337
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
338
+
339
+ const drTxJsonReplacer = (key, value) => {
340
+ switch (key) {
341
+ case "proof":
342
+ case "public_key":
343
+ case "signature":
344
+ case "signatures":
345
+ return undefined;
346
+
347
+ case "reveal":
348
+ case "tally":
349
+ if (Array.isArray(value)) {
350
+ const result = utils.cbor.decode(Uint8Array.from(value));
351
+ return Buffer.isBuffer(result) ? utils.toHexString(value) : result;
352
+ }
353
+
354
+ default:
355
+ return value;
356
+ }
357
+ };
358
+
359
+ const mode = options?.mode || `ethereal`;
360
+ if (!["ethereal", "full"].includes(mode)) {
361
+ throw Error(`Invalid mode value: "${options.mode}"`);
362
+ }
363
+
364
+ const report = await provider.getDataRequest(drTxHash, mode, options?.force);
365
+ console.info(JSON.stringify(report, drTxJsonReplacer, 4));
366
+ }
367
+
368
+ async function superblock(options = {}, args = []) {
369
+ if (args.length === 0) {
370
+ throw Error("No EPOCH was specified");
371
+ }
372
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
373
+ const superblock = await provider.getSuperblock(args[0]);
374
+ console.info(superblock);
375
+ }
376
+
377
+ async function transaction(options = {}, args = []) {
378
+ if (args.length === 0) {
379
+ throw Error("No TX_HASH was specified");
380
+ }
381
+ const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
382
+ if (!helpers.isHexString(txHash)) {
383
+ throw Error("Invalid TX_HASH was provided");
384
+ }
385
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
386
+ const transaction = await provider.getTransaction(txHash);
387
+ console.info(
388
+ `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`,
389
+ );
390
+ }
391
+
392
+ async function utxos(options = {}, args = []) {
393
+ if (args.length < 1) {
394
+ throw Error("No WIT_ADDRESS was specified");
395
+ }
396
+ const now = Math.floor(Date.now() / 1000);
397
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
398
+ let utxos = await provider.getUtxos(args[0], {
399
+ minValue: options["min-value"]
400
+ ? Witnet.Coins.fromWits(Number(options["min-value"])).pedros
401
+ : undefined,
402
+ fromSigner: options.from,
403
+ });
404
+ let totalBalance = 0n;
405
+ if (!options?.verbose) {
406
+ utxos = utils
407
+ .selectUtxos({ utxos, strategy: options?.strategy || "big-first" })
408
+ .filter((utxo) => utxo.timelock <= now)
409
+ .map((utxo) => {
410
+ totalBalance += utxo.value;
411
+ return [utxo.output_pointer, utxo.value];
412
+ });
413
+ helpers.traceTable(utxos, {
414
+ headlines: [":UTXOs", "Value ($pedros)"],
415
+ humanizers: [undefined, helpers.commas],
416
+ colors: [undefined, myellow],
417
+ });
418
+ } else {
419
+ utxos = utxos.map((utxo) => {
420
+ totalBalance += utxo.value;
421
+ return [
422
+ utxo.output_pointer,
423
+ utxo.timelock > now ? gray(moment.unix(utxo.timelock).fromNow()) : "",
424
+ utxo.timelock > now
425
+ ? gray(helpers.commas(utxo.value))
426
+ : myellow(helpers.commas(utxo.value)),
427
+ ];
428
+ });
429
+ helpers.traceTable(utxos, {
430
+ headlines: [":UTXOs", "Timelock", "Value ($pedros)"],
431
+ });
432
+ }
433
+ console.info(
434
+ `^ Showing ${utxos.length} UTXOs: ${lyellow(helpers.whole_wits(totalBalance, 2))}.`,
435
+ );
436
+ }
437
+
438
+ async function validators(options = {}, args = []) {
439
+ if (args.length === 0) {
440
+ throw Error("No WIT_ADDRESS was specified");
441
+ }
442
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
443
+ const query = {
444
+ filter: { withdrawer: args[0] },
445
+ };
446
+ const records = await provider.stakes(query);
447
+ let nanowits = 0;
448
+ if (records.length > 0) {
449
+ helpers.traceTable(
450
+ records.map((record, index) => {
451
+ nanowits += record.value.coins;
452
+ return [
453
+ 1 + index,
454
+ record.key.validator,
455
+ ...(options?.verbose
456
+ ? [
457
+ record.value.nonce,
458
+ record.value.epochs.witnessing,
459
+ record.value.epochs.mining,
460
+ ]
461
+ : []),
462
+ Witnet.Coins.fromNanowits(record.value.coins).wits,
463
+ ];
464
+ }),
465
+ {
466
+ headlines: [
467
+ "RANK",
468
+ "VALIDATORS",
469
+ ...(options?.verbose ? ["Nonce", "LW_Epoch", "LM_Epoch"] : []),
470
+ "STAKED ($WIT)",
471
+ ],
472
+ humanizers: [
473
+ undefined,
474
+ undefined,
475
+ ...(options?.verbose
476
+ ? [helpers.commas, helpers.commas, helpers.commas]
477
+ : []),
478
+ helpers.commas,
479
+ ],
480
+ colors: [
481
+ undefined,
482
+ green,
483
+ ...(options?.verbose
484
+ ? [undefined, magenta, cyan, myellow]
485
+ : [myellow]),
486
+ ],
487
+ },
488
+ );
489
+ console.info(
490
+ `^ ${records.length} validators for withdrawer ${mgreen(
491
+ args[0],
492
+ )}: ${lyellow(helpers.whole_wits(nanowits, 2))}`,
493
+ );
494
+ } else {
495
+ console.info(`> No validators found for withdrawer ${mmagenta(args[0])}.`);
496
+ }
497
+ }
498
+
499
+ async function valueTransfer(options = {}, args = []) {
500
+ if (args.length === 0) {
501
+ throw Error("No VT_TX_HASH was specified");
502
+ }
503
+ const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0];
504
+ if (!helpers.isHexString(txHash)) {
505
+ throw Error("Invalid VT_TX_HASH was provided");
506
+ }
507
+ const mode = options?.mode || `full`;
508
+ if (!["ethereal", "full", "simple"].includes(mode)) {
509
+ throw Error(`Invalid mode value: "${options.mode}"`);
510
+ }
511
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
512
+ const transaction = await provider.getValueTransfer(
513
+ txHash,
514
+ mode,
515
+ options?.force,
516
+ );
517
+ console.info(
518
+ `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`,
519
+ );
520
+ }
521
+
522
+ async function withdrawers(options = {}, args = []) {
523
+ if (args.length === 0) {
524
+ throw Error("No WIT_ADDRESS was specified");
525
+ }
526
+ const provider = new Witnet.JsonRpcProvider(options?.provider);
527
+ const query = {
528
+ filter: { validator: args[0] },
529
+ };
530
+ const records = await provider.stakes(query);
531
+ let nanowits = 0;
532
+ if (records.length > 0) {
533
+ helpers.traceTable(
534
+ records.map((record, index) => {
535
+ nanowits += record.value.coins;
536
+ return [
537
+ 1 + index,
538
+ record.key.withdrawer,
539
+ ...(options?.verbose
540
+ ? [
541
+ record.value.nonce,
542
+ record.value.epochs.witnessing,
543
+ record.value.epochs.mining,
544
+ ]
545
+ : []),
546
+ Witnet.Coins.fromNanowits(record.value.coins).wits,
547
+ ];
548
+ }),
549
+ {
550
+ headlines: [
551
+ "RANK",
552
+ "WITHDRAWERS",
553
+ ...(options?.verbose ? ["Nonce", "LW_Epoch", "LM_Epoch"] : []),
554
+ "STAKED ($WIT)",
555
+ ],
556
+ humanizers: [
557
+ undefined,
558
+ undefined,
559
+ ...(options?.verbose
560
+ ? [helpers.commas, helpers.commas, helpers.commas]
561
+ : []),
562
+ helpers.commas,
563
+ ],
564
+ colors: [
565
+ undefined,
566
+ green,
567
+ ...(options?.verbose
568
+ ? [undefined, magenta, cyan, myellow]
569
+ : [myellow]),
570
+ ],
571
+ },
572
+ );
573
+ console.info(
574
+ `^ ${records.length} withdrawers for validator ${mgreen(
575
+ args[0],
576
+ )}: ${lyellow(helpers.whole_wits(nanowits, 2))}`,
577
+ );
578
+ } else {
579
+ console.info(`> No withdrawers found for validator ${mmagenta(args[0])}.`);
580
+ }
581
+ }