From 50ffa31643c2a54d474fb3e4f349f2177e7899bd Mon Sep 17 00:00:00 2001 From: Wesley Shillingford Date: Wed, 17 Apr 2019 18:24:58 +0100 Subject: [PATCH] Emit "nano_" prefixed addresses (#1771) * Emit "nano_" prefixed addresses * Ensure tests pass with both "nano_" and "xrb_" prefixed accounts * Update buffer * Simplify wallet test logic --- nano/core_test/interface.cpp | 14 ++++++++++++-- nano/core_test/uint256_union.cpp | 28 +++++++++++++++++++++++----- nano/core_test/wallet.cpp | 13 +++++++++++-- nano/lib/numbers.cpp | 4 ++-- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/nano/core_test/interface.cpp b/nano/core_test/interface.cpp index 560cd3b4..8ad69dcf 100644 --- a/nano/core_test/interface.cpp +++ b/nano/core_test/interface.cpp @@ -26,9 +26,16 @@ TEST (interface, xrb_uint256_to_string) TEST (interface, xrb_uint256_to_address) { nano::uint256_union zero (0); - char text[65] = { 0 }; + char text[66] = { 0 }; xrb_uint256_to_address (zero.bytes.data (), text); - ASSERT_STREQ ("xrb_1111111111111111111111111111111111111111111111111111hifc8npp", text); + + /* + * Handle both "xrb_" and "nano_" results, since it is not + * specified which is returned + */ + auto account_alpha = "1111111111111111111111111111111111111111111111111111hifc8npp"; + auto prefix = text[0] == 'x' ? "xrb" : "nano"; + ASSERT_STREQ (boost::str (boost::format ("%1%_%2%") % prefix % account_alpha).c_str (), text); } TEST (interface, xrb_uint512_to_string) @@ -68,6 +75,9 @@ TEST (interface, xrb_valid_address) ASSERT_EQ (0, xrb_valid_address ("xrb_1111111111111111111111111111111111111111111111111111hifc8npp")); ASSERT_EQ (1, xrb_valid_address ("xrb_1111111111111111111111111111111111111111111111111111hifc8nppp")); ASSERT_EQ (1, xrb_valid_address ("xrb_1111111211111111111111111111111111111111111111111111hifc8npp")); + ASSERT_EQ (0, xrb_valid_address ("nano_1111111111111111111111111111111111111111111111111111hifc8npp")); + ASSERT_EQ (1, xrb_valid_address ("nano_1111111111111111111111111111111111111111111111111111hifc8nppp")); + ASSERT_EQ (1, xrb_valid_address ("nano_1111111211111111111111111111111111111111111111111111hifc8npp")); } TEST (interface, xrb_seed_create) diff --git a/nano/core_test/uint256_union.cpp b/nano/core_test/uint256_union.cpp index 2273a1fc..bce9dd25 100644 --- a/nano/core_test/uint256_union.cpp +++ b/nano/core_test/uint256_union.cpp @@ -386,11 +386,17 @@ TEST (uint256_union, decode_account_variations) char account[66] = { 0 }; xrb_uint256_to_address (pub.bytes.data (), account); + /* + * Handle different offsets for the underscore separator + * for "xrb_" prefixed and "nano_" prefixed accounts + */ + unsigned offset = (account[0] == 'x') ? 4 : 5; + // Replace first digit after xrb_ with '0'..'9', make sure only one of them is valid int errors = 0; for (int variation = 0; variation < 10; variation++) { - account[4] = static_cast (variation + 48); + account[offset] = static_cast (variation + 48); errors += xrb_valid_address (account); } @@ -404,8 +410,14 @@ TEST (uint256_union, account_transcode) auto text (nano::test_genesis_key.pub.to_account ()); ASSERT_FALSE (value.decode_account (text)); ASSERT_EQ (nano::test_genesis_key.pub, value); - ASSERT_EQ ('_', text[3]); - text[3] = '-'; + + /* + * Handle different offsets for the underscore separator + * for "xrb_" prefixed and "nano_" prefixed accounts + */ + unsigned offset = (text.front () == 'x') ? 3 : 4; + ASSERT_EQ ('_', text[offset]); + text[offset] = '-'; nano::uint256_union value2; ASSERT_FALSE (value2.decode_account (text)); ASSERT_EQ (value, value2); @@ -416,9 +428,15 @@ TEST (uint256_union, account_encode_lex) nano::uint256_union min ("0000000000000000000000000000000000000000000000000000000000000000"); nano::uint256_union max ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); auto min_text (min.to_account ()); - ASSERT_EQ (64, min_text.size ()); auto max_text (max.to_account ()); - ASSERT_EQ (64, max_text.size ()); + + /* + * Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts + */ + unsigned length = (min_text.front () == 'x') ? 64 : 65; + ASSERT_EQ (length, min_text.size ()); + ASSERT_EQ (length, max_text.size ()); + auto previous (min_text); for (auto i (1); i != 1000; ++i) { diff --git a/nano/core_test/wallet.cpp b/nano/core_test/wallet.cpp index 297ce734..cb52f9c4 100644 --- a/nano/core_test/wallet.cpp +++ b/nano/core_test/wallet.cpp @@ -315,7 +315,12 @@ TEST (account, encode_zero) nano::uint256_union number0 (0); std::string str0; number0.encode_account (str0); - ASSERT_EQ (64, str0.size ()); + + /* + * 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::uint256_union number1; ASSERT_FALSE (number1.decode_account (str0)); ASSERT_EQ (number0, number1); @@ -327,7 +332,11 @@ TEST (account, encode_all) number0.decode_hex ("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); std::string str0; number0.encode_account (str0); - ASSERT_EQ (64, str0.size ()); + + /* + * Handle different lengths for "xrb_" prefixed and "nano_" prefixed accounts + */ + ASSERT_EQ ((str0.front () == 'x') ? 64 : 65, str0.size ()); nano::uint256_union number1; ASSERT_FALSE (number1.decode_account (str0)); ASSERT_EQ (number0, number1); diff --git a/nano/lib/numbers.cpp b/nano/lib/numbers.cpp index 0418b2d2..8431bd4c 100644 --- a/nano/lib/numbers.cpp +++ b/nano/lib/numbers.cpp @@ -43,7 +43,7 @@ uint8_t account_decode (char value) void nano::uint256_union::encode_account (std::string & destination_a) const { assert (destination_a.empty ()); - destination_a.reserve (64); + destination_a.reserve (65); uint64_t check (0); blake2b_state hash; blake2b_init (&hash, 5); @@ -58,7 +58,7 @@ void nano::uint256_union::encode_account (std::string & destination_a) const number_l >>= 5; destination_a.push_back (account_encode (r)); } - destination_a.append ("_brx"); // xrb_ + destination_a.append ("_onan"); // nano_ std::reverse (destination_a.begin (), destination_a.end ()); }