Line data Source code
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" 18 : #include "sapling/incrementalmerkletree.h" 19 : #include "sapling/note.h" 20 : #include "sapling/noteencryption.h" 21 : 22 192 : struct SpendDescriptionInfo { 23 : libzcash::SaplingExpandedSpendingKey expsk; 24 : libzcash::SaplingNote note; 25 : uint256 alpha; 26 : uint256 anchor; 27 : SaplingWitness witness; 28 : 29 : SpendDescriptionInfo( 30 : const libzcash::SaplingExpandedSpendingKey& _expsk, 31 : const libzcash::SaplingNote& _note, 32 : const uint256& _anchor, 33 : const SaplingWitness& _witness); 34 : }; 35 : 36 3821 : struct OutputDescriptionInfo { 37 : uint256 ovk; 38 : libzcash::SaplingNote note; 39 : std::array<unsigned char, ZC_MEMO_SIZE> memo; 40 : 41 2396 : OutputDescriptionInfo( 42 : const uint256& _ovk, 43 : const libzcash::SaplingNote& _note, 44 2396 : const std::array<unsigned char, ZC_MEMO_SIZE>& _memo): 45 : ovk(_ovk), 46 : note(_note), 47 2396 : memo(_memo) 48 : {} 49 : 50 : Optional<OutputDescription> Build(void* ctx); 51 : }; 52 : 53 9591 : struct TransparentInputInfo { 54 : CScript scriptPubKey; 55 : CAmount value; 56 : 57 2374 : TransparentInputInfo(const CScript& _scriptPubKey, CAmount _value): 58 : scriptPubKey(_scriptPubKey), 59 2374 : value(_value) 60 : {} 61 : }; 62 : 63 : // Dummy constants used during fee-calculation loop 64 : extern const OutputDescription DUMMY_SHIELD_OUT; 65 : extern const SpendDescription DUMMY_SHIELD_SPEND; 66 : extern const SaplingTxData::binding_sig_t DUMMY_SHIELD_BINDSIG; 67 : 68 : class TransactionBuilderResult { 69 : private: 70 : Optional<CTransaction> maybeTx; 71 : Optional<std::string> maybeError; 72 : public: 73 : TransactionBuilderResult() = delete; 74 : explicit TransactionBuilderResult(const CTransaction& tx); 75 : explicit TransactionBuilderResult(const std::string& error); 76 : bool IsTx(); 77 : bool IsError(); 78 : CTransaction GetTxOrThrow(); 79 : Optional<CTransaction> GetTx(); 80 : std::string GetError(); 81 : }; 82 : 83 : class TransactionBuilder 84 : { 85 : private: 86 : Consensus::Params consensusParams; 87 : const CKeyStore* keystore; 88 : CMutableTransaction mtx; 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 : 95 : Optional<std::pair<uint256, libzcash::SaplingPaymentAddress>> saplingChangeAddr; 96 : Optional<CTxDestination> tChangeAddr; 97 : 98 : public: 99 : explicit TransactionBuilder( 100 : const Consensus::Params& consensusParams, 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( 110 : const libzcash::SaplingExpandedSpendingKey& expsk, 111 : const libzcash::SaplingNote& note, 112 : const uint256& anchor, 113 : const SaplingWitness& witness); 114 : 115 : void AddSaplingOutput( 116 : const uint256& ovk, 117 : const libzcash::SaplingPaymentAddress& to, 118 : CAmount value, 119 : const std::array<unsigned char, ZC_MEMO_SIZE>& memo); 120 : 121 72 : void AddSaplingOutput( 122 : const uint256& ovk, 123 : const libzcash::SaplingPaymentAddress& to, 124 : CAmount value) 125 : { 126 72 : const std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}}; 127 72 : AddSaplingOutput(ovk, to, value, memo); 128 72 : } 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 142 : TransactionBuilderResult ProveAndSign(); 143 : // Add dummy Sapling Spend/Output descriptions, binding sig, and transparent signatures 144 : TransactionBuilderResult AddDummySignatures(); 145 : // Remove Sapling Spend/Output descriptions, binding sig, and transparent signatures 146 : void ClearProofsAndSignatures(); 147 : }; 148 : 149 : #endif // PIVX_SAPLING_TRANSACTION_BUILDER_H