mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Ensure that non-hex characters are caught in address checksumming
This commit is contained in:
parent
8a6692b2cf
commit
6ebc094474
@ -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;
|
||||
h256 hash = keccak256(boost::algorithm::to_lower_copy(s, std::locale::classic()));
|
||||
string ret = "0x";
|
||||
assertThrow(s.length() == 40, InvalidAddress, "");
|
||||
assertThrow(s.find_first_not_of("0123456789abcdefABCDEF") == string::npos, InvalidAddress, "");
|
||||
|
||||
for (size_t i = 0; i < s.length(); ++i)
|
||||
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];
|
||||
unsigned nibble = (unsigned(hash[i / 2]) >> (4 * (1 - (i % 2)))) & 0xf;
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user