From ae23b7730661e095375a1608837d4f8d4d7af983 Mon Sep 17 00:00:00 2001 From: clemahieu Date: Mon, 7 May 2018 22:40:48 +0100 Subject: [PATCH] Supporting nano_ and xrb_ prefixes. (#854) (reverted from commit ec7432bbbff4352686fc09e3e52ea6df0ea951ef) --- rai/core_test/uint256_union.cpp | 7 ----- rai/lib/numbers.cpp | 51 +++++++++++++-------------------- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/rai/core_test/uint256_union.cpp b/rai/core_test/uint256_union.cpp index 71ff1ec756..cffe1e822b 100644 --- a/rai/core_test/uint256_union.cpp +++ b/rai/core_test/uint256_union.cpp @@ -314,13 +314,6 @@ TEST (uint256_union, decode_account_v1) ASSERT_EQ (rai::rai_test_account, key); } -TEST (uint256_union, decode_nano_variant) -{ - rai::uint256_union key; - ASSERT_FALSE (key.decode_account ("xrb_1111111111111111111111111111111111111111111111111111hifc8npp")); - ASSERT_FALSE (key.decode_account ("nano_1111111111111111111111111111111111111111111111111111hifc8npp")); -} - TEST (uint256_union, decode_account_variations) { for (int i = 0; i < 100; i++) diff --git a/rai/lib/numbers.cpp b/rai/lib/numbers.cpp index 07a1e4a8f9..146b6f063a 100644 --- a/rai/lib/numbers.cpp +++ b/rai/lib/numbers.cpp @@ -115,48 +115,37 @@ bool rai::uint256_union::decode_account_v1 (std::string const & source_a) bool rai::uint256_union::decode_account (std::string const & source_a) { - auto error (source_a.size () != 64 && source_a.size () != 65); + auto error (source_a.size () != 64); if (!error) { - auto xrb_prefix (source_a[0] == 'x' && source_a[1] == 'r' && source_a[2] == 'b' && (source_a[3] == '_' || source_a[3] == '-')); - auto nano_prefix (source_a[0] == 'n' && source_a[1] == 'a' && source_a[2] == 'n' && source_a[3] == 'o' && (source_a[4] == '_' || source_a[4] == '-')); - if (xrb_prefix || nano_prefix) + if (source_a[0] == 'x' && source_a[1] == 'r' && source_a[2] == 'b' && (source_a[3] == '_' || source_a[3] == '-') && (source_a[4] == '1' || source_a[4] == '3')) { - auto i (source_a.begin () + (xrb_prefix ? 4 : 5)); - if (*i == '1' || *i == '3') + rai::uint512_t number_l; + for (auto i (source_a.begin () + 4), j (source_a.end ()); !error && i != j; ++i) { - rai::uint512_t number_l; - ++i; - for (auto j (source_a.end ()); !error && i != j; ++i) + uint8_t character (*i); + error = character < 0x30 || character >= 0x80; + if (!error) { - uint8_t character (*i); - error = character < 0x30 || character >= 0x80; + uint8_t byte (account_decode (character)); + error = byte == '~'; if (!error) { - uint8_t byte (account_decode (character)); - error = byte == '~'; - if (!error) - { - number_l <<= 5; - number_l += byte; - } + number_l <<= 5; + number_l += byte; } } - if (!error) - { - *this = (number_l >> 40).convert_to (); - uint64_t check (number_l & static_cast (0xffffffffff)); - uint64_t validation (0); - blake2b_state hash; - blake2b_init (&hash, 5); - blake2b_update (&hash, bytes.data (), bytes.size ()); - blake2b_final (&hash, reinterpret_cast (&validation), 5); - error = check != validation; - } } - else + if (!error) { - error = true; + *this = (number_l >> 40).convert_to (); + uint64_t check (number_l & static_cast (0xffffffffff)); + uint64_t validation (0); + blake2b_state hash; + blake2b_init (&hash, 5); + blake2b_update (&hash, bytes.data (), bytes.size ()); + blake2b_final (&hash, reinterpret_cast (&validation), 5); + error = check != validation; } } else