@witnet/sdk 1.0.3 → 1.0.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 (197) hide show
  1. package/.env_witnet +6 -6
  2. package/LICENSE +21 -21
  3. package/README.md +103 -103
  4. package/dist/bin/helpers.d.ts +88 -0
  5. package/dist/bin/helpers.d.ts.map +1 -0
  6. package/dist/bin/helpers.js +866 -0
  7. package/dist/index.d.ts +4 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +42 -0
  10. package/dist/lib/crypto/account.d.ts +18 -0
  11. package/dist/lib/crypto/account.d.ts.map +1 -0
  12. package/dist/lib/crypto/account.js +152 -0
  13. package/dist/lib/crypto/coinbase.d.ts +9 -0
  14. package/dist/lib/crypto/coinbase.d.ts.map +1 -0
  15. package/dist/lib/crypto/coinbase.js +39 -0
  16. package/dist/lib/crypto/index.d.ts +7 -0
  17. package/dist/lib/crypto/index.d.ts.map +1 -0
  18. package/dist/lib/crypto/index.js +28 -0
  19. package/dist/lib/crypto/interfaces.d.ts +80 -0
  20. package/dist/lib/crypto/interfaces.d.ts.map +1 -0
  21. package/dist/lib/crypto/interfaces.js +3 -0
  22. package/dist/lib/crypto/payloads/DataRequestPayload.d.ts +47 -0
  23. package/dist/lib/crypto/payloads/DataRequestPayload.d.ts.map +1 -0
  24. package/dist/lib/crypto/payloads/DataRequestPayload.js +384 -0
  25. package/dist/lib/crypto/payloads/StakePayload.d.ts +27 -0
  26. package/dist/lib/crypto/payloads/StakePayload.d.ts.map +1 -0
  27. package/dist/lib/crypto/payloads/StakePayload.js +184 -0
  28. package/dist/lib/crypto/payloads/UnstakePayload.d.ts +35 -0
  29. package/dist/lib/crypto/payloads/UnstakePayload.d.ts.map +1 -0
  30. package/dist/lib/crypto/payloads/UnstakePayload.js +244 -0
  31. package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts +24 -0
  32. package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts.map +1 -0
  33. package/dist/lib/crypto/payloads/ValueTransferPayload.js +182 -0
  34. package/dist/lib/crypto/payloads.d.ts +54 -0
  35. package/dist/lib/crypto/payloads.d.ts.map +1 -0
  36. package/dist/lib/crypto/payloads.js +224 -0
  37. package/dist/lib/crypto/signer.d.ts +26 -0
  38. package/dist/lib/crypto/signer.d.ts.map +1 -0
  39. package/dist/lib/crypto/signer.js +299 -0
  40. package/dist/lib/crypto/transmitters/DataRequests.d.ts +14 -0
  41. package/dist/lib/crypto/transmitters/DataRequests.d.ts.map +1 -0
  42. package/dist/lib/crypto/transmitters/DataRequests.js +62 -0
  43. package/dist/lib/crypto/transmitters/StakeDeposits.d.ts +11 -0
  44. package/dist/lib/crypto/transmitters/StakeDeposits.d.ts.map +1 -0
  45. package/dist/lib/crypto/transmitters/StakeDeposits.js +48 -0
  46. package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts +17 -0
  47. package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts.map +1 -0
  48. package/dist/lib/crypto/transmitters/StakeWithdrawals.js +115 -0
  49. package/dist/lib/crypto/transmitters/ValueTransfers.d.ts +10 -0
  50. package/dist/lib/crypto/transmitters/ValueTransfers.d.ts.map +1 -0
  51. package/dist/lib/crypto/transmitters/ValueTransfers.js +47 -0
  52. package/dist/lib/crypto/transmitters.d.ts +46 -0
  53. package/dist/lib/crypto/transmitters.d.ts.map +1 -0
  54. package/dist/lib/crypto/transmitters.js +506 -0
  55. package/dist/lib/crypto/types.d.ts +127 -0
  56. package/dist/lib/crypto/types.d.ts.map +1 -0
  57. package/dist/lib/crypto/types.js +261 -0
  58. package/dist/lib/crypto/utils.d.ts +10 -0
  59. package/dist/lib/crypto/utils.d.ts.map +1 -0
  60. package/dist/lib/crypto/utils.js +97 -0
  61. package/dist/lib/crypto/wallet.d.ts +26 -0
  62. package/dist/lib/crypto/wallet.d.ts.map +1 -0
  63. package/dist/lib/crypto/wallet.js +327 -0
  64. package/dist/lib/helpers.d.ts +90 -0
  65. package/dist/lib/helpers.d.ts.map +1 -0
  66. package/dist/lib/helpers.js +1031 -0
  67. package/dist/lib/index.d.ts +5 -0
  68. package/dist/lib/index.d.ts.map +1 -0
  69. package/dist/lib/index.js +21 -0
  70. package/dist/lib/radon/artifacts.d.ts +55 -0
  71. package/dist/lib/radon/artifacts.d.ts.map +1 -0
  72. package/dist/lib/radon/artifacts.js +347 -0
  73. package/dist/lib/radon/ccdr/eth.d.ts +100 -0
  74. package/dist/lib/radon/ccdr/eth.d.ts.map +1 -0
  75. package/dist/lib/radon/ccdr/eth.js +237 -0
  76. package/dist/lib/radon/ccdr/index.d.ts +34 -0
  77. package/dist/lib/radon/ccdr/index.d.ts.map +1 -0
  78. package/dist/lib/radon/ccdr/index.js +63 -0
  79. package/dist/lib/radon/ccdr/wit.d.ts +29 -0
  80. package/dist/lib/radon/ccdr/wit.d.ts.map +1 -0
  81. package/dist/lib/radon/ccdr/wit.js +60 -0
  82. package/dist/lib/radon/filters.d.ts +14 -0
  83. package/dist/lib/radon/filters.d.ts.map +1 -0
  84. package/dist/lib/radon/filters.js +47 -0
  85. package/dist/lib/radon/index.d.ts +36 -0
  86. package/dist/lib/radon/index.d.ts.map +1 -0
  87. package/dist/lib/radon/index.js +154 -0
  88. package/dist/lib/radon/reducers.d.ts +29 -0
  89. package/dist/lib/radon/reducers.d.ts.map +1 -0
  90. package/dist/lib/radon/reducers.js +101 -0
  91. package/dist/lib/radon/retrievals.d.ts +120 -0
  92. package/dist/lib/radon/retrievals.d.ts.map +1 -0
  93. package/dist/lib/radon/retrievals.js +358 -0
  94. package/dist/lib/radon/sources.d.ts +102 -0
  95. package/dist/lib/radon/sources.d.ts.map +1 -0
  96. package/dist/lib/radon/sources.js +294 -0
  97. package/dist/lib/radon/types.d.ts +521 -0
  98. package/dist/lib/radon/types.d.ts.map +1 -0
  99. package/dist/lib/radon/types.js +1066 -0
  100. package/dist/lib/radon/utils.d.ts +55 -0
  101. package/dist/lib/radon/utils.d.ts.map +1 -0
  102. package/dist/lib/radon/utils.js +181 -0
  103. package/dist/lib/rpc/farm.d.ts +66 -0
  104. package/dist/lib/rpc/farm.d.ts.map +1 -0
  105. package/dist/lib/rpc/farm.js +808 -0
  106. package/dist/lib/rpc/index.d.ts +3 -0
  107. package/dist/lib/rpc/index.d.ts.map +1 -0
  108. package/dist/lib/rpc/index.js +19 -0
  109. package/dist/lib/rpc/node.d.ts +38 -0
  110. package/dist/lib/rpc/node.d.ts.map +1 -0
  111. package/dist/lib/rpc/node.js +335 -0
  112. package/dist/lib/rpc/nodes.d.ts +40 -0
  113. package/dist/lib/rpc/nodes.d.ts.map +1 -0
  114. package/dist/lib/rpc/nodes.js +531 -0
  115. package/dist/lib/rpc/provider.d.ts +72 -0
  116. package/dist/lib/rpc/provider.d.ts.map +1 -0
  117. package/dist/lib/rpc/provider.js +402 -0
  118. package/dist/lib/rpc/reporter.d.ts +18 -0
  119. package/dist/lib/rpc/reporter.d.ts.map +1 -0
  120. package/dist/lib/rpc/reporter.js +99 -0
  121. package/dist/lib/rpc/types.d.ts +396 -0
  122. package/dist/lib/rpc/types.d.ts.map +1 -0
  123. package/dist/lib/rpc/types.js +81 -0
  124. package/dist/lib/rpc/wallet.d.ts +72 -0
  125. package/dist/lib/rpc/wallet.d.ts.map +1 -0
  126. package/dist/lib/rpc/wallet.js +41 -0
  127. package/dist/lib/types.d.ts +19 -0
  128. package/dist/lib/types.d.ts.map +1 -0
  129. package/dist/lib/types.js +7 -0
  130. package/dist/lib/utils.d.ts +5 -0
  131. package/dist/lib/utils.d.ts.map +1 -0
  132. package/dist/lib/utils.js +51 -0
  133. package/dist/package.json +1 -1
  134. package/dist/src/bin/helpers.js +1 -1
  135. package/dist/src/index.js +1 -1
  136. package/dist/src/lib/crypto/account.js +1 -1
  137. package/dist/src/lib/crypto/coinbase.js +1 -1
  138. package/dist/src/lib/crypto/index.js +1 -1
  139. package/dist/src/lib/crypto/interfaces.js +1 -1
  140. package/dist/src/lib/crypto/payloads/DataRequestPayload.js +1 -1
  141. package/dist/src/lib/crypto/payloads/StakePayload.js +1 -1
  142. package/dist/src/lib/crypto/payloads/UnstakePayload.js +1 -1
  143. package/dist/src/lib/crypto/payloads/ValueTransferPayload.js +1 -1
  144. package/dist/src/lib/crypto/payloads.js +1 -1
  145. package/dist/src/lib/crypto/signer.js +1 -1
  146. package/dist/src/lib/crypto/transmitters/DataRequests.js +1 -1
  147. package/dist/src/lib/crypto/transmitters/StakeDeposits.js +1 -1
  148. package/dist/src/lib/crypto/transmitters/StakeWithdrawals.js +1 -1
  149. package/dist/src/lib/crypto/transmitters/ValueTransfers.js +1 -1
  150. package/dist/src/lib/crypto/transmitters.js +1 -1
  151. package/dist/src/lib/crypto/types.js +1 -1
  152. package/dist/src/lib/crypto/utils.js +1 -1
  153. package/dist/src/lib/crypto/wallet.js +1 -1
  154. package/dist/src/lib/index.js +1 -1
  155. package/dist/src/lib/radon/ccdr/eth.js +1 -1
  156. package/dist/src/lib/radon/ccdr/index.js +1 -1
  157. package/dist/src/lib/radon/ccdr/wit.js +1 -1
  158. package/dist/src/lib/radon/filters.js +1 -1
  159. package/dist/src/lib/radon/index.js +1 -1
  160. package/dist/src/lib/radon/reducers.js +1 -1
  161. package/dist/src/lib/radon/types.js +1 -1
  162. package/dist/src/lib/radon/utils.js +1 -1
  163. package/dist/src/lib/rpc/index.js +1 -1
  164. package/dist/src/lib/rpc/nodes.js +1 -1
  165. package/dist/src/lib/rpc/provider.js +1 -1
  166. package/dist/src/lib/rpc/reporter.js +1 -1
  167. package/dist/src/lib/rpc/types.js +1 -1
  168. package/dist/src/lib/types.d.ts +1 -1
  169. package/dist/src/lib/types.d.ts.map +1 -1
  170. package/dist/src/lib/types.js +1 -1
  171. package/dist/src/lib/utils.js +1 -1
  172. package/dist/witnet/assets/index.js +1 -1
  173. package/dist/witnet/assets/modals/index.js +1 -1
  174. package/dist/witnet/assets/modals/web3/eth.js +1 -1
  175. package/dist/witnet/assets/modals/web3/ipfs.js +1 -1
  176. package/dist/witnet/assets/modals/web3/wit.js +1 -1
  177. package/dist/witnet/assets/requests.js +1 -1
  178. package/package.json +1 -1
  179. package/src/bin/cli/history.js +31 -31
  180. package/src/bin/cli/inspect.js +405 -405
  181. package/src/bin/cli/network.js +594 -594
  182. package/src/bin/cli/nodes.js +364 -364
  183. package/src/bin/cli/radon.js +815 -815
  184. package/src/bin/cli/wallet.js +1117 -1117
  185. package/src/bin/helpers.js +840 -840
  186. package/src/bin/postinstall.js +9 -9
  187. package/src/bin/toolkit.js +295 -295
  188. package/witnet/assets/_index.js +8 -8
  189. package/witnet/assets/_requests.js +25 -25
  190. package/witnet/assets/_sources.js +36 -36
  191. package/witnet/assets/_templates.js +36 -36
  192. package/witnet/assets/index.js +4 -4
  193. package/witnet/assets/modals/index.js +9 -9
  194. package/witnet/assets/modals/web3/eth.js +29 -29
  195. package/witnet/assets/modals/web3/ipfs.js +19 -19
  196. package/witnet/assets/modals/web3/wit.js +21 -21
  197. package/witnet/assets/requests.js +95 -95
