PIVX Core  5.6.99
P2P Digital Currency
transaction_builder.h
Go to the documentation of this file.
1 // Copyright (c) 2018-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 #ifndef PIVX_SAPLING_TRANSACTION_BUILDER_H
7 #define PIVX_SAPLING_TRANSACTION_BUILDER_H
8 
9 #include "coins.h"
10 #include "consensus/params.h"
11 #include "keystore.h"
12 #include "optional.h"
13 #include "primitives/transaction.h"
14 #include "script/script.h"
15 #include "script/standard.h"
16 #include "uint256.h"
17 #include "sapling/address.h"
19 #include "sapling/note.h"
20 #include "sapling/noteencryption.h"
21 
28 
31  const libzcash::SaplingNote& _note,
32  const uint256& _anchor,
33  const SaplingWitness& _witness);
34 };
35 
39  std::array<unsigned char, ZC_MEMO_SIZE> memo;
40 
42  const uint256& _ovk,
43  const libzcash::SaplingNote& _note,
44  const std::array<unsigned char, ZC_MEMO_SIZE>& _memo):
45  ovk(_ovk),
46  note(_note),
47  memo(_memo)
48  {}
49 
51 };
52 
56 
57  TransparentInputInfo(const CScript& _scriptPubKey, CAmount _value):
58  scriptPubKey(_scriptPubKey),
59  value(_value)
60  {}
61 };
62 
63 // Dummy constants used during fee-calculation loop
67 
69 private:
72 public:
74  explicit TransactionBuilderResult(const CTransaction& tx);
75  explicit TransactionBuilderResult(const std::string& error);
76  bool IsTx();
77  bool IsError();
80  std::string GetError();
81 };
82 
84 {
85 private:
89  CAmount fee = -1; // Verified in Build(). Must be set before.
90 
91  std::vector<SpendDescriptionInfo> spends;
92  std::vector<OutputDescriptionInfo> outputs;
93  std::vector<TransparentInputInfo> tIns;
94 
97 
98 public:
99  explicit TransactionBuilder(
101  CKeyStore* keyStore = nullptr);
102 
103  void Clear();
104 
105  void SetFee(CAmount _fee);
106 
107  // Throws if the anchor does not match the anchor used by
108  // previously-added Sapling spends.
109  void AddSaplingSpend(
111  const libzcash::SaplingNote& note,
112  const uint256& anchor,
113  const SaplingWitness& witness);
114 
115  void AddSaplingOutput(
116  const uint256& ovk,
118  CAmount value,
119  const std::array<unsigned char, ZC_MEMO_SIZE>& memo);
120 
122  const uint256& ovk,
124  CAmount value)
125  {
126  const std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}};
127  AddSaplingOutput(ovk, to, value, memo);
128  }
129 
130  // Assumes that the value correctly corresponds to the provided UTXO.
131  void AddTransparentInput(const COutPoint& utxo, const CScript& scriptPubKey, CAmount value);
132 
133  void AddTransparentOutput(const CTxOut& out);
134  void AddTransparentOutput(const CTxDestination& dest, CAmount value);
135 
136  void SendChangeTo(const libzcash::SaplingPaymentAddress& changeAddr, const uint256& ovk);
137 
138  void SendChangeTo(const CTxDestination& changeAddr);
139 
140  TransactionBuilderResult Build(bool fDummySig = false);
141  // Add Sapling Spend/Output descriptions, binding sig, and transparent signatures
143  // Add dummy Sapling Spend/Output descriptions, binding sig, and transparent signatures
145  // Remove Sapling Spend/Output descriptions, binding sig, and transparent signatures
147 };
148 
149 #endif // PIVX_SAPLING_TRANSACTION_BUILDER_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
A virtual base class for key stores.
Definition: keystore.h:23
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:72
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
An output of a transaction.
Definition: transaction.h:137
A shielded output to a transaction.
std::array< unsigned char, BINDINGSIG_SIZE > binding_sig_t
A shielded input to a transaction.
std::vector< OutputDescriptionInfo > outputs
void AddTransparentOutput(const CTxOut &out)
void SetFee(CAmount _fee)
std::vector< TransparentInputInfo > tIns
void AddSaplingSpend(const libzcash::SaplingExpandedSpendingKey &expsk, const libzcash::SaplingNote &note, const uint256 &anchor, const SaplingWitness &witness)
void AddSaplingOutput(const uint256 &ovk, const libzcash::SaplingPaymentAddress &to, CAmount value)
TransactionBuilderResult AddDummySignatures()
TransactionBuilderResult Build(bool fDummySig=false)
void AddSaplingOutput(const uint256 &ovk, const libzcash::SaplingPaymentAddress &to, CAmount value, const std::array< unsigned char, ZC_MEMO_SIZE > &memo)
Consensus::Params consensusParams
TransactionBuilderResult ProveAndSign()
const CKeyStore * keystore
std::vector< SpendDescriptionInfo > spends
void AddTransparentInput(const COutPoint &utxo, const CScript &scriptPubKey, CAmount value)
TransactionBuilder(const Consensus::Params &consensusParams, CKeyStore *keyStore=nullptr)
CMutableTransaction mtx
void SendChangeTo(const libzcash::SaplingPaymentAddress &changeAddr, const uint256 &ovk)
Optional< CTxDestination > tChangeAddr
Optional< std::pair< uint256, libzcash::SaplingPaymentAddress > > saplingChangeAddr
Optional< CTransaction > GetTx()
Optional< CTransaction > maybeTx
Optional< std::string > maybeError
Sapling functions.
Definition: address.h:30
256-bit opaque blob.
Definition: uint256.h:138
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:12
boost::variant< CNoDestination, CKeyID, CScriptID, CExchangeKeyID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:72
A mutable version of CTransaction.
Definition: transaction.h:409
Parameters that influence chain consensus.
Definition: params.h:171
std::array< unsigned char, ZC_MEMO_SIZE > memo
OutputDescriptionInfo(const uint256 &_ovk, const libzcash::SaplingNote &_note, const std::array< unsigned char, ZC_MEMO_SIZE > &_memo)
Optional< OutputDescription > Build(void *ctx)
libzcash::SaplingNote note
libzcash::SaplingNote note
SpendDescriptionInfo(const libzcash::SaplingExpandedSpendingKey &_expsk, const libzcash::SaplingNote &_note, const uint256 &_anchor, const SaplingWitness &_witness)
libzcash::SaplingExpandedSpendingKey expsk
TransparentInputInfo(const CScript &_scriptPubKey, CAmount _value)
bool error(const char *fmt, const Args &... args)
Definition: system.h:77
const SaplingTxData::binding_sig_t DUMMY_SHIELD_BINDSIG
const SpendDescription DUMMY_SHIELD_SPEND
const OutputDescription DUMMY_SHIELD_OUT