@witnet/sdk 1.0.4 → 1.0.5

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 (211) hide show
  1. package/.env_witnet +6 -6
  2. package/LICENSE +21 -21
  3. package/README.md +103 -103
  4. package/dist/package.json +3 -4
  5. package/dist/src/bin/helpers.js +1 -1
  6. package/dist/src/index.js +1 -1
  7. package/dist/src/lib/crypto/account.d.ts +3 -1
  8. package/dist/src/lib/crypto/account.d.ts.map +1 -1
  9. package/dist/src/lib/crypto/account.js +7 -1
  10. package/dist/src/lib/crypto/coinbase.js +1 -1
  11. package/dist/src/lib/crypto/index.js +1 -1
  12. package/dist/src/lib/crypto/interfaces.d.ts +2 -0
  13. package/dist/src/lib/crypto/interfaces.d.ts.map +1 -1
  14. package/dist/src/lib/crypto/interfaces.js +1 -1
  15. package/dist/src/lib/crypto/payloads/DataRequestPayload.js +1 -1
  16. package/dist/src/lib/crypto/payloads/StakePayload.js +1 -1
  17. package/dist/src/lib/crypto/payloads/UnstakePayload.js +1 -1
  18. package/dist/src/lib/crypto/payloads/ValueTransferPayload.js +1 -1
  19. package/dist/src/lib/crypto/payloads.d.ts.map +1 -1
  20. package/dist/src/lib/crypto/payloads.js +12 -1
  21. package/dist/src/lib/crypto/signer.d.ts +1 -0
  22. package/dist/src/lib/crypto/signer.d.ts.map +1 -1
  23. package/dist/src/lib/crypto/signer.js +18 -1
  24. package/dist/src/lib/crypto/transmitters/DataRequests.js +1 -1
  25. package/dist/src/lib/crypto/transmitters/StakeDeposits.js +1 -1
  26. package/dist/src/lib/crypto/transmitters/StakeWithdrawals.js +1 -1
  27. package/dist/src/lib/crypto/transmitters/ValueTransfers.js +1 -1
  28. package/dist/src/lib/crypto/transmitters.js +4 -4
  29. package/dist/src/lib/crypto/types.d.ts.map +1 -1
  30. package/dist/src/lib/crypto/types.js +2 -1
  31. package/dist/src/lib/crypto/utils.js +1 -1
  32. package/dist/src/lib/crypto/wallet.d.ts +3 -1
  33. package/dist/src/lib/crypto/wallet.d.ts.map +1 -1
  34. package/dist/src/lib/crypto/wallet.js +10 -1
  35. package/dist/src/lib/index.js +1 -1
  36. package/dist/src/lib/radon/ccdr/eth.js +1 -1
  37. package/dist/src/lib/radon/ccdr/index.js +1 -1
  38. package/dist/src/lib/radon/ccdr/wit.js +1 -1
  39. package/dist/src/lib/radon/filters.js +1 -1
  40. package/dist/src/lib/radon/index.js +1 -1
  41. package/dist/src/lib/radon/reducers.js +1 -1
  42. package/dist/src/lib/radon/types.js +1 -1
  43. package/dist/src/lib/radon/utils.js +1 -1
  44. package/dist/src/lib/rpc/index.js +1 -1
  45. package/dist/src/lib/rpc/nodes.js +1 -1
  46. package/dist/src/lib/rpc/provider.js +1 -1
  47. package/dist/src/lib/rpc/types.d.ts +6 -4
  48. package/dist/src/lib/rpc/types.d.ts.map +1 -1
  49. package/dist/src/lib/rpc/types.js +1 -1
  50. package/dist/src/lib/types.d.ts +0 -1
  51. package/dist/src/lib/types.d.ts.map +1 -1
  52. package/dist/src/lib/types.js +1 -1
  53. package/dist/src/lib/utils.js +1 -1
  54. package/dist/witnet/assets/index.js +1 -1
  55. package/dist/witnet/assets/modals/index.js +1 -1
  56. package/dist/witnet/assets/modals/web3/eth.js +1 -1
  57. package/dist/witnet/assets/modals/web3/ipfs.js +1 -1
  58. package/dist/witnet/assets/modals/web3/wit.js +1 -1
  59. package/dist/witnet/assets/requests.js +1 -1
  60. package/package.json +3 -4
  61. package/src/bin/cli/history.js +31 -31
  62. package/src/bin/cli/inspect.js +405 -405
  63. package/src/bin/cli/network.js +594 -594
  64. package/src/bin/cli/nodes.js +364 -364
  65. package/src/bin/cli/radon.js +817 -815
  66. package/src/bin/cli/wallet.js +1121 -1117
  67. package/src/bin/helpers.js +840 -840
  68. package/src/bin/postinstall.js +9 -9
  69. package/src/bin/toolkit.js +295 -295
  70. package/witnet/assets/_index.js +8 -8
  71. package/witnet/assets/_requests.js +25 -25
  72. package/witnet/assets/_sources.js +36 -36
  73. package/witnet/assets/_templates.js +36 -36
  74. package/witnet/assets/index.js +4 -4
  75. package/witnet/assets/modals/index.js +9 -9
  76. package/witnet/assets/modals/web3/eth.js +29 -29
  77. package/witnet/assets/modals/web3/ipfs.js +19 -19
  78. package/witnet/assets/modals/web3/wit.js +21 -21
  79. package/witnet/assets/requests.js +95 -95
  80. package/dist/bin/helpers.d.ts +0 -88
  81. package/dist/bin/helpers.d.ts.map +0 -1
  82. package/dist/bin/helpers.js +0 -866
  83. package/dist/index.d.ts +0 -4
  84. package/dist/index.d.ts.map +0 -1
  85. package/dist/index.js +0 -42
  86. package/dist/lib/crypto/account.d.ts +0 -18
  87. package/dist/lib/crypto/account.d.ts.map +0 -1
  88. package/dist/lib/crypto/account.js +0 -152
  89. package/dist/lib/crypto/coinbase.d.ts +0 -9
  90. package/dist/lib/crypto/coinbase.d.ts.map +0 -1
  91. package/dist/lib/crypto/coinbase.js +0 -39
  92. package/dist/lib/crypto/index.d.ts +0 -7
  93. package/dist/lib/crypto/index.d.ts.map +0 -1
  94. package/dist/lib/crypto/index.js +0 -28
  95. package/dist/lib/crypto/interfaces.d.ts +0 -80
  96. package/dist/lib/crypto/interfaces.d.ts.map +0 -1
  97. package/dist/lib/crypto/interfaces.js +0 -3
  98. package/dist/lib/crypto/payloads/DataRequestPayload.d.ts +0 -47
  99. package/dist/lib/crypto/payloads/DataRequestPayload.d.ts.map +0 -1
  100. package/dist/lib/crypto/payloads/DataRequestPayload.js +0 -384
  101. package/dist/lib/crypto/payloads/StakePayload.d.ts +0 -27
  102. package/dist/lib/crypto/payloads/StakePayload.d.ts.map +0 -1
  103. package/dist/lib/crypto/payloads/StakePayload.js +0 -184
  104. package/dist/lib/crypto/payloads/UnstakePayload.d.ts +0 -35
  105. package/dist/lib/crypto/payloads/UnstakePayload.d.ts.map +0 -1
  106. package/dist/lib/crypto/payloads/UnstakePayload.js +0 -244
  107. package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts +0 -24
  108. package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts.map +0 -1
  109. package/dist/lib/crypto/payloads/ValueTransferPayload.js +0 -182
  110. package/dist/lib/crypto/payloads.d.ts +0 -54
  111. package/dist/lib/crypto/payloads.d.ts.map +0 -1
  112. package/dist/lib/crypto/payloads.js +0 -224
  113. package/dist/lib/crypto/signer.d.ts +0 -26
  114. package/dist/lib/crypto/signer.d.ts.map +0 -1
  115. package/dist/lib/crypto/signer.js +0 -299
  116. package/dist/lib/crypto/transmitters/DataRequests.d.ts +0 -14
  117. package/dist/lib/crypto/transmitters/DataRequests.d.ts.map +0 -1
  118. package/dist/lib/crypto/transmitters/DataRequests.js +0 -62
  119. package/dist/lib/crypto/transmitters/StakeDeposits.d.ts +0 -11
  120. package/dist/lib/crypto/transmitters/StakeDeposits.d.ts.map +0 -1
  121. package/dist/lib/crypto/transmitters/StakeDeposits.js +0 -48
  122. package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts +0 -17
  123. package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts.map +0 -1
  124. package/dist/lib/crypto/transmitters/StakeWithdrawals.js +0 -115
  125. package/dist/lib/crypto/transmitters/ValueTransfers.d.ts +0 -10
  126. package/dist/lib/crypto/transmitters/ValueTransfers.d.ts.map +0 -1
  127. package/dist/lib/crypto/transmitters/ValueTransfers.js +0 -47
  128. package/dist/lib/crypto/transmitters.d.ts +0 -46
  129. package/dist/lib/crypto/transmitters.d.ts.map +0 -1
  130. package/dist/lib/crypto/transmitters.js +0 -506
  131. package/dist/lib/crypto/types.d.ts +0 -127
  132. package/dist/lib/crypto/types.d.ts.map +0 -1
  133. package/dist/lib/crypto/types.js +0 -261
  134. package/dist/lib/crypto/utils.d.ts +0 -10
  135. package/dist/lib/crypto/utils.d.ts.map +0 -1
  136. package/dist/lib/crypto/utils.js +0 -97
  137. package/dist/lib/crypto/wallet.d.ts +0 -26
  138. package/dist/lib/crypto/wallet.d.ts.map +0 -1
  139. package/dist/lib/crypto/wallet.js +0 -327
  140. package/dist/lib/helpers.d.ts +0 -90
  141. package/dist/lib/helpers.d.ts.map +0 -1
  142. package/dist/lib/helpers.js +0 -1031
  143. package/dist/lib/index.d.ts +0 -5
  144. package/dist/lib/index.d.ts.map +0 -1
  145. package/dist/lib/index.js +0 -21
  146. package/dist/lib/radon/artifacts.d.ts +0 -55
  147. package/dist/lib/radon/artifacts.d.ts.map +0 -1
  148. package/dist/lib/radon/artifacts.js +0 -347
  149. package/dist/lib/radon/ccdr/eth.d.ts +0 -100
  150. package/dist/lib/radon/ccdr/eth.d.ts.map +0 -1
  151. package/dist/lib/radon/ccdr/eth.js +0 -237
  152. package/dist/lib/radon/ccdr/index.d.ts +0 -34
  153. package/dist/lib/radon/ccdr/index.d.ts.map +0 -1
  154. package/dist/lib/radon/ccdr/index.js +0 -63
  155. package/dist/lib/radon/ccdr/wit.d.ts +0 -29
  156. package/dist/lib/radon/ccdr/wit.d.ts.map +0 -1
  157. package/dist/lib/radon/ccdr/wit.js +0 -60
  158. package/dist/lib/radon/filters.d.ts +0 -14
  159. package/dist/lib/radon/filters.d.ts.map +0 -1
  160. package/dist/lib/radon/filters.js +0 -47
  161. package/dist/lib/radon/index.d.ts +0 -36
  162. package/dist/lib/radon/index.d.ts.map +0 -1
  163. package/dist/lib/radon/index.js +0 -154
  164. package/dist/lib/radon/reducers.d.ts +0 -29
  165. package/dist/lib/radon/reducers.d.ts.map +0 -1
  166. package/dist/lib/radon/reducers.js +0 -101
  167. package/dist/lib/radon/retrievals.d.ts +0 -120
  168. package/dist/lib/radon/retrievals.d.ts.map +0 -1
  169. package/dist/lib/radon/retrievals.js +0 -358
  170. package/dist/lib/radon/sources.d.ts +0 -102
  171. package/dist/lib/radon/sources.d.ts.map +0 -1
  172. package/dist/lib/radon/sources.js +0 -294
  173. package/dist/lib/radon/types.d.ts +0 -521
  174. package/dist/lib/radon/types.d.ts.map +0 -1
  175. package/dist/lib/radon/types.js +0 -1066
  176. package/dist/lib/radon/utils.d.ts +0 -55
  177. package/dist/lib/radon/utils.d.ts.map +0 -1
  178. package/dist/lib/radon/utils.js +0 -181
  179. package/dist/lib/rpc/farm.d.ts +0 -66
  180. package/dist/lib/rpc/farm.d.ts.map +0 -1
  181. package/dist/lib/rpc/farm.js +0 -808
  182. package/dist/lib/rpc/index.d.ts +0 -3
  183. package/dist/lib/rpc/index.d.ts.map +0 -1
  184. package/dist/lib/rpc/index.js +0 -19
  185. package/dist/lib/rpc/node.d.ts +0 -38
  186. package/dist/lib/rpc/node.d.ts.map +0 -1
  187. package/dist/lib/rpc/node.js +0 -335
  188. package/dist/lib/rpc/nodes.d.ts +0 -40
  189. package/dist/lib/rpc/nodes.d.ts.map +0 -1
  190. package/dist/lib/rpc/nodes.js +0 -531
  191. package/dist/lib/rpc/provider.d.ts +0 -72
  192. package/dist/lib/rpc/provider.d.ts.map +0 -1
  193. package/dist/lib/rpc/provider.js +0 -402
  194. package/dist/lib/rpc/reporter.d.ts +0 -18
  195. package/dist/lib/rpc/reporter.d.ts.map +0 -1
  196. package/dist/lib/rpc/reporter.js +0 -99
  197. package/dist/lib/rpc/types.d.ts +0 -396
  198. package/dist/lib/rpc/types.d.ts.map +0 -1
  199. package/dist/lib/rpc/types.js +0 -81
  200. package/dist/lib/rpc/wallet.d.ts +0 -72
  201. package/dist/lib/rpc/wallet.d.ts.map +0 -1
  202. package/dist/lib/rpc/wallet.js +0 -41
  203. package/dist/lib/types.d.ts +0 -19
  204. package/dist/lib/types.d.ts.map +0 -1
  205. package/dist/lib/types.js +0 -7
  206. package/dist/lib/utils.d.ts +0 -5
  207. package/dist/lib/utils.d.ts.map +0 -1
  208. package/dist/lib/utils.js +0 -51
  209. package/dist/src/lib/rpc/reporter.d.ts +0 -17
  210. package/dist/src/lib/rpc/reporter.d.ts.map +0 -1
  211. package/dist/src/lib/rpc/reporter.js +0 -27
@@ -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
+ }