@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,9 +1,9 @@
1
- #!/usr/bin/env node
2
-
3
- const exec = require("child_process").execSync
4
- const fs = require("fs")
5
-
6
- if (!fs.existsSync(".no-postinstall")) {
7
- // download proper witnet_toolkit binary, according to arch and os
8
- exec(`node ./src/bin/toolkit.js --update --version`)
9
- }
1
+ #!/usr/bin/env node
2
+
3
+ const exec = require("child_process").execSync
4
+ const fs = require("fs")
5
+
6
+ if (!fs.existsSync(".no-postinstall")) {
7
+ // download proper witnet_toolkit binary, according to arch and os
8
+ exec(`node ./src/bin/toolkit.js --update --version`)
9
+ }
@@ -1,295 +1,295 @@
1
- #!/usr/bin/env node
2
-
3
- /// IMPORTS ===========================================================================================================
4
-
5
- const axios = require("axios")
6
- require("dotenv").config()
7
- const fs = require("fs")
8
- const os = require("os")
9
- const path = require("path")
10
-
11
- const {
12
- colors,
13
- deleteExtraFlags, extractFromArgs,
14
- showUsage, showUsageError, showUsageSubcommand, showVersion,
15
- toolkitRun,
16
- prompt,
17
- } = require("./helpers")
18
-
19
- /// CONSTANTS =======================================================================================================
20
-
21
- const version = "2.0.8"
22
- const toolkitDownloadUrlBase = `https://github.com/witnet/witnet-rust/releases/download/${version}/`
23
- const toolkitDownloadNames = {
24
- win32: (arch) => `witnet_toolkit-${arch}-pc-windows-msvc.exe`,
25
- // TODO: detect armv7
26
- linux: (arch) => `witnet_toolkit-${arch}-unknown-linux-gnu${arch === "arm" ? "eabihf" : ""}`,
27
- darwin: (arch) => `witnet_toolkit-${arch}-apple-darwin`,
28
- }
29
- const toolkitFileNames = {
30
- win32: (arch) => `witnet_toolkit-${version}-${arch}-pc-windows-msvc.exe`,
31
- // TODO: detect armv7
32
- linux: (arch) => `witnet_toolkit-${version}-${arch}-unknown-linux-gnu${arch === "arm" ? "eabihf" : ""}`,
33
- darwin: (arch) =>
34
- arch === "arm64"
35
- // m1 mac
36
- ? `witnet_toolkit-${version}-${arch}-unknown-linux-gnu`
37
- // intel mac
38
- : `witnet_toolkit-${version}-${arch}-apple-darwin`,
39
- }
40
- const archsMap = {
41
- arm64: "aarch64",
42
- x64: "x86_64",
43
- }
44
-
45
- /// ENVIRONMENT ACQUISITION =========================================================================================
46
-
47
- const args = process.argv
48
- const binDir = __dirname
49
-
50
- const toolkitDirPath = path.resolve(binDir, "../../witnet/")
51
- const platform = guessPlatform()
52
- const arch = guessArch()
53
- const toolkitDownloadName = guessToolkitDownloadName(platform, arch)
54
- const toolkitFileName = guessToolkitFileName(platform, arch)
55
- const toolkitBinPath = guessToolkitBinPath(toolkitDirPath, platform, arch)
56
- const toolkitIsDownloaded = checkToolkitIsDownloaded(toolkitBinPath)
57
-
58
- function guessPlatform () {
59
- return os.platform()
60
- }
61
- function guessArch () {
62
- const rawArch = os.arch()
63
- return archsMap[rawArch] || rawArch
64
- }
65
- function guessDownloadUrl (toolkitFileName) {
66
- return `${toolkitDownloadUrlBase}${toolkitFileName}`
67
- }
68
- function guessToolkitDownloadName (platform, arch) {
69
- return (toolkitDownloadNames[platform] || toolkitDownloadNames.linux)(arch)
70
- }
71
- function guessToolkitFileName (platform, arch) {
72
- return (toolkitFileNames[platform] || toolkitFileNames.linux)(arch)
73
- }
74
- function guessToolkitBinPath (toolkitDirPath, platform, arch) {
75
- const fileName = guessToolkitFileName(platform, arch)
76
-
77
- return path.resolve(toolkitDirPath, fileName)
78
- }
79
- function checkToolkitIsDownloaded (toolkitBinPath) {
80
- return fs.existsSync(toolkitBinPath)
81
- }
82
-
83
- /// HELPER FUNCTIONS ================================================================================================
84
-
85
- async function downloadToolkit (toolkitDownloadName, toolkitFileName, toolkitBinPath, platform, arch) {
86
- const downloadUrl = guessDownloadUrl(toolkitDownloadName)
87
- console.info("Downloading", downloadUrl, "into", toolkitBinPath)
88
-
89
- const file = fs.createWriteStream(toolkitBinPath)
90
- axios({
91
- method: "get",
92
- url: downloadUrl,
93
- responseType: "stream",
94
- }).then(function (response) {
95
- response.data.pipe(file)
96
- })
97
-
98
- return new Promise((resolve, reject) => {
99
- file.on("finish", () => {
100
- file.close(() => {
101
- if (file.bytesWritten > 1000000) {
102
- fs.chmodSync(toolkitBinPath, 0o755)
103
- resolve()
104
- } else {
105
- reject(`No suitable witnet_toolkit binary found. Maybe your OS (${platform}) or architecture \
106
- (${arch}) are not yet supported. Feel free to complain about it in the Witnet community on Discord: \
107
- https://discord.gg/2rTFYXHmPm `)
108
- }
109
- })
110
- })
111
- const errorHandler = (err) => {
112
- fs.unlink(downloadUrl, () => {
113
- reject(err)
114
- })
115
- }
116
- file.on("error", errorHandler)
117
- })
118
- }
119
-
120
- /// COMMAND HANDLERS ================================================================================================
121
-
122
- async function installCommand (settings) {
123
- if (!settings.checks.toolkitIsDownloaded) {
124
- // Skip confirmation if install is forced
125
- if (!settings.force) {
126
- console.info(`The witnet_toolkit ${version} native binary hasn't been downloaded yet (this is a requirement).`)
127
- const will = await prompt("Do you want to download it now? (Y/n)")
128
- // Abort if not confirmed
129
- if (!["", "y"].includes(will.toLowerCase())) {
130
- console.error("Aborted download of witnet_toolkit native binary.")
131
- return
132
- }
133
- }
134
-
135
- return forcedInstallCommand(settings)
136
- }
137
- }
138
-
139
- async function forcedInstallCommand (settings) {
140
- if (!fs.existsSync(".env_witnet")) {
141
- fs.cpSync("node_modules/@witnet/sdk/.env_witnet", ".env_witnet")
142
- }
143
- return downloadToolkit(
144
- settings.paths.toolkitDownloadName,
145
- settings.paths.toolkitFileName,
146
- settings.paths.toolkitBinPath,
147
- settings.system.platform,
148
- settings.system.arch
149
- )
150
- .catch((err) => {
151
- console.error("Error updating witnet_toolkit binary:", err)
152
- })
153
- }
154
-
155
- async function versionCommand (settings) {
156
- return fallbackBinaryCommand(settings, ["--version"])
157
- }
158
-
159
- async function fallbackBinaryCommand (settings, args) {
160
- const toolkitOutput = await toolkitRun(settings, args.slice(1))
161
- .catch((err) => {
162
- let errorMessage = err.message.split("\n").slice(1).join("\n").trim()
163
- const errorRegex = /.*^error: (?<message>.*)$.*/gm
164
- const matched = errorRegex.exec(err.message)
165
- if (matched) {
166
- errorMessage = matched.groups.message
167
- }
168
- console.error(errorMessage || err)
169
- })
170
- if (toolkitOutput) console.info(toolkitOutput)
171
- }
172
-
173
- /// PROCESS SETTINGS ===============================================================================================
174
-
175
- const settings = {
176
- paths: {
177
- toolkitBinPath,
178
- toolkitDirPath,
179
- toolkitDownloadName,
180
- toolkitFileName,
181
- },
182
- checks: {
183
- toolkitIsDownloaded,
184
- },
185
- system: {
186
- platform,
187
- arch,
188
- },
189
- }
190
-
191
- if (args.includes("--debug")) {
192
- settings.debug = true
193
- args.splice(args.indexOf("--debug"), 1)
194
- }
195
-
196
- if (args.includes("--force")) {
197
- settings.force = true
198
- args.splice(args.indexOf("--force"), 1)
199
- }
200
-
201
- if (args.includes("--help")) {
202
- settings.help = true
203
- args.splice(args.indexOf("--help"), 1)
204
- }
205
-
206
- if (args.includes("--version")) {
207
- settings.showVersion = true
208
- args.splice(args.indexOf("--version"), 1)
209
- }
210
-
211
- if (args.includes("--update")) {
212
- settings.update = true
213
- args.splice(args.indexOf("--update"), 1)
214
- }
215
-
216
- /// MAIN LOGIC ======================================================================================================
217
-
218
- const mainRouter = {
219
- "--": fallbackBinaryCommand,
220
- update: forcedInstallCommand,
221
- install: installCommand,
222
- version: versionCommand,
223
- }
224
-
225
- async function main () {
226
- if (settings.showVersion) {
227
- showVersion()
228
- }
229
- // Run installCommand before anything else, mainly to ensure that the witnet_toolkit binary
230
- // has been downloaded, unless we're intentionally installing or updating the binary.
231
- if (settings.update) {
232
- await forcedInstallCommand(settings)
233
- }
234
- let args = process.argv.slice(2)
235
-
236
- // Allow options and flags to be placed as first argument...
237
- if (args[0] && args[0].startsWith('--') && args[0].length > 2) {
238
- for (let index = 1; index < args.length; index ++) {
239
- if (!args[index].startsWith('--')) {
240
- try {
241
- const module = require(`./cli/${args[index]}`)
242
- args = [ args[index], ...args.slice(0, index).concat(args.slice(index + 1)) ]
243
- break;
244
- } catch {}
245
- }
246
- }
247
- }
248
-
249
- let flags
250
- let options
251
- if (args[0] && mainRouter[args[0]]) {
252
- await mainRouter[args[0]](settings, args.slice(1))
253
- process.exit(0)
254
- } else if (args[0] && !args[0].startsWith("--")) {
255
- try {
256
- const cmd = args[0]
257
- const module = require(`./cli/${cmd}`);
258
- [args, flags] = extractFromArgs(args.slice(1), module?.flags)
259
- if (args && args[0] && module.subcommands && module?.router[args[0]]) {
260
- const subcmd = args[0]
261
- if (settings?.help) {
262
- showUsageSubcommand(cmd, subcmd, module)
263
- } else {
264
- [args, options] = extractFromArgs(args.slice(1), { ...module.router[subcmd]?.options })
265
- args = deleteExtraFlags(args)
266
- await module.subcommands[subcmd]({ ...settings, ...flags, ...options }, args)
267
- .catch(err => {
268
- showUsageError(cmd, subcmd, module, err, settings)
269
- })
270
- }
271
- } else {
272
- showUsage(cmd, module)
273
- }
274
- process.exit(0)
275
- } catch (e) {
276
- console.error(`EXCEPTION:\n${e}\n`)
277
- }
278
- }
279
- console.info("USAGE:")
280
- console.info(` ${colors.white("npx witnet")} [FLAGS] <COMMAND>`)
281
- console.info("\nFLAGS:")
282
- console.info(" --debug Outputs stack trace in case of error.")
283
- console.info(" --force Avoids asking the user to confirm operation.")
284
- console.info(" --help Describes command or subcommand usage.")
285
- console.info(" --update Forces update of underlying binaries.")
286
- console.info(" --version Prints toolkit name and version as first line.")
287
- console.info("\nCOMMANDS:")
288
- console.info(" inspect Inspect public data from the Wit/Oracle blockchain.")
289
- console.info(" network Dynamic information from the Wit/Oracle P2P network.")
290
- console.info(" nodes Interact with your private Wit/Oracle nodes, if reachable.")
291
- console.info(" radon Manage Radon requests and templates within your project.")
292
- console.info(" wallet Simple CLI wallet for spending and staking your Wits.")
293
- }
294
-
295
- main()
1
+ #!/usr/bin/env node
2
+
3
+ /// IMPORTS ===========================================================================================================
4
+
5
+ const axios = require("axios")
6
+ require("dotenv").config()
7
+ const fs = require("fs")
8
+ const os = require("os")
9
+ const path = require("path")
10
+
11
+ const {
12
+ colors,
13
+ deleteExtraFlags, extractFromArgs,
14
+ showUsage, showUsageError, showUsageSubcommand, showVersion,
15
+ toolkitRun,
16
+ prompt,
17
+ } = require("./helpers")
18
+
19
+ /// CONSTANTS =======================================================================================================
20
+
21
+ const version = "2.0.8"
22
+ const toolkitDownloadUrlBase = `https://github.com/witnet/witnet-rust/releases/download/${version}/`
23
+ const toolkitDownloadNames = {
24
+ win32: (arch) => `witnet_toolkit-${arch}-pc-windows-msvc.exe`,
25
+ // TODO: detect armv7
26
+ linux: (arch) => `witnet_toolkit-${arch}-unknown-linux-gnu${arch === "arm" ? "eabihf" : ""}`,
27
+ darwin: (arch) => `witnet_toolkit-${arch}-apple-darwin`,
28
+ }
29
+ const toolkitFileNames = {
30
+ win32: (arch) => `witnet_toolkit-${version}-${arch}-pc-windows-msvc.exe`,
31
+ // TODO: detect armv7
32
+ linux: (arch) => `witnet_toolkit-${version}-${arch}-unknown-linux-gnu${arch === "arm" ? "eabihf" : ""}`,
33
+ darwin: (arch) =>
34
+ arch === "arm64"
35
+ // m1 mac
36
+ ? `witnet_toolkit-${version}-${arch}-unknown-linux-gnu`
37
+ // intel mac
38
+ : `witnet_toolkit-${version}-${arch}-apple-darwin`,
39
+ }
40
+ const archsMap = {
41
+ arm64: "aarch64",
42
+ x64: "x86_64",
43
+ }
44
+
45
+ /// ENVIRONMENT ACQUISITION =========================================================================================
46
+
47
+ const args = process.argv
48
+ const binDir = __dirname
49
+
50
+ const toolkitDirPath = path.resolve(binDir, "../../witnet/")
51
+ const platform = guessPlatform()
52
+ const arch = guessArch()
53
+ const toolkitDownloadName = guessToolkitDownloadName(platform, arch)
54
+ const toolkitFileName = guessToolkitFileName(platform, arch)
55
+ const toolkitBinPath = guessToolkitBinPath(toolkitDirPath, platform, arch)
56
+ const toolkitIsDownloaded = checkToolkitIsDownloaded(toolkitBinPath)
57
+
58
+ function guessPlatform () {
59
+ return os.platform()
60
+ }
61
+ function guessArch () {
62
+ const rawArch = os.arch()
63
+ return archsMap[rawArch] || rawArch
64
+ }
65
+ function guessDownloadUrl (toolkitFileName) {
66
+ return `${toolkitDownloadUrlBase}${toolkitFileName}`
67
+ }
68
+ function guessToolkitDownloadName (platform, arch) {
69
+ return (toolkitDownloadNames[platform] || toolkitDownloadNames.linux)(arch)
70
+ }
71
+ function guessToolkitFileName (platform, arch) {
72
+ return (toolkitFileNames[platform] || toolkitFileNames.linux)(arch)
73
+ }
74
+ function guessToolkitBinPath (toolkitDirPath, platform, arch) {
75
+ const fileName = guessToolkitFileName(platform, arch)
76
+
77
+ return path.resolve(toolkitDirPath, fileName)
78
+ }
79
+ function checkToolkitIsDownloaded (toolkitBinPath) {
80
+ return fs.existsSync(toolkitBinPath)
81
+ }
82
+
83
+ /// HELPER FUNCTIONS ================================================================================================
84
+
85
+ async function downloadToolkit (toolkitDownloadName, toolkitFileName, toolkitBinPath, platform, arch) {
86
+ const downloadUrl = guessDownloadUrl(toolkitDownloadName)
87
+ console.info("Downloading", downloadUrl, "into", toolkitBinPath)
88
+
89
+ const file = fs.createWriteStream(toolkitBinPath)
90
+ axios({
91
+ method: "get",
92
+ url: downloadUrl,
93
+ responseType: "stream",
94
+ }).then(function (response) {
95
+ response.data.pipe(file)
96
+ })
97
+
98
+ return new Promise((resolve, reject) => {
99
+ file.on("finish", () => {
100
+ file.close(() => {
101
+ if (file.bytesWritten > 1000000) {
102
+ fs.chmodSync(toolkitBinPath, 0o755)
103
+ resolve()
104
+ } else {
105
+ reject(`No suitable witnet_toolkit binary found. Maybe your OS (${platform}) or architecture \
106
+ (${arch}) are not yet supported. Feel free to complain about it in the Witnet community on Discord: \
107
+ https://discord.gg/2rTFYXHmPm `)
108
+ }
109
+ })
110
+ })
111
+ const errorHandler = (err) => {
112
+ fs.unlink(downloadUrl, () => {
113
+ reject(err)
114
+ })
115
+ }
116
+ file.on("error", errorHandler)
117
+ })
118
+ }
119
+
120
+ /// COMMAND HANDLERS ================================================================================================
121
+
122
+ async function installCommand (settings) {
123
+ if (!settings.checks.toolkitIsDownloaded) {
124
+ // Skip confirmation if install is forced
125
+ if (!settings.force) {
126
+ console.info(`The witnet_toolkit ${version} native binary hasn't been downloaded yet (this is a requirement).`)
127
+ const will = await prompt("Do you want to download it now? (Y/n)")
128
+ // Abort if not confirmed
129
+ if (!["", "y"].includes(will.toLowerCase())) {
130
+ console.error("Aborted download of witnet_toolkit native binary.")
131
+ return
132
+ }
133
+ }
134
+
135
+ return forcedInstallCommand(settings)
136
+ }
137
+ }
138
+
139
+ async function forcedInstallCommand (settings) {
140
+ if (!fs.existsSync(".env_witnet")) {
141
+ fs.cpSync("node_modules/@witnet/sdk/.env_witnet", ".env_witnet")
142
+ }
143
+ return downloadToolkit(
144
+ settings.paths.toolkitDownloadName,
145
+ settings.paths.toolkitFileName,
146
+ settings.paths.toolkitBinPath,
147
+ settings.system.platform,
148
+ settings.system.arch
149
+ )
150
+ .catch((err) => {
151
+ console.error("Error updating witnet_toolkit binary:", err)
152
+ })
153
+ }
154
+
155
+ async function versionCommand (settings) {
156
+ return fallbackBinaryCommand(settings, ["--version"])
157
+ }
158
+
159
+ async function fallbackBinaryCommand (settings, args) {
160
+ const toolkitOutput = await toolkitRun(settings, args.slice(1))
161
+ .catch((err) => {
162
+ let errorMessage = err.message.split("\n").slice(1).join("\n").trim()
163
+ const errorRegex = /.*^error: (?<message>.*)$.*/gm
164
+ const matched = errorRegex.exec(err.message)
165
+ if (matched) {
166
+ errorMessage = matched.groups.message
167
+ }
168
+ console.error(errorMessage || err)
169
+ })
170
+ if (toolkitOutput) console.info(toolkitOutput)
171
+ }
172
+
173
+ /// PROCESS SETTINGS ===============================================================================================
174
+
175
+ const settings = {
176
+ paths: {
177
+ toolkitBinPath,
178
+ toolkitDirPath,
179
+ toolkitDownloadName,
180
+ toolkitFileName,
181
+ },
182
+ checks: {
183
+ toolkitIsDownloaded,
184
+ },
185
+ system: {
186
+ platform,
187
+ arch,
188
+ },
189
+ }
190
+
191
+ if (args.includes("--debug")) {
192
+ settings.debug = true
193
+ args.splice(args.indexOf("--debug"), 1)
194
+ }
195
+
196
+ if (args.includes("--force")) {
197
+ settings.force = true
198
+ args.splice(args.indexOf("--force"), 1)
199
+ }
200
+
201
+ if (args.includes("--help")) {
202
+ settings.help = true
203
+ args.splice(args.indexOf("--help"), 1)
204
+ }
205
+
206
+ if (args.includes("--version")) {
207
+ settings.showVersion = true
208
+ args.splice(args.indexOf("--version"), 1)
209
+ }
210
+
211
+ if (args.includes("--update")) {
212
+ settings.update = true
213
+ args.splice(args.indexOf("--update"), 1)
214
+ }
215
+
216
+ /// MAIN LOGIC ======================================================================================================
217
+
218
+ const mainRouter = {
219
+ "--": fallbackBinaryCommand,
220
+ update: forcedInstallCommand,
221
+ install: installCommand,
222
+ version: versionCommand,
223
+ }
224
+
225
+ async function main () {
226
+ if (settings.showVersion) {
227
+ showVersion()
228
+ }
229
+ // Run installCommand before anything else, mainly to ensure that the witnet_toolkit binary
230
+ // has been downloaded, unless we're intentionally installing or updating the binary.
231
+ if (settings.update) {
232
+ await forcedInstallCommand(settings)
233
+ }
234
+ let args = process.argv.slice(2)
235
+
236
+ // Allow options and flags to be placed as first argument...
237
+ if (args[0] && args[0].startsWith('--') && args[0].length > 2) {
238
+ for (let index = 1; index < args.length; index ++) {
239
+ if (!args[index].startsWith('--')) {
240
+ try {
241
+ const module = require(`./cli/${args[index]}`)
242
+ args = [ args[index], ...args.slice(0, index).concat(args.slice(index + 1)) ]
243
+ break;
244
+ } catch {}
245
+ }
246
+ }
247
+ }
248
+
249
+ let flags
250
+ let options
251
+ if (args[0] && mainRouter[args[0]]) {
252
+ await mainRouter[args[0]](settings, args.slice(1))
253
+ process.exit(0)
254
+ } else if (args[0] && !args[0].startsWith("--")) {
255
+ try {
256
+ const cmd = args[0]
257
+ const module = require(`./cli/${cmd}`);
258
+ [args, flags] = extractFromArgs(args.slice(1), module?.flags)
259
+ if (args && args[0] && module.subcommands && module?.router[args[0]]) {
260
+ const subcmd = args[0]
261
+ if (settings?.help) {
262
+ showUsageSubcommand(cmd, subcmd, module)
263
+ } else {
264
+ [args, options] = extractFromArgs(args.slice(1), { ...module.router[subcmd]?.options })
265
+ args = deleteExtraFlags(args)
266
+ await module.subcommands[subcmd]({ ...settings, ...flags, ...options }, args)
267
+ .catch(err => {
268
+ showUsageError(cmd, subcmd, module, err, settings)
269
+ })
270
+ }
271
+ } else {
272
+ showUsage(cmd, module)
273
+ }
274
+ process.exit(0)
275
+ } catch (e) {
276
+ console.error(`EXCEPTION:\n${e}\n`)
277
+ }
278
+ }
279
+ console.info("USAGE:")
280
+ console.info(` ${colors.white("npx witnet")} [FLAGS] <COMMAND>`)
281
+ console.info("\nFLAGS:")
282
+ console.info(" --debug Outputs stack trace in case of error.")
283
+ console.info(" --force Avoids asking the user to confirm operation.")
284
+ console.info(" --help Describes command or subcommand usage.")
285
+ console.info(" --update Forces update of underlying binaries.")
286
+ console.info(" --version Prints toolkit name and version as first line.")
287
+ console.info("\nCOMMANDS:")
288
+ console.info(" inspect Inspect public data from the Wit/Oracle blockchain.")
289
+ console.info(" network Dynamic information from the Wit/Oracle P2P network.")
290
+ console.info(" nodes Interact with your private Wit/Oracle nodes, if reachable.")
291
+ console.info(" radon Manage Radon requests and templates within your project.")
292
+ console.info(" wallet Simple CLI wallet for spending and staking your Wits.")
293
+ }
294
+
295
+ main()
@@ -1,8 +1,8 @@
1
- const { assets } = require("@witnet/sdk")
2
-
3
- module.exports = {
4
- legacy: assets,
5
- requests: require("./requests"),
6
- sources: require("./sources"),
7
- templates: require("./templates"),
8
- }
1
+ const { assets } = require("@witnet/sdk")
2
+
3
+ module.exports = {
4
+ legacy: assets,
5
+ requests: require("./requests"),
6
+ sources: require("./sources"),
7
+ templates: require("./templates"),
8
+ }