mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-16 13:01:19 +02:00
Add parent address encoding
This commit is contained in:
parent
d39d440fcf
commit
18ee3417d8
2 changed files with 42 additions and 12 deletions
|
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <base58.h>
|
#include <base58.h>
|
||||||
#include <bech32.h>
|
#include <bech32.h>
|
||||||
|
#include <chainparams.h>
|
||||||
#include <script/script.h>
|
#include <script/script.h>
|
||||||
#include <utilstrencodings.h>
|
#include <utilstrencodings.h>
|
||||||
|
|
||||||
|
|
@ -22,20 +23,24 @@ class DestinationEncoder : public boost::static_visitor<std::string>
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
const CChainParams& m_params;
|
const CChainParams& m_params;
|
||||||
|
// ELEMENTS:
|
||||||
|
const bool for_parent;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DestinationEncoder(const CChainParams& params) : m_params(params) {}
|
DestinationEncoder(const CChainParams& params, const bool for_parent) : m_params(params), for_parent(for_parent) {}
|
||||||
|
|
||||||
std::string operator()(const CKeyID& id) const
|
std::string operator()(const CKeyID& id) const
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
|
CChainParams::Base58Type type = for_parent ? CChainParams::PARENT_PUBKEY_ADDRESS : CChainParams::PUBKEY_ADDRESS;
|
||||||
|
std::vector<unsigned char> data = m_params.Base58Prefix(type);
|
||||||
data.insert(data.end(), id.begin(), id.end());
|
data.insert(data.end(), id.begin(), id.end());
|
||||||
return EncodeBase58Check(data);
|
return EncodeBase58Check(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string operator()(const CScriptID& id) const
|
std::string operator()(const CScriptID& id) const
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> data = m_params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
|
CChainParams::Base58Type type = for_parent ? CChainParams::PARENT_SCRIPT_ADDRESS : CChainParams::SCRIPT_ADDRESS;
|
||||||
|
std::vector<unsigned char> data = m_params.Base58Prefix(type);
|
||||||
data.insert(data.end(), id.begin(), id.end());
|
data.insert(data.end(), id.begin(), id.end());
|
||||||
return EncodeBase58Check(data);
|
return EncodeBase58Check(data);
|
||||||
}
|
}
|
||||||
|
|
@ -64,13 +69,15 @@ public:
|
||||||
std::vector<unsigned char> data = {(unsigned char)id.version};
|
std::vector<unsigned char> data = {(unsigned char)id.version};
|
||||||
data.reserve(1 + (id.length * 8 + 4) / 5);
|
data.reserve(1 + (id.length * 8 + 4) / 5);
|
||||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
|
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
|
||||||
return bech32::Encode(m_params.Bech32HRP(), data);
|
const std::string& hrp = for_parent ? m_params.ParentBech32HRP() : m_params.Bech32HRP();
|
||||||
|
return bech32::Encode(hrp, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string operator()(const CNoDestination& no) const { return {}; }
|
std::string operator()(const CNoDestination& no) const { return {}; }
|
||||||
|
std::string operator()(const NullData& nd) const { return {}; }
|
||||||
};
|
};
|
||||||
|
|
||||||
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params)
|
CTxDestination DecodeDestination(const std::string& str, const CChainParams& params, const bool for_parent)
|
||||||
{
|
{
|
||||||
std::vector<unsigned char> data;
|
std::vector<unsigned char> data;
|
||||||
uint160 hash;
|
uint160 hash;
|
||||||
|
|
@ -78,14 +85,16 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
|
||||||
// base58-encoded Bitcoin addresses.
|
// base58-encoded Bitcoin addresses.
|
||||||
// Public-key-hash-addresses have version 0 (or 111 testnet).
|
// Public-key-hash-addresses have version 0 (or 111 testnet).
|
||||||
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
|
// The data vector contains RIPEMD160(SHA256(pubkey)), where pubkey is the serialized public key.
|
||||||
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(CChainParams::PUBKEY_ADDRESS);
|
CChainParams::Base58Type type_pkh = for_parent ? CChainParams::PARENT_PUBKEY_ADDRESS : CChainParams::PUBKEY_ADDRESS;
|
||||||
|
const std::vector<unsigned char>& pubkey_prefix = params.Base58Prefix(type_pkh);
|
||||||
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
|
if (data.size() == hash.size() + pubkey_prefix.size() && std::equal(pubkey_prefix.begin(), pubkey_prefix.end(), data.begin())) {
|
||||||
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
|
std::copy(data.begin() + pubkey_prefix.size(), data.end(), hash.begin());
|
||||||
return CKeyID(hash);
|
return CKeyID(hash);
|
||||||
}
|
}
|
||||||
// Script-hash-addresses have version 5 (or 196 testnet).
|
// Script-hash-addresses have version 5 (or 196 testnet).
|
||||||
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
|
// The data vector contains RIPEMD160(SHA256(cscript)), where cscript is the serialized redemption script.
|
||||||
const std::vector<unsigned char>& script_prefix = params.Base58Prefix(CChainParams::SCRIPT_ADDRESS);
|
CChainParams::Base58Type type_sh = for_parent ? CChainParams::PARENT_SCRIPT_ADDRESS : CChainParams::SCRIPT_ADDRESS;
|
||||||
|
const std::vector<unsigned char>& script_prefix = params.Base58Prefix(type_sh);
|
||||||
if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
|
if (data.size() == hash.size() + script_prefix.size() && std::equal(script_prefix.begin(), script_prefix.end(), data.begin())) {
|
||||||
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
|
std::copy(data.begin() + script_prefix.size(), data.end(), hash.begin());
|
||||||
return CScriptID(hash);
|
return CScriptID(hash);
|
||||||
|
|
@ -93,7 +102,8 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
|
||||||
}
|
}
|
||||||
data.clear();
|
data.clear();
|
||||||
auto bech = bech32::Decode(str);
|
auto bech = bech32::Decode(str);
|
||||||
if (bech.second.size() > 0 && bech.first == params.Bech32HRP()) {
|
const std::string& hrp = for_parent ? params.ParentBech32HRP() : params.Bech32HRP();
|
||||||
|
if (bech.second.size() > 0 && bech.first == hrp) {
|
||||||
// Bech32 decoding
|
// Bech32 decoding
|
||||||
int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16)
|
int version = bech.second[0]; // The first 5 bit symbol is the witness version (0-16)
|
||||||
// The rest of the symbols are converted witness program bytes.
|
// The rest of the symbols are converted witness program bytes.
|
||||||
|
|
@ -208,20 +218,36 @@ std::string EncodeExtKey(const CExtKey& key)
|
||||||
|
|
||||||
std::string EncodeDestination(const CTxDestination& dest)
|
std::string EncodeDestination(const CTxDestination& dest)
|
||||||
{
|
{
|
||||||
return boost::apply_visitor(DestinationEncoder(Params()), dest);
|
return boost::apply_visitor(DestinationEncoder(Params(), false), dest);
|
||||||
}
|
}
|
||||||
|
|
||||||
CTxDestination DecodeDestination(const std::string& str)
|
CTxDestination DecodeDestination(const std::string& str)
|
||||||
{
|
{
|
||||||
return DecodeDestination(str, Params());
|
return DecodeDestination(str, Params(), false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsValidDestinationString(const std::string& str, const CChainParams& params)
|
bool IsValidDestinationString(const std::string& str, const CChainParams& params)
|
||||||
{
|
{
|
||||||
return IsValidDestination(DecodeDestination(str, params));
|
return IsValidDestination(DecodeDestination(str, params, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsValidDestinationString(const std::string& str)
|
bool IsValidDestinationString(const std::string& str)
|
||||||
{
|
{
|
||||||
return IsValidDestinationString(str, Params());
|
return IsValidDestination(DecodeDestination(str, Params(), false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// ELEMENTS
|
||||||
|
|
||||||
|
std::string EncodeParentDestination(const CTxDestination& dest)
|
||||||
|
{
|
||||||
|
return boost::apply_visitor(DestinationEncoder(Params(), true), dest);
|
||||||
|
}
|
||||||
|
|
||||||
|
CTxDestination DecodeParentDestination(const std::string& str)
|
||||||
|
{
|
||||||
|
return DecodeDestination(str, Params(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ELEMENTS
|
||||||
|
//
|
||||||
|
|
|
||||||
|
|
@ -26,4 +26,8 @@ CTxDestination DecodeDestination(const std::string& str);
|
||||||
bool IsValidDestinationString(const std::string& str);
|
bool IsValidDestinationString(const std::string& str);
|
||||||
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
|
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
|
||||||
|
|
||||||
|
// ELEMENTS
|
||||||
|
std::string EncodeParentDestination(const CTxDestination& dest);
|
||||||
|
CTxDestination DecodeParentDestination(const std::string& str);
|
||||||
|
|
||||||
#endif // BITCOIN_KEY_IO_H
|
#endif // BITCOIN_KEY_IO_H
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue