@witnet/sdk 1.0.4 → 1.0.6

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 (213) 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 +2 -2
  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.d.ts +1 -1
  29. package/dist/src/lib/crypto/transmitters.d.ts.map +1 -1
  30. package/dist/src/lib/crypto/transmitters.js +4 -4
  31. package/dist/src/lib/crypto/types.d.ts.map +1 -1
  32. package/dist/src/lib/crypto/types.js +2 -1
  33. package/dist/src/lib/crypto/utils.js +1 -1
  34. package/dist/src/lib/crypto/wallet.d.ts +3 -1
  35. package/dist/src/lib/crypto/wallet.d.ts.map +1 -1
  36. package/dist/src/lib/crypto/wallet.js +10 -1
  37. package/dist/src/lib/index.js +1 -1
  38. package/dist/src/lib/radon/ccdr/eth.js +1 -1
  39. package/dist/src/lib/radon/ccdr/index.js +1 -1
  40. package/dist/src/lib/radon/ccdr/wit.js +1 -1
  41. package/dist/src/lib/radon/filters.js +1 -1
  42. package/dist/src/lib/radon/index.js +1 -1
  43. package/dist/src/lib/radon/reducers.js +1 -1
  44. package/dist/src/lib/radon/types.js +1 -1
  45. package/dist/src/lib/radon/utils.js +1 -1
  46. package/dist/src/lib/rpc/index.js +1 -1
  47. package/dist/src/lib/rpc/nodes.js +1 -1
  48. package/dist/src/lib/rpc/provider.js +1 -1
  49. package/dist/src/lib/rpc/types.d.ts +6 -4
  50. package/dist/src/lib/rpc/types.d.ts.map +1 -1
  51. package/dist/src/lib/rpc/types.js +1 -1
  52. package/dist/src/lib/types.d.ts +0 -1
  53. package/dist/src/lib/types.d.ts.map +1 -1
  54. package/dist/src/lib/types.js +1 -1
  55. package/dist/src/lib/utils.js +1 -1
  56. package/dist/witnet/assets/index.js +1 -1
  57. package/dist/witnet/assets/modals/index.js +1 -1
  58. package/dist/witnet/assets/modals/web3/eth.js +1 -1
  59. package/dist/witnet/assets/modals/web3/ipfs.js +1 -1
  60. package/dist/witnet/assets/modals/web3/wit.js +1 -1
  61. package/dist/witnet/assets/requests.js +1 -1
  62. package/package.json +3 -4
  63. package/src/bin/cli/history.js +31 -31
  64. package/src/bin/cli/inspect.js +405 -405
  65. package/src/bin/cli/network.js +594 -594
  66. package/src/bin/cli/nodes.js +364 -364
  67. package/src/bin/cli/radon.js +817 -815
  68. package/src/bin/cli/wallet.js +1121 -1117
  69. package/src/bin/helpers.js +840 -840
  70. package/src/bin/postinstall.js +9 -9
  71. package/src/bin/toolkit.js +295 -295
  72. package/witnet/assets/_index.js +8 -8
  73. package/witnet/assets/_requests.js +25 -25
  74. package/witnet/assets/_sources.js +36 -36
  75. package/witnet/assets/_templates.js +36 -36
  76. package/witnet/assets/index.js +4 -4
  77. package/witnet/assets/modals/index.js +9 -9
  78. package/witnet/assets/modals/web3/eth.js +29 -29
  79. package/witnet/assets/modals/web3/ipfs.js +19 -19
  80. package/witnet/assets/modals/web3/wit.js +21 -21
  81. package/witnet/assets/requests.js +95 -95
  82. package/dist/bin/helpers.d.ts +0 -88
  83. package/dist/bin/helpers.d.ts.map +0 -1
  84. package/dist/bin/helpers.js +0 -866
  85. package/dist/index.d.ts +0 -4
  86. package/dist/index.d.ts.map +0 -1
  87. package/dist/index.js +0 -42
  88. package/dist/lib/crypto/account.d.ts +0 -18
  89. package/dist/lib/crypto/account.d.ts.map +0 -1
  90. package/dist/lib/crypto/account.js +0 -152
  91. package/dist/lib/crypto/coinbase.d.ts +0 -9
  92. package/dist/lib/crypto/coinbase.d.ts.map +0 -1
  93. package/dist/lib/crypto/coinbase.js +0 -39
  94. package/dist/lib/crypto/index.d.ts +0 -7
  95. package/dist/lib/crypto/index.d.ts.map +0 -1
  96. package/dist/lib/crypto/index.js +0 -28
  97. package/dist/lib/crypto/interfaces.d.ts +0 -80
  98. package/dist/lib/crypto/interfaces.d.ts.map +0 -1
  99. package/dist/lib/crypto/interfaces.js +0 -3
  100. package/dist/lib/crypto/payloads/DataRequestPayload.d.ts +0 -47
  101. package/dist/lib/crypto/payloads/DataRequestPayload.d.ts.map +0 -1
  102. package/dist/lib/crypto/payloads/DataRequestPayload.js +0 -384
  103. package/dist/lib/crypto/payloads/StakePayload.d.ts +0 -27
  104. package/dist/lib/crypto/payloads/StakePayload.d.ts.map +0 -1
  105. package/dist/lib/crypto/payloads/StakePayload.js +0 -184
  106. package/dist/lib/crypto/payloads/UnstakePayload.d.ts +0 -35
  107. package/dist/lib/crypto/payloads/UnstakePayload.d.ts.map +0 -1
  108. package/dist/lib/crypto/payloads/UnstakePayload.js +0 -244
  109. package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts +0 -24
  110. package/dist/lib/crypto/payloads/ValueTransferPayload.d.ts.map +0 -1
  111. package/dist/lib/crypto/payloads/ValueTransferPayload.js +0 -182
  112. package/dist/lib/crypto/payloads.d.ts +0 -54
  113. package/dist/lib/crypto/payloads.d.ts.map +0 -1
  114. package/dist/lib/crypto/payloads.js +0 -224
  115. package/dist/lib/crypto/signer.d.ts +0 -26
  116. package/dist/lib/crypto/signer.d.ts.map +0 -1
  117. package/dist/lib/crypto/signer.js +0 -299
  118. package/dist/lib/crypto/transmitters/DataRequests.d.ts +0 -14
  119. package/dist/lib/crypto/transmitters/DataRequests.d.ts.map +0 -1
  120. package/dist/lib/crypto/transmitters/DataRequests.js +0 -62
  121. package/dist/lib/crypto/transmitters/StakeDeposits.d.ts +0 -11
  122. package/dist/lib/crypto/transmitters/StakeDeposits.d.ts.map +0 -1
  123. package/dist/lib/crypto/transmitters/StakeDeposits.js +0 -48
  124. package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts +0 -17
  125. package/dist/lib/crypto/transmitters/StakeWithdrawals.d.ts.map +0 -1
  126. package/dist/lib/crypto/transmitters/StakeWithdrawals.js +0 -115
  127. package/dist/lib/crypto/transmitters/ValueTransfers.d.ts +0 -10
  128. package/dist/lib/crypto/transmitters/ValueTransfers.d.ts.map +0 -1
  129. package/dist/lib/crypto/transmitters/ValueTransfers.js +0 -47
  130. package/dist/lib/crypto/transmitters.d.ts +0 -46
  131. package/dist/lib/crypto/transmitters.d.ts.map +0 -1
  132. package/dist/lib/crypto/transmitters.js +0 -506
  133. package/dist/lib/crypto/types.d.ts +0 -127
  134. package/dist/lib/crypto/types.d.ts.map +0 -1
  135. package/dist/lib/crypto/types.js +0 -261
  136. package/dist/lib/crypto/utils.d.ts +0 -10
  137. package/dist/lib/crypto/utils.d.ts.map +0 -1
  138. package/dist/lib/crypto/utils.js +0 -97
  139. package/dist/lib/crypto/wallet.d.ts +0 -26
  140. package/dist/lib/crypto/wallet.d.ts.map +0 -1
  141. package/dist/lib/crypto/wallet.js +0 -327
  142. package/dist/lib/helpers.d.ts +0 -90
  143. package/dist/lib/helpers.d.ts.map +0 -1
  144. package/dist/lib/helpers.js +0 -1031
  145. package/dist/lib/index.d.ts +0 -5
  146. package/dist/lib/index.d.ts.map +0 -1
  147. package/dist/lib/index.js +0 -21
  148. package/dist/lib/radon/artifacts.d.ts +0 -55
  149. package/dist/lib/radon/artifacts.d.ts.map +0 -1
  150. package/dist/lib/radon/artifacts.js +0 -347
  151. package/dist/lib/radon/ccdr/eth.d.ts +0 -100
  152. package/dist/lib/radon/ccdr/eth.d.ts.map +0 -1
  153. package/dist/lib/radon/ccdr/eth.js +0 -237
  154. package/dist/lib/radon/ccdr/index.d.ts +0 -34
  155. package/dist/lib/radon/ccdr/index.d.ts.map +0 -1
  156. package/dist/lib/radon/ccdr/index.js +0 -63
  157. package/dist/lib/radon/ccdr/wit.d.ts +0 -29
  158. package/dist/lib/radon/ccdr/wit.d.ts.map +0 -1
  159. package/dist/lib/radon/ccdr/wit.js +0 -60
  160. package/dist/lib/radon/filters.d.ts +0 -14
  161. package/dist/lib/radon/filters.d.ts.map +0 -1
  162. package/dist/lib/radon/filters.js +0 -47
  163. package/dist/lib/radon/index.d.ts +0 -36
  164. package/dist/lib/radon/index.d.ts.map +0 -1
  165. package/dist/lib/radon/index.js +0 -154
  166. package/dist/lib/radon/reducers.d.ts +0 -29
  167. package/dist/lib/radon/reducers.d.ts.map +0 -1
  168. package/dist/lib/radon/reducers.js +0 -101
  169. package/dist/lib/radon/retrievals.d.ts +0 -120
  170. package/dist/lib/radon/retrievals.d.ts.map +0 -1
  171. package/dist/lib/radon/retrievals.js +0 -358
  172. package/dist/lib/radon/sources.d.ts +0 -102
  173. package/dist/lib/radon/sources.d.ts.map +0 -1
  174. package/dist/lib/radon/sources.js +0 -294
  175. package/dist/lib/radon/types.d.ts +0 -521
  176. package/dist/lib/radon/types.d.ts.map +0 -1
  177. package/dist/lib/radon/types.js +0 -1066
  178. package/dist/lib/radon/utils.d.ts +0 -55
  179. package/dist/lib/radon/utils.d.ts.map +0 -1
  180. package/dist/lib/radon/utils.js +0 -181
  181. package/dist/lib/rpc/farm.d.ts +0 -66
  182. package/dist/lib/rpc/farm.d.ts.map +0 -1
  183. package/dist/lib/rpc/farm.js +0 -808
  184. package/dist/lib/rpc/index.d.ts +0 -3
  185. package/dist/lib/rpc/index.d.ts.map +0 -1
  186. package/dist/lib/rpc/index.js +0 -19
  187. package/dist/lib/rpc/node.d.ts +0 -38
  188. package/dist/lib/rpc/node.d.ts.map +0 -1
  189. package/dist/lib/rpc/node.js +0 -335
  190. package/dist/lib/rpc/nodes.d.ts +0 -40
  191. package/dist/lib/rpc/nodes.d.ts.map +0 -1
  192. package/dist/lib/rpc/nodes.js +0 -531
  193. package/dist/lib/rpc/provider.d.ts +0 -72
  194. package/dist/lib/rpc/provider.d.ts.map +0 -1
  195. package/dist/lib/rpc/provider.js +0 -402
  196. package/dist/lib/rpc/reporter.d.ts +0 -18
  197. package/dist/lib/rpc/reporter.d.ts.map +0 -1
  198. package/dist/lib/rpc/reporter.js +0 -99
  199. package/dist/lib/rpc/types.d.ts +0 -396
  200. package/dist/lib/rpc/types.d.ts.map +0 -1
  201. package/dist/lib/rpc/types.js +0 -81
  202. package/dist/lib/rpc/wallet.d.ts +0 -72
  203. package/dist/lib/rpc/wallet.d.ts.map +0 -1
  204. package/dist/lib/rpc/wallet.js +0 -41
  205. package/dist/lib/types.d.ts +0 -19
  206. package/dist/lib/types.d.ts.map +0 -1
  207. package/dist/lib/types.js +0 -7
  208. package/dist/lib/utils.d.ts +0 -5
  209. package/dist/lib/utils.d.ts.map +0 -1
  210. package/dist/lib/utils.js +0 -51
  211. package/dist/src/lib/rpc/reporter.d.ts +0 -17
  212. package/dist/src/lib/rpc/reporter.d.ts.map +0 -1
  213. package/dist/src/lib/rpc/reporter.js +0 -27
