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
71 lines (59 loc) · 2.23 KB
/
Copy pathAddress.cpp
File metadata and controls
71 lines (59 loc) · 2.23 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// 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 "../Hash.h"
#include "../HexCoding.h"
#include "../PrivateKey.h"
#include <TrezorCrypto/sha3.h>
using namespace TW;
using namespace TW::Icon;
static const std::string addressPrefix = "hx";
static const std::string contractPrefix = "cx";
bool Address::isValid(const std::string& string) {
if (string.size() != Address::size * 2 + 2) {
return false;
}
if (!std::equal(addressPrefix.begin(), addressPrefix.end(), string.begin()) &&
!std::equal(contractPrefix.begin(), contractPrefix.end(), string.begin())) {
return false;
}
return true;
}
Address::Address(const std::string& string) {
if (!isValid(string)) {
throw std::invalid_argument("Invalid address data");
}
if (std::equal(addressPrefix.begin(), addressPrefix.end(), string.begin())) {
type = TWIconAddressTypeAddress;
} else if (std::equal(contractPrefix.begin(), contractPrefix.end(), string.begin())) {
type = TWIconAddressTypeContract;
} else {
throw std::invalid_argument("Invalid address prefix");
}
const auto data = parse_hex(string.begin() + 2, string.end());
std::copy(data.begin(), data.end(), bytes.begin());
}
Address::Address(const std::vector<uint8_t>& data, TWIconAddressType type) : type(type) {
if (!isValid(data)) {
throw std::invalid_argument("Invalid address data");
}
std::copy(data.begin(), data.end(), bytes.begin());
}
Address::Address(const PublicKey& publicKey, TWIconAddressType type) : type(type) {
auto hash = std::array<uint8_t, Hash::sha256Size>();
sha3_256(publicKey.bytes.data() + 1, publicKey.bytes.size() - 1, hash.data());
std::copy(hash.end() - Address::size, hash.end(), bytes.begin());
}
std::string Address::string() const {
switch (type) {
case TWIconAddressTypeAddress:
return addressPrefix + hex(bytes);
case TWIconAddressTypeContract:
return contractPrefix + hex(bytes);
default:
return "";
}
}