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());
97 int version = dec.data[0];
105 data.reserve(((dec.data.size() - 1) * 5) / 8);
106 if (ConvertBits<5, 8, false>([&](
unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
110 if (data.size() == keyid.
size()) {
111 std::copy(data.begin(), data.end(), keyid.
begin());
117 if (data.size() == scriptid.
size()) {
118 std::copy(data.begin(), data.end(), scriptid.
begin());
124 if (version > 16 || data.size() < 2 || data.size() > 40) {
129 std::copy(data.begin(), data.end(), unk.
program);
141 std::vector<unsigned char> data;
144 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
145 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
146 bool compressed = data.size() == 33 + privkey_prefix.size();
147 key.
Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
160 data.insert(data.end(), key.
begin(), key.
end());
172 std::vector<unsigned char> data;
175 if (data.size() ==
BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
176 key.
Decode(data.data() + prefix.size());
185 size_t size = data.size();
187 key.
Encode(data.data() + size);
195 std::vector<unsigned char> data;
198 if (data.size() ==
BIP32_EXTKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
199 key.
Decode(data.data() + prefix.size());
208 size_t size = data.size();
210 key.
Encode(data.data() + size);
218 return boost::apply_visitor(DestinationEncoder(
Params()), dest);
Bech32 encoding as defined in BIP173.
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...
std::string Encode(Encoding encoding, const std::string &hrp, const data &values)
Encode a Bech32 or Bech32m string.
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])
Bech32m encoding as defined in BIP350.
CTxDestination subtype to encode any future Witness version.
DecodeResult Decode(const std::string &str)
Decode a Bech32 or Bech32m string.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
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.
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