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
64 lines (53 loc) · 2.02 KB
/
Address.cpp
File metadata and controls
64 lines (53 loc) · 2.02 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
// 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 "../Base58.h"
#include "../Hash.h"
#include "../HexCoding.h"
using namespace TW::Nebulas;
bool Address::isValid(const std::string &string) {
auto data = Base58::bitcoin.decode(string);
if (data.size() != (size_t)Address::size) {
return false;
}
if (data[0] != Address::AddressPrefix) {
return false;
}
if (data[1] != Address::NormalType && data[1] != Address::ContractType) {
return false;
}
Data content(data.begin(), data.begin() + 22);
Data checksum(data.begin() + 22, data.end());
auto dataSha3 = Hash::sha3_256(content);
return ::memcmp(dataSha3.data(), checksum.data(), 4) == 0;
}
Address::Address(const std::string &string) {
if (!isValid(string)) {
throw std::invalid_argument("Invalid address string");
}
auto data = Base58::bitcoin.decode(string);
std::copy(data.begin(), data.end(), bytes.begin());
}
Address::Address(const Data &data) {
if (!Base58Address::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("Nebulas::Address needs an extended SECP256k1 public key.");
}
const auto data = publicKey.hash(
{Address::AddressPrefix, Address::NormalType},
static_cast<Data (*)(const byte *, const byte *)>(Hash::sha3_256ripemd), false);
std::copy(data.begin(), data.end(), bytes.begin());
auto checksum = Hash::sha3_256(data);
std::copy(checksum.begin(), checksum.begin() + 4, bytes.begin() + 22);
}
std::string Address::string() const {
return Base58::bitcoin.encode(bytes);
}