Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

witness node accepts private key from file #1812

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
68 changes: 54 additions & 14 deletions libraries/plugins/witness/witness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include <graphene/utilities/key_conversion.hpp>

#include <fc/thread/thread.hpp>
#include <fc/io/fstream.hpp>

#include <boost/filesystem/path.hpp>

#include <iostream>

Expand Down Expand Up @@ -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<vector<boost::filesystem::path>>()->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);
}
Expand All @@ -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<std::pair<chain::public_key_type, std::string>>
(key_id_to_wif_pair_string, 5);
fc::optional<fc::ecc::private_key> 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<fc::ecc::private_key>(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");
Expand All @@ -94,24 +127,31 @@ void witness_plugin::plugin_initialize(const boost::program_options::variables_m
const std::vector<std::string> key_id_to_wif_pair_strings = options["private-key"].as<std::vector<std::string>>();
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<std::pair<chain::public_key_type, std::string> >
(key_id_to_wif_pair_string, 5);
ilog("Public Key: ${public}", ("public", key_id_to_wif_pair.first));
fc::optional<fc::ecc::private_key> 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<boost::filesystem::path> key_id_to_wif_pair_files =
options["private-key-file"].as<std::vector<boost::filesystem::path>>();
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<fc::ecc::private_key>(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"))
Expand Down