diff options
author | wadeAlexC <wade.alex.c@gmail.com> | 2017-10-05 21:28:25 +0800 |
---|---|---|
committer | Alex Beregszaszi <alex@rtfs.hu> | 2017-11-17 08:46:44 +0800 |
commit | 8a6692b2cfb7cf53db6731acd6a9908bd36b5475 (patch) | |
tree | 205a7099eacf17a44932c3285371a7007bf980ea /libdevcore | |
parent | 2927ce0bd43e7d6aa3ba8e13bd123225adf1c7e8 (diff) | |
download | dexon-solidity-8a6692b2cfb7cf53db6731acd6a9908bd36b5475.tar.gz dexon-solidity-8a6692b2cfb7cf53db6731acd6a9908bd36b5475.tar.zst dexon-solidity-8a6692b2cfb7cf53db6731acd6a9908bd36b5475.zip |
Improves address literal checksum error message
Diffstat (limited to 'libdevcore')
-rw-r--r-- | libdevcore/CommonData.cpp | 25 | ||||
-rw-r--r-- | libdevcore/CommonData.h | 4 |
2 files changed, 18 insertions, 11 deletions
diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp index db11e61c..85ad685b 100644 --- a/libdevcore/CommonData.cpp +++ b/libdevcore/CommonData.cpp @@ -86,20 +86,23 @@ bool dev::passesAddressChecksum(string const& _str, bool _strict) )) return true; + return _str == dev::getChecksummedAddress(_str); +} + +string dev::getChecksummedAddress(string const& _addr) +{ + string s = _addr.substr(0, 2) == "0x" ? _addr.substr(2) : _addr; h256 hash = keccak256(boost::algorithm::to_lower_copy(s, std::locale::classic())); - for (size_t i = 0; i < 40; ++i) + string ret = "0x"; + + for (size_t i = 0; i < s.length(); ++i) { char addressCharacter = s[i]; - bool lowerCase; - if ('a' <= addressCharacter && addressCharacter <= 'f') - lowerCase = true; - else if ('A' <= addressCharacter && addressCharacter <= 'F') - lowerCase = false; - else - continue; unsigned nibble = (unsigned(hash[i / 2]) >> (4 * (1 - (i % 2)))) & 0xf; - if ((nibble >= 8) == lowerCase) - return false; + if (nibble >= 8) + ret += toupper(addressCharacter); + else + ret += tolower(addressCharacter); } - return true; + return ret; } diff --git a/libdevcore/CommonData.h b/libdevcore/CommonData.h index 765707f8..e76a0949 100644 --- a/libdevcore/CommonData.h +++ b/libdevcore/CommonData.h @@ -209,4 +209,8 @@ bool contains(T const& _t, V const& _v) /// are considered valid. bool passesAddressChecksum(std::string const& _str, bool _strict); +/// @returns the checksummed version of an address +/// @param hex strings that look like an address +std::string getChecksummedAddress(std::string const& _addr); + } |