aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore/CommonData.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libdevcore/CommonData.cpp')
-rw-r--r--libdevcore/CommonData.cpp27
1 files changed, 17 insertions, 10 deletions
diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp
index db11e61c..445d11cd 100644
--- a/libdevcore/CommonData.cpp
+++ b/libdevcore/CommonData.cpp
@@ -21,6 +21,7 @@
#include <libdevcore/CommonData.h>
#include <libdevcore/Exceptions.h>
+#include <libdevcore/Assertions.h>
#include <libdevcore/SHA3.h>
#include <boost/algorithm/string.hpp>
@@ -86,20 +87,26 @@ 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;
+ assertThrow(s.length() == 40, InvalidAddress, "");
+ assertThrow(s.find_first_not_of("0123456789abcdefABCDEF") == string::npos, InvalidAddress, "");
+
h256 hash = keccak256(boost::algorithm::to_lower_copy(s, std::locale::classic()));
+
+ string ret = "0x";
for (size_t i = 0; i < 40; ++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;
}