LCOV - code coverage report
Current view: top level - src/sapling - address.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 24 25 96.0 %
Date: 2025-02-23 09:33:43 Functions: 5 5 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2016-2020 The ZCash developers
       2             : // Copyright (c) 2021 The PIVX Core developers
       3             : // Distributed under the MIT software license, see the accompanying
       4             : // file COPYING or https://www.opensource.org/licenses/mit-license.php.
       5             : 
       6             : #ifndef PIVX_SAPLING_ADDRESS_H
       7             : #define PIVX_SAPLING_ADDRESS_H
       8             : 
       9             : #include "optional.h"
      10             : #include "sapling/sapling.h"
      11             : #include "serialize.h"
      12             : #include "uint256.h"
      13             : 
      14             : #include <array>
      15             : #include <boost/variant.hpp>
      16             : 
      17             : namespace libzcash {
      18             : class InvalidEncoding {
      19             : public:
      20             :     friend bool operator==(const InvalidEncoding &a, const InvalidEncoding &b) { return true; }
      21           0 :     friend bool operator<(const InvalidEncoding &a, const InvalidEncoding &b) { return true; }
      22             : };
      23             : 
      24             : const size_t SerializedSaplingPaymentAddressSize = 43;
      25             : const size_t SerializedSaplingFullViewingKeySize = 96;
      26             : const size_t SerializedSaplingExpandedSpendingKeySize = 96;
      27             : const size_t SerializedSaplingSpendingKeySize = 32;
      28             : 
      29             : //! Sapling functions.
      30             : class SaplingPaymentAddress {
      31             : public:
      32             :     diversifier_t d = {{0}};
      33             :     uint256 pk_d{UINT256_ZERO};
      34             : 
      35       12218 :     SaplingPaymentAddress() {}
      36        6849 :     SaplingPaymentAddress(const diversifier_t& _d, const uint256& _pk_d) : d(_d), pk_d(_pk_d) { }
      37             : 
      38       38223 :     SERIALIZE_METHODS(SaplingPaymentAddress, obj) { READWRITE(obj.d, obj.pk_d); }
      39             : 
      40             :     //! Get the 256-bit SHA256d hash of this payment address.
      41             :     uint256 GetHash() const;
      42             : 
      43        2850 :     friend inline bool operator==(const SaplingPaymentAddress& a, const SaplingPaymentAddress& b) {
      44        2850 :         return a.d == b.d && a.pk_d == b.pk_d;
      45             :     }
      46      276221 :     friend inline bool operator<(const SaplingPaymentAddress& a, const SaplingPaymentAddress& b) {
      47      276221 :         return (a.d < b.d ||
      48      186271 :                 (a.d == b.d && a.pk_d < b.pk_d));
      49             :     }
      50             : };
      51             : 
      52             : class SaplingIncomingViewingKey : public uint256 {
      53             : public:
      54       17759 :     SaplingIncomingViewingKey() : uint256() { }
      55        7240 :     explicit SaplingIncomingViewingKey(uint256 ivk) : uint256(ivk) { }
      56             : 
      57             :     // Can pass in diversifier for Sapling addr
      58             :     Optional<SaplingPaymentAddress> address(diversifier_t d) const;
      59             : };
      60             : 
      61             : class SaplingFullViewingKey {
      62             : public:
      63             :     uint256 ak;
      64             :     uint256 nk;
      65             :     uint256 ovk;
      66             : 
      67             :     SaplingFullViewingKey() : ak(), nk(), ovk() { }
      68       15230 :     SaplingFullViewingKey(uint256 ak, uint256 nk, uint256 ovk) : ak(ak), nk(nk), ovk(ovk) { }
      69             : 
      70       10145 :     SERIALIZE_METHODS(SaplingFullViewingKey, obj) { READWRITE(obj.ak, obj.nk, obj.ovk); }
      71             : 
      72             :     //! Get the fingerprint of this full viewing key (as defined in ZIP 32).
      73             :     uint256 GetFingerprint() const;
      74             : 
      75             :     SaplingIncomingViewingKey in_viewing_key() const;
      76             :     bool is_valid() const;
      77             : 
      78        1133 :     friend inline bool operator==(const SaplingFullViewingKey& a, const SaplingFullViewingKey& b) {
      79        1133 :         return a.ak == b.ak && a.nk == b.nk && a.ovk == b.ovk;
      80             :     }
      81       14186 :     friend inline bool operator<(const SaplingFullViewingKey& a, const SaplingFullViewingKey& b) {
      82       14186 :         return (a.ak < b.ak ||
      83       14186 :             (a.ak == b.ak && a.nk < b.nk) ||
      84       14177 :             (a.ak == b.ak && a.nk == b.nk && a.ovk < b.ovk));
      85             :     }
      86             : };
      87             : 
      88             : 
      89             : class SaplingExpandedSpendingKey {
      90             : public:
      91             :     uint256 ask;
      92             :     uint256 nsk;
      93             :     uint256 ovk;
      94             : 
      95         316 :     SaplingExpandedSpendingKey() : ask(), nsk(), ovk() { }
      96          72 :     SaplingExpandedSpendingKey(uint256 ask, uint256 nsk, uint256 ovk) : ask(ask), nsk(nsk), ovk(ovk) { }
      97             : 
      98       24265 :     SERIALIZE_METHODS(SaplingExpandedSpendingKey, obj) { READWRITE(obj.ask, obj.nsk, obj.ovk); }
      99             : 
     100             :     SaplingFullViewingKey full_viewing_key() const;
     101             :     bool IsNull() { return ask.IsNull() && nsk.IsNull() && ovk.IsNull(); }
     102             : 
     103        2012 :     friend inline bool operator==(const SaplingExpandedSpendingKey& a, const SaplingExpandedSpendingKey& b) {
     104        2012 :         return a.ask == b.ask && a.nsk == b.nsk && a.ovk == b.ovk;
     105             :     }
     106             :     friend inline bool operator<(const SaplingExpandedSpendingKey& a, const SaplingExpandedSpendingKey& b) {
     107             :         return (a.ask < b.ask ||
     108             :             (a.ask == b.ask && a.nsk < b.nsk) ||
     109             :             (a.ask == b.ask && a.nsk == b.nsk && a.ovk < b.ovk));
     110             :     }
     111             : };
     112             : 
     113             : class SaplingSpendingKey : public uint256 {
     114             : public:
     115             :     SaplingSpendingKey() : uint256() { }
     116          23 :     explicit SaplingSpendingKey(uint256 sk) : uint256(sk) { }
     117             : 
     118             :     static SaplingSpendingKey random();
     119             : 
     120             :     SaplingExpandedSpendingKey expanded_spending_key() const;
     121             :     SaplingFullViewingKey full_viewing_key() const;
     122             : 
     123             :     // Can derive Sapling addr from default diversifier
     124             :     SaplingPaymentAddress default_address() const;
     125             : };
     126             : 
     127             : typedef boost::variant<InvalidEncoding, SaplingPaymentAddress> PaymentAddress;
     128             : 
     129             : }
     130             : 
     131             : /** Check whether a PaymentAddress is not an InvalidEncoding. */
     132             : bool IsValidPaymentAddress(const libzcash::PaymentAddress& zaddr);
     133             : 
     134             : #endif // PIVX_SAPLING_ADDRESS_H

Generated by: LCOV version 1.14