From 6f1a54cd1689925a8bcae3f55eaef2a640b4d270 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 29 Jun 2021 19:49:00 +0100 Subject: [PATCH] Introduce libzcash::RawAddress type Currently, `libzcash::PaymentAddress` serves two purposes: it represents the result of parsing a string encoding provided by a user, and it holds the possible shielded protocol addresses that can be used in transaction outputs. In order to add support for Unified Addresses, we split these purposes across two separate types. We also add two new visitors to enable converting between these types: - `RecipientForPaymentAddress` returns the "preferred protocol address" that should be used in transaction outputs, or `std::nullopt` if the payment address encoding was invalid. - `GetRawAddresses` returns all protocol addresses contained within the given payment address, or an empty set if the payment address encoding was invalid. For the existing address encodings, the implementations of these are trivial. --- src/zcash/Address.cpp | 36 ++++++++++++++++++++++++++++++++++++ src/zcash/Address.hpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/src/zcash/Address.cpp b/src/zcash/Address.cpp index 4c1fad1c7..40d4c4089 100644 --- a/src/zcash/Address.cpp +++ b/src/zcash/Address.cpp @@ -35,3 +35,39 @@ bool IsValidViewingKey(const libzcash::ViewingKey& vk) { bool IsValidSpendingKey(const libzcash::SpendingKey& zkey) { return !std::holds_alternative(zkey); } + +std::optional RecipientForPaymentAddress::operator()( + const libzcash::InvalidEncoding& no) const +{ + return std::nullopt; +} + +std::optional RecipientForPaymentAddress::operator()( + const libzcash::SproutPaymentAddress &zaddr) const +{ + return zaddr; +} + +std::optional RecipientForPaymentAddress::operator()( + const libzcash::SaplingPaymentAddress &zaddr) const +{ + return zaddr; +} + +std::set GetRawAddresses::operator()( + const libzcash::InvalidEncoding& no) const +{ + return {}; +} + +std::set GetRawAddresses::operator()( + const libzcash::SproutPaymentAddress &zaddr) const +{ + return {zaddr}; +} + +std::set GetRawAddresses::operator()( + const libzcash::SaplingPaymentAddress &zaddr) const +{ + return {zaddr}; +} diff --git a/src/zcash/Address.hpp b/src/zcash/Address.hpp index a8fbacac9..cb1edf082 100644 --- a/src/zcash/Address.hpp +++ b/src/zcash/Address.hpp @@ -8,12 +8,16 @@ #include namespace libzcash { +/** Protocol addresses that can receive funds in a transaction. */ +typedef std::variant RawAddress; + class InvalidEncoding { public: friend bool operator==(const InvalidEncoding &a, const InvalidEncoding &b) { return true; } friend bool operator<(const InvalidEncoding &a, const InvalidEncoding &b) { return true; } }; +/** Addresses that can appear in a string encoding. */ typedef std::variant PaymentAddress; typedef std::variant ViewingKey; typedef std::variant SpendingKey; @@ -43,4 +47,28 @@ bool IsValidViewingKey(const libzcash::ViewingKey& vk); /** Check whether a SpendingKey is not an InvalidEncoding. */ bool IsValidSpendingKey(const libzcash::SpendingKey& zkey); +/** + * Returns the protocol address that should be used in transaction outputs. + */ +class RecipientForPaymentAddress { +public: + RecipientForPaymentAddress() {} + + std::optional operator()(const libzcash::InvalidEncoding& no) const; + std::optional operator()(const libzcash::SproutPaymentAddress &zaddr) const; + std::optional operator()(const libzcash::SaplingPaymentAddress &zaddr) const; +}; + +/** + * Returns all protocol addresses contained within the given payment address. + */ +class GetRawAddresses { +public: + GetRawAddresses() {} + + std::set operator()(const libzcash::InvalidEncoding& no) const; + std::set operator()(const libzcash::SproutPaymentAddress &zaddr) const; + std::set operator()(const libzcash::SaplingPaymentAddress &zaddr) const; +}; + #endif // ZC_ADDRESS_H_