16 class DestinationEncoder
22 explicit DestinationEncoder(
const CChainParams& params) : m_params(params) {}
24 std::string operator()(
const PKHash&
id)
const 27 data.insert(data.end(),
id.begin(),
id.end());
31 std::string operator()(
const ScriptHash&
id)
const 34 data.insert(data.end(),
id.begin(),
id.end());
40 std::vector<unsigned char> data = {0};
42 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
48 std::vector<unsigned char> data = {0};
50 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
56 if (
id.version < 1 || id.version > 16 ||
id.length < 2 || id.length > 40) {
59 std::vector<unsigned char> data = {(
unsigned char)
id.version};
60 data.reserve(1 + (
id.length * 8 + 4) / 5);
61 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.program,
id.program +
id.length);
65 std::string operator()(
const CNoDestination& no)
const {
return {}; }
70 std::vector<unsigned char> data;
77 if (data.size() == hash.
size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
78 std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.
begin());
84 if (data.size() == hash.
size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
85 std::copy(data.begin() + script_prefix.size(), data.end(), hash.
begin());
91 if (bech.second.size() > 0 && bech.first == params.
Bech32HRP()) {
93 int version = bech.second[0];
95 data.reserve(((bech.second.size() - 1) * 5) / 8);
96 if (ConvertBits<5, 8, false>([&](
unsigned char c) { data.push_back(c); }, bech.second.begin() + 1, bech.second.end())) {
100 if (data.size() == keyid.
size()) {
101 std::copy(data.begin(), data.end(), keyid.
begin());
107 if (data.size() == scriptid.
size()) {
108 std::copy(data.begin(), data.end(), scriptid.
begin());
114 if (version > 16 || data.size() < 2 || data.size() > 40) {
119 std::copy(data.begin(), data.end(), unk.
program);
131 std::vector<unsigned char> data;
134 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
135 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
136 bool compressed = data.size() == 33 + privkey_prefix.size();
137 key.
Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
150 data.insert(data.end(), key.
begin(), key.
end());
162 std::vector<unsigned char> data;
165 if (data.size() ==
BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
166 key.
Decode(data.data() + prefix.size());
175 size_t size = data.size();
177 key.
Encode(data.data() + size);
185 std::vector<unsigned char> data;
188 if (data.size() ==
BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
189 key.
Decode(data.data() + prefix.size());
198 size_t size = data.size();
200 key.
Encode(data.data() + size);
208 return std::visit(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)
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...
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
const unsigned char * begin() const
CExtPubKey DecodeExtPubKey(const std::string &str)
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
CTxDestination subtype to encode any future Witness version.
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])
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
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
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
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 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