]> Git Repo - VerusCoin.git/blob - src/zcash/Address.cpp
Merge pull request #97 from miketout/dev
[VerusCoin.git] / src / zcash / Address.cpp
1 #include "Address.hpp"
2 #include "NoteEncryption.hpp"
3 #include "hash.h"
4 #include "prf.h"
5 #include "streams.h"
6
7 #include <librustzcash.h>
8
9 const unsigned char ZCASH_SAPLING_FVFP_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] =
10     {'Z', 'c', 'a', 's', 'h', 'S', 'a', 'p', 'l', 'i', 'n', 'g', 'F', 'V', 'F', 'P'};
11
12 const uint32_t SAPLING_BRANCH_ID = 0x76b809bb;
13
14 namespace libzcash {
15
16 uint256 SproutPaymentAddress::GetHash() const {
17     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
18     ss << *this;
19     return Hash(ss.begin(), ss.end());
20 }
21
22 uint256 ReceivingKey::pk_enc() const {
23     return ZCNoteEncryption::generate_pubkey(*this);
24 }
25
26 SproutPaymentAddress SproutViewingKey::address() const {
27     return SproutPaymentAddress(a_pk, sk_enc.pk_enc());
28 }
29
30 ReceivingKey SproutSpendingKey::receiving_key() const {
31     return ReceivingKey(ZCNoteEncryption::generate_privkey(*this));
32 }
33
34 SproutViewingKey SproutSpendingKey::viewing_key() const {
35     return SproutViewingKey(PRF_addr_a_pk(*this), receiving_key());
36 }
37
38 SproutSpendingKey SproutSpendingKey::random() {
39     return SproutSpendingKey(random_uint252());
40 }
41
42 SproutPaymentAddress SproutSpendingKey::address() const {
43     return viewing_key().address();
44 }
45
46 //! Sapling
47 uint256 SaplingPaymentAddress::GetHash() const {
48     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
49     ss << *this;
50     return Hash(ss.begin(), ss.end());
51 }
52
53 SaplingFullViewingKey SaplingExpandedSpendingKey::full_viewing_key() const {
54     uint256 ak;
55     uint256 nk;
56     librustzcash_ask_to_ak(ask.begin(), ak.begin());
57     librustzcash_nsk_to_nk(nsk.begin(), nk.begin());
58     return SaplingFullViewingKey(ak, nk, ovk);
59 }
60
61 SaplingExpandedSpendingKey SaplingSpendingKey::expanded_spending_key() const {
62     return SaplingExpandedSpendingKey(PRF_ask(*this), PRF_nsk(*this), PRF_ovk(*this));
63 }
64
65 SaplingFullViewingKey SaplingSpendingKey::full_viewing_key() const {
66     return expanded_spending_key().full_viewing_key();
67 }
68
69 SaplingIncomingViewingKey SaplingFullViewingKey::in_viewing_key() const {
70     uint256 ivk;
71     librustzcash_crh_ivk(ak.begin(), nk.begin(), ivk.begin());
72     return SaplingIncomingViewingKey(ivk);
73 }
74
75 bool SaplingFullViewingKey::is_valid() const {
76     uint256 ivk;
77     librustzcash_crh_ivk(ak.begin(), nk.begin(), ivk.begin());
78     return !ivk.IsNull();
79 }
80
81 uint256 SaplingFullViewingKey::GetFingerprint() const {
82     CBLAKE2bWriter ss(SER_GETHASH, 0, ZCASH_SAPLING_FVFP_PERSONALIZATION);
83     ss << *this;
84     return ss.GetHash();
85 }
86
87
88 SaplingSpendingKey SaplingSpendingKey::random() {
89     while (true) {
90         auto sk = SaplingSpendingKey(random_uint256());
91         if (sk.full_viewing_key().is_valid()) {
92             return sk;
93         }
94     }
95 }
96
97 boost::optional<SaplingPaymentAddress> SaplingIncomingViewingKey::address(diversifier_t d) const {
98     uint256 pk_d;
99     if (librustzcash_check_diversifier(d.data())) {
100         librustzcash_ivk_to_pkd(this->begin(), d.data(), pk_d.begin());
101         return SaplingPaymentAddress(d, pk_d);
102     } else {
103         return boost::none;
104     }
105 }
106
107 SaplingPaymentAddress SaplingSpendingKey::default_address() const {
108     // Iterates within default_diversifier to ensure a valid address is returned
109     auto addrOpt = full_viewing_key().in_viewing_key().address(default_diversifier(*this));
110     assert(addrOpt != boost::none);
111     return addrOpt.value();
112 }
113
114 }
115
116 class IsValidAddressForNetwork : public boost::static_visitor<bool> {
117     private:
118         uint32_t branchId;
119     public:
120         IsValidAddressForNetwork(uint32_t consensusBranchId) : branchId(consensusBranchId) {}
121
122         bool operator()(const libzcash::SproutPaymentAddress &addr) const {
123             return true;
124         }
125
126         bool operator()(const libzcash::InvalidEncoding &addr) const {
127             return false;
128         }
129
130         bool operator()(const libzcash::SaplingPaymentAddress &addr) const {
131             if (SAPLING_BRANCH_ID == branchId)
132                 return true;
133             else
134                 return false;
135         }
136 };
137
138 bool IsValidPaymentAddress(const libzcash::PaymentAddress& zaddr, uint32_t consensusBranchId) {
139     return boost::apply_visitor(IsValidAddressForNetwork(consensusBranchId), zaddr);
140 }
141
142 bool IsValidViewingKey(const libzcash::ViewingKey& vk) {
143     return vk.which() != 0;
144 }
This page took 0.044435 seconds and 4 git commands to generate.