-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcli_parser.cpp
More file actions
1240 lines (1129 loc) · 51.2 KB
/
cli_parser.cpp
File metadata and controls
1240 lines (1129 loc) · 51.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* cli_parser.cpp - Implementation of theCollider's CLI argument parser.
*
* Argv is walked exactly once. Each token is matched against a flat
* std::array<FlagSpec, N> by long_name / short_name; the matching FlagSpec's
* apply() callback handles value consumption and Arguments / CLIFlags
* mutation. The previous implementation was a 600-line if/else chain inline
* in this function; the registry collapses every uniform flag onto a one-line
* entry and keeps each special-case flag's logic local to its callback.
*
* Pro-only flags stay gated by COLLIDER_PRO so the Free build still compiles
* cleanly. In the Free build the Pro entries either disappear from the
* registry (most cases) or remain with a Free-build callback that prints the
* Pro hint and discards the value (so scripts that pass --bloom under a Free
* binary still parse, they just no-op the flag with a notice).
*/
#include "cli/cli_parser.hpp"
#include "cli/flag_spec.hpp"
#include <array>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>
#ifdef _WIN32
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <windows.h>
#include <aclapi.h>
#include <sddl.h>
#else
#include <sys/stat.h>
#include <unistd.h>
#endif
namespace {
// Reject password files that are world-readable. Mirrors the convention
// that SSH applies to identity files (chmod 0600 required). Returns true
// when the file's permissions are safe to read; populates err_msg and
// returns false otherwise. The fallback path returns true to avoid
// hard-failing on platforms whose permission model we cannot inspect.
bool password_file_permissions_safe(const std::string& path,
std::string& err_msg) {
#ifndef _WIN32
struct stat st {};
if (::stat(path.c_str(), &st) != 0) {
// open() will fail with a clearer message a moment later; fall
// through to that error path rather than emitting a duplicate.
return true;
}
if (st.st_mode & (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)) {
err_msg = "[!] --pool-password-file: " + path +
" has group/world-readable permissions ("
"mode=0" + std::to_string(st.st_mode & 0777) +
"). Refusing to read a password from a file other "
"users can see. Run: chmod 600 " + path + "\n";
return false;
}
return true;
#else
// Windows: check the file's DACL. We accept the file only when the
// current user's SID is the only identity granted FILE_GENERIC_READ.
// BUILTIN\Administrators and SYSTEM are always trusted (they can
// read anything anyway), so their presence on the ACL is ignored.
// If the API calls fail we fall through to allow open() (the existing
// behavior); the goal is to catch the obvious "Everyone:Read" case,
// not to be a full ACL auditor.
PSECURITY_DESCRIPTOR psd = nullptr;
PACL pdacl = nullptr;
if (GetNamedSecurityInfoA(
path.c_str(), SE_FILE_OBJECT, DACL_SECURITY_INFORMATION,
nullptr, nullptr, &pdacl, nullptr, &psd) != ERROR_SUCCESS) {
return true;
}
bool safe = true;
if (pdacl != nullptr) {
for (DWORD i = 0; i < pdacl->AceCount; ++i) {
void* ace_raw = nullptr;
if (!GetAce(pdacl, i, &ace_raw)) continue;
auto* hdr = static_cast<ACE_HEADER*>(ace_raw);
if (hdr->AceType != ACCESS_ALLOWED_ACE_TYPE) continue;
auto* ace = static_cast<ACCESS_ALLOWED_ACE*>(ace_raw);
if (!(ace->Mask & FILE_GENERIC_READ)) continue;
PSID sid = reinterpret_cast<PSID>(&ace->SidStart);
LPSTR sid_str = nullptr;
if (!ConvertSidToStringSidA(sid, &sid_str)) continue;
std::string sid_view = sid_str;
LocalFree(sid_str);
// S-1-1-0 = Everyone. S-1-5-7 = ANONYMOUS LOGON. S-1-5-11 =
// Authenticated Users. S-1-5-32-545 = BUILTIN\Users. Any of
// these granting read on a password file is unsafe.
if (sid_view == "S-1-1-0" ||
sid_view == "S-1-5-7" ||
sid_view == "S-1-5-11" ||
sid_view == "S-1-5-32-545") {
safe = false;
break;
}
}
}
if (psd != nullptr) LocalFree(psd);
if (!safe) {
err_msg = "[!] --pool-password-file: " + path +
" has a DACL that grants read access to Everyone / "
"Authenticated Users / BUILTIN\\Users. Refusing to "
"read a password from a file other accounts can see. "
"Right-click the file > Properties > Security and "
"remove those entries, leaving only your own user.\n";
}
return safe;
#endif
}
// Read the first line of `path` into `out`, stripping any trailing
// CR/LF/whitespace. Returns true on success, false on open failure or
// when the file's permissions allow other users to read it. Used by
// --pool-password-file so the secret never appears in argv (which is
// readable via ps / Task Manager / /proc).
bool read_password_file(const std::string& path, std::string& out,
std::string& err_msg) {
if (!password_file_permissions_safe(path, err_msg)) {
return false;
}
std::ifstream f(path);
if (!f.is_open()) {
err_msg = "[!] --pool-password-file: cannot open " + path + "\n";
return false;
}
std::string line;
if (!std::getline(f, line)) {
err_msg = "[!] --pool-password-file: " + path + " is empty\n";
return false;
}
// Strip trailing whitespace (CR for Windows line endings, plus any
// trailing spaces that crept in from a text editor).
while (!line.empty() &&
(line.back() == '\r' || line.back() == '\n' ||
line.back() == ' ' || line.back() == '\t')) {
line.pop_back();
}
out = std::move(line);
return true;
}
// Helper: advance i and return the value at the new slot, or set err_msg and
// return nullptr when the next slot is past the end of argv. Used by every
// VALUE-shaped flag to gate the "--flag without value" case uniformly.
const char* take_value(int& i, int argc, char* argv[],
std::string_view flag, std::string& err_msg) {
if (i + 1 >= argc) {
err_msg = std::string("[!] ") + std::string(flag) +
" requires a value.\n";
return nullptr;
}
return argv[++i];
}
using ::Arguments; // global scope; predates collider namespace
using ::collider::CLIFlags;
using ::collider::cli::FlagSpec;
// ---------------------------------------------------------------------------
// Apply callbacks. One per flag. The body of each callback mirrors exactly
// what the old if/else chain did for that flag, with no behavior change.
// ---------------------------------------------------------------------------
int apply_help(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.help = true;
return 0;
}
int apply_verbose(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.verbose = true;
cli.verbose_set = true;
return 0;
}
int apply_gpus(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--gpus", err_msg);
if (!v) return -1;
args.gpu_ids.clear();
std::string gpus = v;
size_t pos = 0;
while ((pos = gpus.find(',')) != std::string::npos) {
args.gpu_ids.push_back(std::stoi(gpus.substr(0, pos)));
gpus.erase(0, pos + 1);
}
args.gpu_ids.push_back(std::stoi(gpus));
cli.gpu_ids_set = true;
return 0;
}
int apply_batch_size(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--batch-size", err_msg);
if (!v) return -1;
args.batch_size = std::stoull(v);
args.batch_size_auto = false;
cli.batch_size_set = true;
return 0;
}
int apply_benchmark(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.benchmark = true;
return 0;
}
int apply_benchmark_time(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--benchmark-time", err_msg);
if (!v) return -1;
args.benchmark_seconds = std::stoi(v);
cli.benchmark_seconds_set = true;
return 0;
}
int apply_puzzle(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string&) {
// --puzzle [N]: N is optional; only consumed when the next argv exists
// and doesn't start with '-' (so "--puzzle" alone still selects puzzle
// mode without pinning a number, which is how auto-select-easiest works).
args.puzzle_mode = true;
if (i + 1 < argc && argv[i + 1][0] != '-') {
args.puzzle_number = std::stoi(argv[++i]);
cli.puzzle_number_set = true;
}
return 0;
}
int apply_puzzle_target(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-target", err_msg);
if (!v) return -1;
args.puzzle_target = v;
cli.puzzle_target_set = true;
return 0;
}
int apply_puzzle_start(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-start", err_msg);
if (!v) return -1;
args.puzzle_range_start = v;
cli.puzzle_start_set = true;
return 0;
}
int apply_puzzle_end(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-end", err_msg);
if (!v) return -1;
args.puzzle_range_end = v;
cli.puzzle_end_set = true;
return 0;
}
int apply_pubkey(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
// 33-byte compressed pubkey (02/03 + 32B hex) for targets whose pubkey
// isn't in puzzle_history.json. See README for the canonical
// kangaroo-able puzzle list.
const char* v = take_value(i, argc, argv, "--pubkey", err_msg);
if (!v) return -1;
args.puzzle_pubkey = v;
cli.puzzle_pubkey_set = true;
return 0;
}
int apply_sequential(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.puzzle_random = false;
cli.puzzle_random_set = true;
return 0;
}
int apply_random(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.puzzle_random = true;
cli.puzzle_random_set = true;
return 0;
}
int apply_puzzle_checkpoint(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-checkpoint", err_msg);
if (!v) return -1;
args.puzzle_checkpoint = v;
cli.puzzle_checkpoint_set = true;
return 0;
}
int apply_resume_kangaroo(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
// Opt in to loading the kangaroo herd checkpoint at the standalone path
// (the pool path already does this unconditionally for any backend that
// supports it). The SIGINT save side is always on; only the load is
// gated so a user who wants a fresh herd doesn't get a stale one.
args.resume_kangaroo = true;
return 0;
}
int apply_auto_next(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.puzzle_auto_next = true;
cli.puzzle_auto_next_set = true;
return 0;
}
int apply_all_unsolved(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.puzzle_all_unsolved = true;
return 0;
}
int apply_puzzle_min_bits(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-min-bits", err_msg);
if (!v) return -1;
args.puzzle_min_bits = std::stoi(v);
cli.puzzle_min_bits_set = true;
return 0;
}
int apply_puzzle_max_bits(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-max-bits", err_msg);
if (!v) return -1;
args.puzzle_max_bits = std::stoi(v);
cli.puzzle_max_bits_set = true;
return 0;
}
int apply_kangaroo(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.puzzle_kangaroo = true;
cli.puzzle_kangaroo_set = true;
return 0;
}
int apply_dp_bits(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--dp-bits", err_msg);
if (!v) return -1;
args.dp_bits = std::stoi(v);
cli.dp_bits_set = true;
return 0;
}
int apply_bloom(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--bloom", err_msg);
if (!v) return -1;
#ifdef COLLIDER_PRO
args.bloom_file = v;
cli.bloom_file_set = true;
#else
(void)args; (void)cli;
// Pro-only: opportunistic address scanning. Consume the path arg but
// ignore it; one-time hint so the user knows why.
std::cerr << "[Pro] --bloom '" << v
<< "' ignored; opportunistic address scanning is a "
"Pro feature. https://collisionprotocol.com/pro"
<< std::endl;
#endif
return 0;
}
int apply_bloom_tight(int& i, int argc, char* argv[], Arguments& args,
CLIFlags&, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--bloom-tight", err_msg);
if (!v) return -1;
#ifdef COLLIDER_PRO
// dual-bloom pipeline. The --bloom file lives on the GPU and is
// intentionally loose (small enough to fit alongside working buffers).
// At its design fill ratio every bloom probe has a non-trivial FP rate
// (e.g. ~0.02% on a 2.6 GB seen-ever bloom built with -f 1e-3). When
// --track-empty-hits is also on, those FPs flood found-empty.txt with
// noise. --bloom-tight points at a much tighter (.blf) built from the
// same source (e.g. -f 1e-7); we mmap it CPU-side and re-probe every
// empty candidate before logging. Only candidates that hit BOTH the
// loose GPU bloom AND the tight CPU bloom get logged as real empty
// hits. See docs/EMPTY-HITS-WORKFLOW.md.
args.bloom_tight_file = v;
#else
(void)args;
std::cerr << "[Pro] --bloom-tight '" << v
<< "' ignored; dual-bloom is a Pro feature." << std::endl;
#endif
return 0;
}
int apply_verify_set(int& i, int argc, char* argv[], Arguments& args,
CLIFlags&, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--verify-set", err_msg);
if (!v) return -1;
#ifdef COLLIDER_PRO
args.verify_set_file = v;
#else
(void)args;
std::cerr << "[Pro] --verify-set '" << v
<< "' ignored; bloom-hit verification is a Pro feature."
<< std::endl;
#endif
return 0;
}
int apply_verify_set_csv(int& i, int argc, char* argv[], Arguments& args,
CLIFlags&, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--verify-set-csv", err_msg);
if (!v) return -1;
#ifdef COLLIDER_PRO
// route HitVerifier::load_from_csv() from the runner. Operators with
// the raw UTXO CSV (and no pre-built .uvrf) can use it directly without
// building a binary UVRF first.
args.verify_set_csv = v;
#else
(void)args;
std::cerr << "[Pro] --verify-set-csv '" << v
<< "' ignored; Pro feature." << std::endl;
#endif
return 0;
}
int apply_track_empty_hits(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
#ifdef COLLIDER_PRO
// When set, every bloom hit that fails the UVRF funded-set lookup is
// treated as a "real but empty" wallet (had on-chain activity at some
// point, currently 0 balance) and logged to found-empty.txt. Only
// meaningful when the operator's --bloom file is a SEEN-EVER bloom
// (addresses that had any transaction history) and --verify-set is the
// currently-funded subset. With a funded-only bloom this flag just
// promotes bloom false-positives to log spam, so the runner
// auto-enables it only when both seen_tight.blf and
// funded_addresses.uvrf auto-resolve (the seen-ever operator
// configuration). The CLI flag is for operators who want to force it
// on against the auto-detection.
args.track_empty_hits = true;
args.track_empty_hits_user_set = true;
#else
(void)args;
std::cerr << "[Pro] --track-empty-hits ignored; this is a Pro feature."
<< std::endl;
#endif
return 0;
}
int apply_no_track_empty_hits(int&, int, char* [], Arguments& args,
CLIFlags&, std::string&) {
#ifdef COLLIDER_PRO
// Opt-out: pin to false even when auto-detect would have enabled
// empty-hit tracking.
args.track_empty_hits = false;
args.track_empty_hits_user_set = true;
#else
(void)args;
#endif
return 0;
}
int apply_use_texture_bloom(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
#ifdef COLLIDER_PRO
// Opt into the texture-memory bloom probe path. The runtime falls back
// to global memory if more than one GPU is in use (defensive guard) or
// if the device cannot allocate a texture object.
args.use_texture_bloom = true;
#else
(void)args;
std::cerr << "[Pro] --use-texture-bloom ignored; texture bloom is a "
"Pro feature." << std::endl;
#endif
return 0;
}
int apply_brainwallet_warpwallet(int& i, int argc, char* argv[],
Arguments& args, CLIFlags& cli,
std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--brainwallet-warpwallet",
err_msg);
if (!v) return -1;
#ifdef COLLIDER_PRO
// WarpWallet brainwallet derivation
// (scrypt(N=2^18,r=8,p=1) + PBKDF2-SHA256(c=2^16); per Keybase spec).
// Implies --brainwallet. The argument is the salt used in the Keybase
// scheme (typically the user's email address).
args.warpwallet_salt = v;
args.brainwallet_mode = true;
args.pool_mode = false;
args.pool_url.clear();
cli.brainwallet_set = true;
#else
(void)args; (void)cli;
std::cerr << "[Pro] --brainwallet-warpwallet '" << v
<< "' ignored; WarpWallet derivation is a Pro feature."
<< std::endl;
#endif
return 0;
}
int apply_brainwallet(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.brainwallet_mode = true;
args.pool_mode = false; // Brainwallet overrides pool mode from config
args.pool_url.clear(); // Don't leak a pool URL once brainwallet is selected
cli.brainwallet_set = true;
return 0;
}
int apply_brainwallet_v2(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
// Route the brainwallet scan through the v2 kernel chain (multi-scheme
// derivation + multi-address bloom check per priv). Implies
// --brainwallet; defaults scheme_mask=ALL unless --schemes is also
// supplied. Multi-address scanning via --addr-types was removed in
// Q10 cleanup; the v2 orchestrator -> kernel bridge for addr_mask != 0
// is deferred to v1.5.0+. Use the legacy --brainwallet --bloom path
// for multi-address scanning today.
args.brainwallet_mode = true;
args.brainwallet_v2_mode = true;
args.pool_mode = false;
args.pool_url.clear();
cli.brainwallet_set = true;
return 0;
}
int apply_brainwallet_setup(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.brainwallet_setup = true;
return 0;
}
int apply_brute(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
// Consume one or more positive integer length arguments until the next
// flag (-prefixed token) or end of argv. Each length must be in [1,16];
// longer is rejected because 62^17 = 3.0e30 candidates exceeds any
// plausible scan budget and is almost always a typo.
args.brainwallet_mode = true;
args.pool_mode = false;
args.pool_url.clear();
cli.brainwallet_set = true;
int consumed = 0;
while (i + 1 < argc && argv[i + 1][0] != '-') {
const char* tok = argv[++i];
try {
int n = std::stoi(tok);
if (n < 1 || n > 16) {
err_msg = "[!] --brute length must be in [1, 16]; got " +
std::to_string(n) +
" (62^17 already exceeds 3e30 candidates).\n";
return -1;
}
args.brute_lengths.push_back(n);
consumed++;
} catch (const std::exception&) {
err_msg = std::string("[!] --brute expected a positive integer "
"length, got '") +
tok + "'.\n";
return -1;
}
}
if (consumed == 0) {
err_msg = "[!] --brute requires at least one length argument "
"(e.g. --brute 7 8 9).\n";
return -1;
}
return 0;
}
int apply_puzzle_only_v2(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
#ifdef COLLIDER_PRO
// Enable v2 puzzle-mode kernel + multi-scheme.
args.puzzle_only_v2 = true;
args.brainwallet_mode = true; // Goes through the brainwallet pipeline
args.pool_mode = false;
args.pool_url.clear();
return 0;
#else
(void)args;
// Exact text per v1.4.0 spec; do NOT add URLs or extra trailing text.
std::cerr << "I'm sorry, but this is a pro function. If you'd like "
"to try pro, go and visit the website to purchase and "
"download." << std::endl;
return 2;
#endif
}
int apply_puzzle_keys(int& i, int argc, char* argv[], Arguments& args,
CLIFlags&, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--puzzle-keys", err_msg);
if (!v) return -1;
args.puzzle_keys_file = v;
return 0;
}
int apply_schemes(int& i, int argc, char* argv[], Arguments& args,
CLIFlags&, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--schemes", err_msg);
if (!v) return -1;
args.schemes_csv = v;
return 0;
}
int apply_resume(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.resume = true;
cli.resume_set = true;
return 0;
}
int apply_cpu_rules(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.cpu_rules = true;
return 0;
}
int apply_save_interval(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--save-interval", err_msg);
if (!v) return -1;
args.save_interval = std::stoull(v);
cli.save_interval_set = true;
return 0;
}
int apply_calibrate(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.calibrate = true;
return 0;
}
int apply_force_calibrate(int&, int, char* [], Arguments& args,
CLIFlags& cli, std::string&) {
args.calibrate = true;
args.force_calibrate = true;
cli.force_calibrate_set = true;
return 0;
}
int apply_debug(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.debug = true;
cli.debug_set = true;
return 0;
}
int apply_perf_instrument(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
// Opt-in for the per-kernel CUDA-event timing collector. See
// Arguments::perf_instrument in cli_parser.hpp for the off-by-default
// rationale. The runner consults args.perf_instrument before calling
// perf::set_enabled(true); when false, the hot path stays a single
// relaxed load + predicted-not-taken branch on every kernel launch.
args.perf_instrument = true;
return 0;
}
int apply_analyze(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.analyze_puzzles = true;
return 0;
}
int apply_no_smart(int&, int, char* [], Arguments& args, CLIFlags& cli,
std::string&) {
args.smart_select = false;
cli.smart_select_set = true;
return 0;
}
int apply_pool(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--pool", err_msg);
if (!v) return -1;
args.pool_mode = true;
args.pool_url = v;
cli.pool_url_set = true;
return 0;
}
int apply_worker(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--worker", err_msg);
if (!v) return -1;
args.pool_worker = v;
cli.pool_worker_set = true;
return 0;
}
int apply_pool_password(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--pool-password", err_msg);
if (!v) return -1;
// SecureString::assign copies the bytes into wiped-on-free storage.
// The argv byte buffer itself is still operator-visible via
// ps / Task Manager; that is the entire reason --pool-password is
// deprecated in favour of --pool-password-file. SecureString here
// closes the heap-residue half of the leak (no plain std::string
// copy carrying the password past Arguments destruction).
args.pool_password.assign(v, std::strlen(v));
cli.pool_password_set = true;
// Argv is world-readable on every supported platform (ps aux on POSIX,
// Process Explorer / Task Manager on Windows, /proc/<pid>/cmdline on
// Linux). Warn loudly so operators migrate to --pool-password-file
// before v1.6.0 removes this flag.
std::cerr << "[CLI] WARNING: --pool-password leaks via "
"process listing (ps / Task Manager). Use "
"--pool-password-file <path> for production "
"deployments. The --pool-password flag is "
"scheduled for removal in v1.6.0."
<< std::endl;
return 0;
}
int apply_pool_password_file(int& i, int argc, char* argv[],
Arguments& args, CLIFlags& cli,
std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--pool-password-file",
err_msg);
if (!v) return -1;
std::string path = v;
std::string secret;
std::string read_err;
if (!read_password_file(path, secret, read_err)) {
err_msg = read_err;
return -1;
}
args.pool_password_file = path;
// Copy bytes into the SecureString slot, then explicitly wipe the
// transient std::string `secret` before its scope exit so the on-heap
// bytes are zeroed regardless of std::string's destructor behaviour
// (which makes no wipe guarantee and on SSO may not even free()).
args.pool_password.assign(secret.data(), secret.size());
if (!secret.empty()) {
::collider::secure_wipe(secret.data(), secret.size());
}
cli.pool_password_file_set = true;
// Mark pool_password as user-set so the yaml override path does not
// clobber the file-sourced value. --pool-password and
// --pool-password-file are alternative sources for the same field.
cli.pool_password_set = true;
return 0;
}
int apply_pool_api_key(int& i, int argc, char* argv[], Arguments& args,
CLIFlags& cli, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--pool-api-key", err_msg);
if (!v) return -1;
args.pool_api_key = v;
cli.pool_api_key_set = true;
// HTTP pool path was removed (credential-leak review). The remaining
// JLP/JLPS transports authenticate via worker/password in AUTH, not an
// API key. Keep the flag accepting a value to avoid surprising old
// configs / scripts, but emit a one-time deprecation notice so
// operators migrate.
std::cerr << "[CLI] --pool-api-key is deprecated and ignored "
"(HTTP pool transport was removed; use --pool-worker / "
"--pool-password with jlp:// or jlps://)." << std::endl;
return 0;
}
int apply_config(int& i, int argc, char* argv[], Arguments& args,
CLIFlags&, std::string& err_msg) {
const char* v = take_value(i, argc, argv, "--config", err_msg);
if (!v) return -1;
args.config_file = v;
return 0;
}
int apply_no_tui(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
// TUI flag pair. --no-tui forces the legacy flat-line status output
// (suitable for log scrapers, CI, and operators that prefer a quiet
// single-line update). --tui forces the multi-panel TUI even when
// stdout is not a TTY (e.g. piping into a recording wrapper that
// handles ANSI escapes). When neither flag is supplied the runner
// auto-detects via isatty.
args.no_tui = true;
args.no_tui_user_set = true;
return 0;
}
int apply_tui(int&, int, char* [], Arguments& args, CLIFlags&,
std::string&) {
args.no_tui = false;
args.no_tui_user_set = true;
return 0;
}
// ---------------------------------------------------------------------------
// Flag registry. Order is not load-bearing for dispatch (flat lookup), but
// the groupings track the layout of the old if/else chain so reviewers can
// follow the refactor line-by-line. Group labels also drive --help.
// ---------------------------------------------------------------------------
// Deduced size so adding a flag entry doesn't require touching the count.
constexpr FlagSpec kFlagsRaw[] = {
// General
{"--help", "-h", apply_help, "general"},
{"--verbose", "-v", apply_verbose, "general"},
{"--debug", nullptr, apply_debug, "general"},
{"--perf-instrument", nullptr, apply_perf_instrument, "general"},
{"--config", "-c", apply_config, "general"},
// GPU
{"--gpus", "-g", apply_gpus, "gpu"},
{"--batch-size", nullptr, apply_batch_size, "gpu"},
// Benchmark
{"--benchmark", nullptr, apply_benchmark, "benchmark"},
{"--benchmark-time", nullptr, apply_benchmark_time, "benchmark"},
// Puzzle
{"--puzzle", "-P", apply_puzzle, "puzzle"},
{"--puzzle-target", nullptr, apply_puzzle_target, "puzzle"},
{"--puzzle-start", nullptr, apply_puzzle_start, "puzzle"},
{"--puzzle-end", nullptr, apply_puzzle_end, "puzzle"},
{"--pubkey", nullptr, apply_pubkey, "puzzle"},
{"--sequential", nullptr, apply_sequential, "puzzle"},
{"--random", nullptr, apply_random, "puzzle"},
{"--puzzle-checkpoint", nullptr, apply_puzzle_checkpoint, "puzzle"},
{"--resume-kangaroo", nullptr, apply_resume_kangaroo, "puzzle"},
{"--auto-next", nullptr, apply_auto_next, "puzzle"},
{"--all-unsolved", nullptr, apply_all_unsolved, "puzzle"},
{"--puzzle-min-bits", nullptr, apply_puzzle_min_bits, "puzzle"},
{"--puzzle-max-bits", nullptr, apply_puzzle_max_bits, "puzzle"},
{"--kangaroo", nullptr, apply_kangaroo, "puzzle"},
{"--dp-bits", nullptr, apply_dp_bits, "puzzle"},
// Bloom / brainwallet (Pro flags keep their slot in the Free build but
// their callbacks no-op + print the Pro hint).
{"--bloom", nullptr, apply_bloom, "brainwallet"},
{"--bloom-tight", nullptr, apply_bloom_tight, "brainwallet"},
{"--verify-set", nullptr, apply_verify_set, "brainwallet"},
{"--verify-set-csv", nullptr, apply_verify_set_csv, "brainwallet"},
{"--track-empty-hits", nullptr, apply_track_empty_hits, "brainwallet"},
{"--no-track-empty-hits", nullptr, apply_no_track_empty_hits,"brainwallet"},
{"--use-texture-bloom", nullptr, apply_use_texture_bloom, "brainwallet"},
{"--brainwallet-warpwallet",nullptr, apply_brainwallet_warpwallet,"brainwallet"},
{"--brainwallet", nullptr, apply_brainwallet, "brainwallet"},
{"--brainwallet-v2", nullptr, apply_brainwallet_v2, "brainwallet"},
{"--brainwallet-setup", nullptr, apply_brainwallet_setup, "brainwallet"},
{"--brute", nullptr, apply_brute, "brainwallet"},
{"--puzzle-only-v2", nullptr, apply_puzzle_only_v2, "brainwallet"},
{"--puzzle-keys", nullptr, apply_puzzle_keys, "brainwallet"},
{"--schemes", nullptr, apply_schemes, "brainwallet"},
{"--resume", nullptr, apply_resume, "brainwallet"},
{"--cpu-rules", nullptr, apply_cpu_rules, "brainwallet"},
{"--save-interval", nullptr, apply_save_interval, "brainwallet"},
// Calibration / analysis
{"--calibrate", nullptr, apply_calibrate, "calibrate"},
{"--force-calibrate", nullptr, apply_force_calibrate, "calibrate"},
{"--analyze", nullptr, apply_analyze, "calibrate"},
{"--no-smart", nullptr, apply_no_smart, "calibrate"},
// Pool
{"--pool", "-p", apply_pool, "pool"},
{"--worker", "-w", apply_worker, "pool"},
{"--pool-password", nullptr, apply_pool_password, "pool"},
{"--pool-password-file", nullptr, apply_pool_password_file, "pool"},
{"--pool-api-key", nullptr, apply_pool_api_key, "pool"},
// TUI
{"--no-tui", nullptr, apply_no_tui, "tui"},
{"--tui", nullptr, apply_tui, "tui"},
};
constexpr std::size_t kFlagCount = sizeof(kFlagsRaw) / sizeof(kFlagsRaw[0]);
// Look up by long_name OR short_name. Returns nullptr if no match. Linear
// scan is fine: kFlagCount is small enough that std::map / hash table
// would lose to cache-line locality on this workload.
const FlagSpec* find_flag(std::string_view arg) {
for (const auto& f : kFlagsRaw) {
if (arg == f.long_name) return &f;
if (f.short_name && f.short_name[0] != '\0' && arg == f.short_name) {
return &f;
}
}
return nullptr;
}
} // namespace
int validate_mode_mutex(const Arguments& args, std::string& msg) {
int active = 0;
std::vector<std::string> chosen;
if (args.brainwallet_mode) { active++; chosen.emplace_back("--brainwallet"); }
if (args.pool_mode) { active++; chosen.emplace_back("--pool <url>"); }
// --kangaroo is a sub-mode of puzzle solving. Only count it as a top-level
// mode claim when neither brainwallet nor pool is asserted (so combining
// --kangaroo with either still flags the conflict via the != 1 check below).
if (args.puzzle_kangaroo && (args.brainwallet_mode || args.pool_mode)) {
active++;
chosen.emplace_back("--kangaroo");
}
if ((args.puzzle_number > 0 || args.puzzle_all_unsolved) &&
(args.brainwallet_mode || args.pool_mode)) {
active++;
if (args.puzzle_all_unsolved) chosen.emplace_back("--all-unsolved");
else chosen.emplace_back("--puzzle " + std::to_string(args.puzzle_number));
}
if (active <= 1) return 0;
msg = "[!] Conflicting search modes selected: ";
for (size_t i = 0; i < chosen.size(); ++i) {
msg += chosen[i];
if (i + 1 < chosen.size()) msg += ", ";
}
msg += ".\n";
msg += " Pick exactly one of: --brainwallet, --pool <url>, --puzzle <N> [--kangaroo].\n";
msg += " --kangaroo by itself is allowed (it implies puzzle mode);\n";
msg += " --puzzle <N> + --kangaroo is the standard kangaroo-on-puzzle case.\n";
return -1;
}
int parse_args_core(int argc, char* argv[], Arguments& args,
collider::CLIFlags& cli, std::string& err_msg) {
args = Arguments{};
cli = collider::CLIFlags{};
for (int i = 1; i < argc; i++) {
std::string_view arg = argv[i];
const FlagSpec* spec = find_flag(arg);
if (!spec) {
// Unknown flag: silently skip. The legacy parser had this same
// behavior (the trailing else of the if/else chain was empty),
// so a stray token does NOT abort parsing. Preserved to keep
// ctest CLIParser invariants stable.
continue;
}
int rc = spec->apply(i, argc, argv, args, cli, err_msg);
if (rc != 0) {
// Negative: hard parse error; caller will print err_msg + exit.
// Positive: Free-build early-exit code (e.g. --puzzle-only-v2
// returns 2). Propagate either way without modifying err_msg.
return rc;
}
}
// --brute mutex: brute mode replaces the candidate-generation pipeline
// entirely, so it can't co-exist with v2 (which expects the streaming
// generator's multi-scheme expansion) or warpwallet (which routes
// through a CPU-only scrypt+PBKDF2 path).
if (!args.brute_lengths.empty()) {
if (args.brainwallet_v2_mode) {
err_msg = "[!] --brute is incompatible with --brainwallet-v2; the v2 multi-scheme\n"
" pipeline expects the streaming generator. Pick one.\n";
return -1;
}
if (!args.warpwallet_salt.empty()) {
err_msg = "[!] --brute is incompatible with --brainwallet-warpwallet; WarpWallet\n"
" routes through CPU-only scrypt and would take cosmic time on\n"
" bruteforce keyspaces.\n";
return -1;
}
}
return validate_mode_mutex(args, err_msg);
}
Arguments parse_args(int argc, char* argv[], collider::CLIFlags* cli_out) {
Arguments args;
collider::CLIFlags cli;
std::string err;
int rc = parse_args_core(argc, argv, args, cli, err);
if (rc < 0) {
std::cerr << err;
std::cerr << " Run `collider --help` for usage details.\n";
std::exit(1);
}
if (rc > 0) {
// Free-build early-exit code (--puzzle-only-v2 in Free returns 2).
std::exit(rc);
}
if (cli_out) *cli_out = cli;
return args;
}
int parse_args_for_test(int argc, char* argv[], Arguments& args,
collider::CLIFlags& cli, std::string* err_out) {
std::string err;
int rc = parse_args_core(argc, argv, args, cli, err);
if (err_out) *err_out = err;
return rc;
}