evm-contracts-tools 0.0.1

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.
@@ -0,0 +1,411 @@
1
+ #!/usr/bin/env node
2
+
3
+ // SPDX-License-Identifier: AGPL-3.0-or-later
4
+
5
+ /** ----------------------------------------------------------------------
6
+ * Copyright ©
7
+ * Pellegrino Prevete
8
+ * 2024, 2025, 2026
9
+ *
10
+ * All rights reserved
11
+ * ----------------------------------------------------------------------
12
+ *
13
+ * This program is free software: you can redistribute it and/or modify
14
+ * it under the terms of the GNU General Public License as published by
15
+ * the Free Software Foundation, either version 3 of the License, or
16
+ * (at your option) any later version.
17
+ *
18
+ * This program is distributed in the hope that it will be useful,
19
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
+ * GNU General Public License for more details.
22
+ *
23
+ * You should have received a copy of the GNU General Public License
24
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
25
+ */
26
+
27
+ const
28
+ _libcrash =
29
+ require(
30
+ '../libcrash-js/crash-js');
31
+ _cmdline_check =
32
+ _libcrash._cmdline_check;
33
+ _error_display =
34
+ _libcrash._error_display;
35
+ _file_read =
36
+ _libcrash._file_read;
37
+ _file_write =
38
+ _libcrash._file_write;
39
+ _msg_info =
40
+ _libcrash._msg_info
41
+ _msg_error =
42
+ _libcrash._msg_error
43
+ _printf =
44
+ _libcrash._printf
45
+ const
46
+ _contract_get_module =
47
+ require(
48
+ './contract-get');
49
+ _contract_get =
50
+ _contract_get_module._contract_get
51
+ const
52
+ _network_provider_module =
53
+ require(
54
+ '../evm-wallet/network-provider');
55
+ _provider_get =
56
+ _network_provider_module._provider_get;
57
+
58
+ function
59
+ _global_variables() {
60
+ app_name =
61
+ "bytecode-runtime-get";
62
+ wallet_seed =
63
+ "";
64
+ retries_max =
65
+ "";
66
+ api_key_path =
67
+ "";
68
+ target_network =
69
+ "";
70
+ contract_address =
71
+ "";
72
+ contract_abi_path =
73
+ "";
74
+ contract_bytecode_path =
75
+ "";
76
+ // contract_compiler_output_path =
77
+ // "";
78
+ // contract_method =
79
+ // "";
80
+ // method_args =
81
+ // [];
82
+ quiet =
83
+ '';
84
+ }
85
+
86
+ function
87
+ _delay(
88
+ _time) {
89
+ return new Promise(resolve => setTimeout(resolve, _time));
90
+ }
91
+
92
+ function
93
+ _msg_info_contract_obtained(
94
+ _contract) {
95
+ let
96
+ _msg,
97
+ _template;
98
+ _template =
99
+ "Contract '{_contract}' obtained.";
100
+ _msg =
101
+ _template.replaceAll(
102
+ "{_contract}",
103
+ _contract);
104
+ _msg_info(
105
+ _msg);
106
+ }
107
+
108
+ function
109
+ _msg_info_method_obtained(
110
+ _method) {
111
+ let
112
+ _msg;
113
+ _msg =
114
+ "Method obtained.";
115
+ _msg_info(
116
+ _msg);
117
+ }
118
+
119
+ async function
120
+ _bytecode_runtime_get(
121
+ _wallet_seed,
122
+ _retries_max,
123
+ _api_key_path,
124
+ _target_network,
125
+ _contract_address,
126
+ _contract_bytecode_path
127
+ ) {
128
+ let
129
+ _api_key,
130
+ _code,
131
+ _network_provider,
132
+ _passed,
133
+ _wallet_password;
134
+ if ( _api_key_path != "" ) {
135
+ _api_key =
136
+ _file_read(
137
+ _api_key_path);
138
+ }
139
+ else if ( _api_key_path == "" ) {
140
+ _api_key =
141
+ ""
142
+ }
143
+ _retry =
144
+ 1;
145
+ _wallet_password =
146
+ "";
147
+ _passed =
148
+ false;
149
+ while ( _retry <= _retries_max &&
150
+ _passed == false ) {
151
+ try {
152
+ _network_provider =
153
+ await _provider_get(
154
+ _target_network,
155
+ _api_key);
156
+ _code =
157
+ await _network_provider.getCode(
158
+ _contract_address);
159
+ if ( _code != undefined ) {
160
+ _msg_info(
161
+ `Bytecode length: ${_code.length}`);
162
+ _file_write(
163
+ _contract_bytecode_path,
164
+ _code);
165
+ _passed =
166
+ true;
167
+ }
168
+ else if ( _code == undefined ) {
169
+ throw {"error": {
170
+ "message":
171
+ "Code not retrieved." } };
172
+ }
173
+ } catch (
174
+ _error) {
175
+ _error_display(
176
+ _error);
177
+ _retry =
178
+ _retry + 1;
179
+ _msg_error(
180
+ "Increasing retry and waiting 3 seconds.",
181
+ 0);
182
+ await _delay(
183
+ 3000);
184
+ }
185
+ }
186
+ if ( _retry > _retries_max ) {
187
+ throw "Maximum number of retries reached.";
188
+ }
189
+ return _code;
190
+ }
191
+
192
+ function
193
+ _config_show() {
194
+ let
195
+ _line,
196
+ _method_args,
197
+ _text;
198
+ _text =
199
+ [];
200
+ _text.push(
201
+ " bytecode-runtime-get configuration:");
202
+ _text.push(
203
+ " Contract address: {contract_address}".replace(
204
+ "{contract_address}",
205
+ contract_address));
206
+ _text.push(
207
+ " Contract bytecode: {contract_bytecode_path}".replace(
208
+ "{contract_bytecode_path}",
209
+ contract_bytecode_path));
210
+ _text.push(
211
+ " Max retries: {retries_max}".replace(
212
+ "{retries_max}",
213
+ retries_max));
214
+ _text.push(
215
+ " Credentials options:");
216
+ _text.push(
217
+ " Wallet seed: {wallet_seed}".replace(
218
+ "{wallet_seed}",
219
+ wallet_seed));
220
+ _text.push(
221
+ " API Key path: {api_key_path}".replace(
222
+ "{api_key_path}",
223
+ api_key_path));
224
+ _text.push(
225
+ " Network options:");
226
+ _text.push(
227
+ " Target network: {target_network}".replace(
228
+ "{target_network}",
229
+ target_network));
230
+ for (_line of _text) {
231
+ _msg_info(
232
+ _line);
233
+ }
234
+ }
235
+
236
+ function
237
+ _usage(
238
+ _exit) {
239
+ let
240
+ _line,
241
+ _text;
242
+ _text = [
243
+ "Retrieves the runtime bytecode for an address.",
244
+ "",
245
+ "Usage:",
246
+ " bytecode-runtime-get",
247
+ " <quiet>",
248
+ " <wallet_seed>",
249
+ " <retries_max>",
250
+ " <api_key_path>",
251
+ " <target_network>",
252
+ " <contract_address>",
253
+ " <contract_bytecode_path>",
254
+ "",
255
+ "Args:",
256
+ " <quiet> Can be 'y' or 'n'",
257
+ " Default: y",
258
+ " <wallet_seed> Path of the file containing",
259
+ " the seed phrase.",
260
+ " <retries_max> Maximum number of retries before",
261
+ " failing.",
262
+ " <api_key_path> Path of the API key for the",
263
+ " contract ABI provider service",
264
+ " <target_network> Network on which the contract",
265
+ " resides.",
266
+ " <contract_address> Address of the contract",
267
+ " <contract_bytecode_path> Path in which to save",
268
+ " the runtime bytecode.",
269
+ "",
270
+ " Options:",
271
+ " -h This help."
272
+ ];
273
+ for (_line of _text) {
274
+ _msg_info(
275
+ _line);
276
+ }
277
+ process.exit(
278
+ _exit);
279
+ }
280
+
281
+ function
282
+ _overrides_set() {
283
+ if ( retries_max == "" ) {
284
+ retries_max =
285
+ 100;
286
+ }
287
+ }
288
+
289
+ function
290
+ _cmdline_parse() {
291
+ quiet =
292
+ "y";
293
+ process.argv.forEach(
294
+ function (
295
+ _value,
296
+ _index,
297
+ _array) {
298
+ if ( _index == 2 ) {
299
+ quiet =
300
+ _value;
301
+ }
302
+ if ( _index == 3 ) {
303
+ wallet_seed =
304
+ _value;
305
+ }
306
+ if ( _index == 4 ) {
307
+ retries_max =
308
+ _value;
309
+ }
310
+ if ( _index == 5 ) {
311
+ api_key_path =
312
+ _value;
313
+ }
314
+ if ( _index == 6 ) {
315
+ target_network =
316
+ _value;
317
+ }
318
+ if ( _index == 7 ) {
319
+ contract_address =
320
+ _value;
321
+ }
322
+ if ( _index == 8 ) {
323
+ contract_bytecode_path =
324
+ _value;
325
+ }
326
+ if ( _value == "-h" ||
327
+ _value == "--help" ) {
328
+ _usage(
329
+ 0);
330
+ }
331
+ }
332
+ );
333
+ if ( contract_address == "" ) {
334
+ _msg_error(
335
+ "It is required to set a contract address.",
336
+ 0);
337
+ quiet =
338
+ "n";
339
+ _usage(
340
+ 1);
341
+ }
342
+ if ( target_network == "" ) {
343
+ _msg_error(
344
+ "It is required to set a network.",
345
+ 0);
346
+ quiet =
347
+ "n";
348
+ _usage(
349
+ 1);
350
+ }
351
+ if ( contract_bytecode_path == "" ) {
352
+ _msg_error(
353
+ "It is required to set a path in which to save the bytecode.",
354
+ 0);
355
+ quiet =
356
+ "n";
357
+ _usage(
358
+ 1);
359
+ }
360
+ }
361
+
362
+ async function
363
+ _bytecode_runtime_get_cmdline(
364
+ _app_opts) {
365
+ let
366
+ _code;
367
+ try {
368
+ _code =
369
+ await _bytecode_runtime_get.apply(
370
+ null,
371
+ _app_opts);
372
+ if ( _code != undefined ) {
373
+ _printf(
374
+ typeof(
375
+ _code));
376
+ // _printf(
377
+ // _code.toString());
378
+ }
379
+ else {
380
+ throw "Bytecode runtime retrieval failed."
381
+ }
382
+ } catch (_error) {
383
+ _msg_error(
384
+ _error,
385
+ 1);
386
+ }
387
+ }
388
+
389
+ if ( _cmdline_check(
390
+ "bytecode-runtime-get") ) {
391
+ _global_variables();
392
+ _cmdline_parse();
393
+ _overrides_set();
394
+ _config_show();
395
+ app_opts = [
396
+ wallet_seed,
397
+ retries_max,
398
+ api_key_path,
399
+ target_network,
400
+ contract_address,
401
+ contract_bytecode_path,
402
+ ];
403
+ _bytecode_runtime_get_cmdline(
404
+ app_opts);
405
+ }
406
+
407
+ module.exports = {
408
+ _bytecode_runtime_get:
409
+ _bytecode_runtime_get
410
+ };
411
+