22 class DestinationEncoder
28 explicit DestinationEncoder(
const CChainParams& params) : m_params(params) {}
30 std::string operator()(
const PKHash&
id)
const 33 data.insert(data.end(),
id.begin(),
id.end());
37 std::string operator()(
const ScriptHash&
id)
const 40 data.insert(data.end(),
id.begin(),
id.end());
46 std::vector<unsigned char> data = {0};
48 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
54 std::vector<unsigned char> data = {0};
56 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); },
id.begin(),
id.end());
62 std::vector<unsigned char> data = {1};
64 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); }, tap.
begin(), tap.
end());
70 const std::vector<unsigned char>& program =
id.GetWitnessProgram();
71 if (
id.GetWitnessVersion() < 1 ||
id.GetWitnessVersion() > 16 || program.size() < 2 || program.size() > 40) {
74 std::vector<unsigned char> data = {(
unsigned char)
id.GetWitnessVersion()};
75 data.reserve(1 + (program.size() * 8 + 4) / 5);
76 ConvertBits<8, 5, true>([&](
unsigned char c) { data.push_back(c); }, program.begin(), program.end());
80 std::string operator()(
const CNoDestination& no)
const {
return {}; }
86 std::vector<unsigned char> data;
98 if (data.size() == hash.
size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
99 std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.
begin());
105 if (data.size() == hash.
size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
106 std::copy(data.begin() + script_prefix.size(), data.end(), hash.
begin());
111 if ((data.size() >= script_prefix.size() &&
112 std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) ||
113 (data.size() >= pubkey_prefix.size() &&
114 std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin()))) {
115 error_str =
"Invalid length for Base58 address (P2PKH or P2SH)";
117 error_str =
"Invalid or unsupported Base58-encoded address.";
120 }
else if (!is_bech32) {
123 error_str =
"Invalid or unsupported Segwit (Bech32) or Base58 encoding.";
125 error_str =
"Invalid checksum or length of Base58 address (P2PKH or P2SH)";
133 if (dec.data.empty()) {
134 error_str =
"Empty Bech32 data section";
139 error_str =
strprintf(
"Invalid or unsupported prefix for Segwit (Bech32) address (expected %s, got %s).", params.
Bech32HRP(), dec.hrp);
142 int version = dec.data[0];
143 if (version == 0 && dec.encoding != bech32::Encoding::BECH32) {
144 error_str =
"Version 0 witness address must use Bech32 checksum";
148 error_str =
"Version 1+ witness address must use Bech32m checksum";
152 data.reserve(((dec.data.size() - 1) * 5) / 8);
153 if (ConvertBits<5, 8, false>([&](
unsigned char c) { data.push_back(c); }, dec.data.begin() + 1, dec.data.end())) {
155 std::string_view byte_str{data.size() == 1 ?
"byte" :
"bytes"};
160 if (data.size() == keyid.
size()) {
161 std::copy(data.begin(), data.end(), keyid.
begin());
167 if (data.size() == scriptid.
size()) {
168 std::copy(data.begin(), data.end(), scriptid.
begin());
173 error_str =
strprintf(
"Invalid Bech32 v0 address program size (%d %s), per BIP141", data.size(), byte_str);
180 std::copy(data.begin(), data.end(), tap.
begin());
189 error_str =
"Invalid Bech32 address witness version";
194 error_str =
strprintf(
"Invalid Bech32 address program size (%d %s)", data.size(), byte_str);
200 error_str =
strprintf(
"Invalid padding in Bech32 data section");
207 error_str = res.first;
208 if (error_locations) *error_locations = std::move(res.second);
216 std::vector<unsigned char> data;
219 if ((data.size() == 32 + privkey_prefix.size() || (data.size() == 33 + privkey_prefix.size() && data.back() == 1)) &&
220 std::equal(privkey_prefix.begin(), privkey_prefix.end(), data.begin())) {
221 bool compressed = data.size() == 33 + privkey_prefix.size();
222 key.
Set(data.begin() + privkey_prefix.size(), data.begin() + privkey_prefix.size() + 32, compressed);
247 std::vector<unsigned char> data;
260 size_t size = data.size();
262 key.
Encode(data.data() + size);
270 std::vector<unsigned char> data;
286 size_t size = data.size();
288 key.
Encode(data.data() + size);
296 return std::visit(DestinationEncoder(
Params()), dest);
306 std::string error_msg;
312 std::string error_msg;
static constexpr size_t size()
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination corresponds to one with an address.
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)
const std::string & Bech32HRP() const
static constexpr unsigned int size()
CExtKey DecodeExtKey(const std::string &str)
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
const std::byte * end() const
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)
constexpr unsigned char * begin()
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
std::pair< std::string, std::vector< int > > LocateErrors(const std::string &str, CharLimit limit)
Find index of an incorrect character in a Bech32 string.
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
const unsigned char * begin() const
DecodeResult Decode(const std::string &str, CharLimit limit)
Decode a Bech32 or Bech32m string.
Bech32m encoding as defined in BIP350.
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.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
bool IsPayToAnchor() const
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
std::string EncodeBase58Check(Span< const unsigned char > input)
Encode a byte span into a base58-encoded string, including checksum.
std::string EncodeExtPubKey(const CExtPubKey &key)
static bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
const CChainParams & Params()
Return the currently selected parameters.
std::variant< CNoDestination, PubKeyDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessV1Taproot, PayToAnchor, WitnessUnknown > CTxDestination
A txout script categorized into standard templates.
const std::byte * begin() 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)
unsigned char * UCharCast(char *c)
An encapsulated private key.
CKey DecodeSecret(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)
const unsigned char * end() const
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