@@ -1,364 +1,364 @@
1
- const qrcode = require("qrcode-terminal")
2
- const helpers = require("../helpers")
3
- const { Witnet } = require("../../../dist/src")
4
-
5
- const { cyan, gray, green, red, yellow, white, mcyan, mgreen, mmagenta, mred, myellow, lcyan, lgreen, lmagenta, lyellow } = helpers.colors
6
-
7
- /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
- /// CLI SUBMODULE CONSTANTS ===========================================================================================
9
-
10
- module.exports = {
11
- envars: {
12
- WITNET_TOOLKIT_FARM_NODES: "=> URLs to your own nodes' HTTP/JSON private endpoints, if no otherwise specified.",
13
- },
14
- flags: {
15
- nodes: {
16
- hint: "Private URLs to your node's HTTP/JSON endpoints, other than default.",
17
- param: "JSON_HTTP_URL[;..]",
18
- },
19
- },
20
- router: {
21
- authorize: {
22
- hint: "Generate stake authorization codes for given withdrawer.",
23
- params: "WITHDRAWER",
24
- options: {
25
- qrcodes: {
26
- hint: "Print authorization QR codes, scannable from myWitWallet.",
27
- },
28
- },
29
- },
30
- balance: {
31
- hint: "List endpoints that connect to your nodes, addresses and available balances.",
32
- },
33
- masterKeys: {
34
- hint: "Export nodes' master keys.",
35
- },
36
- publicKeys: {
37
- hint: "Export nodes' public keys.",
38
- },
39
- peers: {
40
- hint: "List and manage node farm's peers.",
41
- options: {
42
- add: {
43
- hint: "Add new peer addresses for the nodes to try to connect to.",
44
- param: "P2P_IP:PORT[;..]",
45
- },
46
- "clear-buckets": {
47
- hint: "Clear out all peering buckets.",
48
- },
49
- reset: {
50
- hint: "Clear all peers from the buckets and initialize to those in config.",
51
- },
52
- },
53
- },
54
- rankings: {
55
- hint: "Sort identities by their current mining power ranking.",
56
- options: {
57
- witnessing: { hint: "Sort by witnessing power ranking instead." },
58
- },
59
- },
60
- rewind: {
61
- hint: "Rewind blockchain state on farm nodes to this epoch.",
62
- params: "EPOCH",
63
- },
64
- stats: {
65
- hint: "Report farm stats.",
66
- },
67
- syncStatus: {
68
- hint: "Report current sync status for every node in the farm.",
69
- },
70
- withdrawers: {
71
- hint: "List withdrawers and stake entries currently delegating to the farm nodes.",
72
- },
73
- },
74
- subcommands: {
75
- addresses,
76
- authorize,
77
- balance,
78
- masterKeys,
79
- publicKeys,
80
- peers,
81
- rankings,
82
- rewind,
83
- stats,
84
- syncStatus,
85
- withdrawers,
86
- },
87
- }
88
-
89
- /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90
- /// CLI SUBMODULE COMMANDS ============================================================================================
91
-
92
- async function _initializeFarm (options = {}) {
93
- return new Witnet.JsonRpcNodeFarm(options?.nodes)
94
- }
95
-
96
- async function addresses (options = {}) {
97
- const farm = await _initializeFarm(options)
98
- const addresses = Object.entries(await farm.addresses())
99
- if (addresses.length === 1 && !(addresses[0][1] instanceof Error)) {
100
- console.info(lcyan(addresses[0][1]))
101
- qrcode.generate(addresses[0][1])
102
- } else {
103
- helpers.traceTable(
104
- addresses.map(([url, pkh]) => [
105
- pkh instanceof Error ? red(url) : mcyan(url),
106
- pkh instanceof Error ? red(pkh) : lcyan(pkh),
107
- ]), {
108
- headlines: ["NODES", ":Public Key Hash"],
109
- },
110
- )
111
- }
112
- }
113
-
114
- async function authorize (options = {}, args = []) {
115
- if (args.length === 0) {
116
- throw Error("Withdrawer address must be specified")
117
- } else {
118
- Witnet.PublicKeyHash.fromBech32(args[0])
119
- const farm = await _initializeFarm(options)
120
- const authcodes = await farm.authorizeStakes(args[0].toLowerCase())
121
- Object.entries(authcodes).forEach(([url, [validator, authcode]]) => {
122
- if (url instanceof Error) {
123
- console.error(url)
124
- } else if (validator instanceof Error) {
125
- console.error("Endpoint:", url)
126
- console.error(validator)
127
- } else if (authcode instanceof Error) {
128
- console.info("Validator address: ", mcyan(validator))
129
- console.error(authcode)
130
- } else {
131
- console.info("Validator address: ", mcyan(validator))
132
- console.info(`${white(authcode)}`)
133
- if (options?.qrcodes) qrcode.generate(authcode)
134
- }
135
- console.info()
136
- })
137
- if (options?.qrcodes) {
138
- console.info("=".repeat(102))
139
- console.info("^ Withdrawer address:", lmagenta(args[0].toLowerCase()))
140
- } else {
141
- console.info("Withdrawer address:", lmagenta(args[0].toLowerCase()))
142
- }
143
- }
144
- }
145
-
146
- async function balance (options = {}) {
147
- const farm = await _initializeFarm(options)
148
- const balances = await farm.balances()
149
- helpers.traceTable(
150
- Object.entries(balances).map(([url, [pkh, balance]]) => [
151
- pkh instanceof Error ? red(url) : mcyan(url),
152
- pkh instanceof Error ? red(pkh) : lcyan(pkh),
153
- ...(balance instanceof Error
154
- ? new Array(4).fill(gray("n/a"))
155
- : [
156
- gray(Witnet.Coins.fromNanowits(balance.locked).wits),
157
- yellow(Witnet.Coins.fromNanowits(balance.staked).wits),
158
- myellow(Witnet.Coins.fromNanowits(balance.unlocked).wits),
159
- lyellow(Witnet.Coins.fromNanowits(balance.locked + balance.staked + balance.unlocked).wits),
160
- ]
161
- ),
162
- ]), {
163
- headlines: ["NODES", ":Validator address", "Locked ($WIT)", "Staked ($WIT)", "Available ($WIT)", "BALANCE ($WIT)"],
164
- humanizers: [,, helpers.commas, helpers.commas, helpers.commas, helpers.commas],
165
- maxColumnWidth: 48,
166
- },
167
- )
168
- }
169
-
170
- async function masterKeys (options = {}) {
171
- const farm = await _initializeFarm(options)
172
- const masterKeys = await farm.masterKeys()
173
- helpers.traceTable(
174
- Object.entries(masterKeys).map(([, [pkh, masterKey]]) => [
175
- // pkh instanceof Error ? cyan(url) : mcyan(url),
176
- pkh instanceof Error ? red(pkh) : mcyan(pkh),
177
- masterKey instanceof Error ? mred(masterKey) : cyan(masterKey),
178
- ]), {
179
- headlines: [":Public key hash", "Secret key"],
180
- maxColumnWidth: 120,
181
- },
182
- )
183
- }
184
-
185
- async function peers (options = {}) {
186
- const farm = await _initializeFarm(options)
187
- const checklists = {}
188
- if (options.reset) {
189
- checklists["Reset peers"] = await farm.initializePeers()
190
- } else if (options["clear-buckets"]) {
191
- checklists["Clear buckets"] = await farm.clearPeers()
192
- }
193
- if (options.add) {
194
- const morePeers = options.add.replaceAll(",", ";").split(";")
195
- checklists["Add peers"] = await farm.addPeers(morePeers)
196
- }
197
- helpers.traceChecklists(checklists)
198
- const peers = await farm.peers()
199
- if (peers.length > 0) {
200
- console.log(peers)
201
- }
202
- }
203
-
204
- async function publicKeys (options = {}) {
205
- const farm = await _initializeFarm(options)
206
- const publicKeys = await farm.publicKeys()
207
- helpers.traceTable(
208
- Object.entries(publicKeys).map(([, [pkh, publicKey]]) => [
209
- pkh instanceof Error ? red(pkh) : mcyan(pkh),
210
- publicKey instanceof Error ? mred(publicKey.toString()) : cyan(publicKey.toString()),
211
- ]), {
212
- headlines: [":Public key hash", "Public key"],
213
- maxColumnWidth: 120,
214
- },
215
- )
216
- }
217
-
218
- async function rankings (options = {}) {
219
- const farm = await _initializeFarm(options)
220
- const addresses = Object.entries(await farm.addresses())
221
- const validators = []
222
- let provider
223
- addresses.forEach(([url, pkh]) => {
224
- if (pkh instanceof Error) {
225
- console.error(`> Skipping node ${url}: ${pkh}`)
226
- } else {
227
- if (!provider) provider = new Witnet.JsonRpcProvider(url)
228
- validators.push(pkh)
229
- }
230
- })
231
- if (validators.length > 0 && provider) {
232
- const query = {
233
- distinct: true,
234
- orderBy: options?.witnessing ? "witnessing" : "mining",
235
- }
236
- const capability = query.orderBy.toUpperCase()
237
- const records = await provider.powers(query)
238
- if (records.length > 0) {
239
- helpers.traceTable(
240
- records
241
- .filter(record => validators.includes(record.validator))
242
- .map(({ power, ranking, validator, withdrawer }) => [
243
- validator,
244
- withdrawer,
245
- power,
246
- ranking,
247
- ]),
248
- {
249
- headlines: [
250
- "VALIDATORS",
251
- "Withdrawer",
252
- `${capability} POWER`,
253
- "G_RANK",
254
- ],
255
- humanizers: [
256
- ,, helpers.commas, helpers.commas,
257
- ],
258
- colors: [
259
- lcyan, mmagenta, green, lgreen,
260
- ],
261
- },
262
- )
263
- } else {
264
- console.info(`> No ${capability} power is currently treasured on the farm.`)
265
- }
266
- } else {
267
- console.info("> No nodes currently available to interact with.")
268
- }
269
- }
270
-
271
- async function rewind (options = {}, args = []) {
272
- if (!args || args.length === 0) {
273
- throw Error("No rewind epoch was provided")
274
- }
275
- if (!options?.force) {
276
- const will = await helpers.prompt("Rewinding will reset some stats. Do you want to proceed anyways? (y/N)")
277
- // Abort if not confirmed
278
- if (!["y"].includes(will.toLowerCase())) {
279
- console.error("Aborted by user.")
280
- return
281
- }
282
- }
283
- const farm = await _initializeFarm(options)
284
- const epoch = parseInt(args[0])
285
- helpers.traceChecklists({
286
- "Rewind chain": await farm.rewind(epoch),
287
- })
288
- syncStatus(options)
289
- }
290
-
291
- async function stats (options = {}) {
292
- const farm = await _initializeFarm(options)
293
- const stats = await farm.stats()
294
- helpers.traceTable(
295
- Object.entries(stats).map(([url, stats]) => [
296
- ...(stats instanceof Error
297
- ? [red(url), gray("n/a"), gray("n/a"), gray("n/a"), gray("n/a"), gray("n/a"), gray("n/a")]
298
- : [
299
- mcyan(url),
300
- stats.block_mined_count,
301
- stats.commits_count,
302
- (stats.block_mined_count / stats.block_proposed_count).toFixed(3),
303
- (stats.commits_count / stats.commits_proposed_count).toFixed(3),
304
- ((stats.dr_eligibility_count - stats.commits_proposed_count) / stats.dr_eligibility_count).toFixed(3),
305
- (stats.slashed_count / stats.commits_count).toFixed(3),
306
- ]
307
- ),
308
- ]), {
309
- headlines: [
310
- "NODES",
311
- "Mined blocks",
312
- "Witnessed DRs",
313
- "M_Acceptancy",
314
- "W_Acceptancy",
315
- "W_Reluctancy",
316
- "W_Falsity",
317
- ],
318
- humanizers: [, helpers.commas, helpers.commas,,,,],
319
- },
320
- )
321
- }
322
-
323
- async function syncStatus (options) {
324
- const farm = await _initializeFarm(options)
325
- const syncStatus = await farm.syncStatus()
326
- helpers.traceTable(
327
- Object.entries(syncStatus).map(([url, status]) => [
328
- status instanceof Error ? red(url) : mcyan(url),
329
- ...(status instanceof Error
330
- ? [red(status), "", "", ""]
331
- : [
332
- status.node_state.trim() === "Synced" ? mgreen(status.node_state) : myellow(status.node_state),
333
- status.current_epoch,
334
- status.chain_beacon.checkpoint,
335
- status.chain_beacon.hashPrevBlock,
336
- ]),
337
- ]), {
338
- headlines: [":NODES", ":Status", "Current epoch", "Checkpoint epoch", ":Checkpoint block hash"],
339
- humanizers: [,, helpers.commas, helpers.commas],
340
- colors: [,, white,, gray], // gray, gray ],
341
- }
342
- )
343
- }
344
-
345
- async function withdrawers (options = {}) {
346
- const farm = await _initializeFarm(options)
347
- const records = await farm.withdrawers()
348
- if (records && Object.keys(records).length > 0) {
349
- helpers.traceTable(
350
- Object.entries(records).map(([withdrawer, [coins, nonce]]) => [
351
- withdrawer,
352
- nonce,
353
- Witnet.Coins.fromNanowits(coins).wits,
354
- ]),
355
- {
356
- headlines: ["WITHDRAWERS", "Latest nonce", "Total staked ($WIT)"],
357
- humanizers: [, helpers.commas, helpers.commas],
358
- colors: [mmagenta,, myellow],
359
- },
360
- )
361
- } else {
362
- console.info("> No withdrawers delegating on the farm at the moment.")
363
- }
364
- }
1
+ const qrcode = require("qrcode-terminal")
2
+ const helpers = require("../helpers")
3
+ const { Witnet } = require("../../../dist/src")
4
+
5
+ const { cyan, gray, green, red, yellow, white, mcyan, mgreen, mmagenta, mred, myellow, lcyan, lgreen, lmagenta, lyellow } = helpers.colors
6
+
7
+ /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
+ /// CLI SUBMODULE CONSTANTS ===========================================================================================
9
+
10
+ module.exports = {
11
+ envars: {
12
+ WITNET_TOOLKIT_FARM_NODES: "=> URLs to your own nodes' HTTP/JSON private endpoints, if no otherwise specified.",
13
+ },
14
+ flags: {
15
+ nodes: {
16
+ hint: "Private URLs to your node's HTTP/JSON endpoints, other than default.",
17
+ param: "JSON_HTTP_URL[;..]",
18
+ },
19
+ },
20
+ router: {
21
+ authorize: {
22
+ hint: "Generate stake authorization codes for given withdrawer.",
23
+ params: "WITHDRAWER",
24
+ options: {
25
+ qrcodes: {
26
+ hint: "Print authorization QR codes, scannable from myWitWallet.",
27
+ },
28
+ },
29
+ },
30
+ balance: {
31
+ hint: "List endpoints that connect to your nodes, addresses and available balances.",
32
+ },
33
+ masterKeys: {
34
+ hint: "Export nodes' master keys.",
35
+ },
36
+ publicKeys: {
37
+ hint: "Export nodes' public keys.",
38
+ },
39
+ peers: {
40
+ hint: "List and manage node farm's peers.",
41
+ options: {
42
+ add: {
43
+ hint: "Add new peer addresses for the nodes to try to connect to.",
44
+ param: "P2P_IP:PORT[;..]",
45
+ },
46
+ "clear-buckets": {
47
+ hint: "Clear out all peering buckets.",
48
+ },
49
+ reset: {
50
+ hint: "Clear all peers from the buckets and initialize to those in config.",
51
+ },
52
+ },
53
+ },
54
+ rankings: {
55
+ hint: "Sort identities by their current mining power ranking.",
56
+ options: {
57
+ witnessing: { hint: "Sort by witnessing power ranking instead." },
58
+ },
59
+ },
60
+ rewind: {
61
+ hint: "Rewind blockchain state on farm nodes to this epoch.",
62
+ params: "EPOCH",
63
+ },
64
+ stats: {
65
+ hint: "Report farm stats.",
66
+ },
67
+ syncStatus: {
68
+ hint: "Report current sync status for every node in the farm.",
69
+ },
70
+ withdrawers: {
71
+ hint: "List withdrawers and stake entries currently delegating to the farm nodes.",
72
+ },
73
+ },
74
+ subcommands: {
75
+ addresses,
76
+ authorize,
77
+ balance,
78
+ masterKeys,
79
+ publicKeys,
80
+ peers,
81
+ rankings,
82
+ rewind,
83
+ stats,
84
+ syncStatus,
85
+ withdrawers,
86
+ },
87
+ }
88
+
89
+ /// ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
90
+ /// CLI SUBMODULE COMMANDS ============================================================================================
91
+
92
+ async function _initializeFarm (options = {}) {
93
+ return new Witnet.JsonRpcNodeFarm(options?.nodes)
94
+ }
95
+
96
+ async function addresses (options = {}) {
97
+ const farm = await _initializeFarm(options)
98
+ const addresses = Object.entries(await farm.addresses())
99
+ if (addresses.length === 1 && !(addresses[0][1] instanceof Error)) {
100
+ console.info(lcyan(addresses[0][1]))
101
+ qrcode.generate(addresses[0][1])
102
+ } else {
103
+ helpers.traceTable(
104
+ addresses.map(([url, pkh]) => [
105
+ pkh instanceof Error ? red(url) : mcyan(url),
106
+ pkh instanceof Error ? red(pkh) : lcyan(pkh),
107
+ ]), {
108
+ headlines: ["NODES", ":Public Key Hash"],
109
+ },
110
+ )
111
+ }
112
+ }
113
+
114
+ async function authorize (options = {}, args = []) {
115
+ if (args.length === 0) {
116
+ throw Error("Withdrawer address must be specified")
117
+ } else {
118
+ Witnet.PublicKeyHash.fromBech32(args[0])
119
+ const farm = await _initializeFarm(options)
120
+ const authcodes = await farm.authorizeStakes(args[0].toLowerCase())
121
+ Object.entries(authcodes).forEach(([url, [validator, authcode]]) => {
122
+ if (url instanceof Error) {
123
+ console.error(url)
124
+ } else if (validator instanceof Error) {
125
+ console.error("Endpoint:", url)
126
+ console.error(validator)
127
+ } else if (authcode instanceof Error) {
128
+ console.info("Validator address: ", mcyan(validator))
129
+ console.error(authcode)
130
+ } else {
131
+ console.info("Validator address: ", mcyan(validator))
132
+ console.info(`${white(authcode)}`)
133
+ if (options?.qrcodes) qrcode.generate(authcode)
134
+ }
135
+ console.info()
136
+ })
137
+ if (options?.qrcodes) {
138
+ console.info("=".repeat(102))
139
+ console.info("^ Withdrawer address:", lmagenta(args[0].toLowerCase()))
140
+ } else {
141
+ console.info("Withdrawer address:", lmagenta(args[0].toLowerCase()))
142
+ }
143
+ }
144
+ }
145
+
146
+ async function balance (options = {}) {
147
+ const farm = await _initializeFarm(options)
148
+ const balances = await farm.balances()
149
+ helpers.traceTable(
150
+ Object.entries(balances).map(([url, [pkh, balance]]) => [
151
+ pkh instanceof Error ? red(url) : mcyan(url),
152
+ pkh instanceof Error ? red(pkh) : lcyan(pkh),
153
+ ...(balance instanceof Error
154
+ ? new Array(4).fill(gray("n/a"))
155
+ : [
156
+ gray(Witnet.Coins.fromNanowits(balance.locked).wits),
157
+ yellow(Witnet.Coins.fromNanowits(balance.staked).wits),
158
+ myellow(Witnet.Coins.fromNanowits(balance.unlocked).wits),
159
+ lyellow(Witnet.Coins.fromNanowits(balance.locked + balance.staked + balance.unlocked).wits),
160
+ ]
161
+ ),
162
+ ]), {
163
+ headlines: ["NODES", ":Validator address", "Locked ($WIT)", "Staked ($WIT)", "Available ($WIT)", "BALANCE ($WIT)"],
164
+ humanizers: [,, helpers.commas, helpers.commas, helpers.commas, helpers.commas],
165
+ maxColumnWidth: 48,
166
+ },
167
+ )
168
+ }
169
+
170
+ async function masterKeys (options = {}) {
171
+ const farm = await _initializeFarm(options)
172
+ const masterKeys = await farm.masterKeys()
173
+ helpers.traceTable(
174
+ Object.entries(masterKeys).map(([, [pkh, masterKey]]) => [
175
+ // pkh instanceof Error ? cyan(url) : mcyan(url),
176
+ pkh instanceof Error ? red(pkh) : mcyan(pkh),
177
+ masterKey instanceof Error ? mred(masterKey) : cyan(masterKey),
178
+ ]), {
179
+ headlines: [":Public key hash", "Secret key"],
180
+ maxColumnWidth: 120,
181
+ },
182
+ )
183
+ }
184
+
185
+ async function peers (options = {}) {
186
+ const farm = await _initializeFarm(options)
187
+ const checklists = {}
188
+ if (options.reset) {
189
+ checklists["Reset peers"] = await farm.initializePeers()
190
+ } else if (options["clear-buckets"]) {
191
+ checklists["Clear buckets"] = await farm.clearPeers()
192
+ }
193
+ if (options.add) {
194
+ const morePeers = options.add.replaceAll(",", ";").split(";")
195
+ checklists["Add peers"] = await farm.addPeers(morePeers)
196
+ }
197
+ helpers.traceChecklists(checklists)
198
+ const peers = await farm.peers()
199
+ if (peers.length > 0) {
200
+ console.log(peers)
201
+ }
202
+ }
203
+
204
+ async function publicKeys (options = {}) {
205
+ const farm = await _initializeFarm(options)
206
+ const publicKeys = await farm.publicKeys()
207
+ helpers.traceTable(
208
+ Object.entries(publicKeys).map(([, [pkh, publicKey]]) => [
209
+ pkh instanceof Error ? red(pkh) : mcyan(pkh),
210
+ publicKey instanceof Error ? mred(publicKey.toString()) : cyan(publicKey.toString()),
211
+ ]), {
212
+ headlines: [":Public key hash", "Public key"],
213
+ maxColumnWidth: 120,
214
+ },
215
+ )
216
+ }
217
+
218
+ async function rankings (options = {}) {
219
+ const farm = await _initializeFarm(options)
220
+ const addresses = Object.entries(await farm.addresses())
221
+ const validators = []
222
+ let provider
223
+ addresses.forEach(([url, pkh]) => {
224
+ if (pkh instanceof Error) {
225
+ console.error(`> Skipping node ${url}: ${pkh}`)
226
+ } else {
227
+ if (!provider) provider = new Witnet.JsonRpcProvider(url)
228
+ validators.push(pkh)
229
+ }
230
+ })
231
+ if (validators.length > 0 && provider) {
232
+ const query = {
233
+ distinct: true,
234
+ orderBy: options?.witnessing ? "witnessing" : "mining",
235
+ }
236
+ const capability = query.orderBy.toUpperCase()
237
+ const records = await provider.powers(query)
238
+ if (records.length > 0) {
239
+ helpers.traceTable(
240
+ records
241
+ .filter(record => validators.includes(record.validator))
242
+ .map(({ power, ranking, validator, withdrawer }) => [
243
+ validator,
244
+ withdrawer,
245
+ power,
246
+ ranking,
247
+ ]),
248
+ {
249
+ headlines: [
250
+ "VALIDATORS",
251
+ "Withdrawer",
252
+ `${capability} POWER`,
253
+ "G_RANK",
254
+ ],
255
+ humanizers: [
256
+ ,, helpers.commas, helpers.commas,
257
+ ],
258
+ colors: [
259
+ lcyan, mmagenta, green, lgreen,
260
+ ],
261
+ },
262
+ )
263
+ } else {
264
+ console.info(`> No ${capability} power is currently treasured on the farm.`)
265
+ }
266
+ } else {
267
+ console.info("> No nodes currently available to interact with.")
268
+ }
269
+ }
270
+
271
+ async function rewind (options = {}, args = []) {
272
+ if (!args || args.length === 0) {
273
+ throw Error("No rewind epoch was provided")
274
+ }
275
+ if (!options?.force) {
276
+ const will = await helpers.prompt("Rewinding will reset some stats. Do you want to proceed anyways? (y/N)")
277
+ // Abort if not confirmed
278
+ if (!["y"].includes(will.toLowerCase())) {
279
+ console.error("Aborted by user.")
280
+ return
281
+ }
282
+ }
283
+ const farm = await _initializeFarm(options)
284
+ const epoch = parseInt(args[0])
285
+ helpers.traceChecklists({
286
+ "Rewind chain": await farm.rewind(epoch),
287
+ })
288
+ syncStatus(options)
289
+ }
290
+
291
+ async function stats (options = {}) {
292
+ const farm = await _initializeFarm(options)
293
+ const stats = await farm.stats()
294
+ helpers.traceTable(
295
+ Object.entries(stats).map(([url, stats]) => [
296
+ ...(stats instanceof Error
297
+ ? [red(url), gray("n/a"), gray("n/a"), gray("n/a"), gray("n/a"), gray("n/a"), gray("n/a")]
298
+ : [
299
+ mcyan(url),
300
+ stats.block_mined_count,
301
+ stats.commits_count,
302
+ (stats.block_mined_count / stats.block_proposed_count).toFixed(3),
303
+ (stats.commits_count / stats.commits_proposed_count).toFixed(3),
304
+ ((stats.dr_eligibility_count - stats.commits_proposed_count) / stats.dr_eligibility_count).toFixed(3),
305
+ (stats.slashed_count / stats.commits_count).toFixed(3),
306
+ ]
307
+ ),
308
+ ]), {
309
+ headlines: [
310
+ "NODES",
311
+ "Mined blocks",
312
+ "Witnessed DRs",
313
+ "M_Acceptancy",
314
+ "W_Acceptancy",
315
+ "W_Reluctancy",
316
+ "W_Falsity",
317
+ ],
318
+ humanizers: [, helpers.commas, helpers.commas,,,,],
319
+ },
320
+ )
321
+ }
322
+
323
+ async function syncStatus (options) {
324
+ const farm = await _initializeFarm(options)
325
+ const syncStatus = await farm.syncStatus()
326
+ helpers.traceTable(
327
+ Object.entries(syncStatus).map(([url, status]) => [
328
+ status instanceof Error ? red(url) : mcyan(url),
329
+ ...(status instanceof Error
330
+ ? [red(status), "", "", ""]
331
+ : [
332
+ status.node_state.trim() === "Synced" ? mgreen(status.node_state) : myellow(status.node_state),
333
+ status.current_epoch,
334
+ status.chain_beacon.checkpoint,
335
+ status.chain_beacon.hashPrevBlock,
336
+ ]),
337
+ ]), {
338
+ headlines: [":NODES", ":Status", "Current epoch", "Checkpoint epoch", ":Checkpoint block hash"],
339
+ humanizers: [,, helpers.commas, helpers.commas],
340
+ colors: [,, white,, gray], // gray, gray ],
341
+ }
342
+ )
343
+ }
344
+
345
+ async function withdrawers (options = {}) {
346
+ const farm = await _initializeFarm(options)
347
+ const records = await farm.withdrawers()
348
+ if (records && Object.keys(records).length > 0) {
349
+ helpers.traceTable(
350
+ Object.entries(records).map(([withdrawer, [coins, nonce]]) => [
351
+ withdrawer,
352
+ nonce,
353
+ Witnet.Coins.fromNanowits(coins).wits,
354
+ ]),
355
+ {
356
+ headlines: ["WITHDRAWERS", "Latest nonce", "Total staked ($WIT)"],
357
+ humanizers: [, helpers.commas, helpers.commas],
358
+ colors: [mmagenta,, myellow],
359
+ },
360
+ )
361
+ } else {
362
+ console.info("> No withdrawers delegating on the farm at the moment.")
363
+ }
364
+ }