]>
Commit | Line | Data |
---|---|---|
4e4aa5b6 | 1 | #ifndef ZC_ADDRESS_H_ |
2 | #define ZC_ADDRESS_H_ | |
369df065 SB |
3 | |
4 | #include "uint256.h" | |
defe37a6 | 5 | #include "uint252.h" |
369df065 | 6 | #include "serialize.h" |
9e1c2c40 | 7 | #include "Zcash.h" |
369df065 | 8 | |
e5eab182 JG |
9 | #include <boost/variant.hpp> |
10 | ||
369df065 | 11 | namespace libzcash { |
e5eab182 JG |
12 | class InvalidEncoding { |
13 | public: | |
14 | friend bool operator==(const InvalidEncoding &a, const InvalidEncoding &b) { return true; } | |
15 | friend bool operator<(const InvalidEncoding &a, const InvalidEncoding &b) { return true; } | |
16 | }; | |
369df065 | 17 | |
74f5b010 JG |
18 | const size_t SerializedSproutPaymentAddressSize = 64; |
19 | const size_t SerializedSproutViewingKeySize = 64; | |
20 | const size_t SerializedSproutSpendingKeySize = 32; | |
3a15b163 | 21 | |
7614198f JG |
22 | const size_t SerializedSaplingPaymentAddressSize = 43; |
23 | const size_t SerializedSaplingFullViewingKeySize = 96; | |
24 | const size_t SerializedSaplingExpandedSpendingKeySize = 96; | |
55f28893 JG |
25 | const size_t SerializedSaplingSpendingKeySize = 32; |
26 | ||
9e1c2c40 | 27 | typedef std::array<unsigned char, ZC_DIVERSIFIER_SIZE> diversifier_t; |
11acfe6e | 28 | |
e5eab182 | 29 | class SproutPaymentAddress { |
369df065 SB |
30 | public: |
31 | uint256 a_pk; | |
32 | uint256 pk_enc; | |
33 | ||
e5eab182 JG |
34 | SproutPaymentAddress() : a_pk(), pk_enc() { } |
35 | SproutPaymentAddress(uint256 a_pk, uint256 pk_enc) : a_pk(a_pk), pk_enc(pk_enc) { } | |
369df065 SB |
36 | |
37 | ADD_SERIALIZE_METHODS; | |
38 | ||
39 | template <typename Stream, typename Operation> | |
68a1a592 | 40 | inline void SerializationOp(Stream& s, Operation ser_action) { |
369df065 SB |
41 | READWRITE(a_pk); |
42 | READWRITE(pk_enc); | |
43 | } | |
7c929cf5 | 44 | |
16d140f4 JG |
45 | //! Get the 256-bit SHA256d hash of this payment address. |
46 | uint256 GetHash() const; | |
47 | ||
e5eab182 | 48 | friend inline bool operator==(const SproutPaymentAddress& a, const SproutPaymentAddress& b) { |
32a103aa JG |
49 | return a.a_pk == b.a_pk && a.pk_enc == b.pk_enc; |
50 | } | |
e5eab182 | 51 | friend inline bool operator<(const SproutPaymentAddress& a, const SproutPaymentAddress& b) { |
32a103aa JG |
52 | return (a.a_pk < b.a_pk || |
53 | (a.a_pk == b.a_pk && a.pk_enc < b.pk_enc)); | |
54 | } | |
369df065 SB |
55 | }; |
56 | ||
642a1caf | 57 | class ReceivingKey : public uint256 { |
369df065 | 58 | public: |
aa666c96 | 59 | ReceivingKey() { } |
642a1caf | 60 | ReceivingKey(uint256 sk_enc) : uint256(sk_enc) { } |
369df065 | 61 | |
aa666c96 JG |
62 | uint256 pk_enc() const; |
63 | }; | |
64 | ||
e5eab182 | 65 | class SproutViewingKey { |
aa666c96 JG |
66 | public: |
67 | uint256 a_pk; | |
68 | ReceivingKey sk_enc; | |
69 | ||
e5eab182 JG |
70 | SproutViewingKey() : a_pk(), sk_enc() { } |
71 | SproutViewingKey(uint256 a_pk, ReceivingKey sk_enc) : a_pk(a_pk), sk_enc(sk_enc) { } | |
aa666c96 JG |
72 | |
73 | ADD_SERIALIZE_METHODS; | |
74 | ||
75 | template <typename Stream, typename Operation> | |
68a1a592 | 76 | inline void SerializationOp(Stream& s, Operation ser_action) { |
aa666c96 JG |
77 | READWRITE(a_pk); |
78 | READWRITE(sk_enc); | |
79 | } | |
80 | ||
e5eab182 | 81 | SproutPaymentAddress address() const; |
aa666c96 | 82 | |
e5eab182 | 83 | friend inline bool operator==(const SproutViewingKey& a, const SproutViewingKey& b) { |
aa666c96 JG |
84 | return a.a_pk == b.a_pk && a.sk_enc == b.sk_enc; |
85 | } | |
e5eab182 | 86 | friend inline bool operator<(const SproutViewingKey& a, const SproutViewingKey& b) { |
aa666c96 JG |
87 | return (a.a_pk < b.a_pk || |
88 | (a.a_pk == b.a_pk && a.sk_enc < b.sk_enc)); | |
89 | } | |
369df065 SB |
90 | }; |
91 | ||
e5eab182 | 92 | class SproutSpendingKey : public uint252 { |
369df065 | 93 | public: |
e5eab182 JG |
94 | SproutSpendingKey() : uint252() { } |
95 | SproutSpendingKey(uint252 a_sk) : uint252(a_sk) { } | |
369df065 | 96 | |
e5eab182 | 97 | static SproutSpendingKey random(); |
369df065 | 98 | |
642a1caf | 99 | ReceivingKey receiving_key() const; |
e5eab182 JG |
100 | SproutViewingKey viewing_key() const; |
101 | SproutPaymentAddress address() const; | |
369df065 SB |
102 | }; |
103 | ||
11acfe6e JG |
104 | //! Sapling functions. |
105 | class SaplingPaymentAddress { | |
106 | public: | |
107 | diversifier_t d; | |
108 | uint256 pk_d; | |
109 | ||
110 | SaplingPaymentAddress() : d(), pk_d() { } | |
111 | SaplingPaymentAddress(diversifier_t d, uint256 pk_d) : d(d), pk_d(pk_d) { } | |
112 | ||
113 | ADD_SERIALIZE_METHODS; | |
114 | ||
115 | template <typename Stream, typename Operation> | |
116 | inline void SerializationOp(Stream& s, Operation ser_action) { | |
117 | READWRITE(d); | |
118 | READWRITE(pk_d); | |
119 | } | |
21737673 JG |
120 | |
121 | //! Get the 256-bit SHA256d hash of this payment address. | |
122 | uint256 GetHash() const; | |
11acfe6e JG |
123 | |
124 | friend inline bool operator==(const SaplingPaymentAddress& a, const SaplingPaymentAddress& b) { | |
125 | return a.d == b.d && a.pk_d == b.pk_d; | |
126 | } | |
127 | friend inline bool operator<(const SaplingPaymentAddress& a, const SaplingPaymentAddress& b) { | |
128 | return (a.d < b.d || | |
129 | (a.d == b.d && a.pk_d < b.pk_d)); | |
130 | } | |
131 | }; | |
132 | ||
0da9aac6 | 133 | class SaplingIncomingViewingKey : public uint256 { |
11acfe6e | 134 | public: |
0da9aac6 JG |
135 | SaplingIncomingViewingKey() : uint256() { } |
136 | SaplingIncomingViewingKey(uint256 ivk) : uint256(ivk) { } | |
11acfe6e JG |
137 | |
138 | // Can pass in diversifier for Sapling addr | |
87c9994c | 139 | boost::optional<SaplingPaymentAddress> address(diversifier_t d) const; |
11acfe6e JG |
140 | }; |
141 | ||
142 | class SaplingFullViewingKey { | |
143 | public: | |
144 | uint256 ak; | |
145 | uint256 nk; | |
146 | uint256 ovk; | |
147 | ||
148 | SaplingFullViewingKey() : ak(), nk(), ovk() { } | |
149 | SaplingFullViewingKey(uint256 ak, uint256 nk, uint256 ovk) : ak(ak), nk(nk), ovk(ovk) { } | |
150 | ||
151 | ADD_SERIALIZE_METHODS; | |
152 | ||
153 | template <typename Stream, typename Operation> | |
55f28893 | 154 | inline void SerializationOp(Stream& s, Operation ser_action) { |
11acfe6e JG |
155 | READWRITE(ak); |
156 | READWRITE(nk); | |
157 | READWRITE(ovk); | |
158 | } | |
159 | ||
55f28893 JG |
160 | //! Get the fingerprint of this full viewing key (as defined in ZIP 32). |
161 | uint256 GetFingerprint() const; | |
162 | ||
0da9aac6 | 163 | SaplingIncomingViewingKey in_viewing_key() const; |
db9f669c | 164 | bool is_valid() const; |
11acfe6e JG |
165 | |
166 | friend inline bool operator==(const SaplingFullViewingKey& a, const SaplingFullViewingKey& b) { | |
167 | return a.ak == b.ak && a.nk == b.nk && a.ovk == b.ovk; | |
168 | } | |
169 | friend inline bool operator<(const SaplingFullViewingKey& a, const SaplingFullViewingKey& b) { | |
170 | return (a.ak < b.ak || | |
171 | (a.ak == b.ak && a.nk < b.nk) || | |
172 | (a.ak == b.ak && a.nk == b.nk && a.ovk < b.ovk)); | |
173 | } | |
174 | }; | |
175 | ||
176 | ||
177 | class SaplingExpandedSpendingKey { | |
178 | public: | |
179 | uint256 ask; | |
180 | uint256 nsk; | |
181 | uint256 ovk; | |
182 | ||
183 | SaplingExpandedSpendingKey() : ask(), nsk(), ovk() { } | |
184 | SaplingExpandedSpendingKey(uint256 ask, uint256 nsk, uint256 ovk) : ask(ask), nsk(nsk), ovk(ovk) { } | |
185 | ||
186 | ADD_SERIALIZE_METHODS; | |
187 | ||
188 | template <typename Stream, typename Operation> | |
55f28893 | 189 | inline void SerializationOp(Stream& s, Operation ser_action) { |
11acfe6e JG |
190 | READWRITE(ask); |
191 | READWRITE(nsk); | |
192 | READWRITE(ovk); | |
193 | } | |
194 | ||
195 | SaplingFullViewingKey full_viewing_key() const; | |
196 | ||
197 | friend inline bool operator==(const SaplingExpandedSpendingKey& a, const SaplingExpandedSpendingKey& b) { | |
198 | return a.ask == b.ask && a.nsk == b.nsk && a.ovk == b.ovk; | |
199 | } | |
200 | friend inline bool operator<(const SaplingExpandedSpendingKey& a, const SaplingExpandedSpendingKey& b) { | |
201 | return (a.ask < b.ask || | |
202 | (a.ask == b.ask && a.nsk < b.nsk) || | |
203 | (a.ask == b.ask && a.nsk == b.nsk && a.ovk < b.ovk)); | |
204 | } | |
205 | }; | |
206 | ||
207 | class SaplingSpendingKey : public uint256 { | |
208 | public: | |
209 | SaplingSpendingKey() : uint256() { } | |
210 | SaplingSpendingKey(uint256 sk) : uint256(sk) { } | |
211 | ||
212 | static SaplingSpendingKey random(); | |
213 | ||
214 | SaplingExpandedSpendingKey expanded_spending_key() const; | |
215 | SaplingFullViewingKey full_viewing_key() const; | |
216 | ||
217 | // Can derive Sapling addr from default diversifier | |
8e91ebf7 | 218 | SaplingPaymentAddress default_address() const; |
11acfe6e JG |
219 | }; |
220 | ||
bec3e62b | 221 | typedef boost::variant<InvalidEncoding, SproutPaymentAddress, SaplingPaymentAddress> PaymentAddress; |
42b2ccec | 222 | typedef boost::variant<InvalidEncoding, SproutViewingKey, SaplingIncomingViewingKey> ViewingKey; |
bec3e62b | 223 | |
369df065 SB |
224 | } |
225 | ||
e5eab182 | 226 | /** Check whether a PaymentAddress is not an InvalidEncoding. */ |
5f63373e | 227 | extern const uint32_t SAPLING_BRANCH_ID; |
228 | bool IsValidPaymentAddress(const libzcash::PaymentAddress& zaddr, uint32_t consensusBranchId = SAPLING_BRANCH_ID); | |
e5eab182 JG |
229 | |
230 | /** Check whether a ViewingKey is not an InvalidEncoding. */ | |
231 | bool IsValidViewingKey(const libzcash::ViewingKey& vk); | |
232 | ||
4e4aa5b6 | 233 | #endif // ZC_ADDRESS_H_ |