aboutsummaryrefslogtreecommitdiffstats
path: root/libdevcore/CommonData.cpp
blob: cb79fa98e97713f4b06ee2cae473d7232840345e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
    This file is part of solidity.

    solidity is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    solidity is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with solidity.  If not, see <http://www.gnu.org/licenses/>.
*/
/** @file CommonData.cpp
 * @author Gav Wood <i@gavwood.com>
 * @date 2014
 */

#include <libdevcore/CommonData.h>
#include <libdevcore/Exceptions.h>
#include <libdevcore/Assertions.h>
#include <libdevcore/Keccak256.h>

#include <boost/algorithm/string.hpp>

using namespace std;
using namespace dev;

int dev::fromHex(char _i, WhenError _throw)
{
    if (_i >= '0' && _i <= '9')
        return _i - '0';
    if (_i >= 'a' && _i <= 'f')
        return _i - 'a' + 10;
    if (_i >= 'A' && _i <= 'F')
        return _i - 'A' + 10;
    if (_throw == WhenError::Throw)
        BOOST_THROW_EXCEPTION(BadHexCharacter() << errinfo_invalidSymbol(_i));
    else
        return -1;
}

bytes dev::fromHex(std::string const& _s, WhenError _throw)
{
    unsigned s = (_s.size() >= 2 && _s[0] == '0' && _s[1] == 'x') ? 2 : 0;
    std::vector<uint8_t> ret;
    ret.reserve((_s.size() - s + 1) / 2);

    if (_s.size() % 2)
    {
        int h = fromHex(_s[s++], WhenError::DontThrow);
        if (h != -1)
            ret.push_back(h);
        else if (_throw == WhenError::Throw)
            BOOST_THROW_EXCEPTION(BadHexCharacter());
        else
            return bytes();
    }
    for (unsigned i = s; i < _s.size(); i += 2)
    {
        int h = fromHex(_s[i], WhenError::DontThrow);
        int l = fromHex(_s[i + 1], WhenError::DontThrow);
        if (h != -1 && l != -1)
            ret.push_back((byte)(h * 16 + l));
        else if (_throw == WhenError::Throw)
            BOOST_THROW_EXCEPTION(BadHexCharacter());
        else
            return bytes();
    }
    return ret;
}


bool dev::passesAddressChecksum(string const& _str, bool _strict)
{
    string s = _str.substr(0, 2) == "0x" ? _str : "0x" + _str;

    if (s.length() != 42)
        return false;

    if (!_strict && (
        s.find_first_of("abcdef") == string::npos ||
        s.find_first_of("ABCDEF") == string::npos
    ))
        return true;

    return s == dev::getChecksummedAddress(s);
}

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];
        unsigned nibble = (unsigned(hash[i / 2]) >> (4 * (1 - (i % 2)))) & 0xf;
        if (nibble >= 8)
            ret += toupper(addressCharacter);
        else
            ret += tolower(addressCharacter);
    }
    return ret;
}