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

Supporting nano_ and xrb_ prefixes. #854

Merged
merged 1 commit into from
May 7, 2018
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
7 changes: 7 additions & 0 deletions rai/core_test/uint256_union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,13 @@ 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++)
Expand Down
51 changes: 31 additions & 20 deletions rai/lib/numbers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,37 +115,48 @@ 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);
auto error (source_a.size () != 64 && source_a.size () != 65);
if (!error)
{
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 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)
{
rai::uint512_t number_l;
for (auto i (source_a.begin () + 4), j (source_a.end ()); !error && i != j; ++i)
auto i (source_a.begin () + (xrb_prefix ? 4 : 5));
if (*i == '1' || *i == '3')
{
uint8_t character (*i);
error = character < 0x30 || character >= 0x80;
if (!error)
rai::uint512_t number_l;
++i;
for (auto j (source_a.end ()); !error && i != j; ++i)
{
uint8_t byte (account_decode (character));
error = byte == '~';
uint8_t character (*i);
error = character < 0x30 || character >= 0x80;
if (!error)
{
number_l <<= 5;
number_l += byte;
uint8_t byte (account_decode (character));
error = byte == '~';
if (!error)
{
number_l <<= 5;
number_l += byte;
}
}
}
if (!error)
{
*this = (number_l >> 40).convert_to<rai::uint256_t> ();
uint64_t check (number_l & static_cast<uint64_t> (0xffffffffff));
uint64_t validation (0);
blake2b_state hash;
blake2b_init (&hash, 5);
blake2b_update (&hash, bytes.data (), bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&validation), 5);
error = check != validation;
}
}
if (!error)
else
{
*this = (number_l >> 40).convert_to<rai::uint256_t> ();
uint64_t check (number_l & static_cast<uint64_t> (0xffffffffff));
uint64_t validation (0);
blake2b_state hash;
blake2b_init (&hash, 5);
blake2b_update (&hash, bytes.data (), bytes.size ());
blake2b_final (&hash, reinterpret_cast<uint8_t *> (&validation), 5);
error = check != validation;
error = true;
}
}
else
Expand Down