forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddress.cpp
More file actions
47 lines (40 loc) · 1.5 KB
/
Copy pathAddress.cpp
File metadata and controls
47 lines (40 loc) · 1.5 KB
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
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#include "Address.h"
#include "AddressChecksum.h"
#include "../Hash.h"
#include "../HexCoding.h"
using namespace TW::Ethereum;
bool Address::isValid(const std::string& string) {
if (string.size() != 42 || string[0] != '0' || string[1] != 'x') {
return false;
}
const auto data = parse_hex(string);
return Address::isValid(data);
}
Address::Address(const std::string& string) {
if (!isValid(string)) {
throw std::invalid_argument("Invalid address data");
}
const auto data = parse_hex(string);
std::copy(data.begin(), data.end(), bytes.begin());
}
Address::Address(const Data& data) {
if (!isValid(data)) {
throw std::invalid_argument("Invalid address data");
}
std::copy(data.begin(), data.end(), bytes.begin());
}
Address::Address(const PublicKey& publicKey) {
if (publicKey.type != TWPublicKeyTypeSECP256k1Extended) {
throw std::invalid_argument("Ethereum::Address needs an extended SECP256k1 public key.");
}
const auto data = publicKey.hash({}, static_cast<Data(*)(const byte*, const byte*)>(Hash::keccak256), true);
std::copy(data.end() - Address::size, data.end(), bytes.begin());
}
std::string Address::string() const {
return checksumed(*this, ChecksumType::eip55);
}