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
73 lines (61 loc) · 2.36 KB
/
Copy pathAddress.cpp
File metadata and controls
73 lines (61 loc) · 2.36 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
72
73
// 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 "OpCode.h"
#include "ParamsBuilder.h"
#include "../Hash.h"
#include <TrezorCrypto/base58.h>
#include <stdexcept>
#include <string>
using namespace TW;
using namespace TW::Ontology;
Address::Address(const PublicKey& publicKey) {
std::vector<uint8_t> builder(publicKey.bytes);
builder.insert(builder.begin(), PUSH_BYTE_33);
builder.push_back(CHECK_SIG);
auto builderData = toScriptHash(builder);
std::copy(builderData.begin(), builderData.end(), data.begin());
}
Address::Address(const std::string& b58Address) {
if (!Address::isValid(b58Address)) {
throw std::runtime_error("Invalid base58 encode address.");
}
Data addressWithVersion(size + 1);
base58_decode_check(b58Address.c_str(), HASHER_SHA2D, addressWithVersion.data(), size + 1);
std::copy(addressWithVersion.begin() + 1, addressWithVersion.end(), data.begin());
}
Address::Address(const std::vector<uint8_t>& bytes) {
if (bytes.size() != size) {
throw std::runtime_error("Invalid bytes data.");
}
std::copy(bytes.begin(), bytes.end(), data.begin());
}
Address::Address(uint8_t m, const std::vector<Data>& publicKeys) {
auto builderData = toScriptHash(ParamsBuilder::fromMultiPubkey(m, publicKeys));
std::copy(builderData.begin(), builderData.end(), data.begin());
}
Data Address::toScriptHash(const Data& data) {
return Hash::ripemd(Hash::sha256(data));
}
bool Address::isValid(const std::string& b58Address) noexcept {
if (b58Address.length() != 34) {
return false;
}
Data addressWithVersion(size + 1);
auto len =
base58_decode_check(b58Address.c_str(), HASHER_SHA2D, addressWithVersion.data(), size + 1);
return len == size + 1;
}
std::string Address::string() const {
std::vector<uint8_t> encodeData(size + 1);
encodeData[0] = version;
std::copy(data.begin(), data.end(), encodeData.begin() + 1);
size_t b58StrSize = 34;
std::string b58Str(b58StrSize, ' ');
base58_encode_check(encodeData.data(), (int)encodeData.size(), HASHER_SHA2D, &b58Str[0],
(int)b58StrSize + 1);
return b58Str;
}