23class DestinationEncoder
29 explicit DestinationEncoder(
const CChainParams& params) : m_params(params) {}
31 std::string operator()(
const PKHash&
id)
const
34 data.insert(
data.end(),
id.begin(),
id.end());
38 std::string operator()(
const ScriptHash&
id)
const
41 data.insert(
data.end(),
id.begin(),
id.end());
47 std::vector<unsigned char>
data = {0};
49 ConvertBits<8, 5, true>([&](
unsigned char c) {
data.push_back(c); },
id.begin(),
id.end());
55 std::vector<unsigned char>
data = {0};
57 ConvertBits<8, 5, true>([&](
unsigned char c) {
data.push_back(c); },
id.begin(),
id.end());
63 std::vector<unsigned char>
data = {1};
65 ConvertBits<8, 5, true>([&](
unsigned char c) {
data.push_back(c); }, tap.
begin(), tap.
end());
71 const std::vector<unsigned char>& program =
id.GetWitnessProgram();
72 if (
id.GetWitnessVersion() < 1 ||
id.GetWitnessVersion() > 16 || program.size() < 2 || program.size() > 40) {
75 std::vector<unsigned char>
data = {(
unsigned char)
id.GetWitnessVersion()};
77 ConvertBits<8, 5, true>([&](
unsigned char c) {
data.push_back(c); }, program.begin(), program.end());
81 std::string operator()(
const CNoDestination& no)
const {
return {}; }
87 std::vector<unsigned char>
data;
99 if (
data.size() == hash.
size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(),
data.begin())) {
100 std::copy(
data.begin() + pubkey_prefix.size(),
data.end(), hash.
begin());
106 if (
data.size() == hash.
size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(),
data.begin())) {
107 std::copy(
data.begin() + script_prefix.size(),
data.end(), hash.
begin());
112 if ((
data.size() >= script_prefix.size() &&
113 std::equal(script_prefix.begin(), script_prefix.end(),
data.begin())) ||
114 (
data.size() >= pubkey_prefix.size() &&
115 std::equal(pubkey_prefix.begin(), pubkey_prefix.end(),
data.begin()))) {
116 error_str =
"Invalid length for Base58 address (P2PKH or P2SH)";
118 error_str =
"Invalid or unsupported Base58-encoded address.";
121 }
else if (!is_bech32) {
124 error_str =
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.";
126 error_str =
"Invalid checksum or length of Base58 address (P2PKH or P2SH)";
134 if (dec.data.empty()) {
135 error_str =
"Empty Bech32 data section";
140 error_str =
strprintf(
"Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.
Bech32HRP(), dec.hrp);
143 int version = dec.data[0];
145 error_str =
"Version 0 witness address must use Bech32 checksum";
149 error_str =
"Version 1+ witness address must use Bech32m checksum";
153 data.reserve(((dec.data.size() - 1) * 5) / 8);
154 if (ConvertBits<5, 8, false>([&](
unsigned char c) {
data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
156 std::string_view byte_str{
data.size() == 1 ?
"byte" :
"bytes"};
168 if (
data.size() == scriptid.
size()) {
174 error_str =
strprintf(
"Invalid Bech32 v0 address program size (%d %s), per BIP141",
data.size(), byte_str);
190 error_str =
"Invalid Bech32 address witness version";
195 error_str =
strprintf(
"Invalid Bech32 address program size (%d %s)",
data.size(), byte_str);
201 error_str =
strprintf(
"Invalid padding in Bech32 data section");
208 error_str = res.first;
209 if (error_locations) *error_locations = std::move(res.second);
217 std::vector<unsigned char>
data;
220 if ((
data.size() == 32 + privkey_prefix.size() || (
data.size() == 33 + privkey_prefix.size() &&
data.back() == 1)) &&
221 std::equal(privkey_prefix.begin(), privkey_prefix.end(),
data.begin())) {
222 bool compressed =
data.size() == 33 + privkey_prefix.size();
223 key.
Set(
data.begin() + privkey_prefix.size(),
data.begin() + privkey_prefix.size() + 32, compressed);
248 std::vector<unsigned char>
data;
261 size_t size =
data.size();
271 std::vector<unsigned char>
data;
287 size_t size =
data.size();
297 return std::visit(DestinationEncoder(
Params()), dest);
307 std::string error_msg;
313 std::string error_msg;
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
static bool DecodeBase58Check(const char *psz, std::vector< unsigned char > &vchRet, int max_ret_len)
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
std::string EncodeBase58Check(std::span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
const CChainParams & Params()
Return the currently selected parameters.
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const std::vector< unsigned char > & Base58Prefix(Base58Type type) const
const std::string & Bech32HRP() const
An encapsulated private key.
bool IsValid() const
Check whether this private key is valid.
const std::byte * begin() const
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
const std::byte * end() const
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool IsPayToAnchor() const
const unsigned char * end() const
const unsigned char * begin() const
static constexpr size_t size()
static constexpr unsigned int size()
constexpr unsigned char * begin()
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
static constexpr size_t WITNESS_V1_TAPROOT_SIZE
static constexpr std::size_t BECH32_WITNESS_PROG_MAX_LEN
Maximum witness length for Bech32 addresses.
bool IsValidDestinationString(const std::string &str, const CChainParams ¶ms)
std::string EncodeExtKey(const CExtKey &key)
CExtPubKey DecodeExtPubKey(const std::string &str)
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg, std::vector< int > *error_locations)
std::string EncodeSecret(const CKey &key)
std::string EncodeDestination(const CTxDestination &dest)
CKey DecodeSecret(const std::string &str)
std::string EncodeExtPubKey(const CExtPubKey &key)
CExtKey DecodeExtKey(const std::string &str)
std::pair< std::string, std::vector< int > > LocateErrors(const std::string &str, CharLimit limit)
Find index of an incorrect character in a Bech32 string.
@ BECH32
Bech32 encoding as defined in BIP173.
@ BECH32M
Bech32m encoding as defined in BIP350.
DecodeResult Decode(const std::string &str, CharLimit limit)
Decode a Bech32 or Bech32m string.
std::string Encode(Encoding encoding, const std::string &hrp, const data &values)
Encode a Bech32 or Bech32m string.
constexpr auto CeilDiv(const Dividend dividend, const Divisor divisor)
Integer ceiling division (for unsigned values).
const unsigned int BIP32_EXTKEY_SIZE
unsigned char * UCharCast(char *c)
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
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.
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.