11 #include <boost/variant/apply_visitor.hpp>
12 #include <boost/variant/static_visitor.hpp>
20 class DestinationEncoder :
public boost::static_visitor<std::string>
26 explicit DestinationEncoder(
const CChainParams& params) : m_params(params) {}
28 std::string operator()(
const PKHash&
id)
const
31 data.insert(data.end(),
id.begin(),
id.end());
35 std::string operator()(
const ScriptHash&
id)
const
38 data.insert(data.end(),
id.begin(),
id.end());
44 std::vector<unsigned char> data = {0};
46 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
52 std::vector<unsigned char> data = {0};
54 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
60 if (
id.version < 1 || id.version > 16 ||
id.length < 2 || id.length > 40) {
63 std::vector<unsigned char> data = {(
unsigned char)
id.version};
64 data.reserve(1 + (
id.length * 8 + 4) / 5);
65 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.program,
id.program +
id.length);
69 std::string operator()(
const CNoDestination& no)
const {
return {}; }
74 std::vector<unsigned char> data;
81 if (data.size() == hash.
size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
82 std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.
begin());
88 if (data.size() == hash.
size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
89 std::copy(data.begin() + script_prefix.size(), data.end(), hash.
begin());
95 if (bech.second.size() > 0 && bech.first == params.
Bech32HRP()) {
97 int version = bech.second[0];
99 data.reserve(((bech.second.size() - 1) * 5) / 8);
100 if (ConvertBits<5, 8, false>([&](
unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) {
104 if (data.size() == keyid.
size()) {
105 std::copy(data.begin(), data.end(), keyid.
begin());
111 if (data.size() == scriptid.
size()) {
112 std::copy(data.begin(), data.end(), scriptid.
begin());
118 if (version > 16 || data.size() < 2 || data.size() > 40) {
123 std::copy(data.begin(), data.end(), unk.
program);
135 std::vector<unsigned char> data;
138 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
139 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
140 bool compressed = data.size() == 33 + privkey_prefix.size();
141 key.
Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
154 data.insert(data.end(), key.
begin(), key.
end());
166 std::vector<unsigned char> data;
169 if (data.size() ==
BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
170 key.
Decode(data.data() + prefix.size());
179 size_t size = data.size();
181 key.
Encode(data.data() + size);
189 std::vector<unsigned char> data;
192 if (data.size() ==
BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
193 key.
Decode(data.data() + prefix.size());
202 size_t size = data.size();
204 key.
Encode(data.data() + size);
212 return boost::apply_visitor(DestinationEncoder(
Params()), dest);
const unsigned char * begin() const
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
unsigned char program[40]
bool IsValidDestinationString(const std::string &str, const CChainParams ¶ms)
const unsigned char * end() const
CExtKey DecodeExtKey(const std::string &str)
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system...
CExtPubKey DecodeExtPubKey(const std::string &str)
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
bool IsValid() const
Check whether this private key is valid.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
CTxDestination subtype to encode any future Witness version.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
std::pair< std::string, data > Decode(const std::string &str)
Decode a Bech32 string.
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
const std::string & Bech32HRP() const
static NODISCARD bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
CTxDestination DecodeDestination(const std::string &str)
std::string EncodeExtPubKey(const CExtPubKey &key)
const CChainParams & Params()
Return the currently selected parameters.
const unsigned int BIP32_EXTKEY_SIZE
std::string EncodeDestination(const CTxDestination &dest)
An encapsulated private key.
std::string Encode(const std::string &hrp, const data &values)
Encode a Bech32 string.
CKey DecodeSecret(const std::string &str)
boost::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
std::string EncodeSecret(const CKey &key)
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
std::string EncodeExtKey(const CExtKey &key)
unsigned int size() const