Numbers streaming operators

This commit is contained in:
Piotr Wójcik 2025-01-21 13:44:28 +01:00
commit 29863ed4df
14 changed files with 297 additions and 317 deletions

View file

@ -186,73 +186,6 @@ TEST (block, change_serialize_json)
ASSERT_EQ (*block1, block2);
}
TEST (uint512_union, parse_zero)
{
nano::uint512_union input (nano::uint512_t (0));
std::string text;
input.encode_hex (text);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_TRUE (output.number ().is_zero ());
}
TEST (uint512_union, parse_zero_short)
{
std::string text ("0");
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_TRUE (output.number ().is_zero ());
}
TEST (uint512_union, parse_one)
{
nano::uint512_union input (nano::uint512_t (1));
std::string text;
input.encode_hex (text);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (1, output.number ());
}
TEST (uint512_union, parse_error_symbol)
{
nano::uint512_union input (nano::uint512_t (1000));
std::string text;
input.encode_hex (text);
text[5] = '!';
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}
TEST (uint512_union, max)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text;
input.encode_hex (text);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (nano::uint512_t ("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), output.number ());
}
TEST (uint512_union, parse_error_overflow)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text;
input.encode_hex (text);
text.push_back (0);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}
TEST (send_block, deserialize)
{
nano::block_builder builder;

View file

@ -345,8 +345,7 @@ TEST (uint256_union, decode_empty)
TEST (uint256_union, parse_zero)
{
nano::uint256_union input (nano::uint256_t (0));
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
nano::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
@ -366,8 +365,7 @@ TEST (uint256_union, parse_zero_short)
TEST (uint256_union, parse_one)
{
nano::uint256_union input (nano::uint256_t (1));
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
nano::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
@ -378,8 +376,7 @@ TEST (uint256_union, parse_one)
TEST (uint256_union, parse_error_symbol)
{
nano::uint256_union input (nano::uint256_t (1000));
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
text[5] = '!';
nano::uint256_union output;
auto error (output.decode_hex (text));
@ -389,8 +386,7 @@ TEST (uint256_union, parse_error_symbol)
TEST (uint256_union, max_hex)
{
nano::uint256_union input (std::numeric_limits<nano::uint256_t>::max ());
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
nano::uint256_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
@ -409,8 +405,7 @@ TEST (uint256_union, decode_dec)
TEST (uint256_union, max_dec)
{
nano::uint256_union input (std::numeric_limits<nano::uint256_t>::max ());
std::string text;
input.encode_dec (text);
std::string text = input.to_string_dec ();
nano::uint256_union output;
auto error (output.decode_dec (text));
ASSERT_FALSE (error);
@ -445,8 +440,7 @@ TEST (uint256_union, decode_dec_leading_zero)
TEST (uint256_union, parse_error_overflow)
{
nano::uint256_union input (std::numeric_limits<nano::uint256_t>::max ());
std::string text;
input.encode_hex (text);
std::string text = input.to_string ();
text.push_back (0);
nano::uint256_union output;
auto error (output.decode_hex (text));
@ -650,6 +644,68 @@ TEST (uint512_union, hash)
}
}
TEST (uint512_union, parse_zero)
{
nano::uint512_union input (nano::uint512_t (0));
std::string text = input.to_string ();
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_TRUE (output.number ().is_zero ());
}
TEST (uint512_union, parse_zero_short)
{
std::string text ("0");
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_TRUE (output.number ().is_zero ());
}
TEST (uint512_union, parse_one)
{
nano::uint512_union input (nano::uint512_t (1));
std::string text = input.to_string ();
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (1, output.number ());
}
TEST (uint512_union, parse_error_symbol)
{
nano::uint512_union input (nano::uint512_t (1000));
std::string text = input.to_string ();
text[5] = '!';
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}
TEST (uint512_union, max)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text = input.to_string ();
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_FALSE (error);
ASSERT_EQ (input, output);
ASSERT_EQ (nano::uint512_t ("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"), output.number ());
}
TEST (uint512_union, parse_error_overflow)
{
nano::uint512_union input (std::numeric_limits<nano::uint512_t>::max ());
std::string text = input.to_string ();
text.push_back (0);
nano::uint512_union output;
auto error (output.decode_hex (text));
ASSERT_TRUE (error);
}
TEST (sat_math, add_sat)
{
// Test uint128_t
@ -743,4 +799,67 @@ TEST (sat_math, sub_sat)
ASSERT_EQ (nano::sub_sat (hundred, nano::uint512_t (200)), min);
ASSERT_EQ (nano::sub_sat (min, max), min);
}
}
TEST (account, encode_zero)
{
nano::account number0{};
std::stringstream stream;
number0.encode_account (stream);
auto str0 = stream.str ();
/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
ASSERT_EQ (65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}
TEST (account, encode_all)
{
nano::account number0;
number0.decode_hex ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
std::stringstream stream;
number0.encode_account (stream);
auto str0 = stream.str ();
/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}
TEST (account, encode_fail)
{
nano::account number0{};
std::stringstream stream;
number0.encode_account (stream);
auto str0 = stream.str ();
str0[16] ^= 1;
nano::account number1;
ASSERT_TRUE (number1.decode_account (str0));
}
TEST (account, known_addresses)
{
nano::account account1{ "0000000000000000000000000000000000000000000000000000000000000000" };
ASSERT_EQ (account1.to_account (), "nano_1111111111111111111111111111111111111111111111111111hifc8npp");
nano::account account2{ "B0311EA55708D6A53C75CDBF88300259C6D018522FE3D4D0A242E431F9E8B6D0" };
ASSERT_EQ (account2.to_account (), "nano_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo");
nano::account account3{ "45C6FF9D1706D61F0821327752671BDA9F9ED2DA40326B01935AB566FB9E08ED" };
ASSERT_EQ (account3.to_account (), "nano_1jg8zygjg3pp5w644emqcbmjqpnzmubfni3kfe1s8pooeuxsw49fdq1mco9j");
nano::account account4{ "E89208DD038FBB269987689621D52292AE9C35941A7484756ECCED92A65093BA" };
ASSERT_EQ (account4.to_account (), "nano_3t6k35gi95xu6tergt6p69ck76ogmitsa8mnijtpxm9fkcm736xtoncuohr3");
nano::account account5{ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" };
ASSERT_EQ (account5.to_account (), "nano_3zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzc3yoon41");
}

View file

@ -319,48 +319,6 @@ TEST (wallet, rekey)
ASSERT_TRUE (wallet.rekey (transaction, "2"));
}
TEST (account, encode_zero)
{
nano::account number0{};
std::string str0;
number0.encode_account (str0);
/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
ASSERT_EQ (65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}
TEST (account, encode_all)
{
nano::account number0;
number0.decode_hex ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
std::string str0;
number0.encode_account (str0);
/*
* Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts
*/
ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ());
nano::account number1;
ASSERT_FALSE (number1.decode_account (str0));
ASSERT_EQ (number0, number1);
}
TEST (account, encode_fail)
{
nano::account number0{};
std::string str0;
number0.encode_account (str0);
str0[16] ^= 1;
nano::account number1;
ASSERT_TRUE (number1.decode_account (str0));
}
TEST (wallet, hash_password)
{
bool init;

View file

@ -472,17 +472,11 @@ void nano::send_block::serialize_json (std::string & string_a, bool single_line)
void nano::send_block::serialize_json (boost::property_tree::ptree & tree) const
{
tree.put ("type", "send");
std::string previous;
hashables.previous.encode_hex (previous);
tree.put ("previous", previous);
tree.put ("previous", hashables.previous.to_string ());
tree.put ("destination", hashables.destination.to_account ());
std::string balance;
hashables.balance.encode_hex (balance);
tree.put ("balance", balance);
std::string signature_l;
signature.encode_hex (signature_l);
tree.put ("balance", hashables.balance.to_string ());
tree.put ("work", nano::to_string_hex (work));
tree.put ("signature", signature_l);
tree.put ("signature", signature.to_string ());
}
bool nano::send_block::deserialize_json (boost::property_tree::ptree const & tree_a)
@ -836,10 +830,8 @@ void nano::open_block::serialize_json (boost::property_tree::ptree & tree) const
tree.put ("source", hashables.source.to_string ());
tree.put ("representative", hashables.representative.to_account ());
tree.put ("account", hashables.account.to_account ());
std::string signature_l;
signature.encode_hex (signature_l);
tree.put ("work", nano::to_string_hex (work));
tree.put ("signature", signature_l);
tree.put ("signature", signature.to_string ());
}
bool nano::open_block::deserialize_json (boost::property_tree::ptree const & tree_a)
@ -1105,9 +1097,7 @@ void nano::change_block::serialize_json (boost::property_tree::ptree & tree) con
tree.put ("previous", hashables.previous.to_string ());
tree.put ("representative", hashables.representative.to_account ());
tree.put ("work", nano::to_string_hex (work));
std::string signature_l;
signature.encode_hex (signature_l);
tree.put ("signature", signature_l);
tree.put ("signature", signature.to_string ());
}
bool nano::change_block::deserialize_json (boost::property_tree::ptree const & tree_a)
@ -1424,9 +1414,7 @@ void nano::state_block::serialize_json (boost::property_tree::ptree & tree) cons
tree.put ("balance", hashables.balance.to_string_dec ());
tree.put ("link", hashables.link.to_string ());
tree.put ("link_as_account", hashables.link.to_account ());
std::string signature_l;
signature.encode_hex (signature_l);
tree.put ("signature", signature_l);
tree.put ("signature", signature.to_string ());
tree.put ("work", nano::to_string_hex (work));
}
@ -1722,16 +1710,10 @@ void nano::receive_block::serialize_json (std::string & string_a, bool single_li
void nano::receive_block::serialize_json (boost::property_tree::ptree & tree) const
{
tree.put ("type", "receive");
std::string previous;
hashables.previous.encode_hex (previous);
tree.put ("previous", previous);
std::string source;
hashables.source.encode_hex (source);
tree.put ("source", source);
std::string signature_l;
signature.encode_hex (signature_l);
tree.put ("previous", hashables.previous.to_string ());
tree.put ("source", hashables.source.to_string ());
tree.put ("work", nano::to_string_hex (work));
tree.put ("signature", signature_l);
tree.put ("signature", signature.to_string ());
}
bool nano::receive_block::deserialize_json (boost::property_tree::ptree const & tree_a)

View file

@ -5,6 +5,8 @@
#include <nano/lib/utility.hpp>
#include <nano/secure/common.hpp>
#include <boost/io/ios_state.hpp>
#include <crypto/ed25519-donna/ed25519.h>
#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
@ -52,33 +54,40 @@ nano::public_key nano::public_key::from_node_id (std::string const & text)
return result;
}
void nano::public_key::encode_account (std::string & destination_a) const
void nano::public_key::encode_account (std::ostream & os) const
{
debug_assert (destination_a.empty ());
destination_a.reserve (65);
uint64_t check (0);
uint64_t check = 0;
blake2b_state hash;
blake2b_init (&hash, 5);
blake2b_update (&hash, bytes.data (), bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&check), 5);
nano::uint512_t number_l (number ());
nano::uint512_t number_l{ number () };
number_l <<= 40;
number_l |= nano::uint512_t (check);
number_l |= nano::uint512_t{ check };
// Pre-calculate all characters in reverse order
std::array<char, 60> encoded{};
for (auto i (0); i < 60; ++i)
{
uint8_t r (number_l & static_cast<uint8_t> (0x1f));
uint8_t r{ number_l & static_cast<uint8_t> (0x1f) };
number_l >>= 5;
destination_a.push_back (account_encode (r));
encoded[59 - i] = account_encode (r);
}
destination_a.append ("_onan"); // nano_
std::reverse (destination_a.begin (), destination_a.end ());
// Write prefix
os << "nano_";
// Write encoded characters
os.write (encoded.data (), encoded.size ());
}
std::string nano::public_key::to_account () const
{
std::string result;
encode_account (result);
return result;
std::stringstream stream;
encode_account (stream);
return stream.str ();
}
nano::public_key const & nano::public_key::null ()
@ -170,13 +179,6 @@ void nano::uint256_union::encrypt (nano::raw_key const & cleartext, nano::raw_ke
enc.ProcessData (bytes.data (), cleartext.bytes.data (), sizeof (cleartext.bytes));
}
std::string nano::uint256_union::to_string () const
{
std::string result;
encode_hex (result);
return result;
}
nano::uint256_union & nano::uint256_union::operator^= (nano::uint256_union const & other_a)
{
auto j (other_a.qwords.begin ());
@ -204,13 +206,11 @@ nano::uint256_union::uint256_union (std::string const & hex_a)
release_assert (!error);
}
void nano::uint256_union::encode_hex (std::string & text) const
void nano::uint256_union::encode_hex (std::ostream & stream) const
{
debug_assert (text.empty ());
std::stringstream stream;
boost::io::ios_flags_saver ifs{ stream };
stream << std::hex << std::uppercase << std::noshowbase << std::setw (64) << std::setfill ('0');
stream << number ();
text = stream.str ();
}
bool nano::uint256_union::decode_hex (std::string const & text)
@ -242,13 +242,11 @@ bool nano::uint256_union::decode_hex (std::string const & text)
return error;
}
void nano::uint256_union::encode_dec (std::string & text) const
void nano::uint256_union::encode_dec (std::ostream & stream) const
{
debug_assert (text.empty ());
std::stringstream stream;
boost::io::ios_flags_saver ifs{ stream };
stream << std::dec << std::noshowbase;
stream << number ();
text = stream.str ();
}
bool nano::uint256_union::decode_dec (std::string const & text)
@ -276,13 +274,29 @@ bool nano::uint256_union::decode_dec (std::string const & text)
return error;
}
void nano::uint512_union::encode_hex (std::string & text) const
std::string nano::uint256_union::to_string () const
{
debug_assert (text.empty ());
std::stringstream stream;
encode_hex (stream);
return stream.str ();
}
std::string nano::uint256_union::to_string_dec () const
{
std::stringstream stream;
encode_dec (stream);
return stream.str ();
}
/*
* uint512_union
*/
void nano::uint512_union::encode_hex (std::ostream & stream) const
{
boost::io::ios_flags_saver ifs{ stream };
stream << std::hex << std::uppercase << std::noshowbase << std::setw (128) << std::setfill ('0');
stream << number ();
text = stream.str ();
}
bool nano::uint512_union::decode_hex (std::string const & text)
@ -312,11 +326,15 @@ bool nano::uint512_union::decode_hex (std::string const & text)
std::string nano::uint512_union::to_string () const
{
std::string result;
encode_hex (result);
return result;
std::stringstream stream;
encode_hex (stream);
return stream.str ();
}
/*
* raw_key
*/
nano::raw_key::~raw_key ()
{
secure_wipe_memory (bytes.data (), bytes.size ());
@ -371,19 +389,21 @@ bool nano::validate_message (nano::public_key const & public_key, nano::uint256_
return validate_message (public_key, message.bytes.data (), sizeof (message.bytes), signature);
}
/*
* uint128_union
*/
nano::uint128_union::uint128_union (std::string const & string_a)
{
auto error (decode_hex (string_a));
release_assert (!error);
}
void nano::uint128_union::encode_hex (std::string & text) const
void nano::uint128_union::encode_hex (std::ostream & stream) const
{
debug_assert (text.empty ());
std::stringstream stream;
boost::io::ios_flags_saver ifs{ stream };
stream << std::hex << std::uppercase << std::noshowbase << std::setw (32) << std::setfill ('0');
stream << number ();
text = stream.str ();
}
bool nano::uint128_union::decode_hex (std::string const & text)
@ -411,13 +431,11 @@ bool nano::uint128_union::decode_hex (std::string const & text)
return error;
}
void nano::uint128_union::encode_dec (std::string & text) const
void nano::uint128_union::encode_dec (std::ostream & stream) const
{
debug_assert (text.empty ());
std::stringstream stream;
boost::io::ios_flags_saver ifs{ stream };
stream << std::dec << std::noshowbase;
stream << number ();
text = stream.str ();
}
bool nano::uint128_union::decode_dec (std::string const & text, bool decimal)
@ -525,6 +543,24 @@ bool nano::uint128_union::decode_dec (std::string const & text, nano::uint128_t
return error;
}
std::string nano::uint128_union::to_string () const
{
std::stringstream stream;
encode_hex (stream);
return stream.str ();
}
std::string nano::uint128_union::to_string_dec () const
{
std::stringstream stream;
encode_dec (stream);
return stream.str ();
}
/*
*
*/
void format_frac (std::ostringstream & stream, nano::uint128_t value, nano::uint128_t scale, int precision)
{
auto reduce = scale;
@ -651,20 +687,6 @@ std::string nano::uint128_union::format_balance (nano::uint128_t scale, int prec
return ::format_balance (number (), scale, precision, group_digits, thousands_sep, decimal_point, grouping);
}
std::string nano::uint128_union::to_string () const
{
std::string result;
encode_hex (result);
return result;
}
std::string nano::uint128_union::to_string_dec () const
{
std::string result;
encode_dec (result);
return result;
}
bool nano::hash_or_account::decode_hex (std::string const & text_a)
{
return raw.decode_hex (text_a);
@ -685,6 +707,10 @@ std::string nano::hash_or_account::to_account () const
return account.to_account ();
}
/*
*
*/
std::string nano::to_string_hex (uint64_t const value_a)
{
std::stringstream stream;
@ -738,34 +764,40 @@ std::string nano::to_string (double const value_a, int const precision_a)
return stream.str ();
}
std::ostream & nano::operator<< (std::ostream & os, const uint128_union & val)
std::ostream & nano::operator<< (std::ostream & os, const nano::uint128_union & val)
{
// TODO: Replace with streaming implementation
os << val.to_string ();
val.encode_hex (os);
return os;
}
std::ostream & nano::operator<< (std::ostream & os, const uint256_union & val)
std::ostream & nano::operator<< (std::ostream & os, const nano::uint256_union & val)
{
// TODO: Replace with streaming implementation
os << val.to_string ();
val.encode_hex (os);
return os;
}
std::ostream & nano::operator<< (std::ostream & os, const uint512_union & val)
std::ostream & nano::operator<< (std::ostream & os, const nano::uint512_union & val)
{
// TODO: Replace with streaming implementation
os << val.to_string ();
val.encode_hex (os);
return os;
}
std::ostream & nano::operator<< (std::ostream & os, const hash_or_account & val)
std::ostream & nano::operator<< (std::ostream & os, const nano::hash_or_account & val)
{
// TODO: Replace with streaming implementation
os << val.to_string ();
val.raw.encode_hex (os);
return os;
}
std::ostream & nano::operator<< (std::ostream & os, const nano::account & val)
{
val.encode_account (os);
return os;
}
/*
*
*/
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4146) // warning C4146: unary minus operator applied to unsigned type, result still unsigned

