From 6e87c4ca7833c9a24bff7ac9620563ed0c1a6b3d Mon Sep 17 00:00:00 2001 From: clemahieu Date: Sun, 26 Oct 2014 11:43:13 -0500 Subject: [PATCH] Changing decode base. Adding wallet_key_valid rpc. --- rai/core/core.cpp | 17 ++++++++++++++++- rai/secure.cpp | 30 ++++++++++++++++++++++++++++++ rai/secure.hpp | 2 ++ rai/test/test_network.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) diff --git a/rai/core/core.cpp b/rai/core/core.cpp index 1ef0ccd3..e8c83017 100644 --- a/rai/core/core.cpp +++ b/rai/core/core.cpp @@ -1684,6 +1684,21 @@ void rai::rpc::operator () (boost::network::http::server ::request con response.content = "RPC control is disabled"; } } + else if (action == "wallet_key_valid") + { + if (enable_control) + { + auto valid (client.wallet.valid_password ()); + boost::property_tree::ptree response_l; + response_l.put ("valid", valid ? "1" : "0"); + set_response (response, response_l); + } + else + { + response = boost::network::http::server::response::stock_reply (boost::network::http::server::response::bad_request); + response.content = "RPC control is disabled"; + } + } else if (action == "validate_account") { std::string account_text (request_l.get ("account")); @@ -1704,7 +1719,7 @@ void rai::rpc::operator () (boost::network::http::server ::request con { std::string amount_text (request_l.get ("amount")); rai::amount amount; - auto error (amount.decode_hex (amount_text)); + auto error (amount.decode_dec (amount_text)); if (!error) { auto error (client.send (account, amount.number ())); diff --git a/rai/secure.cpp b/rai/secure.cpp index 6f1f5782..6a298f31 100644 --- a/rai/secure.cpp +++ b/rai/secure.cpp @@ -161,6 +161,36 @@ bool rai::uint128_union::decode_hex (std::string const & text) return result; } +void rai::uint128_union::encode_dec (std::string & text) const +{ + assert (text.empty ()); + std::stringstream stream; + stream << std::dec << std::noshowbase; + stream << number (); + text = stream.str (); +} + +bool rai::uint128_union::decode_dec (std::string const & text) +{ + auto result (text.size () > 39); + if (!result) + { + std::stringstream stream (text); + stream << std::dec << std::noshowbase; + rai::uint128_t number_l; + try + { + stream >> number_l; + *this = number_l; + } + catch (std::runtime_error &) + { + result = true; + } + } + return result; +} + void rai::uint128_union::clear () { qwords.fill (0); diff --git a/rai/secure.hpp b/rai/secure.hpp index 7c450fc4..d9145776 100644 --- a/rai/secure.hpp +++ b/rai/secure.hpp @@ -43,6 +43,8 @@ namespace rai bool operator == (rai::uint128_union const &) const; void encode_hex (std::string &) const; bool decode_hex (std::string const &); + void encode_dec (std::string &) const; + bool decode_dec (std::string const &); rai::uint128_t number () const; void clear (); std::array bytes; diff --git a/rai/test/test_network.cpp b/rai/test/test_network.cpp index 53dd1386..fbb8db7d 100644 --- a/rai/test/test_network.cpp +++ b/rai/test/test_network.cpp @@ -749,6 +749,31 @@ TEST (rpc, wallet_list) } } +TEST (rpc, wallet_key_valid) +{ + rai::system system (24000, 1); + auto pool (boost::make_shared ()); + rai::rpc rpc (system.service, pool, 25000, *system.clients [0], true); + std::string account; + rai::test_genesis_key.pub.encode_base58check (account); + system.clients [0]->wallet.insert (rai::test_genesis_key.prv); + boost::network::http::server ::request request; + boost::network::http::server ::response response; + request.method = "POST"; + boost::property_tree::ptree request_tree; + request_tree.put ("action", "wallet_key_valid"); + std::stringstream ostream; + boost::property_tree::write_json (ostream, request_tree); + request.body = ostream.str (); + rpc (request, response); + ASSERT_EQ (boost::network::http::server ::response::ok, response.status); + boost::property_tree::ptree response_tree; + std::stringstream istream (response.content); + boost::property_tree::read_json (istream, response_tree); + std::string exists_text (response_tree.get ("valid")); + ASSERT_EQ ("1", exists_text); +} + TEST (parse_endpoint, valid) { std::string string ("127.0.0.1:24000");