From 14af0b653965b198cae091e9b8f983030456d381 Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Fri, 1 Mar 2019 13:31:02 -0500 Subject: [PATCH] Implement blech32 addresses, expose over RPC --- src/key_io.cpp | 70 ++++++++++++++++++++++++++++++++++++---- src/outputtype.cpp | 21 ++++++++++++ src/outputtype.h | 1 + src/wallet/rpcwallet.cpp | 8 +++++ 4 files changed, 93 insertions(+), 7 deletions(-) diff --git a/src/key_io.cpp b/src/key_io.cpp index c11cc76379243..0b693bad38d00 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -71,14 +71,16 @@ class DestinationEncoder : public boost::static_visitor std::string operator()(const WitnessV0KeyHash& id) const { std::vector 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 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 @@ class DestinationEncoder : public boost::static_visitor { std::vector 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 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 @@ class DestinationEncoder : public boost::static_visitor } std::vector 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 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 pubkey_bytes(data.begin(), data.begin()+33); + data = std::vector(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 diff --git a/src/outputtype.cpp b/src/outputtype.cpp index df5879101c038..cfd054e2f8bfc 100644 --- a/src/outputtype.cpp +++ b/src/outputtype.cpp @@ -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 GetAllDestinationsForKey(const CPubKey& key) { PKHash keyid(key); diff --git a/src/outputtype.h b/src/outputtype.h index 4c4d93bc8b534..3b37e78c13ed4 100644 --- a/src/outputtype.h +++ b/src/outputtype.h @@ -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 GetAllDestinationsForKey(const CPubKey& key); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 17a87b79f8513..6232a9c351824 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -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); }