Compiles, needs some extra tweaking

This commit is contained in:
John Poole 2026-02-12 10:53:31 -08:00
commit 18e8d2c8ea
5 changed files with 193 additions and 0 deletions

1
tools/CMakeLists.txt Normal file
View file

@ -0,0 +1 @@
add_subdirectory(keygen)

View file

@ -0,0 +1,22 @@
add_executable(rns-provision main.cpp)
# If microReticulum exports an include directory / target, this may be unnecessary.
# Keep this here as a pragmatic fallback:
target_include_directories(rns-provision PRIVATE
${CMAKE_SOURCE_DIR}/external/microReticulum/src
)
# ArduinoJson is pulled by microReticulum headers; add it to this tool's include path.
target_include_directories(rns-provision PRIVATE
${CMAKE_SOURCE_DIR}/external/microReticulum/src
# ArduinoJson headers:
# - ArduinoJson.h lives at the repo root
# - ArduinoJson/... lives under src/
${CMAKE_BINARY_DIR}/_deps/arduinojson-src
${CMAKE_BINARY_DIR}/_deps/arduinojson-src/src
)
# Link against the microReticulum library target.
# If this target name is wrong in your submodule, change it here.
target_link_libraries(rns-provision PRIVATE ReticulumStatic)

99
tools/keygen/main.cpp Normal file
View file

@ -0,0 +1,99 @@
// keygen - generate microReticulum identity keypairs
//
// Example:
// ./keygen --quantity 6 --format tsv
// ./keygen -q 6 -f json
//
// $Header$
// $Id$
#include <Identity.h>
#include <string>
#include <vector>
#include <iostream>
#include <stdexcept>
static void usage(const char* argv0) {
std::cerr
<< "Usage: " << argv0 << " --quantity N [--format tsv|json] [--public]\n"
<< " -q, --quantity Number of identities to generate (required)\n"
<< " -f, --format Output format: tsv (default) or json\n"
<< " --public Also include public_key in output\n";
}
static bool is_flag(const std::string& a, const char* s) { return a == s; }
int main(int argc, char** argv) {
try {
int quantity = -1;
std::string format = "tsv";
bool include_public = false;
for (int i = 1; i < argc; i++) {
std::string a(argv[i]);
if (is_flag(a, "-h") || is_flag(a, "--help")) {
usage(argv[0]);
return 0;
} else if (is_flag(a, "-q") || is_flag(a, "--quantity")) {
if (i + 1 >= argc) throw std::runtime_error("Missing value for --quantity");
quantity = std::stoi(argv[++i]);
} else if (is_flag(a, "-f") || is_flag(a, "--format")) {
if (i + 1 >= argc) throw std::runtime_error("Missing value for --format");
format = argv[++i];
} else if (is_flag(a, "--public")) {
include_public = true;
} else {
throw std::runtime_error("Unknown argument: " + a);
}
}
if (quantity <= 0) {
usage(argv[0]);
return 2;
}
if (!(format == "tsv" || format == "json")) {
throw std::runtime_error("Invalid --format (must be tsv or json)");
}
if (format == "tsv") {
// header row
std::cout << "n\tid_hex\tprivate_key_hex";
if (include_public) std::cout << "\tpublic_key_hex";
std::cout << "\n";
for (int n = 1; n <= quantity; n++) {
RNS::Identity id(true);
std::cout
<< n << "\t"
<< id.hash().toHex() << "\t"
<< id.get_private_key().toHex();
if (include_public) std::cout << "\t" << id.get_public_key().toHex();
std::cout << "\n";
}
} else {
// json
std::cout << "[\n";
for (int n = 1; n <= quantity; n++) {
RNS::Identity id(true);
std::cout << " {\n";
std::cout << " \"n\": " << n << ",\n";
std::cout << " \"id\": \"" << id.hash().toHex() << "\",\n";
std::cout << " \"private_key\": \"" << id.get_private_key().toHex() << "\"";
if (include_public) {
std::cout << ",\n \"public_key\": \"" << id.get_public_key().toHex() << "\"\n";
} else {
std::cout << "\n";
}
std::cout << " }" << (n == quantity ? "\n" : ",\n");
}
std::cout << "]\n";
}
return 0;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
}