PIVX Core  5.6.99
P2P Digital Currency
utiltest.cpp
Go to the documentation of this file.
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"
11 
12 #include <array>
13 
14 static const std::string T_SECRET_REGTEST = "cND2ZvtabDbJ1gucx9GWH6XT9kgTAqfb6cotPt5Q5CyxVDhid2EN";
15 
17  std::vector<unsigned char, secure_allocator<unsigned char>> rawSeed(32);
18  HDSeed seed(rawSeed);
20 }
21 
22 CKey CreateCkey(bool genNewKey) {
23  CKey tsk;
24  if (genNewKey) tsk.MakeNewKey(true);
25  else tsk = KeyIO::DecodeSecret(T_SECRET_REGTEST);
26  if (!tsk.IsValid()) throw std::runtime_error("CreateCkey:: Invalid priv key");
27  return tsk;
28 }
29 
30 CKey AddTestCKeyToKeyStore(CBasicKeyStore& keyStore, bool genNewKey) {
31  CKey tsk = CreateCkey(genNewKey);
32  keyStore.AddKey(tsk);
33  return tsk;
34 }
35 
36 CKey AddTestCKeyToWallet(CWallet& wallet, bool genNewKey) {
37  LOCK(wallet.cs_wallet);
38  return AddTestCKeyToKeyStore(wallet, genNewKey);
39 }
40 
42  // Generate dummy Sapling note
43  libzcash::SaplingNote note(pa, value);
44  uint256 cm = note.cmu().get();
45  SaplingMerkleTree tree;
46  tree.append(cm);
47  return { note, tree };
48 }
49 
51  CBasicKeyStore& keyStoreFrom,
52  std::vector<TransparentInput> vIn,
53  std::vector<ShieldedDestination> vDest,
54  const CWallet* pwalletIn)
55 {
56  auto builder = TransactionBuilder(consensusParams, &keyStoreFrom);
57  builder.SetFee(0);
58 
59  // From transparent inputs
60  for (const auto& in : vIn) {
61  builder.AddTransparentInput(in.outPoint, in.scriptPubKey, in.amount);
62  }
63 
64  // To shielded addrs
65  for (const auto& dest : vDest) {
66  auto fvk = dest.sk.expsk.full_viewing_key();
67  auto pa = dest.sk.DefaultAddress();
68  builder.AddSaplingOutput(fvk.ovk, pa, dest.amount, {});
69  }
70 
71  CTransaction tx = builder.Build().GetTxOrThrow();
72  CWalletTx wtx {pwalletIn, MakeTransactionRef(tx)};
73  return wtx;
74 }
75 
76 // Two dummy input (to trick coinbase check), one or many shielded outputs
78  CWallet& keyStoreFrom,
79  CAmount inputAmount,
80  std::vector<ShieldedDestination> vDest,
81  bool genNewKey,
82  const CWallet* pwalletIn) {
83  // From taddr
84  CKey tsk = AddTestCKeyToWallet(keyStoreFrom, genNewKey);
85  auto scriptPubKey = GetScriptForDestination(tsk.GetPubKey().GetID());
86 
87  // Two equal dummy inputs to by-pass the coinbase check.
88  TransparentInput dummyInput{COutPoint(), scriptPubKey, inputAmount / 2};
89  std::vector<TransparentInput> vIn = {dummyInput, dummyInput};
90  return GetValidSaplingReceive(consensusParams, keyStoreFrom, vIn, vDest, pwalletIn);
91 }
92 
93 // Single input, single shielded output
95  CWallet& keyStore,
97  CAmount value,
98  bool genNewKey,
99  const CWallet* pwalletIn) {
100  std::vector<ShieldedDestination> vDest;
101  vDest.push_back({sk, value});
102  return GetValidSaplingReceive(
103  consensusParams,
104  keyStore,
105  value,
106  vDest,
107  genNewKey,
108  pwalletIn
109  );
110 }
111 
113  CKey key = CreateCkey(true);
114  return GetScriptForDestination(key.GetPubKey().GetID());
115 }
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
Basic key store, that keeps keys in an address->secret map.
Definition: keystore.h:99
An encapsulated private key.
Definition: key.h:30
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:95
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:158
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:186
virtual bool AddKey(const CKey &key)
Definition: keystore.cpp:14
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:167
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances,...
Definition: wallet.h:577
RecursiveMutex cs_wallet
Definition: wallet.h:720
A transaction with a bunch of additional info that only the owner cares about.
Definition: wallet.h:325
Definition: zip32.h:20
Optional< uint256 > cmu() const
Definition: note.cpp:29
Sapling functions.
Definition: address.h:30
256-bit opaque blob.
Definition: uint256.h:138
@ LOCK
Definition: lockunlock.h:16
CKey DecodeSecret(const std::string &str)
Definition: key_io.cpp:127
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a PIVX scriptPubKey for the given CTxDestination.
Definition: standard.cpp:278
Parameters that influence chain consensus.
Definition: params.h:171
static SaplingExtendedSpendingKey Master(const HDSeed &seed)
Definition: zip32.cpp:117
CKey AddTestCKeyToKeyStore(CBasicKeyStore &keyStore, bool genNewKey)
Definition: utiltest.cpp:30
TestSaplingNote GetTestSaplingNote(const libzcash::SaplingPaymentAddress &pa, CAmount value)
Generate a dummy SaplingNote and a SaplingMerkleTree with that note's commitment.
Definition: utiltest.cpp:41
CKey CreateCkey(bool genNewKey)
Definition: utiltest.cpp:22
CWalletTx GetValidSaplingReceive(const Consensus::Params &consensusParams, CBasicKeyStore &keyStoreFrom, std::vector< TransparentInput > vIn, std::vector< ShieldedDestination > vDest, const CWallet *pwalletIn)
One or many inputs from keyStoreFrom, one or many shielded outputs to pwalletIn (if not nullptr).
Definition: utiltest.cpp:50
libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey()
Definition: utiltest.cpp:16
CScript CreateDummyDestinationScript()
Generates a dummy destination script.
Definition: utiltest.cpp:112
CKey AddTestCKeyToWallet(CWallet &wallet, bool genNewKey)
Definition: utiltest.cpp:36