LCOV - code coverage report
Current view: top level - src/test/librust - utiltest.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 49 49 100.0 %
Date: 2025-02-23 09:33:43 Functions: 9 9 100.0 %

          Line data    Source code
       1             : // Copyright (c) 2016-2020 The Zcash developers
       2             : // Copyright (c) 2020-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 "utiltest.h"
       7             : 
       8             : #include "key_io.h"
       9             : #include "consensus/upgrades.h"
      10             : #include "sapling/transaction_builder.h"
      11             : 
      12             : #include <array>
      13             : 
      14             : static const std::string T_SECRET_REGTEST = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN";
      15             : 
      16          16 : libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey() {
      17          16 :     std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
      18          32 :     HDSeed seed(rawSeed);
      19          32 :     return libzcash::SaplingExtendedSpendingKey::Master(seed);
      20             : }
      21             : 
      22         127 : CKey CreateCkey(bool genNewKey) {
      23         127 :     CKey tsk;
      24         127 :     if (genNewKey) tsk.MakeNewKey(true);
      25          10 :     else tsk = KeyIO::DecodeSecret(T_SECRET_REGTEST);
      26         127 :     if (!tsk.IsValid()) throw std::runtime_error("CreateCkey:: Invalid priv key");
      27         127 :     return tsk;
      28             : }
      29             : 
      30         126 : CKey AddTestCKeyToKeyStore(CBasicKeyStore& keyStore, bool genNewKey) {
      31         126 :     CKey tsk = CreateCkey(genNewKey);
      32         126 :     keyStore.AddKey(tsk);
      33         126 :     return tsk;
      34             : }
      35             : 
      36         121 : CKey AddTestCKeyToWallet(CWallet& wallet, bool genNewKey) {
      37         121 :     LOCK(wallet.cs_wallet);
      38         242 :     return AddTestCKeyToKeyStore(wallet, genNewKey);
      39             : }
      40             : 
      41           9 : TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress& pa, CAmount value) {
      42             :     // Generate dummy Sapling note
      43           9 :     libzcash::SaplingNote note(pa, value);
      44          18 :     uint256 cm = note.cmu().get();
      45          18 :     SaplingMerkleTree tree;
      46           9 :     tree.append(cm);
      47          18 :     return { note, tree };
      48             : }
      49             : 
      50         121 : CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams,
      51             :                                  CBasicKeyStore& keyStoreFrom,
      52             :                                  std::vector<TransparentInput> vIn,
      53             :                                  std::vector<ShieldedDestination> vDest,
      54             :                                  const CWallet* pwalletIn)
      55             : {
      56         242 :     auto builder = TransactionBuilder(consensusParams, &keyStoreFrom);
      57         121 :     builder.SetFee(0);
      58             : 
      59             :     // From transparent inputs
      60         363 :     for (const auto& in : vIn) {
      61         242 :         builder.AddTransparentInput(in.outPoint, in.scriptPubKey, in.amount);
      62             :     }
      63             : 
      64             :     // To shielded addrs
      65         243 :     for (const auto& dest : vDest) {
      66         122 :         auto fvk = dest.sk.expsk.full_viewing_key();
      67         122 :         auto pa = dest.sk.DefaultAddress();
      68         122 :         builder.AddSaplingOutput(fvk.ovk, pa, dest.amount, {});
      69             :     }
      70             : 
      71         121 :     CTransaction tx = builder.Build().GetTxOrThrow();
      72         121 :     CWalletTx wtx {pwalletIn, MakeTransactionRef(tx)};
      73         242 :     return wtx;
      74             : }
      75             : 
      76             : // Two dummy input (to trick coinbase check), one or many shielded outputs
      77         121 : CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams,
      78             :                                  CWallet& keyStoreFrom,
      79             :                                  CAmount inputAmount,
      80             :                                  std::vector<ShieldedDestination> vDest,
      81             :                                  bool genNewKey,
      82             :                                  const CWallet* pwalletIn) {
      83             :     // From taddr
      84         121 :     CKey tsk = AddTestCKeyToWallet(keyStoreFrom, genNewKey);
      85         242 :     auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
      86             : 
      87             :     // Two equal dummy inputs to by-pass the coinbase check.
      88         242 :     TransparentInput dummyInput{COutPoint(), scriptPubKey, inputAmount / 2};
      89         605 :     std::vector<TransparentInput> vIn = {dummyInput, dummyInput};
      90         363 :     return GetValidSaplingReceive(consensusParams, keyStoreFrom, vIn, vDest, pwalletIn);
      91             : }
      92             : 
      93             : // Single input, single shielded output
      94         118 : CWalletTx GetValidSaplingReceive(const Consensus::Params& consensusParams,
      95             :                                  CWallet& keyStore,
      96             :                                  const libzcash::SaplingExtendedSpendingKey &sk,
      97             :                                  CAmount value,
      98             :                                  bool genNewKey,
      99             :                                  const CWallet* pwalletIn) {
     100         118 :     std::vector<ShieldedDestination> vDest;
     101         118 :     vDest.push_back({sk, value});
     102         118 :     return GetValidSaplingReceive(
     103             :             consensusParams,
     104             :             keyStore,
     105             :             value,
     106             :             vDest,
     107             :             genNewKey,
     108             :             pwalletIn
     109         354 :     );
     110             : }
     111             : 
     112           1 : CScript CreateDummyDestinationScript() {
     113           1 :     CKey key = CreateCkey(true);
     114           3 :     return GetScriptForDestination(key.GetPubKey().GetID());
     115             : }

Generated by: LCOV version 1.14