12 #include <boost/variant/apply_visitor.hpp> 13 #include <boost/variant/static_visitor.hpp> 21 class DestinationEncoder :
public boost::static_visitor<std::string>
27 explicit DestinationEncoder(
const CChainParams& params) : m_params(params) {}
29 std::string operator()(
const CKeyID&
id)
const 32 data.insert(data.end(),
id.begin(),
id.end());
36 std::string operator()(
const CScriptID&
id)
const 39 data.insert(data.end(),
id.begin(),
id.end());
45 std::vector<unsigned char> data = {0};
47 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
53 std::vector<unsigned char> data = {0};
55 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
61 if (
id.version < 1 || id.version > 16 ||
id.length < 2 || id.length > 40) {
64 std::vector<unsigned char> data = {(
unsigned char)
id.version};
65 data.reserve(1 + (
id.length * 8 + 4) / 5);
66 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.program,
id.program +
id.length);
70 std::string operator()(
const CNoDestination& no)
const {
return {}; }
75 std::vector<unsigned char> data;
82 if (data.size() == hash.
size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
83 std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.
begin());
89 if (data.size() == hash.
size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
90 std::copy(data.begin() + script_prefix.size(), data.end(), hash.
begin());
96 if (bech.second.size() > 0 && bech.first == params.
Bech32HRP()) {
98 int version = bech.second[0];
100 data.reserve(((bech.second.size() - 1) * 5) / 8);
101 if (ConvertBits<5, 8, false>([&](
unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) {
105 if (data.size() == keyid.
size()) {
106 std::copy(data.begin(), data.end(), keyid.
begin());
112 if (data.size() == scriptid.
size()) {
113 std::copy(data.begin(), data.end(), scriptid.
begin());
119 if (version > 16 || data.size() < 2 || data.size() > 40) {
124 std::copy(data.begin(), data.end(), unk.
program);
136 std::vector<unsigned char> data;
139 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
140 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
141 bool compressed = data.size() == 33 + privkey_prefix.size();
142 key.
Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
153 data.insert(data.end(), key.
begin(), key.
end());
165 std::vector<unsigned char> data;
178 size_t size = data.size();
180 key.
Encode(data.data() + size);
188 std::vector<unsigned char> data;
201 size_t size = data.size();
203 key.
Encode(data.data() + size);
211 return boost::apply_visitor(DestinationEncoder(
Params()), dest);
unsigned char program[40]
bool IsValidDestinationString(const std::string &str, const CChainParams ¶ms)
const std::string & Bech32HRP() const
CExtKey DecodeExtKey(const std::string &str)
UniValue ret(UniValue::VARR)
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
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...
const unsigned char * begin() const
CExtPubKey DecodeExtPubKey(const std::string &str)
void memory_cleanse(void *ptr, size_t len)
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
boost::variant< CNoDestination, CKeyID, CScriptID, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
CTxDestination subtype to encode any future Witness version.
bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet)
Decode a base58-encoded string (psz) that includes a checksum into a byte vector (vchRet), return true if decoding is successful.
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
unsigned int size() const
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])
CTxDestination DecodeDestination(const std::string &str)
std::string EncodeExtPubKey(const CExtPubKey &key)
const CChainParams & Params()
Return the currently selected parameters.
const unsigned char * end() const
const unsigned int BIP32_EXTKEY_SIZE
A reference to a CKey: the Hash360 of its serialized public key.
A reference to a CScript: the Hash360 of its serialization (see script.h)
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)
std::string EncodeBase58Check(const std::vector< unsigned char > &vchIn)
Encode a byte vector into a base58-encoded string, including checksum.
std::string EncodeSecret(const CKey &key)
std::string EncodeExtKey(const CExtKey &key)
bool IsValid() const
Check whether this private key is valid.
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const