forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase58Address.h
96 lines (79 loc) · 2.83 KB
/
Base58Address.h
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2017 Trust Wallet.
#pragma once
#include "Base58.h"
#include "Data.h"
#include "PublicKey.h"
#include <array>
#include <string>
namespace TW {
template <std::size_t S>
class Base58Address {
public:
/// Number of bytes in an address.
static const size_t size = S;
/// Address data consisting of a prefix byte followed by the public key
/// hash.
std::array<byte, size> bytes;
/// Determines whether a collection of bytes makes a valid address.
template <typename T>
static bool isValid(const T& data) {
return data.size() == size;
}
/// Determines whether a string makes a valid address.
static bool isValid(const std::string& string) {
const auto decoded = Base58::decodeCheck(string);
if (decoded.size() != Base58Address::size) {
return false;
}
return true;
}
/// Determines whether a string makes a valid address, and the prefix is
/// within the valid set.
static bool isValid(const std::string& string, const std::vector<Data>& validPrefixes) {
const auto decoded = Base58::decodeCheck(string);
if (decoded.size() != Base58Address::size) {
return false;
}
for (const auto& prefix : validPrefixes) {
if (has_prefix(decoded, prefix)) {
return true;
}
}
return false;
}
Base58Address() = default;
/// Initializes an address with a string representation.
explicit Base58Address(const std::string& string) {
const auto decoded = Base58::decodeCheck(string);
if (decoded.size() != Base58Address::size) {
throw std::invalid_argument("Invalid address string");
}
std::copy(decoded.begin(), decoded.end(), bytes.begin());
}
/// Initializes an address with a collection of bytes.
explicit Base58Address(const Data& data) {
if (!isValid(data)) {
throw std::invalid_argument("Invalid address key data");
}
std::copy(data.begin(), data.end(), bytes.begin());
}
/// Initializes an address with a public key and a prefix.
Base58Address(const PublicKey& publicKey, const Data& prefix) {
if (publicKey.type != TWPublicKeyTypeSECP256k1) {
throw std::invalid_argument("Bitcoin::Address needs a compressed SECP256k1 public key.");
}
const auto data = publicKey.hash(prefix, Hash::HasherSha256ripemd);
std::copy(data.begin(), data.end(), bytes.begin());
}
/// Returns a string representation of the address.
std::string string() const {
return Base58::encodeCheck(bytes);
}
};
template <std::size_t S>
inline bool operator==(const Base58Address<S>& lhs, const Base58Address<S>& rhs) {
return lhs.bytes == rhs.bytes;
}
} // namespace TW