diff --git a/src/key_io.cpp b/src/key_io.cpp index 46e2559077102..4b6965bf74875 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -341,3 +341,21 @@ std::vector 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 data_in = {}; + // Set 0 as the silent payments version + std::vector 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); +} + diff --git a/src/key_io.h b/src/key_io.h index 9c67d819141d1..c2e167533ec1b 100644 --- a/src/key_io.h +++ b/src/key_io.h @@ -27,6 +27,7 @@ CTxDestination DecodeDestination(const std::string& str, std::string& error_msg, std::pair DecodeSilentData(const std::vector& data); std::vector 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);