PIVX Core  5.6.99
P2P Digital Currency
sapling_keystore_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2016-2020 The ZCash developers
2 // Copyright (c) 2020 The PIVX Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "test/test_pivx.h"
8 
9 #include "sapling/address.h"
10 #include "sapling/sapling_util.h"
11 
12 #include <boost/test/unit_test.hpp>
13 #include <univalue.h>
14 
16 
17 // In script_tests.cpp
18 extern UniValue read_json(const std::string& jsondata);
19 
20 BOOST_FIXTURE_TEST_SUITE(sapling_keystore_tests, BasicTestingSetup)
21 
22 
23 BOOST_AUTO_TEST_CASE(saplingKeys) {
24  // ["sk, ask, nsk, ovk, ak, nk, ivk, default_d, default_pk_d, note_v, note_r, note_cm, note_pos, note_nf"],
25  UniValue sapling_keys = read_json(std::string(json_tests::sapling_key_components, json_tests::sapling_key_components + sizeof(json_tests::sapling_key_components)));
26 
27  // Skipping over comments in sapling_key_components.json file
28  for (size_t i = 2; i < 12; i++) {
29  uint256 skSeed, ask, nsk, ovk, ak, nk, ivk;
30  skSeed.SetHex(sapling_keys[i][0].getValStr());
31  ask.SetHex(sapling_keys[i][1].getValStr());
32  nsk.SetHex(sapling_keys[i][2].getValStr());
33  ovk.SetHex(sapling_keys[i][3].getValStr());
34  ak.SetHex(sapling_keys[i][4].getValStr());
35  nk.SetHex(sapling_keys[i][5].getValStr());
36  ivk.SetHex(sapling_keys[i][6].getValStr());
37 
38  diversifier_t default_d;
39  std::copy_n(ParseHex(sapling_keys[i][7].getValStr()).begin(), 11, default_d.begin());
40 
41  uint256 default_pk_d;
42  default_pk_d.SetHex(sapling_keys[i][8].getValStr());
43 
44  auto sk = libzcash::SaplingSpendingKey(skSeed);
45 
46  // Check that expanded spending key from primitives and from sk are the same
47  auto exp_sk_2 = libzcash::SaplingExpandedSpendingKey(ask, nsk, ovk);
48  auto exp_sk = sk.expanded_spending_key();
49  BOOST_CHECK(exp_sk == exp_sk_2);
50 
51  // Check that full viewing key derived from sk and expanded sk are the same
52  auto full_viewing_key = sk.full_viewing_key();
53  BOOST_CHECK(full_viewing_key == exp_sk.full_viewing_key());
54 
55  // Check that full viewing key from primitives and from sk are the same
56  auto full_viewing_key_2 = libzcash::SaplingFullViewingKey(ak, nk, ovk);
57  BOOST_CHECK(full_viewing_key == full_viewing_key_2);
58 
59  // Check that incoming viewing key from primitives and from sk are the same
60  auto in_viewing_key = full_viewing_key.in_viewing_key();
61  auto in_viewing_key_2 = libzcash::SaplingIncomingViewingKey(ivk);
62  BOOST_CHECK(in_viewing_key == in_viewing_key_2);
63 
64  // Check that the default address from primitives and from sk method are the same
65  auto default_addr = sk.default_address();
66  auto addrOpt2 = in_viewing_key.address(default_d);
67  BOOST_CHECK(addrOpt2);
68  auto default_addr_2 = addrOpt2.value();
69  BOOST_CHECK(default_addr == default_addr_2);
70 
71  auto default_addr_3 = libzcash::SaplingPaymentAddress(default_d, default_pk_d);
72  BOOST_CHECK(default_addr_2 == default_addr_3);
73  BOOST_CHECK(default_addr == default_addr_3);
74  }
75 }
76 
77 
78 BOOST_AUTO_TEST_CASE(StoreAndRetrieveSaplingSpendingKey) {
79  CBasicKeyStore keyStore;
83 
84  std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
85  HDSeed seed(rawSeed);
87  auto extfvk = sk.ToXFVK();
88  auto ivk = extfvk.fvk.in_viewing_key();
89  auto addr = sk.DefaultAddress();
90 
91  // Sanity-check: we can't get a key we haven't added
92  BOOST_CHECK(!keyStore.HaveSaplingSpendingKey(extfvk));
93  BOOST_CHECK(!keyStore.GetSaplingSpendingKey(extfvk, skOut));
94  // Sanity-check: we can't get a full viewing key we haven't added
95  BOOST_CHECK(!keyStore.HaveSaplingFullViewingKey(ivk));
96  BOOST_CHECK(!keyStore.GetSaplingFullViewingKey(ivk, extfvkOut));
97  // Sanity-check: we can't get an incoming viewing key we haven't added
99  BOOST_CHECK(!keyStore.GetSaplingIncomingViewingKey(addr, ivkOut));
100 
101  // When we specify the default address, we get the full mapping
102  keyStore.AddSaplingSpendingKey(sk);
103  BOOST_CHECK(keyStore.HaveSaplingSpendingKey(extfvk));
104  BOOST_CHECK(keyStore.GetSaplingSpendingKey(extfvk, skOut));
105  BOOST_CHECK(keyStore.HaveSaplingFullViewingKey(ivk));
106  BOOST_CHECK(keyStore.GetSaplingFullViewingKey(ivk, extfvkOut));
108  BOOST_CHECK(keyStore.GetSaplingIncomingViewingKey(addr, ivkOut));
109  BOOST_CHECK(sk == skOut);
110  BOOST_CHECK(extfvk == extfvkOut);
111  BOOST_CHECK(ivk == ivkOut);
112 }
113 
114 BOOST_AUTO_TEST_CASE(StoreAndRetrieveSaplingFullViewingKey) {
115  CBasicKeyStore keyStore;
119 
121  auto extfvk = sk.ToXFVK();
122  auto ivk = extfvk.fvk.in_viewing_key();
123  auto addr = sk.DefaultAddress();
124 
125  // Sanity-check: we can't get a full viewing key we haven't added
126  BOOST_CHECK(!keyStore.HaveSaplingFullViewingKey(ivk));
127  BOOST_CHECK(!keyStore.GetSaplingFullViewingKey(ivk, extfvkOut));
128 
129  // and we shouldn't have a spending key or incoming viewing key either
130  BOOST_CHECK(!keyStore.HaveSaplingSpendingKey(extfvk));
131  BOOST_CHECK(!keyStore.GetSaplingSpendingKey(extfvk, skOut));
133  BOOST_CHECK(!keyStore.GetSaplingIncomingViewingKey(addr, ivkOut));
134 
135  // and we can't find the default address in our list of addresses
136  std::set<libzcash::SaplingPaymentAddress> addresses;
137  keyStore.GetSaplingPaymentAddresses(addresses);
138  BOOST_CHECK(!addresses.count(addr));
139 
140  // When we add the full viewing key, we should have it
141  keyStore.AddSaplingFullViewingKey(extfvk);
142  BOOST_CHECK(keyStore.HaveSaplingFullViewingKey(ivk));
143  BOOST_CHECK(keyStore.GetSaplingFullViewingKey(ivk, extfvkOut));
144  BOOST_CHECK(extfvk == extfvkOut);
145 
146  // We should still not have the spending key...
147  BOOST_CHECK(!keyStore.HaveSaplingSpendingKey(extfvk));
148  BOOST_CHECK(!keyStore.GetSaplingSpendingKey(extfvk, skOut));
149 
150  // ... but we should have an incoming viewing key
152  BOOST_CHECK(keyStore.GetSaplingIncomingViewingKey(addr, ivkOut));
153  BOOST_CHECK(ivk == ivkOut);
154 
155  // ... and we should find the default address in our list of addresses
156  addresses.clear();
157  keyStore.GetSaplingPaymentAddresses(addresses);
158  BOOST_CHECK(addresses.count(addr));
159 }
160 
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:99
virtual bool HaveSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr) const
Definition: keystore.cpp:199
virtual bool GetSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk, libzcash::SaplingExtendedFullViewingKey &extfvkOut) const
Definition: keystore.cpp:215
virtual bool HaveSaplingFullViewingKey(const libzcash::SaplingIncomingViewingKey &ivk) const
Definition: keystore.cpp:194
void GetSaplingPaymentAddresses(std::set< libzcash::SaplingPaymentAddress > &setAddress) const
Definition: keystore.cpp:252
bool HaveSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk) const
Definition: keystore.cpp:189
bool GetSaplingSpendingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk, libzcash::SaplingExtendedSpendingKey &skOut) const
Definition: keystore.cpp:204
virtual bool GetSaplingIncomingViewingKey(const libzcash::SaplingPaymentAddress &addr, libzcash::SaplingIncomingViewingKey &ivkOut) const
Definition: keystore.cpp:228
virtual bool AddSaplingFullViewingKey(const libzcash::SaplingExtendedFullViewingKey &extfvk)
Support for Sapling full viewing keys.
Definition: keystore.cpp:164
bool AddSaplingSpendingKey(const libzcash::SaplingExtendedSpendingKey &sk)
Sapling.
Definition: keystore.cpp:148
Definition: zip32.h:20
void SetHex(const char *psz)
Definition: uint256.cpp:31
Sapling functions.
Definition: address.h:30
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
std::array< unsigned char, ZC_DIVERSIFIER_SIZE > diversifier_t
Definition: sapling.h:38
BOOST_AUTO_TEST_CASE(saplingKeys)
UniValue read_json(const std::string &jsondata)
Basic testing setup.
Definition: test_pivx.h:51
static SaplingExtendedSpendingKey Master(const HDSeed &seed)
Definition: zip32.cpp:117
std::vector< unsigned char > ParseHex(const char *psz)
libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey()
Definition: utiltest.cpp:16