View file

@ -49,9 +49,9 @@ public:
*/
explicit uint128_union (std::string const &);
void encode_hex (std::string &) const;
void encode_hex (std::ostream &) const;
bool decode_hex (std::string const &);
void encode_dec (std::string &) const;
void encode_dec (std::ostream &) const;
bool decode_dec (std::string const &, bool = false);
bool decode_dec (std::string const &, nano::uint128_t);
@ -151,9 +151,9 @@ public:
uint256_union & operator^= (uint256_union const &);
uint256_union operator^ (uint256_union const &) const;
void encode_hex (std::string &) const;
void encode_hex (std::ostream &) const;
bool decode_hex (std::string const &);
void encode_dec (std::string &) const;
void encode_dec (std::ostream &) const;
bool decode_dec (std::string const &);
void clear ()
@ -173,6 +173,7 @@ public:
}
std::string to_string () const;
std::string to_string_dec () const;
public:
union
@ -236,7 +237,7 @@ public:
static const public_key & null ();
bool decode_node_id (std::string const &);
void encode_account (std::string &) const;
void encode_account (std::ostream &) const;
bool decode_account (std::string const &);
std::string to_node_id () const;
@ -427,7 +428,7 @@ public:
return *this;
}
void encode_hex (std::string &) const;
void encode_hex (std::ostream &) const;
bool decode_hex (std::string const &);
void clear ()
@ -519,6 +520,7 @@ std::ostream & operator<< (std::ostream &, const uint128_union &);
std::ostream & operator<< (std::ostream &, const uint256_union &);
std::ostream & operator<< (std::ostream &, const uint512_union &);
std::ostream & operator<< (std::ostream &, const hash_or_account &);
std::ostream & operator<< (std::ostream &, const account &);
/**
* Convert a double to string in fixed format

View file

@ -24,10 +24,7 @@ nano::error nano::wallet_config::parse (std::string const & wallet_a, std::strin
nano::error nano::wallet_config::serialize_toml (nano::tomlconfig & toml) const
{
std::string wallet_string;
wallet.encode_hex (wallet_string);
toml.put ("wallet", wallet_string, "Wallet identifier\ntype:string,hex");
toml.put ("wallet", wallet.to_string (), "Wallet identifier\ntype:string,hex");
toml.put ("account", account.to_account (), "Current wallet account\ntype:string,account");
return toml.get_error ();
}

View file

@ -446,8 +446,7 @@ int main (int argc, char * const * argv)
using time_point = std::chrono::system_clock::time_point;
time_point ts (std::chrono::duration_cast<time_point::duration> (std::chrono::nanoseconds (i->first)));
std::time_t timestamp = std::chrono::system_clock::to_time_t (ts);
std::string weight;
i->second.encode_dec (weight);
std::string weight = i->second.to_string_dec ();
std::cout << boost::str (boost::format ("Timestamp %1% Weight %2%\n") % ctime (&timestamp) % weight);
}
}

View file

@ -14,7 +14,7 @@ std::unique_ptr<nanoapi::BlockStateT> nano::ipc::flatbuffers_builder::from (nano
block->balance = block_a.balance ().to_string_dec ();
block->link = block_a.link_field ().value ().to_string ();
block->link_as_account = block_a.link_field ().value ().to_account ();
block_a.signature.encode_hex (block->signature);
block->signature = block_a.signature.to_string ();
block->work = nano::to_string_hex (block_a.work);
if (is_state_send_a)
@ -43,7 +43,7 @@ std::unique_ptr<nanoapi::BlockSendT> nano::ipc::flatbuffers_builder::from (nano:
block->balance = block_a.balance ().to_string_dec ();
block->destination = block_a.hashables.destination.to_account ();
block->previous = block_a.previous ().to_string ();
block_a.signature.encode_hex (block->signature);
block->signature = block_a.signature.to_string ();
block->work = nano::to_string_hex (block_a.work);
return block;
}
@ -54,7 +54,7 @@ std::unique_ptr<nanoapi::BlockReceiveT> nano::ipc::flatbuffers_builder::from (na
block->hash = block_a.hash ().to_string ();
block->source = block_a.source_field ().value ().to_string ();
block->previous = block_a.previous ().to_string ();
block_a.signature.encode_hex (block->signature);
block->signature = block_a.signature.to_string ();
block->work = nano::to_string_hex (block_a.work);
return block;
}
@ -66,7 +66,7 @@ std::unique_ptr<nanoapi::BlockOpenT> nano::ipc::flatbuffers_builder::from (nano:
block->source = block_a.source_field ().value ().to_string ();
block->account = block_a.account ().to_account ();
block->representative = block_a.representative_field ().value ().to_account ();
block_a.signature.encode_hex (block->signature);
block->signature = block_a.signature.to_string ();
block->work = nano::to_string_hex (block_a.work);
return block;
}
@ -77,7 +77,7 @@ std::unique_ptr<nanoapi::BlockChangeT> nano::ipc::flatbuffers_builder::from (nan
block->hash = block_a.hash ().to_string ();
block->previous = block_a.previous ().to_string ();
block->representative = block_a.representative_field ().value ().to_account ();
block_a.signature.encode_hex (block->signature);
block->signature = block_a.signature.to_string ();
block->work = nano::to_string_hex (block_a.work);
return block;
}

View file

@ -618,8 +618,7 @@ void nano::json_handler::account_info ()
response_l.put ("open_block", info.open_block.to_string ());
response_l.put ("representative_block", node.ledger.representative (transaction, info.head).to_string ());
nano::amount balance_l (info.balance);
std::string balance;
balance_l.encode_dec (balance);
std::string balance = balance_l.to_string_dec ();
response_l.put ("balance", balance);
@ -635,8 +634,7 @@ void nano::json_handler::account_info ()
// block_height and confirmed height are the same, so can just reuse balance
confirmed_balance_l = balance_l;
}
std::string confirmed_balance;
confirmed_balance_l.encode_dec (confirmed_balance);
std::string confirmed_balance = confirmed_balance_l.to_string_dec ();
response_l.put ("confirmed_balance", confirmed_balance);
}
@ -2181,8 +2179,7 @@ void nano::json_handler::delegators ()
{
if (info.balance.number () >= threshold.number ())
{
std::string balance;
nano::uint128_union (info.balance).encode_dec (balance);
std::string balance = nano::uint128_union (info.balance).to_string_dec ();
nano::account const & delegator (i->first);
delegators.put (delegator.to_account (), balance);
}
@ -2734,8 +2731,7 @@ void nano::json_handler::ledger ()
response_a.put ("frontier", info.head.to_string ());
response_a.put ("open_block", info.open_block.to_string ());
response_a.put ("representative_block", node.ledger.representative (transaction, info.head).to_string ());
std::string balance;
nano::uint128_union (info.balance).encode_dec (balance);
std::string balance = nano::uint128_union (info.balance).to_string_dec ();
response_a.put ("balance", balance);
response_a.put ("modified_timestamp", std::to_string (info.modified));
response_a.put ("block_count", std::to_string (info.block_count));
@ -2787,8 +2783,7 @@ void nano::json_handler::ledger ()
response_a.put ("frontier", info.head.to_string ());
response_a.put ("open_block", info.open_block.to_string ());
response_a.put ("representative_block", node.ledger.representative (transaction, info.head).to_string ());
std::string balance;
(i->first).encode_dec (balance);
std::string balance = (i->first).to_string_dec ();
response_a.put ("balance", balance);
response_a.put ("modified_timestamp", std::to_string (info.modified));
response_a.put ("block_count", std::to_string (info.block_count));
@ -4650,8 +4645,7 @@ void nano::json_handler::wallet_ledger ()
entry.put ("frontier", info->head.to_string ());
entry.put ("open_block", info->open_block.to_string ());
entry.put ("representative_block", node.ledger.representative (block_transaction, info->head).to_string ());
std::string balance;
nano::uint128_union (info->balance).encode_dec (balance);
std::string balance = nano::uint128_union (info->balance).to_string_dec ();
entry.put ("balance", balance);
entry.put ("modified_timestamp", std::to_string (info->modified));
entry.put ("block_count", std::to_string (info->block_count));

View file

@ -1935,11 +1935,9 @@ void nano_qt::advanced_actions::refresh_ledger ()
QList<QStandardItem *> items;
items.push_back (new QStandardItem (QString (i->first.to_account ().c_str ())));
nano::account_info const & info (i->second);
std::string balance;
nano::amount (info.balance.number () / wallet.rendering_ratio).encode_dec (balance);
std::string balance = nano::amount (info.balance.number () / wallet.rendering_ratio).to_string_dec ();
items.push_back (new QStandardItem (QString (balance.c_str ())));
std::string block_hash;
info.head.encode_hex (block_hash);
std::string block_hash = info.head.to_string ();
items.push_back (new QStandardItem (QString (block_hash.c_str ())));
ledger_model->appendRow (items);
}

View file

@ -373,12 +373,9 @@ TEST (wallet, DISABLED_process_block)
QTest::mouseClick (wallet->advanced.enter_block, Qt::LeftButton);
ASSERT_EQ (wallet->block_entry.window, wallet->main_stack->currentWidget ());
nano::send_block send (latest, key1.pub, 0, nano::dev::genesis_key.prv, nano::dev::genesis_key.pub, *system.work.generate (latest));
std::string previous;
send.hashables.previous.encode_hex (previous);
std::string balance;
send.hashables.balance.encode_hex (balance);
std::string signature;
send.signature.encode_hex (signature);
std::string previous = send.hashables.previous.to_string ();
std::string balance = send.hashables.balance.to_string ();
std::string signature = send.signature.to_string ();
std::string block_json;
send.serialize_json (block_json);
block_json.erase (std::remove (block_json.begin (), block_json.end (), '\n'), block_json.end ());

View file

@ -101,8 +101,7 @@ void nano::rpc_handler::process_request (nano::rpc_handler_request_params const
{
nano::uint128_union random_id;
nano::random_pool::generate_block (random_id.bytes.data (), random_id.bytes.size ());
std::string random_id_text;
random_id.encode_hex (random_id_text);
std::string random_id_text = random_id.to_string ();
request.put ("id", random_id_text);
std::stringstream ostream;
boost::property_tree::write_json (ostream, request);

View file

@ -192,8 +192,7 @@ TEST (rpc, wallet_contains)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_contains");
request.put ("account", nano::dev::genesis_key.pub.to_account ());
@ -208,8 +207,7 @@ TEST (rpc, wallet_doesnt_contain)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_contains");
request.put ("account", nano::dev::genesis_key.pub.to_account ());
@ -236,8 +234,7 @@ TEST (rpc, validate_account_invalid)
nano::test::system system;
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
std::string account;
nano::dev::genesis_key.pub.encode_account (account);
std::string account = nano::dev::genesis_key.pub.to_account ();
account[0] ^= 0x1;
boost::property_tree::ptree request;
request.put ("action", "validate_account_number");
@ -254,8 +251,7 @@ TEST (rpc, send)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", nano::dev::genesis_key.pub.to_account ());
@ -277,8 +273,7 @@ TEST (rpc, send_fail)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", nano::dev::genesis_key.pub.to_account ());
@ -295,8 +290,7 @@ TEST (rpc, send_work)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", nano::dev::genesis_key.pub.to_account ());
@ -324,8 +318,7 @@ TEST (rpc, send_work_disabled)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", nano::dev::genesis_key.pub.to_account ());
@ -342,8 +335,7 @@ TEST (rpc, send_idempotent)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", nano::dev::genesis_key.pub.to_account ());
@ -384,8 +376,7 @@ TEST (rpc, send_epoch_2)
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "send");
request.put ("source", nano::dev::genesis_key.pub.to_account ());
@ -437,11 +428,9 @@ TEST (rpc, wallet_add)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
nano::keypair key1;
std::string key_text;
key1.prv.encode_hex (key_text);
std::string key_text = key1.prv.to_string ();
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_add");
request.put ("key", key_text);
@ -457,8 +446,7 @@ TEST (rpc, wallet_password_valid)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "password_valid");
auto response (wait_response (system, rpc_ctx, request));
@ -472,8 +460,7 @@ TEST (rpc, wallet_password_change)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "password_change");
request.put ("password", "test");
@ -502,8 +489,7 @@ TEST (rpc, wallet_password_enter)
system.wallet (0)->store.password.value (password_l);
}
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "password_enter");
request.put ("password", "");
@ -518,8 +504,7 @@ TEST (rpc, wallet_representative)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_representative");
auto response (wait_response (system, rpc_ctx, request));
@ -533,8 +518,7 @@ TEST (rpc, wallet_representative_set)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
nano::keypair key;
request.put ("action", "wallet_representative_set");
@ -551,8 +535,7 @@ TEST (rpc, wallet_representative_set_force)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
nano::keypair key;
request.put ("action", "wallet_representative_set");
@ -585,8 +568,7 @@ TEST (rpc, account_list)
system.wallet (0)->insert_adhoc (key2.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "account_list");
auto response (wait_response (system, rpc_ctx, request));
@ -613,8 +595,7 @@ TEST (rpc, wallet_key_valid)
system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_key_valid");
auto response (wait_response (system, rpc_ctx, request));
@ -2468,8 +2449,7 @@ TEST (rpc, account_representative_set_epoch_2_insufficient_work)
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "account_representative_set");
request.put ("account", nano::dev::genesis_key.pub.to_account ());
@ -3923,8 +3903,7 @@ TEST (rpc, json_block_input)
boost::property_tree::ptree request;
request.put ("action", "sign");
request.put ("json_block", "true");
std::string wallet;
node1->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node1->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("account", key.pub.to_account ());
boost::property_tree::ptree json;
@ -4574,8 +4553,7 @@ TEST (rpc, block_create)
boost::property_tree::ptree request1;
request1.put ("action", "block_create");
request1.put ("type", "open");
std::string key_text;
key.prv.encode_hex (key_text);
std::string key_text = key.prv.to_string ();
request1.put ("key", key_text);
request1.put ("representative", nano::dev::genesis_key.pub.to_account ());
request1.put ("source", send->hash ().to_string ());
@ -4925,8 +4903,7 @@ TEST (rpc, wallet_lock)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
{
auto transaction (system.wallet (0)->wallets.tx_begin_read ());
ASSERT_TRUE (system.wallet (0)->store.valid_password (transaction));
@ -4946,8 +4923,7 @@ TEST (rpc, wallet_locked)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_locked");
auto response (wait_response (system, rpc_ctx, request));
@ -5052,8 +5028,7 @@ TEST (rpc, wallet_add_watch)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);
boost::property_tree::ptree request;
std::string wallet;
node->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("action", "wallet_add_watch");
boost::property_tree::ptree entry;
@ -5678,8 +5653,7 @@ TEST (rpc, sign_block)
auto const rpc_ctx = add_rpc (system, node1);
boost::property_tree::ptree request;
request.put ("action", "sign");
std::string wallet;
node1->wallets.items.begin ()->first.encode_hex (wallet);
std::string wallet = node1->wallets.items.begin ()->first.to_string ();
request.put ("wallet", wallet);
request.put ("account", key.pub.to_account ());
std::string json;
@ -6416,8 +6390,7 @@ TEST (rpc, receive)
nano::test::system system;
auto node = add_ipc_enabled_node (system);
auto wallet = system.wallet (0);
std::string wallet_text;
node->wallets.items.begin ()->first.encode_hex (wallet_text);
std::string wallet_text = node->wallets.items.begin ()->first.to_string ();
wallet->insert_adhoc (nano::dev::genesis_key.prv);
nano::keypair key1;
wallet->insert_adhoc (key1.prv);
@ -6457,8 +6430,7 @@ TEST (rpc, receive_unopened)
nano::test::system system;
auto node = add_ipc_enabled_node (system);
auto wallet = system.wallet (0);
std::string wallet_text;
node->wallets.items.begin ()->first.encode_hex (wallet_text);
std::string wallet_text = node->wallets.items.begin ()->first.to_string ();
wallet->insert_adhoc (nano::dev::genesis_key.prv);
// Test receiving for unopened account
nano::keypair key1;
@ -6515,8 +6487,7 @@ TEST (rpc, receive_work_disabled)
config.work_threads = 0;
auto node = add_ipc_enabled_node (system, config);
auto wallet = system.wallet (1);
std::string wallet_text;
node->wallets.items.begin ()->first.encode_hex (wallet_text);
std::string wallet_text = node->wallets.items.begin ()->first.to_string ();
wallet->insert_adhoc (nano::dev::genesis_key.prv);
nano::keypair key1;
ASSERT_TRUE (worker_node.work_generation_enabled ());
@ -6549,8 +6520,7 @@ TEST (rpc, receive_pruned)
auto node2 = add_ipc_enabled_node (system, node_config, node_flags);
auto wallet1 = system.wallet (0);
auto wallet2 = system.wallet (1);
std::string wallet_text;
node2->wallets.items.begin ()->first.encode_hex (wallet_text);
std::string wallet_text = node2->wallets.items.begin ()->first.to_string ();
wallet1->insert_adhoc (nano::dev::genesis_key.prv);
nano::keypair key1;
wallet2->insert_adhoc (key1.prv);