19std::vector<uint8_t> PackAddrData(
const T &
id, uint8_t type) {
20 uint8_t version_byte(type << 3);
21 size_t size =
id.size();
22 uint8_t encoded_size = 0;
49 throw std::runtime_error(
50 "Error packing cashaddr: invalid address length");
52 version_byte |= encoded_size;
53 std::vector<uint8_t> data = {version_byte};
54 data.insert(data.end(), std::begin(
id), std::end(
id));
56 std::vector<uint8_t> converted;
60 converted.reserve(((size + 1) * 8 + 4) / 5);
61 ConvertBits<8, 5, true>([&](uint8_t c) { converted.push_back(c); },
62 std::begin(data), std::end(data));
68class CashAddrEncoder {
70 explicit CashAddrEncoder(
const CChainParams &p) : params(p) {}
72 std::string operator()(
const PKHash &
id)
const {
73 std::vector<uint8_t> data = PackAddrData(
id,
PUBKEY_TYPE);
77 std::string operator()(
const ScriptHash &
id)
const {
78 std::vector<uint8_t> data = PackAddrData(
id,
SCRIPT_TYPE);
82 std::string operator()(
const CNoDestination &)
const {
return ""; }
92 return std::visit(CashAddrEncoder(params), dst);
97 std::vector<uint8_t> data = PackAddrData(content.
hash, content.
type);
105 if (content.
hash.size() == 0) {
113 const std::string &expectedPrefix) {
116 if (
prefix != expectedPrefix) {
120 if (payload.empty()) {
124 std::vector<uint8_t> data;
125 data.reserve(payload.size() * 5 / 8);
126 if (!ConvertBits<5, 8, false>([&](uint8_t c) { data.push_back(c); },
127 begin(payload), end(payload))) {
132 uint8_t version = data[0];
133 if (version & 0x80) {
139 uint32_t hash_size = 20 + 4 * (version & 0x03);
140 if (version & 0x04) {
145 if (data.size() != hash_size + 1) {
150 data.erase(data.begin());
151 return {type, std::move(data)};
155 if (content.
hash.size() != 20) {
161 std::copy(begin(content.
hash), end(content.
hash), hash.
begin());
163 switch (content.
type) {
176 return PackAddrData(content.
hash, content.
type);
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams ¶ms)
CashAddrContent DecodeCashAddrContent(const std::string &addr, const std::string &expectedPrefix)
CTxDestination DecodeCashAddrDestination(const CashAddrContent &content)
CTxDestination DecodeCashAddr(const std::string &addr, const CChainParams ¶ms)
std::vector< uint8_t > PackCashAddrContent(const CashAddrContent &content)
CChainParams defines various tweakable parameters of a given instance of the Bitcoin system.
const std::string & CashAddrPrefix() const
std::pair< std::string, data > Decode(const std::string &str, const std::string &default_prefix)
Decode a cashaddr string.
std::string Encode(const std::string &prefix, const data &payload)
Encode a cashaddr string.
std::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
std::vector< uint8_t > hash