PIVX Core  5.6.99
P2P Digital Currency
zip32.h
Go to the documentation of this file.
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"
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 class HDSeed {
21 private:
23 
24 public:
25  HDSeed() {}
26  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  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).
46 
47 namespace libzcash {
48 
50 
52  uint8_t depth;
53  uint32_t parentFVKTag;
54  uint32_t childIndex;
58 
59  SERIALIZE_METHODS(SaplingExtendedFullViewingKey, obj) { READWRITE(obj.depth, obj.parentFVKTag, obj.childIndex, obj.chaincode, obj.fvk, obj.dk); }
60 
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.
68 
70 
72  return (
73  a.depth == b.depth &&
74  a.parentFVKTag == b.parentFVKTag &&
75  a.childIndex == b.childIndex &&
76  a.chaincode == b.chaincode &&
77  a.fvk == b.fvk &&
78  a.dk == b.dk);
79  }
81  return (a.depth < b.depth ||
82  (a.depth == b.depth && a.childIndex < b.childIndex) ||
83  (a.depth == b.depth && a.childIndex == b.childIndex && a.fvk < b.fvk));
84  }
85 };
86 
88  uint8_t depth;
89  uint32_t parentFVKTag;
90  uint32_t childIndex;
94 
95  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 
102 
104 
106  {
107  return a.depth == b.depth &&
108  a.parentFVKTag == b.parentFVKTag &&
109  a.childIndex == b.childIndex &&
110  a.chaincode == b.chaincode &&
111  a.expsk == b.expsk &&
112  a.dk == b.dk;
113  }
114 };
115 
116 typedef boost::variant<InvalidEncoding, SaplingExtendedSpendingKey> SpendingKey;
117 typedef boost::variant<InvalidEncoding, SaplingExtendedFullViewingKey> ViewingKey;
118 
119 }
120 
122 bool IsValidSpendingKey(const libzcash::SpendingKey& zkey);
123 
126 
127 #endif // PIVX_SAPLING_ZIP32_H
Definition: zip32.h:20
friend bool operator!=(const HDSeed &a, const HDSeed &b)
Definition: zip32.h:38
uint256 Fingerprint() const
Definition: zip32.cpp:31
HDSeed(const CPrivKey &seedIn)
Definition: zip32.h:26
CPrivKey RawSeed() const
Definition: zip32.h:31
CPrivKey seed
Definition: zip32.h:22
HDSeed()
Definition: zip32.h:25
bool IsNull() const
Definition: zip32.h:29
friend bool operator==(const HDSeed &a, const HDSeed &b)
Definition: zip32.h:33
static HDSeed Random(size_t len=32)
Definition: zip32.cpp:23
88-bit opaque blob.
Definition: uint256.h:116
Sapling functions.
Definition: address.h:30
256-bit opaque blob.
Definition: uint256.h:138
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (PRIVATE_KEY_SIZE bytes)
Definition: key.h:20
boost::variant< InvalidEncoding, SaplingExtendedSpendingKey > SpendingKey
Definition: zip32.h:116
boost::variant< InvalidEncoding, SaplingExtendedFullViewingKey > ViewingKey
Definition: zip32.h:117
blob88 diversifier_index_t
Definition: zip32.h:49
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
#define READWRITE(...)
Definition: serialize.h:183
SERIALIZE_METHODS(SaplingExtendedFullViewingKey, obj)
Definition: zip32.h:59
Optional< std::pair< diversifier_index_t, libzcash::SaplingPaymentAddress > > Address(diversifier_index_t j) const
Definition: zip32.cpp:85
friend bool operator==(const SaplingExtendedFullViewingKey &a, const SaplingExtendedFullViewingKey &b)
Definition: zip32.h:71
libzcash::SaplingFullViewingKey fvk
Definition: zip32.h:56
Optional< SaplingExtendedFullViewingKey > Derive(uint32_t i) const
Definition: zip32.cpp:63
friend bool operator<(const SaplingExtendedFullViewingKey &a, const SaplingExtendedFullViewingKey &b)
Definition: zip32.h:80
libzcash::SaplingPaymentAddress DefaultAddress() const
Definition: zip32.cpp:106
friend bool operator==(const SaplingExtendedSpendingKey &a, const SaplingExtendedSpendingKey &b)
Definition: zip32.h:105
SERIALIZE_METHODS(SaplingExtendedSpendingKey, obj)
Definition: zip32.h:95
libzcash::SaplingExpandedSpendingKey expsk
Definition: zip32.h:92
libzcash::SaplingPaymentAddress DefaultAddress() const
Definition: zip32.cpp:162
SaplingExtendedSpendingKey Derive(uint32_t i) const
Definition: zip32.cpp:132
static SaplingExtendedSpendingKey Master(const HDSeed &seed)
Definition: zip32.cpp:117
SaplingExtendedFullViewingKey ToXFVK() const
Definition: zip32.cpp:150
const size_t ZIP32_XFVK_SIZE
Definition: zip32.h:17
const uint32_t ZIP32_HARDENED_KEY_LIMIT
Definition: zip32.h:16
bool IsValidSpendingKey(const libzcash::SpendingKey &zkey)
Check whether a SpendingKey is not an InvalidEncoding.
Definition: zip32.cpp:169
const size_t ZIP32_XSK_SIZE
Definition: zip32.h:18
bool IsValidViewingKey(const libzcash::ViewingKey &vk)
Check whether a ViewingKey is not an InvalidEncoding.
Definition: zip32.cpp:173
uint256 ovkForShieldingFromTaddr(HDSeed &seed)
Definition: zip32.cpp:38