@@ -1,405 +1,405 @@
1
- const moment = require("moment")
2
-
3
- const helpers = require("../helpers")
4
- const { utils, Witnet } = require("../../../dist/src")
5
-
6
- const { cyan, gray, green, lyellow, magenta, mgreen, mmagenta, myellow, yellow } = helpers.colors
7
-
8
- /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9
- /// CLI SUBMODULE CONSTANTS ===========================================================================================
10
-
11
- module.exports = {
12
- envars: {
13
- WITNET_SDK_PROVIDER_URL: "=> Wit/Oracle RPC provider(s) to connect to, if no otherwise specified.",
14
- },
15
- flags: {
16
- provider: {
17
- hint: "Public Wit/Oracle JSON-RPC provider, other than default.",
18
- param: ":http-url",
19
- },
20
- verbose: {
21
- hint: "Outputs validators' nonce and last validation epochs.",
22
- },
23
- },
24
- router: {
25
- balance: {
26
- hint: "Show available Wits on given address.",
27
- params: "WIT_ADDRESS",
28
- },
29
- block: {
30
- hint: "Get block data given its block hash.",
31
- params: "BLOCK_HASH",
32
- },
33
- dataRequest: {
34
- hint: "Get query parameters and result to some data request transaction.",
35
- params: "DR_TX_HASH",
36
- options: {
37
- force: {
38
- hint: "Get data even if the WIT/RPC provider is not synced."
39
- },
40
- mode: {
41
- hint: "Possible report formats (default: `ethereal`).",
42
- param: "`ethereal` | `full``",
43
- },
44
- },
45
- },
46
- "dataRequests*": {
47
- hint: "Search for in-flight or recently solved data request transactions.",
48
- params: "BYTECODE | RAD_HASH | DDR_HASH",
49
- options: {
50
- limit: { hint: "Limit output records (default: 100).", param: "LIMIT" },
51
- since: {
52
- hint: "Number of past epochs to search for (default: 256; max: 2048).",
53
- param: "EPOCH|MINUS_EPOCHS",
54
- },
55
- "min-unitary-reward": {
56
- hint: "Filters out those providing less unitary reward than specified.",
57
- param: "NANOWITS",
58
- },
59
- "min-witnesses": {
60
- hint: "Filters out those solved with less than specified witnesses.",
61
- param: "NUM_WITNESSES",
62
- },
63
- },
64
- },
65
- superblock: {
66
- hint: "Show superblock metadata for given epoch.",
67
- params: "EPOCH",
68
- },
69
- transaction: {
70
- hint: "Report transaction details given its transaction hash.",
71
- params: "TX_HASH",
72
- },
73
- validators: {
74
- hint: "List validators treasuring delegated stake from the specified address.",
75
- params: "WIT_ADDRESS",
76
- },
77
- valueTransfer: {
78
- hint: "Report value transfer details given its transaction hash.",
79
- params: "VT_TX_HASH",
80
- options: {
81
- force: {
82
- hint: "Get data even if the WIT/RPC provider is not synced."
83
- },
84
- mode: {
85
- hint: "Possible report formats (default: `full`).",
86
- param: "`ethereal` | `full` | `simple`",
87
- },
88
- },
89
- },
90
- withdrawers: {
91
- hint: "List withdrawers currently delegating stake to the specified address.",
92
- params: "WIT_ADDRESS",
93
- },
94
- utxos: {
95
- hint: "List UTXOs available to the specified address.",
96
- params: "WIT_ADDRESS",
97
- options: {
98
- "small-first": {
99
- hint: "Outputs smallest UTXOs first (default: false).",
100
- },
101
- },
102
- },
103
- },
104
- subcommands: {
105
- balance, block, dataRequest, superblock, transaction, validators, withdrawers, utxos, valueTransfer,
106
- },
107
- }
108
-
109
- /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110
- /// CLI SUBMODULE COMMANDS ============================================================================================
111
-
112
- async function balance (options = {}, args = []) {
113
- if (args.length === 0) {
114
- throw Error("No WIT_ADDRESS was specified")
115
- }
116
- const pkh = args[0]
117
- const provider = new Witnet.JsonRpcProvider(options?.provider)
118
- const balance = await provider.getBalance(pkh)
119
- const records = []
120
- records.push([
121
- Witnet.Coins.fromNanowits(balance.locked).wits,
122
- Witnet.Coins.fromNanowits(balance.staked).wits,
123
- Witnet.Coins.fromNanowits(balance.unlocked).wits,
124
- Witnet.Coins.fromNanowits(balance.locked + balance.staked + balance.unlocked).wits,
125
- ])
126
- helpers.traceTable(records, {
127
- headlines: ["Locked ($WIT)", "Staked ($WIT)", "Available ($WIT)", "BALANCE ($WIT)"],
128
- humanizers: [helpers.commas, helpers.commas, helpers.commas, helpers.commas],
129
- colors: [gray, yellow, myellow, lyellow],
130
- })
131
- }
132
-
133
- async function block (options = {}, args = []) {
134
- if (args.length === 0) {
135
- throw Error("No BLOCK_HASH was specified")
136
- }
137
- const blockHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
138
- if (!helpers.isHexString(blockHash)) {
139
- throw Error("Invalid BLOCK_HASH was provided")
140
- }
141
- const provider = await Witnet.JsonRpcProvider.fromEnv(options?.provider)
142
- const block = await provider.getBlock(blockHash)
143
- console.info(gray(JSON.stringify(block, (key, value) => {
144
- switch (key) {
145
- case "bytes":
146
- case "der":
147
- case "proof":
148
- return Array.isArray(value) ? helpers.toHexString(value, true) : value
149
-
150
- case "public_key":
151
- return Array.isArray(value)
152
- ? helpers.toHexString(value, true)
153
- : (typeof value === 'object' ? Witnet.PublicKey.fromProtobuf(value).hash().toBech32(provider.network) : value)
154
-
155
- default:
156
- return value
157
- }
158
- }, 2)))
159
- }
160
-
161
- async function dataRequest (options = {}, args = []) {
162
- if (args.length === 0) {
163
- throw Error("No DR_TX_HASH was specified")
164
- }
165
- const drTxHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
166
- if (!helpers.isHexString(drTxHash)) {
167
- throw Error("Invalid DR_TX_HASH was provided")
168
- }
169
- const provider = new Witnet.JsonRpcProvider(options?.provider)
170
-
171
- const drTxJsonReplacer = (key, value) => {
172
- switch (key) {
173
- case "proof":
174
- case "public_key":
175
- case "signature":
176
- case "signatures":
177
- return undefined
178
-
179
- case "reveal":
180
- case "tally":
181
- if (Array.isArray(value)) {
182
- const result = utils.cbor.decode(Uint8Array.from(value))
183
- return Buffer.isBuffer(result) ? utils.toHexString(value) : result
184
- }
185
-
186
- default:
187
- return value
188
- }
189
- }
190
-
191
- const mode = options?.mode || `ethereal`
192
- if (!["ethereal", "full"].includes(mode)) {
193
- throw Error(`Invalid mode value: "${options.mode}"`)
194
- }
195
-
196
- const report = await provider.getDataRequest(drTxHash, mode, options?.force)
197
- console.info(JSON.stringify(report, drTxJsonReplacer, 4))
198
- }
199
-
200
- async function superblock (options = {}, args = []) {
201
- if (args.length === 0) {
202
- throw Error("No EPOCH was specified")
203
- }
204
- const provider = new Witnet.JsonRpcProvider(options?.provider)
205
- const superblock = await provider.getSuperblock(args[0])
206
- console.info(superblock)
207
- }
208
-
209
- async function transaction (options = {}, args = []) {
210
- if (args.length === 0) {
211
- throw Error("No TX_HASH was specified")
212
- }
213
- const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
214
- if (!helpers.isHexString(txHash)) {
215
- throw Error("Invalid TX_HASH was provided")
216
- }
217
- const provider = new Witnet.JsonRpcProvider(options?.provider)
218
- const transaction = await provider.getTransaction(txHash)
219
- console.info(
220
- `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`
221
- )
222
- }
223
-
224
- async function utxos (options = {}, args = []) {
225
- if (args.length < 1) {
226
- throw Error("No WIT_ADDRESS was specified")
227
- }
228
- const now = Math.floor(Date.now() / 1000)
229
- const provider = new Witnet.JsonRpcProvider(options?.provider)
230
- let utxos = await provider.getUtxos(args[0], options["small-first"] || false)
231
- let totalBalance = 0n
232
- if (!options?.verbose) {
233
- utxos = utxos
234
- .filter(utxo => utxo.timelock <= now)
235
- .map(utxo => {
236
- totalBalance += utxo.value
237
- return [
238
- utxo.output_pointer,
239
- utxo.value,
240
- ]
241
- })
242
- helpers.traceTable(utxos, {
243
- headlines: [":UTXOs", "Value ($pedros)"],
244
- humanizers: [, helpers.commas],
245
- colors: [, myellow],
246
- })
247
- } else {
248
- utxos = utxos
249
- .map(utxo => {
250
- totalBalance += utxo.value
251
- return [
252
- utxo.output_pointer,
253
- utxo.timelock > now ? gray(moment.unix(utxo.timelock).fromNow()) : "",
254
- utxo.timelock > now ? gray(helpers.commas(utxo.value)) : myellow(helpers.commas(utxo.value)),
255
- ]
256
- })
257
- helpers.traceTable(utxos, {
258
- headlines: [":UTXOs", "Timelock", "Value ($pedros)"],
259
- })
260
- }
261
- console.info(`^ Showing ${utxos.length} UTXOs: ${lyellow(helpers.whole_wits(totalBalance, 2))}.`)
262
- }
263
-
264
- async function validators (options = {}, args = []) {
265
- if (args.length === 0) {
266
- throw Error("No WIT_ADDRESS was specified")
267
- }
268
- const provider = new Witnet.JsonRpcProvider(options?.provider)
269
- const query = {
270
- filter: { withdrawer: args[0] },
271
- }
272
- const records = await provider.stakes(query)
273
- let nanowits = 0
274
- if (records.length > 0) {
275
- helpers.traceTable(
276
- records.map((record, index) => {
277
- nanowits += record.value.coins
278
- return [
279
- 1 + index,
280
- record.key.validator,
281
- ...(
282
- options?.verbose
283
- ? [record.value.nonce, record.value.epochs.witnessing, record.value.epochs.mining]
284
- : []
285
- ),
286
- Witnet.Coins.fromNanowits(record.value.coins).wits,
287
- ]
288
- }), {
289
- headlines: [
290
- "RANK",
291
- "VALIDATORS",
292
- ...(
293
- options?.verbose
294
- ? ["Nonce", "LW_Epoch", "LM_Epoch"]
295
- : []
296
- ),
297
- "STAKED ($WIT)",
298
- ],
299
- humanizers: [
300
- ,, ...(
301
- options?.verbose
302
- ? [helpers.commas, helpers.commas, helpers.commas]
303
- : []
304
- ),
305
- helpers.commas,
306
- ],
307
- colors: [, green, ...(
308
- options?.verbose
309
- ? [, magenta, cyan, myellow]
310
- : [myellow]
311
- )],
312
- }
313
- )
314
- console.info(
315
- `^ ${records.length} validators for withdrawer ${
316
- mgreen(args[0])
317
- }: ${
318
- lyellow(helpers.whole_wits(nanowits, 2))
319
- }`
320
- )
321
- } else {
322
- console.info(`> No validators found for withdrawer ${mmagenta(args[0])}.`)
323
- }
324
- }
325
-
326
- async function valueTransfer (options = {}, args = []) {
327
- if (args.length === 0) {
328
- throw Error("No VT_TX_HASH was specified")
329
- }
330
- const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
331
- if (!helpers.isHexString(txHash)) {
332
- throw Error("Invalid VT_TX_HASH was provided")
333
- }
334
- const mode = options?.mode || `full`
335
- if (!["ethereal", "full", "simple"].includes(mode)) {
336
- throw Error(`Invalid mode value: "${options.mode}"`)
337
- }
338
- const provider = new Witnet.JsonRpcProvider(options?.provider)
339
- const transaction = await provider.getValueTransfer(txHash, mode, options?.force)
340
- console.info(
341
- `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`
342
- )
343
- }
344
-
345
- async function withdrawers (options = {}, args = []) {
346
- if (args.length === 0) {
347
- throw Error("No WIT_ADDRESS was specified")
348
- }
349
- const provider = new Witnet.JsonRpcProvider(options?.provider)
350
- const query = {
351
- filter: { validator: args[0] },
352
- }
353
- const records = await provider.stakes(query)
354
- let nanowits = 0
355
- if (records.length > 0) {
356
- helpers.traceTable(
357
- records.map((record, index) => {
358
- nanowits += record.value.coins
359
- return [
360
- 1 + index,
361
- record.key.withdrawer,
362
- ...(
363
- options?.verbose
364
- ? [record.value.nonce, record.value.epochs.witnessing, record.value.epochs.mining]
365
- : []
366
- ),
367
- Witnet.Coins.fromNanowits(record.value.coins).wits,
368
- ]
369
- }), {
370
- headlines: [
371
- "RANK",
372
- "WITHDRAWERS",
373
- ...(
374
- options?.verbose
375
- ? ["Nonce", "LW_Epoch", "LM_Epoch"]
376
- : []
377
- ),
378
- "STAKED ($WIT)",
379
- ],
380
- humanizers: [
381
- ,, ...(
382
- options?.verbose
383
- ? [helpers.commas, helpers.commas, helpers.commas]
384
- : []
385
- ),
386
- helpers.commas,
387
- ],
388
- colors: [, green, ...(
389
- options?.verbose
390
- ? [, magenta, cyan, myellow]
391
- : [myellow]
392
- )],
393
- }
394
- )
395
- console.info(
396
- `^ ${records.length} withdrawers for validator ${
397
- mgreen(args[0])
398
- }: ${
399
- lyellow(helpers.whole_wits(nanowits, 2))
400
- }`
401
- )
402
- } else {
403
- console.info(`> No withdrawers found for validator ${mmagenta(args[0])}.`)
404
- }
405
- }
1
+ const moment = require("moment")
2
+
3
+ const helpers = require("../helpers")
4
+ const { utils, Witnet } = require("../../../dist/src")
5
+
6
+ const { cyan, gray, green, lyellow, magenta, mgreen, mmagenta, myellow, yellow } = helpers.colors
7
+
8
+ /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
9
+ /// CLI SUBMODULE CONSTANTS ===========================================================================================
10
+
11
+ module.exports = {
12
+ envars: {
13
+ WITNET_SDK_PROVIDER_URL: "=> Wit/Oracle RPC provider(s) to connect to, if no otherwise specified.",
14
+ },
15
+ flags: {
16
+ provider: {
17
+ hint: "Public Wit/Oracle JSON-RPC provider, other than default.",
18
+ param: ":http-url",
19
+ },
20
+ verbose: {
21
+ hint: "Outputs validators' nonce and last validation epochs.",
22
+ },
23
+ },
24
+ router: {
25
+ balance: {
26
+ hint: "Show available Wits on given address.",
27
+ params: "WIT_ADDRESS",
28
+ },
29
+ block: {
30
+ hint: "Get block data given its block hash.",
31
+ params: "BLOCK_HASH",
32
+ },
33
+ dataRequest: {
34
+ hint: "Get query parameters and result to some data request transaction.",
35
+ params: "DR_TX_HASH",
36
+ options: {
37
+ force: {
38
+ hint: "Get data even if the WIT/RPC provider is not synced."
39
+ },
40
+ mode: {
41
+ hint: "Possible report formats (default: `ethereal`).",
42
+ param: "`ethereal` | `full``",
43
+ },
44
+ },
45
+ },
46
+ "dataRequests*": {
47
+ hint: "Search for in-flight or recently solved data request transactions.",
48
+ params: "BYTECODE | RAD_HASH | DDR_HASH",
49
+ options: {
50
+ limit: { hint: "Limit output records (default: 100).", param: "LIMIT" },
51
+ since: {
52
+ hint: "Number of past epochs to search for (default: 256; max: 2048).",
53
+ param: "EPOCH|MINUS_EPOCHS",
54
+ },
55
+ "min-unitary-reward": {
56
+ hint: "Filters out those providing less unitary reward than specified.",
57
+ param: "NANOWITS",
58
+ },
59
+ "min-witnesses": {
60
+ hint: "Filters out those solved with less than specified witnesses.",
61
+ param: "NUM_WITNESSES",
62
+ },
63
+ },
64
+ },
65
+ superblock: {
66
+ hint: "Show superblock metadata for given epoch.",
67
+ params: "EPOCH",
68
+ },
69
+ transaction: {
70
+ hint: "Report transaction details given its transaction hash.",
71
+ params: "TX_HASH",
72
+ },
73
+ validators: {
74
+ hint: "List validators treasuring delegated stake from the specified address.",
75
+ params: "WIT_ADDRESS",
76
+ },
77
+ valueTransfer: {
78
+ hint: "Report value transfer details given its transaction hash.",
79
+ params: "VT_TX_HASH",
80
+ options: {
81
+ force: {
82
+ hint: "Get data even if the WIT/RPC provider is not synced."
83
+ },
84
+ mode: {
85
+ hint: "Possible report formats (default: `full`).",
86
+ param: "`ethereal` | `full` | `simple`",
87
+ },
88
+ },
89
+ },
90
+ withdrawers: {
91
+ hint: "List withdrawers currently delegating stake to the specified address.",
92
+ params: "WIT_ADDRESS",
93
+ },
94
+ utxos: {
95
+ hint: "List UTXOs available to the specified address.",
96
+ params: "WIT_ADDRESS",
97
+ options: {
98
+ "small-first": {
99
+ hint: "Outputs smallest UTXOs first (default: false).",
100
+ },
101
+ },
102
+ },
103
+ },
104
+ subcommands: {
105
+ balance, block, dataRequest, superblock, transaction, validators, withdrawers, utxos, valueTransfer,
106
+ },
107
+ }
108
+
109
+ /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
110
+ /// CLI SUBMODULE COMMANDS ============================================================================================
111
+
112
+ async function balance (options = {}, args = []) {
113
+ if (args.length === 0) {
114
+ throw Error("No WIT_ADDRESS was specified")
115
+ }
116
+ const pkh = args[0]
117
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
118
+ const balance = await provider.getBalance(pkh)
119
+ const records = []
120
+ records.push([
121
+ Witnet.Coins.fromNanowits(balance.locked).wits,
122
+ Witnet.Coins.fromNanowits(balance.staked).wits,
123
+ Witnet.Coins.fromNanowits(balance.unlocked).wits,
124
+ Witnet.Coins.fromNanowits(balance.locked + balance.staked + balance.unlocked).wits,
125
+ ])
126
+ helpers.traceTable(records, {
127
+ headlines: ["Locked ($WIT)", "Staked ($WIT)", "Available ($WIT)", "BALANCE ($WIT)"],
128
+ humanizers: [helpers.commas, helpers.commas, helpers.commas, helpers.commas],
129
+ colors: [gray, yellow, myellow, lyellow],
130
+ })
131
+ }
132
+
133
+ async function block (options = {}, args = []) {
134
+ if (args.length === 0) {
135
+ throw Error("No BLOCK_HASH was specified")
136
+ }
137
+ const blockHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
138
+ if (!helpers.isHexString(blockHash)) {
139
+ throw Error("Invalid BLOCK_HASH was provided")
140
+ }
141
+ const provider = await Witnet.JsonRpcProvider.fromEnv(options?.provider)
142
+ const block = await provider.getBlock(blockHash)
143
+ console.info(gray(JSON.stringify(block, (key, value) => {
144
+ switch (key) {
145
+ case "bytes":
146
+ case "der":
147
+ case "proof":
148
+ return Array.isArray(value) ? helpers.toHexString(value, true) : value
149
+
150
+ case "public_key":
151
+ return Array.isArray(value)
152
+ ? helpers.toHexString(value, true)
153
+ : (typeof value === 'object' ? Witnet.PublicKey.fromProtobuf(value).hash().toBech32(provider.network) : value)
154
+
155
+ default:
156
+ return value
157
+ }
158
+ }, 2)))
159
+ }
160
+
161
+ async function dataRequest (options = {}, args = []) {
162
+ if (args.length === 0) {
163
+ throw Error("No DR_TX_HASH was specified")
164
+ }
165
+ const drTxHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
166
+ if (!helpers.isHexString(drTxHash)) {
167
+ throw Error("Invalid DR_TX_HASH was provided")
168
+ }
169
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
170
+
171
+ const drTxJsonReplacer = (key, value) => {
172
+ switch (key) {
173
+ case "proof":
174
+ case "public_key":
175
+ case "signature":
176
+ case "signatures":
177
+ return undefined
178
+
179
+ case "reveal":
180
+ case "tally":
181
+ if (Array.isArray(value)) {
182
+ const result = utils.cbor.decode(Uint8Array.from(value))
183
+ return Buffer.isBuffer(result) ? utils.toHexString(value) : result
184
+ }
185
+
186
+ default:
187
+ return value
188
+ }
189
+ }
190
+
191
+ const mode = options?.mode || `ethereal`
192
+ if (!["ethereal", "full"].includes(mode)) {
193
+ throw Error(`Invalid mode value: "${options.mode}"`)
194
+ }
195
+
196
+ const report = await provider.getDataRequest(drTxHash, mode, options?.force)
197
+ console.info(JSON.stringify(report, drTxJsonReplacer, 4))
198
+ }
199
+
200
+ async function superblock (options = {}, args = []) {
201
+ if (args.length === 0) {
202
+ throw Error("No EPOCH was specified")
203
+ }
204
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
205
+ const superblock = await provider.getSuperblock(args[0])
206
+ console.info(superblock)
207
+ }
208
+
209
+ async function transaction (options = {}, args = []) {
210
+ if (args.length === 0) {
211
+ throw Error("No TX_HASH was specified")
212
+ }
213
+ const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
214
+ if (!helpers.isHexString(txHash)) {
215
+ throw Error("Invalid TX_HASH was provided")
216
+ }
217
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
218
+ const transaction = await provider.getTransaction(txHash)
219
+ console.info(
220
+ `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`
221
+ )
222
+ }
223
+
224
+ async function utxos (options = {}, args = []) {
225
+ if (args.length < 1) {
226
+ throw Error("No WIT_ADDRESS was specified")
227
+ }
228
+ const now = Math.floor(Date.now() / 1000)
229
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
230
+ let utxos = await provider.getUtxos(args[0], options["small-first"] || false)
231
+ let totalBalance = 0n
232
+ if (!options?.verbose) {
233
+ utxos = utxos
234
+ .filter(utxo => utxo.timelock <= now)
235
+ .map(utxo => {
236
+ totalBalance += utxo.value
237
+ return [
238
+ utxo.output_pointer,
239
+ utxo.value,
240
+ ]
241
+ })
242
+ helpers.traceTable(utxos, {
243
+ headlines: [":UTXOs", "Value ($pedros)"],
244
+ humanizers: [, helpers.commas],
245
+ colors: [, myellow],
246
+ })
247
+ } else {
248
+ utxos = utxos
249
+ .map(utxo => {
250
+ totalBalance += utxo.value
251
+ return [
252
+ utxo.output_pointer,
253
+ utxo.timelock > now ? gray(moment.unix(utxo.timelock).fromNow()) : "",
254
+ utxo.timelock > now ? gray(helpers.commas(utxo.value)) : myellow(helpers.commas(utxo.value)),
255
+ ]
256
+ })
257
+ helpers.traceTable(utxos, {
258
+ headlines: [":UTXOs", "Timelock", "Value ($pedros)"],
259
+ })
260
+ }
261
+ console.info(`^ Showing ${utxos.length} UTXOs: ${lyellow(helpers.whole_wits(totalBalance, 2))}.`)
262
+ }
263
+
264
+ async function validators (options = {}, args = []) {
265
+ if (args.length === 0) {
266
+ throw Error("No WIT_ADDRESS was specified")
267
+ }
268
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
269
+ const query = {
270
+ filter: { withdrawer: args[0] },
271
+ }
272
+ const records = await provider.stakes(query)
273
+ let nanowits = 0
274
+ if (records.length > 0) {
275
+ helpers.traceTable(
276
+ records.map((record, index) => {
277
+ nanowits += record.value.coins
278
+ return [
279
+ 1 + index,
280
+ record.key.validator,
281
+ ...(
282
+ options?.verbose
283
+ ? [record.value.nonce, record.value.epochs.witnessing, record.value.epochs.mining]
284
+ : []
285
+ ),
286
+ Witnet.Coins.fromNanowits(record.value.coins).wits,
287
+ ]
288
+ }), {
289
+ headlines: [
290
+ "RANK",
291
+ "VALIDATORS",
292
+ ...(
293
+ options?.verbose
294
+ ? ["Nonce", "LW_Epoch", "LM_Epoch"]
295
+ : []
296
+ ),
297
+ "STAKED ($WIT)",
298
+ ],
299
+ humanizers: [
300
+ ,, ...(
301
+ options?.verbose
302
+ ? [helpers.commas, helpers.commas, helpers.commas]
303
+ : []
304
+ ),
305
+ helpers.commas,
306
+ ],
307
+ colors: [, green, ...(
308
+ options?.verbose
309
+ ? [, magenta, cyan, myellow]
310
+ : [myellow]
311
+ )],
312
+ }
313
+ )
314
+ console.info(
315
+ `^ ${records.length} validators for withdrawer ${
316
+ mgreen(args[0])
317
+ }: ${
318
+ lyellow(helpers.whole_wits(nanowits, 2))
319
+ }`
320
+ )
321
+ } else {
322
+ console.info(`> No validators found for withdrawer ${mmagenta(args[0])}.`)
323
+ }
324
+ }
325
+
326
+ async function valueTransfer (options = {}, args = []) {
327
+ if (args.length === 0) {
328
+ throw Error("No VT_TX_HASH was specified")
329
+ }
330
+ const txHash = args[0].startsWith("0x") ? args[0].slice(2) : args[0]
331
+ if (!helpers.isHexString(txHash)) {
332
+ throw Error("Invalid VT_TX_HASH was provided")
333
+ }
334
+ const mode = options?.mode || `full`
335
+ if (!["ethereal", "full", "simple"].includes(mode)) {
336
+ throw Error(`Invalid mode value: "${options.mode}"`)
337
+ }
338
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
339
+ const transaction = await provider.getValueTransfer(txHash, mode, options?.force)
340
+ console.info(
341
+ `${yellow(JSON.stringify(transaction, utils.txJsonReplacer, 2))}`
342
+ )
343
+ }
344
+
345
+ async function withdrawers (options = {}, args = []) {
346
+ if (args.length === 0) {
347
+ throw Error("No WIT_ADDRESS was specified")
348
+ }
349
+ const provider = new Witnet.JsonRpcProvider(options?.provider)
350
+ const query = {
351
+ filter: { validator: args[0] },
352
+ }
353
+ const records = await provider.stakes(query)
354
+ let nanowits = 0
355
+ if (records.length > 0) {
356
+ helpers.traceTable(
357
+ records.map((record, index) => {
358
+ nanowits += record.value.coins
359
+ return [
360
+ 1 + index,
361
+ record.key.withdrawer,
362
+ ...(
363
+ options?.verbose
364
+ ? [record.value.nonce, record.value.epochs.witnessing, record.value.epochs.mining]
365
+ : []
366
+ ),
367
+ Witnet.Coins.fromNanowits(record.value.coins).wits,
368
+ ]
369
+ }), {
370
+ headlines: [
371
+ "RANK",
372
+ "WITHDRAWERS",
373
+ ...(
374
+ options?.verbose
375
+ ? ["Nonce", "LW_Epoch", "LM_Epoch"]
376
+ : []
377
+ ),
378
+ "STAKED ($WIT)",
379
+ ],
380
+ humanizers: [
381
+ ,, ...(
382
+ options?.verbose
383
+ ? [helpers.commas, helpers.commas, helpers.commas]
384
+ : []
385
+ ),
386
+ helpers.commas,
387
+ ],
388
+ colors: [, green, ...(
389
+ options?.verbose
390
+ ? [, magenta, cyan, myellow]
391
+ : [myellow]
392
+ )],
393
+ }
394
+ )
395
+ console.info(
396
+ `^ ${records.length} withdrawers for validator ${
397
+ mgreen(args[0])
398
+ }: ${
399
+ lyellow(helpers.whole_wits(nanowits, 2))
400
+ }`
401
+ )
402
+ } else {
403
+ console.info(`> No withdrawers found for validator ${mmagenta(args[0])}.`)
404
+ }
405
+ }