mirror of
https://github.com/ElementsProject/elements.git
synced 2026-08-15 12:51:00 +02:00
Implement blech32 addresses, expose over RPC
This commit is contained in:
parent
0f28780364
commit
14af0b6539
4 changed files with 93 additions and 7 deletions
|
|
@ -71,14 +71,16 @@ public:
|
|||
std::string operator()(const WitnessV0KeyHash& id) const
|
||||
{
|
||||
std::vector<unsigned char> data = {0};
|
||||
data.reserve(33);
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
|
||||
data.reserve(53);
|
||||
if (id.blinding_pubkey.IsFullyValid()) {
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.blinding_pubkey.begin(), id.blinding_pubkey.end());
|
||||
std::vector<unsigned char> bytes(id.blinding_pubkey.begin(), id.blinding_pubkey.end());
|
||||
bytes.insert(bytes.end(), id.begin(), id.end());
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, bytes.begin(), bytes.end());
|
||||
const std::string& hrp = for_parent ? m_params.ParentBlech32HRP() : m_params.Blech32HRP();
|
||||
return blech32::Encode(hrp, data);
|
||||
}
|
||||
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
|
||||
const std::string& hrp = for_parent ? m_params.ParentBech32HRP() : m_params.Bech32HRP();
|
||||
return bech32::Encode(hrp, data);
|
||||
}
|
||||
|
|
@ -87,13 +89,15 @@ public:
|
|||
{
|
||||
std::vector<unsigned char> data = {0};
|
||||
data.reserve(53);
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
|
||||
if (id.blinding_pubkey.IsFullyValid()) {
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.blinding_pubkey.begin(), id.blinding_pubkey.end());
|
||||
std::vector<unsigned char> bytes(id.blinding_pubkey.begin(), id.blinding_pubkey.end());
|
||||
bytes.insert(bytes.end(), id.begin(), id.end());
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, bytes.begin(), bytes.end());
|
||||
const std::string& hrp = for_parent ? m_params.ParentBlech32HRP() : m_params.Blech32HRP();
|
||||
return blech32::Encode(hrp, data);
|
||||
}
|
||||
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.begin(), id.end());
|
||||
const std::string& hrp = for_parent ? m_params.ParentBech32HRP() : m_params.Bech32HRP();
|
||||
return bech32::Encode(hrp, data);
|
||||
}
|
||||
|
|
@ -105,13 +109,15 @@ public:
|
|||
}
|
||||
std::vector<unsigned char> data = {(unsigned char)id.version};
|
||||
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);
|
||||
if (id.blinding_pubkey.IsFullyValid()) {
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.blinding_pubkey.begin(), id.blinding_pubkey.end());
|
||||
std::vector<unsigned char> bytes(id.blinding_pubkey.begin(), id.blinding_pubkey.end());
|
||||
bytes.insert(bytes.end(), id.program, id.program + id.length);
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, bytes.begin(), bytes.end());
|
||||
const std::string& hrp = for_parent ? m_params.ParentBlech32HRP() : m_params.Blech32HRP();
|
||||
return blech32::Encode(hrp, data);
|
||||
}
|
||||
|
||||
ConvertBits<8, 5, true>([&](unsigned char c) { data.push_back(c); }, id.program, id.program + id.length);
|
||||
const std::string& hrp = for_parent ? m_params.ParentBech32HRP() : m_params.Bech32HRP();
|
||||
return bech32::Encode(hrp, data);
|
||||
}
|
||||
|
|
@ -201,6 +207,56 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par
|
|||
return unk;
|
||||
}
|
||||
}
|
||||
// ELEMENTS confidential addresses: version + 8to5(ecdhkey || witness program)
|
||||
data.clear();
|
||||
auto blech = blech32::Decode(str);
|
||||
const std::string& bl_hrp = for_parent ? params.ParentBlech32HRP() : params.Blech32HRP();
|
||||
if (blech.second.size() > 0 && blech.first == bl_hrp) {
|
||||
// Blech32 decoding
|
||||
int version = blech.second[0]; // The first 5 bit symbol is the witness version (0-16)
|
||||
|
||||
data.reserve(((blech.second.size() - 1) * 5) / 8);
|
||||
|
||||
// The rest of the symbols are converted blinding pubkey and witness program bytes.
|
||||
if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, blech.second.begin() + 1, blech.second.end())) {
|
||||
// Must be long enough for blinding key and other data taken below
|
||||
if (data.size() < 34) {
|
||||
return CNoDestination();
|
||||
}
|
||||
std::vector<unsigned char> pubkey_bytes(data.begin(), data.begin()+33);
|
||||
data = std::vector<unsigned char>(data.begin()+33, data.end());
|
||||
CPubKey blinding_pubkey(pubkey_bytes);
|
||||
if (version == 0) {
|
||||
{
|
||||
WitnessV0KeyHash keyid;
|
||||
if (data.size() == keyid.size()) {
|
||||
std::copy(data.begin(), data.end(), keyid.begin());
|
||||
keyid.blinding_pubkey = blinding_pubkey;
|
||||
return keyid;
|
||||
}
|
||||
}
|
||||
{
|
||||
WitnessV0ScriptHash scriptid;
|
||||
if (data.size() == scriptid.size()) {
|
||||
std::copy(data.begin(), data.end(), scriptid.begin());
|
||||
scriptid.blinding_pubkey = blinding_pubkey;
|
||||
return scriptid;
|
||||
}
|
||||
}
|
||||
return CNoDestination();
|
||||
}
|
||||
if (version > 16 || data.size() < 2 || data.size() > 40) {
|
||||
return CNoDestination();
|
||||
}
|
||||
WitnessUnknown unk;
|
||||
unk.version = version;
|
||||
std::copy(data.begin(), data.end(), unk.program);
|
||||
unk.blinding_pubkey = blinding_pubkey;
|
||||
unk.length = data.size();
|
||||
return unk;
|
||||
}
|
||||
}
|
||||
|
||||
return CNoDestination();
|
||||
}
|
||||
} // namespace
|
||||
|
|
|
|||
|
|
@ -61,6 +61,27 @@ CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type)
|
|||
}
|
||||
}
|
||||
|
||||
// Elements
|
||||
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType type, const CPubKey& blinding_pubkey)
|
||||
{
|
||||
switch (type) {
|
||||
case OutputType::LEGACY: return PKHash(key, blinding_pubkey);
|
||||
case OutputType::P2SH_SEGWIT:
|
||||
case OutputType::BECH32: {
|
||||
if (!key.IsCompressed()) return PKHash(key, blinding_pubkey);
|
||||
CTxDestination witdest = WitnessV0KeyHash(PKHash(key), blinding_pubkey);
|
||||
CScript witprog = GetScriptForDestination(witdest);
|
||||
if (type == OutputType::P2SH_SEGWIT) {
|
||||
return ScriptHash(witprog, blinding_pubkey);
|
||||
} else {
|
||||
return witdest;
|
||||
}
|
||||
}
|
||||
default: assert(false);
|
||||
}
|
||||
}
|
||||
//
|
||||
|
||||
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key)
|
||||
{
|
||||
PKHash keyid(key);
|
||||
|
|
|
|||
|
|
@ -34,6 +34,7 @@ const std::string& FormatOutputType(OutputType type);
|
|||
* The caller must make sure LearnRelatedScripts has been called beforehand.
|
||||
*/
|
||||
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType);
|
||||
CTxDestination GetDestinationForKey(const CPubKey& key, OutputType, const CPubKey& blinding_pubkey);
|
||||
|
||||
/** Get all destinations (potentially) supported by the wallet for the given key. */
|
||||
std::vector<CTxDestination> GetAllDestinationsForKey(const CPubKey& key);
|
||||
|
|
|
|||
|
|
@ -203,6 +203,10 @@ static UniValue getnewaddress(const JSONRPCRequest& request)
|
|||
}
|
||||
pwallet->LearnRelatedScripts(newKey, output_type);
|
||||
CTxDestination dest = GetDestinationForKey(newKey, output_type);
|
||||
if (g_con_elementswitness) {
|
||||
CPubKey blinding_pubkey = pwallet->GetBlindingPubKey(GetScriptForDestination(dest));
|
||||
dest = GetDestinationForKey(newKey, output_type, blinding_pubkey);
|
||||
}
|
||||
|
||||
pwallet->SetAddressBook(dest, label, "receive");
|
||||
|
||||
|
|
@ -258,6 +262,10 @@ static UniValue getrawchangeaddress(const JSONRPCRequest& request)
|
|||
|
||||
pwallet->LearnRelatedScripts(vchPubKey, output_type);
|
||||
CTxDestination dest = GetDestinationForKey(vchPubKey, output_type);
|
||||
if (g_con_elementswitness) {
|
||||
CPubKey blinding_pubkey = pwallet->GetBlindingPubKey(GetScriptForDestination(dest));
|
||||
dest = GetDestinationForKey(vchPubKey, output_type, blinding_pubkey);
|
||||
}
|
||||
|
||||
return EncodeDestination(dest);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue