From 89e4e4ea7a2aa082ee61d0dc05cefa4ac1909286 Mon Sep 17 00:00:00 2001 From: crypto-ape <43807588+crypto-ape@users.noreply.github.com> Date: Mon, 5 Aug 2019 17:11:17 +0200 Subject: [PATCH] witness_node can accept private keys from files --- .../include/graphene/witness/witness.hpp | 1 + libraries/plugins/witness/witness.cpp | 68 +++++++++++++++---- 2 files changed, 55 insertions(+), 14 deletions(-) diff --git a/libraries/plugins/witness/include/graphene/witness/witness.hpp b/libraries/plugins/witness/include/graphene/witness/witness.hpp index 0d2eab27ac..8ca09a5b27 100644 --- a/libraries/plugins/witness/include/graphene/witness/witness.hpp +++ b/libraries/plugins/witness/include/graphene/witness/witness.hpp @@ -71,6 +71,7 @@ class witness_plugin : public graphene::app::plugin { void schedule_production_loop(); block_production_condition::block_production_condition_enum block_production_loop(); block_production_condition::block_production_condition_enum maybe_produce_block( fc::limited_mutable_variant_object& capture ); + void add_private_key(const std::string& key_id_to_wif_pair_string); /// Fetch signing keys of all witnesses in the cache from object database and update the cache accordingly void refresh_witness_key_cache(); diff --git a/libraries/plugins/witness/witness.cpp b/libraries/plugins/witness/witness.cpp index eda18b1ce8..05bbb9564d 100644 --- a/libraries/plugins/witness/witness.cpp +++ b/libraries/plugins/witness/witness.cpp @@ -29,6 +29,9 @@ #include #include +#include + +#include #include @@ -74,6 +77,10 @@ void witness_plugin::plugin_set_program_options( DEFAULT_VALUE_VECTOR(std::make_pair(chain::public_key_type(default_priv_key.get_public_key()), graphene::utilities::key_to_wif(default_priv_key))), "Tuple of [PublicKey, WIF private key] (may specify multiple times)") + ("private-key-file", bpo::value>()->composing()->multitoken(), + "Path to a file containing tuples of [PublicKey, WIF private key]." + " The file has to contain exactly one tuple (i.e. private - public key pair) per line." + " This option may be specified multiple times, thus multiple files can be provided.") ; config_file_options.add(command_line_options); } @@ -83,6 +90,32 @@ std::string witness_plugin::plugin_name()const return "witness"; } +void witness_plugin::add_private_key(const std::string& key_id_to_wif_pair_string) +{ + auto key_id_to_wif_pair = graphene::app::dejsonify> + (key_id_to_wif_pair_string, 5); + fc::optional private_key = graphene::utilities::wif_to_key(key_id_to_wif_pair.second); + if (!private_key) + { + // the key isn't in WIF format; see if they are still passing the old native private key format. This is + // just here to ease the transition, can be removed soon + try + { + private_key = fc::variant(key_id_to_wif_pair.second, 2).as(1); + } + catch (const fc::exception&) + { + FC_THROW("Invalid WIF-format private key ${key_string}", ("key_string", key_id_to_wif_pair.second)); + } + } + + if (_private_keys.find(key_id_to_wif_pair.first) == _private_keys.end()) + { + ilog("Public Key: ${public}", ("public", key_id_to_wif_pair.first)); + _private_keys[key_id_to_wif_pair.first] = *private_key; + } +} + void witness_plugin::plugin_initialize(const boost::program_options::variables_map& options) { try { ilog("witness plugin: plugin_initialize() begin"); @@ -94,24 +127,31 @@ void witness_plugin::plugin_initialize(const boost::program_options::variables_m const std::vector key_id_to_wif_pair_strings = options["private-key"].as>(); for (const std::string& key_id_to_wif_pair_string : key_id_to_wif_pair_strings) { - auto key_id_to_wif_pair = graphene::app::dejsonify > - (key_id_to_wif_pair_string, 5); - ilog("Public Key: ${public}", ("public", key_id_to_wif_pair.first)); - fc::optional private_key = graphene::utilities::wif_to_key(key_id_to_wif_pair.second); - if (!private_key) + add_private_key(key_id_to_wif_pair_string); + } + } + if (options.count("private-key-file")) + { + const std::vector key_id_to_wif_pair_files = + options["private-key-file"].as>(); + for (const boost::filesystem::path& key_id_to_wif_pair_file : key_id_to_wif_pair_files) + { + if (fc::exists(key_id_to_wif_pair_file)) { - // the key isn't in WIF format; see if they are still passing the old native private key format. This is - // just here to ease the transition, can be removed soon - try - { - private_key = fc::variant(key_id_to_wif_pair.second, 2).as(1); - } - catch (const fc::exception&) + std::string file_content; + fc::read_file_contents(key_id_to_wif_pair_file, file_content); + std::istringstream file_content_as_stream(file_content); + + std::string line; // key_id_to_wif_pair_string + while (std::getline(file_content_as_stream, line)) { - FC_THROW("Invalid WIF-format private key ${key_string}", ("key_string", key_id_to_wif_pair.second)); + add_private_key(line); } } - _private_keys[key_id_to_wif_pair.first] = *private_key; + else + { + FC_THROW("Failed to load private key file from ${path}", ("path", key_id_to_wif_pair_file.string())); + } } } if(options.count("required-participation"))