trustwallet / wallet-core
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 "Address.h"
8
#include "AddressChecksum.h"
9
#include "../Hash.h"
10
#include "../HexCoding.h"
11

12
using namespace TW::Ethereum;
13

14 1
bool Address::isValid(const std::string& string) {
15 1
    if (string.size() != 42 || string[0] != '0' || string[1] != 'x') {
16 1
        return false;
17
    }
18 1
    const auto data = parse_hex(string);
19 1
    return Address::isValid(data);
20
}
21

22 1
Address::Address(const std::string& string) {
23 1
    if (!isValid(string)) {
24 1
        throw std::invalid_argument("Invalid address data");
25
    }
26 1
    const auto data = parse_hex(string);
27 1
    std::copy(data.begin(), data.end(), bytes.begin());
28
}
29

30 1
Address::Address(const Data& data) {
31 1
    if (!isValid(data)) {
32 0
        throw std::invalid_argument("Invalid address data");
33
    }
34 1
    std::copy(data.begin(), data.end(), bytes.begin());
35
}
36

37 1
Address::Address(const PublicKey& publicKey) {
38 1
    if (publicKey.type != TWPublicKeyTypeSECP256k1Extended) {
39 0
        throw std::invalid_argument("Ethereum::Address needs an extended SECP256k1 public key.");
40
    }
41 1
    const auto data = publicKey.hash({}, static_cast<Hash::HasherSimpleType>(Hash::keccak256), true);
42 1
    std::copy(data.end() - Address::size, data.end(), bytes.begin());
43
}
44

45 1
std::string Address::string() const {
46 1
    return checksumed(*this, ChecksumType::eip55);
47
}

Read our documentation on viewing source code .

Loading