1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#include "AddressChecksum.h"
8

9
#include "../Hash.h"
10
#include "../HexCoding.h"
11
#include <cctype>
12

13
using namespace TW;
14
using namespace TW::Ethereum;
15

16 1
std::string Ethereum::checksumed(const Address& address, enum ChecksumType type) {
17 1
    const auto addressString = hex(address.bytes);
18 1
    const auto hash = hex(Hash::keccak256(addressString));
19

20 1
    std::string string = "0x";
21 1
    for (auto i = 0; i < std::min(addressString.size(), hash.size()); i += 1) {
22 1
        const auto a = addressString[i];
23 1
        const auto h = hash[i];
24 1
        if (a >= '0' && a <= '9') {
25 1
            string.push_back(a);
26 1
        } else if ((h >= '8' && h <= '9') || (h >= 'a' && h <= 'f')) {
27 1
            switch (type) {
28
            case eip55:
29 1
                string.push_back(static_cast<char>(toupper(a)));
30 1
                break;
31
            case wanchain:
32 1
                string.push_back(static_cast<char>(tolower(a)));
33 1
                break;
34
            }
35 1
        } else {
36 1
            switch (type) {
37
            case eip55:
38 1
                string.push_back(static_cast<char>(tolower(a)));
39 1
                break;
40
            case wanchain:
41 1
                string.push_back(static_cast<char>(toupper(a)));
42 1
                break;
43
            }
44
        }
45
    }
46

47 1
    return string;
48
}

Read our documentation on viewing source code .

Loading