12 #include <boost/variant/static_visitor.hpp>
20 std::vector<uint8_t> PackAddrData(
const T &
id, uint8_t type) {
21 uint8_t version_byte(type << 3);
22 size_t size =
id.size();
23 uint8_t encoded_size = 0;
50 throw std::runtime_error(
51 "Error packing cashaddr: invalid address length");
53 version_byte |= encoded_size;
54 std::vector<uint8_t> data = {version_byte};
55 data.insert(data.end(), std::begin(
id), std::end(
id));
57 std::vector<uint8_t> converted;
61 converted.reserve(((size + 1) * 8 + 4) / 5);
62 ConvertBits<8, 5, true>([&](uint8_t c) { converted.push_back(c); },
63 std::begin(data), std::end(data));
69 class CashAddrEncoder :
public boost::static_visitor<std::string> {
71 explicit CashAddrEncoder(
const CChainParams &p) : params(p) {}
73 std::string operator()(
const PKHash &
id)
const {
74 std::vector<uint8_t> data = PackAddrData(
id,
PUBKEY_TYPE);
78 std::string operator()(
const ScriptHash &
id)
const {
79 std::vector<uint8_t> data = PackAddrData(
id,
SCRIPT_TYPE);
83 std::string operator()(
const CNoDestination &)
const {
return ""; }
93 return boost::apply_visitor(CashAddrEncoder(params), dst);
98 std::vector<uint8_t> data = PackAddrData(content.
hash, content.
type);
106 if (content.
hash.size() == 0) {
114 const std::string &expectedPrefix) {
117 if (
prefix != expectedPrefix) {
121 if (payload.empty()) {
125 std::vector<uint8_t> data;
126 data.reserve(payload.size() * 5 / 8);
127 if (!ConvertBits<5, 8, false>([&](uint8_t c) { data.push_back(c); },
128 begin(payload), end(payload))) {
133 uint8_t version = data[0];
134 if (version & 0x80) {
140 uint32_t hash_size = 20 + 4 * (version & 0x03);
141 if (version & 0x04) {
146 if (data.size() != hash_size + 1) {
151 data.erase(data.begin());
152 return {type, std::move(data)};
156 if (content.
hash.size() != 20) {
162 std::copy(begin(content.
hash), end(content.
hash), hash.
begin());
164 switch (content.
type) {
177 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)
std::vector< uint8_t > PackCashAddrContent(const CashAddrContent &content)
CTxDestination DecodeCashAddr(const std::string &addr, const CChainParams ¶ms)
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.
boost::variant< CNoDestination, PKHash, ScriptHash > CTxDestination
A txout script template with a specific destination.
std::vector< uint8_t > hash