Line data Source code
1 : // Copyright (c) 2016-2020 The ZCash developers 2 : // Copyright (c) 2021 The PIVX Core developers 3 : // Distributed under the MIT software license, see the accompanying 4 : // file COPYING or https://www.opensource.org/licenses/mit-license.php. 5 : 6 : #include "sapling/address.h" 7 : 8 : #include "hash.h" 9 : #include "sapling/noteencryption.h" 10 : #include "sapling/prf.h" 11 : #include "sapling/sapling_util.h" 12 : #include "streams.h" 13 : 14 : #include <librustzcash.h> 15 : 16 : const unsigned char ZCASH_SAPLING_FVFP_PERSONALIZATION[crypto_generichash_blake2b_PERSONALBYTES] = 17 : {'Z', 'c', 'a', 's', 'h', 'S', 'a', 'p', 'l', 'i', 'n', 'g', 'F', 'V', 'F', 'P'}; 18 : 19 : namespace libzcash { 20 : 21 : 22 : //! Sapling 23 0 : uint256 SaplingPaymentAddress::GetHash() const { 24 0 : CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); 25 0 : ss << *this; 26 0 : return Hash(ss.begin(), ss.end()); 27 : } 28 : 29 15220 : SaplingFullViewingKey SaplingExpandedSpendingKey::full_viewing_key() const { 30 15220 : uint256 ak; 31 15220 : uint256 nk; 32 15220 : librustzcash_ask_to_ak(ask.begin(), ak.begin()); 33 15220 : librustzcash_nsk_to_nk(nsk.begin(), nk.begin()); 34 15220 : return SaplingFullViewingKey(ak, nk, ovk); 35 : } 36 : 37 62 : SaplingExpandedSpendingKey SaplingSpendingKey::expanded_spending_key() const { 38 62 : return SaplingExpandedSpendingKey(PRF_ask(*this), PRF_nsk(*this), PRF_ovk(*this)); 39 : } 40 : 41 45 : SaplingFullViewingKey SaplingSpendingKey::full_viewing_key() const { 42 45 : return expanded_spending_key().full_viewing_key(); 43 : } 44 : 45 7230 : SaplingIncomingViewingKey SaplingFullViewingKey::in_viewing_key() const { 46 7230 : uint256 ivk; 47 7230 : librustzcash_crh_ivk(ak.begin(), nk.begin(), ivk.begin()); 48 7230 : return SaplingIncomingViewingKey(ivk); 49 : } 50 : 51 10 : bool SaplingFullViewingKey::is_valid() const { 52 10 : uint256 ivk; 53 10 : librustzcash_crh_ivk(ak.begin(), nk.begin(), ivk.begin()); 54 10 : return !ivk.IsNull(); 55 : } 56 : 57 216 : uint256 SaplingFullViewingKey::GetFingerprint() const { 58 216 : CBLAKE2bWriter ss(SER_GETHASH, 0, ZCASH_SAPLING_FVFP_PERSONALIZATION); 59 216 : ss << *this; 60 216 : return ss.GetHash(); 61 : } 62 : 63 : 64 10 : SaplingSpendingKey SaplingSpendingKey::random() { 65 10 : while (true) { 66 10 : auto sk = SaplingSpendingKey(random_uint256()); 67 10 : if (sk.full_viewing_key().is_valid()) { 68 10 : return sk; 69 : } 70 0 : } 71 : } 72 : 73 5350 : Optional<SaplingPaymentAddress> SaplingIncomingViewingKey::address(diversifier_t d) const { 74 5350 : uint256 pk_d; 75 5350 : if (librustzcash_check_diversifier(d.data())) { 76 5350 : librustzcash_ivk_to_pkd(this->begin(), d.data(), pk_d.begin()); 77 5350 : return SaplingPaymentAddress(d, pk_d); 78 : } else { 79 0 : return nullopt; 80 : } 81 : } 82 : 83 18 : SaplingPaymentAddress SaplingSpendingKey::default_address() const { 84 : // Iterates within default_diversifier to ensure a valid address is returned 85 36 : auto addrOpt = full_viewing_key().in_viewing_key().address(default_diversifier(*this)); 86 18 : assert(addrOpt != nullopt); 87 18 : return addrOpt.value(); 88 : } 89 : 90 : } 91 : 92 7582 : bool IsValidPaymentAddress(const libzcash::PaymentAddress& zaddr) { 93 7582 : return zaddr.which() != 0; 94 : }