Skip to content

Commit

Permalink
Implement blech32 addresses, expose over RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs authored and stevenroose committed Mar 20, 2019
1 parent 0f28780 commit 14af0b6
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
70 changes: 63 additions & 7 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,16 @@ class DestinationEncoder : public boost::static_visitor<std::string>
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);
}
Expand All @@ -87,13 +89,15 @@ class DestinationEncoder : public boost::static_visitor<std::string>
{
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);
}
Expand All @@ -105,13 +109,15 @@ class DestinationEncoder : public boost::static_visitor<std::string>
}
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);
}
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions src/outputtype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/outputtype.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 14af0b6

Please sign in to comment.