Skip to content

Commit

Permalink
Add a function for encoding an SP address
Browse files Browse the repository at this point in the history
  • Loading branch information
josibake committed Jul 21, 2023
1 parent ae6019b commit 439e57a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/key_io.cpp
Expand Up @@ -341,3 +341,21 @@ std::vector<unsigned char> DecodeSilentAddress(const std::string& str)
if ((version == 0 && silent_payment_data.size() != SILENT_PAYMENT_V0_DATA_SIZE) || silent_payment_data.size() < SILENT_PAYMENT_V0_DATA_SIZE) return {};
return silent_payment_data;
}

std::string EncodeSilentDestination(const CPubKey& scan_pubkey, const CPubKey& spend_pubkey)
{
// The data_in is scan_pubkey + spend_pubkey
std::vector<unsigned char> data_in = {};
// Set 0 as the silent payments version
std::vector<unsigned char> data_out = {0};

data_in.insert(data_in.end(), scan_pubkey.begin(), scan_pubkey.end());
data_in.insert(data_in.end(), spend_pubkey.begin(), spend_pubkey.end());

ConvertBits<8, 5, true>([&](unsigned char c) { data_out.push_back(c); }, data_in.begin(), data_in.end());

std::string hrp = Params().SilentPaymentHRP();

return bech32::Encode(bech32::Encoding::BECH32M, hrp, data_out);
}

1 change: 1 addition & 0 deletions src/key_io.h
Expand Up @@ -27,6 +27,7 @@ CTxDestination DecodeDestination(const std::string& str, std::string& error_msg,

std::pair<CPubKey, CPubKey> DecodeSilentData(const std::vector<unsigned char>& data);
std::vector<unsigned char> DecodeSilentAddress(const std::string& str);
std::string EncodeSilentDestination(const CPubKey& scan_pubkey, const CPubKey& spend_pubkey);

bool IsValidDestinationString(const std::string& str);
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
Expand Down

0 comments on commit 439e57a

Please sign in to comment.