aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore
diff options
context:
space:
mode:
authorAlex Beregszaszi <alex@rtfs.hu>2017-10-20 22:23:28 +0800
committerAlex Beregszaszi <alex@rtfs.hu>2017-11-17 08:46:45 +0800
commit6ebc094474837d922ac00a92c54c903c5eb78585 (patch)
tree833f1816986db66e465deb27e35930bfd4597a67 /libdevcore
parent8a6692b2cfb7cf53db6731acd6a9908bd36b5475 (diff)
downloaddexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.tar
dexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.tar.gz
dexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.tar.bz2
dexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.tar.lz
dexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.tar.xz
dexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.tar.zst
dexon-solidity-6ebc094474837d922ac00a92c54c903c5eb78585.zip
Ensure that non-hex characters are caught in address checksumming
Diffstat (limited to 'libdevcore')
-rw-r--r--libdevcore/CommonData.cpp8
-rw-r--r--libdevcore/Exceptions.h1
2 files changed, 7 insertions, 2 deletions
diff --git a/libdevcore/CommonData.cpp b/libdevcore/CommonData.cpp
index 85ad685b..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>
@@ -92,10 +93,13 @@ bool dev::passesAddressChecksum(string const& _str, bool _strict)
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 < s.length(); ++i)
+ string ret = "0x";
+ for (size_t i = 0; i < 40; ++i)
{
char addressCharacter = s[i];
unsigned nibble = (unsigned(hash[i / 2]) >> (4 * (1 - (i % 2)))) & 0xf;
diff --git a/libdevcore/Exceptions.h b/libdevcore/Exceptions.h
index a3e638bf..cfe72fbf 100644
--- a/libdevcore/Exceptions.h
+++ b/libdevcore/Exceptions.h
@@ -44,6 +44,7 @@ private:
#define DEV_SIMPLE_EXCEPTION(X) struct X: virtual Exception { const char* what() const noexcept override { return #X; } }
+DEV_SIMPLE_EXCEPTION(InvalidAddress);
DEV_SIMPLE_EXCEPTION(BadHexCharacter);
DEV_SIMPLE_EXCEPTION(FileError);