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 : 8 : #include "sapling/address.h" 9 : #include "sapling/key_io_sapling.h" 10 : 11 : #include <string> 12 : #include <vector> 13 : 14 : #include <boost/test/unit_test.hpp> 15 : 16 : 17 : BOOST_FIXTURE_TEST_SUITE(sapling_key_tests, BasicTestingSetup) 18 : 19 2 : BOOST_AUTO_TEST_CASE(ps_address_test) 20 : { 21 1 : SelectParams(CBaseChainParams::REGTEST); 22 : 23 1 : std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32); 24 2 : HDSeed seed(rawSeed); 25 1 : auto m = libzcash::SaplingExtendedSpendingKey::Master(seed); 26 : 27 1001 : for (uint32_t i = 0; i < 1000; i++) { 28 1000 : auto sk = m.Derive(i); 29 1000 : { 30 1000 : std::string sk_string = KeyIO::EncodeSpendingKey(sk); 31 2000 : BOOST_CHECK(sk_string.compare(0, 26, Params().Bech32HRP(CChainParams::SAPLING_EXTENDED_SPEND_KEY)) == 0); 32 : 33 2000 : libzcash::SpendingKey spendingkey2 = KeyIO::DecodeSpendingKey(sk_string); 34 2000 : BOOST_CHECK(IsValidSpendingKey(spendingkey2)); 35 : 36 1000 : BOOST_ASSERT(boost::get<libzcash::SaplingExtendedSpendingKey>(&spendingkey2) != nullptr); 37 1000 : auto sk2 = boost::get<libzcash::SaplingExtendedSpendingKey>(spendingkey2); 38 2000 : BOOST_CHECK(sk == sk2); 39 : } 40 1000 : { 41 1000 : libzcash::SaplingPaymentAddress addr = sk.DefaultAddress(); 42 : 43 2000 : std::string addr_string = KeyIO::EncodePaymentAddress(addr); 44 2000 : BOOST_CHECK(addr_string.compare(0, 12, Params().Bech32HRP(CChainParams::SAPLING_PAYMENT_ADDRESS)) == 0); 45 : 46 2000 : auto paymentaddr2 = KeyIO::DecodePaymentAddress(addr_string); 47 2000 : BOOST_CHECK(IsValidPaymentAddress(paymentaddr2)); 48 : 49 1000 : BOOST_ASSERT(boost::get<libzcash::SaplingPaymentAddress>(&paymentaddr2) != nullptr); 50 1000 : auto addr2 = boost::get<libzcash::SaplingPaymentAddress>(paymentaddr2); 51 2000 : BOOST_CHECK(addr == addr2); 52 : } 53 : } 54 1 : } 55 : 56 2 : BOOST_AUTO_TEST_CASE(EncodeAndDecodeSapling) 57 : { 58 1 : SelectParams(CBaseChainParams::MAIN); 59 : 60 1 : std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32); 61 2 : HDSeed seed(rawSeed); 62 1 : libzcash::SaplingExtendedSpendingKey m = libzcash::SaplingExtendedSpendingKey::Master(seed); 63 : 64 1001 : for (uint32_t i = 0; i < 1000; i++) { 65 1000 : auto sk = m.Derive(i); 66 1000 : { 67 1000 : std::string sk_string = KeyIO::EncodeSpendingKey(sk); 68 3000 : BOOST_CHECK( 69 : sk_string.substr(0, 26) == 70 : Params().Bech32HRP(CChainParams::SAPLING_EXTENDED_SPEND_KEY)); 71 : 72 2000 : auto spendingkey2 = KeyIO::DecodeSpendingKey(sk_string); 73 2000 : BOOST_CHECK(IsValidSpendingKey(spendingkey2)); 74 : 75 2000 : BOOST_CHECK(boost::get<libzcash::SaplingExtendedSpendingKey>(&spendingkey2) != nullptr); 76 1000 : auto sk2 = boost::get<libzcash::SaplingExtendedSpendingKey>(spendingkey2); 77 2000 : BOOST_CHECK(sk == sk2); 78 : } 79 : 80 1000 : { 81 1000 : auto extfvk = sk.ToXFVK(); 82 1000 : std::string vk_string = KeyIO::EncodeViewingKey(extfvk); 83 2000 : BOOST_CHECK( 84 : vk_string.substr(0, 7) == 85 : Params().Bech32HRP(CChainParams::SAPLING_EXTENDED_FVK)); 86 : 87 2000 : auto viewingkey2 = KeyIO::DecodeViewingKey(vk_string); 88 2000 : BOOST_CHECK(IsValidViewingKey(viewingkey2)); 89 : 90 2000 : BOOST_CHECK(boost::get<libzcash::SaplingExtendedFullViewingKey>(&viewingkey2) != nullptr); 91 1000 : auto extfvk2 = boost::get<libzcash::SaplingExtendedFullViewingKey>(viewingkey2); 92 2000 : BOOST_CHECK(extfvk == extfvk2); 93 : } 94 : 95 1000 : { 96 1000 : auto addr = sk.DefaultAddress(); 97 : 98 2000 : std::string addr_string = KeyIO::EncodePaymentAddress(addr); 99 2000 : BOOST_CHECK( 100 : addr_string.substr(0, 2) == 101 : Params().Bech32HRP(CChainParams::SAPLING_PAYMENT_ADDRESS)); 102 : 103 2000 : auto paymentaddr2 = KeyIO::DecodePaymentAddress(addr_string); 104 2000 : BOOST_CHECK(IsValidPaymentAddress(paymentaddr2)); 105 : 106 2000 : BOOST_CHECK(boost::get<libzcash::SaplingPaymentAddress>(&paymentaddr2) != nullptr); 107 1000 : auto addr2 = boost::get<libzcash::SaplingPaymentAddress>(paymentaddr2); 108 2000 : BOOST_CHECK(addr == addr2); 109 : } 110 : } 111 1 : } 112 : 113 : BOOST_AUTO_TEST_SUITE_END()