Add debug printing for receivers and recipient addresses.

This commit is contained in:
Kris Nuttycombe 2022-03-18 11:08:04 -06:00
parent 2d673ca704
commit 7c3afad111
3 changed files with 38 additions and 33 deletions

View File

@ -69,40 +69,8 @@ TEST(Keys, EncodeAndDecodeSapling)
#define MAKE_STRING(x) std::string((x), (x) + sizeof(x))
namespace libzcash {
class ReceiverToString {
public:
ReceiverToString() {}
std::string operator()(const OrchardRawAddress &zaddr) const {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << zaddr;
return tfm::format("Orchard(%s)", HexStr(ss.begin(), ss.end()));
}
std::string operator()(const SaplingPaymentAddress &zaddr) const {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << zaddr;
return tfm::format("Sapling(%s)", HexStr(ss.begin(), ss.end()));
}
std::string operator()(const CScriptID &p2sh) const {
return tfm::format("P2SH(%s)", p2sh.GetHex());
}
std::string operator()(const CKeyID &p2pkh) const {
return tfm::format("P2PKH(%s)", p2pkh.GetHex());
}
std::string operator()(const UnknownReceiver &unknown) const {
return tfm::format(
"Unknown(%x, %s)",
unknown.typecode,
HexStr(unknown.data.begin(), unknown.data.end()));
}
};
void PrintTo(const Receiver& receiver, std::ostream* os) {
*os << std::visit(ReceiverToString(), receiver);
*os << DebugPrintReceiver(receiver);
}
void PrintTo(const UnifiedAddress& ua, std::ostream* os) {
*os << "UnifiedAddress(" << testing::PrintToString(ua.GetReceiversAsParsed()) << ")";

View File

@ -2,9 +2,11 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
#include "tinyformat.h"
#include "zcash/Address.hpp"
#include "unified.h"
#include "util/match.h"
#include "utilstrencodings.h"
#include <rust/unified_keys.h>
@ -39,6 +41,37 @@ Receiver libzcash::RecipientAddressToReceiver(const RecipientAddress& recipient)
}, recipient);
}
std::string libzcash::DebugPrintReceiver(const Receiver& receiver) {
return std::visit(match {
[&](const OrchardRawAddress &zaddr) {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << zaddr;
return tfm::format("Orchard(%s)", HexStr(ss.begin(), ss.end()));
},
[&](const SaplingPaymentAddress &zaddr) {
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
ss << zaddr;
return tfm::format("Sapling(%s)", HexStr(ss.begin(), ss.end()));
},
[&](const CScriptID &p2sh) {
return tfm::format("P2SH(%s)", p2sh.GetHex());
},
[&](const CKeyID &p2pkh) {
return tfm::format("P2PKH(%s)", p2pkh.GetHex());
},
[&](const UnknownReceiver &unknown) {
return tfm::format(
"Unknown(%x, %s)",
unknown.typecode,
HexStr(unknown.data.begin(), unknown.data.end()));
}
}, receiver);
};
std::string libzcash::DebugPrintRecipientAddress(const RecipientAddress& addr) {
return DebugPrintReceiver(RecipientAddressToReceiver(addr));
}
std::optional<ZcashdUnifiedSpendingKey> ZcashdUnifiedSpendingKey::ForAccount(
const HDSeed& seed,
uint32_t bip44CoinType,

View File

@ -57,6 +57,8 @@ typedef std::variant<
libzcash::SaplingPaymentAddress,
libzcash::OrchardRawAddress> RecipientAddress;
std::string DebugPrintRecipientAddress(const RecipientAddress& add);
class TransparentChangeRequest {
private:
const diversifier_index_t& index;
@ -126,6 +128,8 @@ typedef std::variant<
Receiver RecipientAddressToReceiver(const RecipientAddress& recipient);
std::string DebugPrintReceiver(const Receiver& receiver);
/**
* An internal identifier for a unified full viewing key, derived as a
* blake2b hash of the serialized form of the UFVK.