Line data Source code
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" 7 : #include "test/librust/utiltest.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 : 15 : #include "data/sapling_key_components.json.h" 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 2 : 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 3 : 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 11 : for (size_t i = 2; i < 12; i++) { 29 130 : uint256 skSeed, ask, nsk, ovk, ak, nk, ivk; 30 10 : skSeed.SetHex(sapling_keys[i][0].getValStr()); 31 10 : ask.SetHex(sapling_keys[i][1].getValStr()); 32 10 : nsk.SetHex(sapling_keys[i][2].getValStr()); 33 10 : ovk.SetHex(sapling_keys[i][3].getValStr()); 34 10 : ak.SetHex(sapling_keys[i][4].getValStr()); 35 10 : nk.SetHex(sapling_keys[i][5].getValStr()); 36 10 : ivk.SetHex(sapling_keys[i][6].getValStr()); 37 : 38 10 : diversifier_t default_d; 39 10 : std::copy_n(ParseHex(sapling_keys[i][7].getValStr()).begin(), 11, default_d.begin()); 40 : 41 10 : uint256 default_pk_d; 42 10 : default_pk_d.SetHex(sapling_keys[i][8].getValStr()); 43 : 44 10 : auto sk = libzcash::SaplingSpendingKey(skSeed); 45 : 46 : // Check that expanded spending key from primitives and from sk are the same 47 10 : auto exp_sk_2 = libzcash::SaplingExpandedSpendingKey(ask, nsk, ovk); 48 10 : auto exp_sk = sk.expanded_spending_key(); 49 20 : BOOST_CHECK(exp_sk == exp_sk_2); 50 : 51 : // Check that full viewing key derived from sk and expanded sk are the same 52 10 : auto full_viewing_key = sk.full_viewing_key(); 53 20 : 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 10 : auto full_viewing_key_2 = libzcash::SaplingFullViewingKey(ak, nk, ovk); 57 20 : 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 10 : auto in_viewing_key = full_viewing_key.in_viewing_key(); 61 10 : auto in_viewing_key_2 = libzcash::SaplingIncomingViewingKey(ivk); 62 20 : 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 10 : auto default_addr = sk.default_address(); 66 20 : auto addrOpt2 = in_viewing_key.address(default_d); 67 20 : BOOST_CHECK(addrOpt2); 68 10 : auto default_addr_2 = addrOpt2.value(); 69 20 : BOOST_CHECK(default_addr == default_addr_2); 70 : 71 10 : auto default_addr_3 = libzcash::SaplingPaymentAddress(default_d, default_pk_d); 72 20 : BOOST_CHECK(default_addr_2 == default_addr_3); 73 20 : BOOST_CHECK(default_addr == default_addr_3); 74 : } 75 1 : } 76 : 77 : 78 2 : BOOST_AUTO_TEST_CASE(StoreAndRetrieveSaplingSpendingKey) { 79 1 : CBasicKeyStore keyStore; 80 1 : libzcash::SaplingExtendedSpendingKey skOut; 81 1 : libzcash::SaplingExtendedFullViewingKey extfvkOut; 82 1 : libzcash::SaplingIncomingViewingKey ivkOut; 83 : 84 2 : std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32); 85 2 : HDSeed seed(rawSeed); 86 1 : auto sk = libzcash::SaplingExtendedSpendingKey::Master(seed); 87 1 : auto extfvk = sk.ToXFVK(); 88 1 : auto ivk = extfvk.fvk.in_viewing_key(); 89 1 : auto addr = sk.DefaultAddress(); 90 : 91 : // Sanity-check: we can't get a key we haven't added 92 2 : BOOST_CHECK(!keyStore.HaveSaplingSpendingKey(extfvk)); 93 2 : BOOST_CHECK(!keyStore.GetSaplingSpendingKey(extfvk, skOut)); 94 : // Sanity-check: we can't get a full viewing key we haven't added 95 2 : BOOST_CHECK(!keyStore.HaveSaplingFullViewingKey(ivk)); 96 2 : BOOST_CHECK(!keyStore.GetSaplingFullViewingKey(ivk, extfvkOut)); 97 : // Sanity-check: we can't get an incoming viewing key we haven't added 98 2 : BOOST_CHECK(!keyStore.HaveSaplingIncomingViewingKey(addr)); 99 2 : BOOST_CHECK(!keyStore.GetSaplingIncomingViewingKey(addr, ivkOut)); 100 : 101 : // When we specify the default address, we get the full mapping 102 1 : keyStore.AddSaplingSpendingKey(sk); 103 2 : BOOST_CHECK(keyStore.HaveSaplingSpendingKey(extfvk)); 104 2 : BOOST_CHECK(keyStore.GetSaplingSpendingKey(extfvk, skOut)); 105 2 : BOOST_CHECK(keyStore.HaveSaplingFullViewingKey(ivk)); 106 2 : BOOST_CHECK(keyStore.GetSaplingFullViewingKey(ivk, extfvkOut)); 107 2 : BOOST_CHECK(keyStore.HaveSaplingIncomingViewingKey(addr)); 108 2 : BOOST_CHECK(keyStore.GetSaplingIncomingViewingKey(addr, ivkOut)); 109 2 : BOOST_CHECK(sk == skOut); 110 2 : BOOST_CHECK(extfvk == extfvkOut); 111 2 : BOOST_CHECK(ivk == ivkOut); 112 1 : } 113 : 114 2 : BOOST_AUTO_TEST_CASE(StoreAndRetrieveSaplingFullViewingKey) { 115 1 : CBasicKeyStore keyStore; 116 1 : libzcash::SaplingExtendedSpendingKey skOut; 117 1 : libzcash::SaplingExtendedFullViewingKey extfvkOut; 118 1 : libzcash::SaplingIncomingViewingKey ivkOut; 119 : 120 1 : auto sk = GetTestMasterSaplingSpendingKey(); 121 1 : auto extfvk = sk.ToXFVK(); 122 1 : auto ivk = extfvk.fvk.in_viewing_key(); 123 1 : auto addr = sk.DefaultAddress(); 124 : 125 : // Sanity-check: we can't get a full viewing key we haven't added 126 2 : BOOST_CHECK(!keyStore.HaveSaplingFullViewingKey(ivk)); 127 2 : BOOST_CHECK(!keyStore.GetSaplingFullViewingKey(ivk, extfvkOut)); 128 : 129 : // and we shouldn't have a spending key or incoming viewing key either 130 2 : BOOST_CHECK(!keyStore.HaveSaplingSpendingKey(extfvk)); 131 2 : BOOST_CHECK(!keyStore.GetSaplingSpendingKey(extfvk, skOut)); 132 2 : BOOST_CHECK(!keyStore.HaveSaplingIncomingViewingKey(addr)); 133 2 : BOOST_CHECK(!keyStore.GetSaplingIncomingViewingKey(addr, ivkOut)); 134 : 135 : // and we can't find the default address in our list of addresses 136 2 : std::set<libzcash::SaplingPaymentAddress> addresses; 137 1 : keyStore.GetSaplingPaymentAddresses(addresses); 138 3 : BOOST_CHECK(!addresses.count(addr)); 139 : 140 : // When we add the full viewing key, we should have it 141 1 : keyStore.AddSaplingFullViewingKey(extfvk); 142 2 : BOOST_CHECK(keyStore.HaveSaplingFullViewingKey(ivk)); 143 2 : BOOST_CHECK(keyStore.GetSaplingFullViewingKey(ivk, extfvkOut)); 144 2 : BOOST_CHECK(extfvk == extfvkOut); 145 : 146 : // We should still not have the spending key... 147 2 : BOOST_CHECK(!keyStore.HaveSaplingSpendingKey(extfvk)); 148 2 : BOOST_CHECK(!keyStore.GetSaplingSpendingKey(extfvk, skOut)); 149 : 150 : // ... but we should have an incoming viewing key 151 2 : BOOST_CHECK(keyStore.HaveSaplingIncomingViewingKey(addr)); 152 2 : BOOST_CHECK(keyStore.GetSaplingIncomingViewingKey(addr, ivkOut)); 153 2 : BOOST_CHECK(ivk == ivkOut); 154 : 155 : // ... and we should find the default address in our list of addresses 156 1 : addresses.clear(); 157 1 : keyStore.GetSaplingPaymentAddresses(addresses); 158 3 : BOOST_CHECK(addresses.count(addr)); 159 1 : } 160 : 161 : BOOST_AUTO_TEST_SUITE_END()