Line data Source code
1 : // Copyright (c) 2018-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_ZIP32_H 7 : #define PIVX_SAPLING_ZIP32_H 8 : 9 : #include "key.h" 10 : #include "optional.h" 11 : #include "sapling/address.h" 12 : #include "serialize.h" 13 : #include "support/allocators/zeroafterfree.h" 14 : #include "uint256.h" 15 : 16 : const uint32_t ZIP32_HARDENED_KEY_LIMIT = 0x80000000; 17 : const size_t ZIP32_XFVK_SIZE = 169; 18 : const size_t ZIP32_XSK_SIZE = 169; 19 : 20 3014 : class HDSeed { 21 : private: 22 : CPrivKey seed; 23 : 24 : public: 25 2 : HDSeed() {} 26 1506 : explicit HDSeed(const CPrivKey& seedIn) : seed(seedIn) {} 27 : 28 : static HDSeed Random(size_t len = 32); 29 : bool IsNull() const { return seed.empty(); }; 30 : uint256 Fingerprint() const; 31 1508 : CPrivKey RawSeed() const { return seed; } 32 : 33 : friend bool operator==(const HDSeed& a, const HDSeed& b) 34 : { 35 : return a.seed == b.seed; 36 : } 37 : 38 : friend bool operator!=(const HDSeed& a, const HDSeed& b) 39 : { 40 : return !(a == b); 41 : } 42 : }; 43 : 44 : // This is not part of ZIP 32, but is here because it's linked to the seed (for now). 45 : uint256 ovkForShieldingFromTaddr(HDSeed& seed); 46 : 47 : namespace libzcash { 48 : 49 : typedef blob88 diversifier_index_t; 50 : 51 : struct SaplingExtendedFullViewingKey { 52 : uint8_t depth; 53 : uint32_t parentFVKTag; 54 : uint32_t childIndex; 55 : uint256 chaincode; 56 : libzcash::SaplingFullViewingKey fvk; 57 : uint256 dk; 58 : 59 9929 : SERIALIZE_METHODS(SaplingExtendedFullViewingKey, obj) { READWRITE(obj.depth, obj.parentFVKTag, obj.childIndex, obj.chaincode, obj.fvk, obj.dk); } 60 : 61 : Optional<SaplingExtendedFullViewingKey> Derive(uint32_t i) const; 62 : 63 : // Returns the first index starting from j that generates a valid 64 : // payment address, along with the corresponding address. Returns 65 : // an error if the diversifier space is exhausted. 66 : Optional<std::pair<diversifier_index_t, libzcash::SaplingPaymentAddress>> 67 : Address(diversifier_index_t j) const; 68 : 69 : libzcash::SaplingPaymentAddress DefaultAddress() const; 70 : 71 1002 : friend inline bool operator==(const SaplingExtendedFullViewingKey& a, const SaplingExtendedFullViewingKey& b) { 72 1002 : return ( 73 2004 : a.depth == b.depth && 74 1002 : a.parentFVKTag == b.parentFVKTag && 75 1002 : a.childIndex == b.childIndex && 76 1002 : a.chaincode == b.chaincode && 77 2004 : a.fvk == b.fvk && 78 1002 : a.dk == b.dk); 79 : } 80 158283 : friend inline bool operator<(const SaplingExtendedFullViewingKey& a, const SaplingExtendedFullViewingKey& b) { 81 147587 : return (a.depth < b.depth || 82 305870 : (a.depth == b.depth && a.childIndex < b.childIndex) || 83 29882 : (a.depth == b.depth && a.childIndex == b.childIndex && a.fvk < b.fvk)); 84 : } 85 : }; 86 : 87 : struct SaplingExtendedSpendingKey { 88 : uint8_t depth; 89 : uint32_t parentFVKTag; 90 : uint32_t childIndex; 91 : uint256 chaincode; 92 : libzcash::SaplingExpandedSpendingKey expsk; 93 : uint256 dk; 94 : 95 24265 : SERIALIZE_METHODS(SaplingExtendedSpendingKey, obj) { READWRITE(obj.depth, obj.parentFVKTag, obj.childIndex, obj.chaincode, obj.expsk, obj.dk); } 96 : 97 : static SaplingExtendedSpendingKey Master(const HDSeed& seed); 98 : 99 : SaplingExtendedSpendingKey Derive(uint32_t i) const; 100 : 101 : SaplingExtendedFullViewingKey ToXFVK() const; 102 : 103 : libzcash::SaplingPaymentAddress DefaultAddress() const; 104 : 105 2002 : friend bool operator==(const SaplingExtendedSpendingKey& a, const SaplingExtendedSpendingKey& b) 106 : { 107 4004 : return a.depth == b.depth && 108 2002 : a.parentFVKTag == b.parentFVKTag && 109 2002 : a.childIndex == b.childIndex && 110 2002 : a.chaincode == b.chaincode && 111 4004 : a.expsk == b.expsk && 112 2002 : a.dk == b.dk; 113 : } 114 : }; 115 : 116 : typedef boost::variant<InvalidEncoding, SaplingExtendedSpendingKey> SpendingKey; 117 : typedef boost::variant<InvalidEncoding, SaplingExtendedFullViewingKey> ViewingKey; 118 : 119 : } 120 : 121 : /** Check whether a SpendingKey is not an InvalidEncoding. */ 122 : bool IsValidSpendingKey(const libzcash::SpendingKey& zkey); 123 : 124 : /** Check whether a ViewingKey is not an InvalidEncoding. */ 125 : bool IsValidViewingKey(const libzcash::ViewingKey& vk); 126 : 127 : #endif // PIVX_SAPLING_ZIP32_H