PIVX Core  5.6.99
P2P Digital Currency
sapling_key_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"
7 
8 #include "sapling/address.h"
10 
11 #include <string>
12 #include <vector>
13 
14 #include <boost/test/unit_test.hpp>
15 
16 
18 
19 BOOST_AUTO_TEST_CASE(ps_address_test)
20 {
22 
23  std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
24  HDSeed seed(rawSeed);
26 
27  for (uint32_t i = 0; i < 1000; i++) {
28  auto sk = m.Derive(i);
29  {
30  std::string sk_string = KeyIO::EncodeSpendingKey(sk);
31  BOOST_CHECK(sk_string.compare(0, 26, Params().Bech32HRP(CChainParams::SAPLING_EXTENDED_SPEND_KEY)) == 0);
32 
33  libzcash::SpendingKey spendingkey2 = KeyIO::DecodeSpendingKey(sk_string);
34  BOOST_CHECK(IsValidSpendingKey(spendingkey2));
35 
36  BOOST_ASSERT(boost::get<libzcash::SaplingExtendedSpendingKey>(&spendingkey2) != nullptr);
37  auto sk2 = boost::get<libzcash::SaplingExtendedSpendingKey>(spendingkey2);
38  BOOST_CHECK(sk == sk2);
39  }
40  {
41  libzcash::SaplingPaymentAddress addr = sk.DefaultAddress();
42 
43  std::string addr_string = KeyIO::EncodePaymentAddress(addr);
44  BOOST_CHECK(addr_string.compare(0, 12, Params().Bech32HRP(CChainParams::SAPLING_PAYMENT_ADDRESS)) == 0);
45 
46  auto paymentaddr2 = KeyIO::DecodePaymentAddress(addr_string);
47  BOOST_CHECK(IsValidPaymentAddress(paymentaddr2));
48 
49  BOOST_ASSERT(boost::get<libzcash::SaplingPaymentAddress>(&paymentaddr2) != nullptr);
50  auto addr2 = boost::get<libzcash::SaplingPaymentAddress>(paymentaddr2);
51  BOOST_CHECK(addr == addr2);
52  }
53  }
54 }
55 
56 BOOST_AUTO_TEST_CASE(EncodeAndDecodeSapling)
57 {
59 
60  std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
61  HDSeed seed(rawSeed);
63 
64  for (uint32_t i = 0; i < 1000; i++) {
65  auto sk = m.Derive(i);
66  {
67  std::string sk_string = KeyIO::EncodeSpendingKey(sk);
69  sk_string.substr(0, 26) ==
71 
72  auto spendingkey2 = KeyIO::DecodeSpendingKey(sk_string);
73  BOOST_CHECK(IsValidSpendingKey(spendingkey2));
74 
75  BOOST_CHECK(boost::get<libzcash::SaplingExtendedSpendingKey>(&spendingkey2) != nullptr);
76  auto sk2 = boost::get<libzcash::SaplingExtendedSpendingKey>(spendingkey2);
77  BOOST_CHECK(sk == sk2);
78  }
79 
80  {
81  auto extfvk = sk.ToXFVK();
82  std::string vk_string = KeyIO::EncodeViewingKey(extfvk);
84  vk_string.substr(0, 7) ==
86 
87  auto viewingkey2 = KeyIO::DecodeViewingKey(vk_string);
88  BOOST_CHECK(IsValidViewingKey(viewingkey2));
89 
90  BOOST_CHECK(boost::get<libzcash::SaplingExtendedFullViewingKey>(&viewingkey2) != nullptr);
91  auto extfvk2 = boost::get<libzcash::SaplingExtendedFullViewingKey>(viewingkey2);
92  BOOST_CHECK(extfvk == extfvk2);
93  }
94 
95  {
96  auto addr = sk.DefaultAddress();
97 
98  std::string addr_string = KeyIO::EncodePaymentAddress(addr);
100  addr_string.substr(0, 2) ==
102 
103  auto paymentaddr2 = KeyIO::DecodePaymentAddress(addr_string);
104  BOOST_CHECK(IsValidPaymentAddress(paymentaddr2));
105 
106  BOOST_CHECK(boost::get<libzcash::SaplingPaymentAddress>(&paymentaddr2) != nullptr);
107  auto addr2 = boost::get<libzcash::SaplingPaymentAddress>(paymentaddr2);
108  BOOST_CHECK(addr == addr2);
109  }
110  }
111 }
112 
bool IsValidPaymentAddress(const libzcash::PaymentAddress &zaddr)
Check whether a PaymentAddress is not an InvalidEncoding.
Definition: address.cpp:92
void SelectParams(const std::string &network)
Sets the params returned by Params() to those for the given chain name.
const CChainParams & Params()
Return the currently selected parameters.
static const std::string REGTEST
static const std::string MAIN
Chain name strings.
@ SAPLING_PAYMENT_ADDRESS
Definition: chainparams.h:60
@ SAPLING_EXTENDED_SPEND_KEY
Definition: chainparams.h:63
@ SAPLING_EXTENDED_FVK
Definition: chainparams.h:64
Definition: zip32.h:20
Sapling functions.
Definition: address.h:30
BOOST_AUTO_TEST_SUITE_END()
libzcash::SpendingKey DecodeSpendingKey(const std::string &str)
std::string EncodePaymentAddress(const libzcash::PaymentAddress &zaddr)
libzcash::ViewingKey DecodeViewingKey(const std::string &str)
std::string EncodeViewingKey(const libzcash::ViewingKey &vk)
libzcash::PaymentAddress DecodePaymentAddress(const std::string &str)
std::string EncodeSpendingKey(const libzcash::SpendingKey &zkey)
boost::variant< InvalidEncoding, SaplingExtendedSpendingKey > SpendingKey
Definition: zip32.h:116
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_AUTO_TEST_CASE(ps_address_test)
Basic testing setup.
Definition: test_pivx.h:51
SaplingExtendedSpendingKey Derive(uint32_t i) const
Definition: zip32.cpp:132
static SaplingExtendedSpendingKey Master(const HDSeed &seed)
Definition: zip32.cpp:117
bool IsValidSpendingKey(const libzcash::SpendingKey &zkey)
Check whether a SpendingKey is not an InvalidEncoding.
Definition: zip32.cpp:169
bool IsValidViewingKey(const libzcash::ViewingKey &vk)
Check whether a ViewingKey is not an InvalidEncoding.
Definition: zip32.cpp:173