1 // Copyright (c) 2018 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
9 #include "support/allocators/secure.h"
11 #include "zcash/Address.hpp"
13 #include <boost/optional.hpp>
15 const uint32_t ZIP32_HARDENED_KEY_LIMIT = 0x80000000;
16 const size_t ZIP32_XFVK_SIZE = 169;
17 const size_t ZIP32_XSK_SIZE = 169;
19 typedef std::vector<unsigned char, secure_allocator<unsigned char>> RawHDSeed;
27 HDSeed(RawHDSeed& seedIn) : seed(seedIn) {}
29 static HDSeed Random(size_t len = 32);
30 bool IsNull() const { return seed.empty(); };
31 uint256 Fingerprint() const;
32 RawHDSeed RawSeed() const { return seed; }
34 friend bool operator==(const HDSeed& a, const HDSeed& b)
36 return a.seed == b.seed;
39 friend bool operator!=(const HDSeed& a, const HDSeed& b)
45 // This is not part of ZIP 32, but is here because it's linked to the HD seed.
46 uint256 ovkForShieldingFromTaddr(HDSeed& seed);
50 typedef blob88 diversifier_index_t;
52 struct SaplingExtendedFullViewingKey {
54 uint32_t parentFVKTag;
57 libzcash::SaplingFullViewingKey fvk;
60 ADD_SERIALIZE_METHODS;
62 template <typename Stream, typename Operation>
63 inline void SerializationOp(Stream& s, Operation ser_action) {
65 READWRITE(parentFVKTag);
66 READWRITE(childIndex);
72 boost::optional<SaplingExtendedFullViewingKey> Derive(uint32_t i) const;
74 // Returns the first index starting from j that generates a valid
75 // payment address, along with the corresponding address. Returns
76 // an error if the diversifier space is exhausted.
77 boost::optional<std::pair<diversifier_index_t, libzcash::SaplingPaymentAddress>>
78 Address(diversifier_index_t j) const;
80 libzcash::SaplingPaymentAddress DefaultAddress() const;
82 friend inline bool operator==(const SaplingExtendedFullViewingKey& a, const SaplingExtendedFullViewingKey& b) {
85 a.parentFVKTag == b.parentFVKTag &&
86 a.childIndex == b.childIndex &&
87 a.chaincode == b.chaincode &&
91 friend inline bool operator<(const SaplingExtendedFullViewingKey& a, const SaplingExtendedFullViewingKey& b) {
92 return (a.depth < b.depth ||
93 (a.depth == b.depth && a.childIndex < b.childIndex) ||
94 (a.depth == b.depth && a.childIndex == b.childIndex && a.fvk < b.fvk));
98 struct SaplingExtendedSpendingKey {
100 uint32_t parentFVKTag;
103 libzcash::SaplingExpandedSpendingKey expsk;
106 ADD_SERIALIZE_METHODS;
108 template <typename Stream, typename Operation>
109 inline void SerializationOp(Stream& s, Operation ser_action) {
111 READWRITE(parentFVKTag);
112 READWRITE(childIndex);
113 READWRITE(chaincode);
118 static SaplingExtendedSpendingKey Master(const HDSeed& seed);
120 SaplingExtendedSpendingKey Derive(uint32_t i) const;
122 SaplingExtendedFullViewingKey ToXFVK() const;
124 libzcash::SaplingPaymentAddress DefaultAddress() const;
126 friend bool operator==(const SaplingExtendedSpendingKey& a, const SaplingExtendedSpendingKey& b)
128 return a.depth == b.depth &&
129 a.parentFVKTag == b.parentFVKTag &&
130 a.childIndex == b.childIndex &&
131 a.chaincode == b.chaincode &&
132 a.expsk == b.expsk &&
137 typedef boost::variant<InvalidEncoding, SproutSpendingKey, SaplingExtendedSpendingKey> SpendingKey;
141 /** Check whether a SpendingKey is not an InvalidEncoding. */
142 bool IsValidSpendingKey(const libzcash::SpendingKey& zkey);
144 #endif // ZCASH_ZIP32_H