19 class DestinationEncoder
25 explicit DestinationEncoder(
const CChainParams& params) : m_params(params) {}
27 std::string operator()(
const PKHash&
id)
const
30 data.insert(data.end(),
id.begin(),
id.end());
34 std::string operator()(
const ScriptHash&
id)
const
37 data.insert(data.end(),
id.begin(),
id.end());
43 std::vector<unsigned char> data = {0};
45 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
51 std::vector<unsigned char> data = {0};
53 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
59 std::vector<unsigned char> data = {1};
61 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); }, tap.
begin(), tap.
end());
67 if (
id.version < 1 || id.version > 16 ||
id.length < 2 || id.length > 40) {
70 std::vector<unsigned char> data = {(
unsigned char)
id.version};
71 data.reserve(1 + (
id.length * 8 + 4) / 5);
72 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.program,
id.program +
id.length);
76 std::string operator()(
const CNoDestination& no)
const {
return {}; }
81 std::vector<unsigned char> data;
93 if (data.size() == hash.
size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
94 std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.
begin());
100 if (data.size() == hash.
size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
101 std::copy(data.begin() + script_prefix.size(), data.end(), hash.
begin());
106 if ((data.size() >= script_prefix.size() &&
107 std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) ||
108 (data.size() >= pubkey_prefix.size() &&
109 std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) {
110 error_str =
"Invalid length for Base58 address (P2PKH or P2SH)";
112 error_str =
"Invalid or unsupported Base58-encoded address.";
115 }
else if (!is_bech32) {
118 error_str =
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.";
120 error_str =
"Invalid checksum or length of Base58 address (P2PKH or P2SH)";
128 if (dec.data.empty()) {
129 error_str =
"Empty Bech32 data section";
134 error_str =
strprintf(
"Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.
Bech32HRP(), dec.hrp);
137 int version = dec.data[0];
139 error_str =
"Version 0 witness address must use Bech32 checksum";
143 error_str =
"Version 1+ witness address must use Bech32m checksum";
147 data.reserve(((dec.data.size() - 1) * 5) / 8);
148 if (ConvertBits<5, 8, false>([&](
unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
150 std::string_view byte_str{data.size() == 1 ?
"byte" :
"bytes"};
155 if (data.size() == keyid.
size()) {
156 std::copy(data.begin(), data.end(), keyid.
begin());
162 if (data.size() == scriptid.
size()) {
163 std::copy(data.begin(), data.end(), scriptid.
begin());
168 error_str =
strprintf(
"Invalid Bech32 v0 address program size (%d %s), per BIP141", data.size(), byte_str);
175 std::copy(data.begin(), data.end(), tap.
begin());
180 error_str =
"Invalid Bech32 address witness version";
185 error_str =
strprintf(
"Invalid Bech32 address program size (%d %s)", data.size(), byte_str);
191 std::copy(data.begin(), data.end(), unk.
program);
195 error_str =
strprintf(
"Invalid padding in Bech32 data section");
202 error_str = res.first;
203 if (error_locations) *error_locations = std::move(res.second);
211 std::vector<unsigned char> data;
214 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
215 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
216 bool compressed = data.size() == 33 + privkey_prefix.size();
217 key.
Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
230 data.insert(data.end(), key.
begin(), key.
end());
242 std::vector<unsigned char> data;
255 size_t size = data.size();
257 key.
Encode(data.data() + size);
265 std::vector<unsigned char> data;
278 size_t size = data.size();
280 key.
Encode(data.data() + size);
288 return std::visit(DestinationEncoder(
Params()), dest);
298 std::string error_msg;
304 std::string error_msg;
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
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)
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.
const unsigned char * end() 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 Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
const unsigned char * begin() const
const unsigned char * begin() const
static constexpr size_t size()
const unsigned char * end() const
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)
@ BECH32
Bech32 encoding as defined in BIP173.
@ BECH32M
Bech32m encoding as defined in BIP350.
std::string Encode(Encoding encoding, const std::string &hrp, const data &values)
Encode a Bech32 or Bech32m string.
DecodeResult Decode(const std::string &str)
Decode a Bech32 or Bech32m string.
std::pair< std::string, std::vector< int > > LocateErrors(const std::string &str)
Find index of an incorrect character in a Bech32 string.
const unsigned int BIP32_EXTKEY_SIZE
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
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.
unsigned char program[40]
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.