1 // Copyright (c) 2018 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
5 #ifndef TRANSACTION_BUILDER_H
6 #define TRANSACTION_BUILDER_H
9 #include "consensus/params.h"
11 #include "primitives/transaction.h"
12 #include "script/script.h"
13 #include "script/standard.h"
15 #include "zcash/Address.hpp"
16 #include "zcash/IncrementalMerkleTree.hpp"
17 #include "zcash/JoinSplit.hpp"
18 #include "zcash/Note.hpp"
19 #include "zcash/NoteEncryption.hpp"
21 #include <boost/optional.hpp>
23 struct SpendDescriptionInfo {
24 libzcash::SaplingExpandedSpendingKey expsk;
25 libzcash::SaplingNote note;
28 SaplingWitness witness;
31 libzcash::SaplingExpandedSpendingKey expsk,
32 libzcash::SaplingNote note,
34 SaplingWitness witness);
37 struct OutputDescriptionInfo {
39 libzcash::SaplingNote note;
40 std::array<unsigned char, ZC_MEMO_SIZE> memo;
42 OutputDescriptionInfo(
44 libzcash::SaplingNote note,
45 std::array<unsigned char, ZC_MEMO_SIZE> memo) : ovk(ovk), note(note), memo(memo) {}
48 struct TransparentInputInfo {
54 CAmount value) : scriptPubKey(scriptPubKey), value(value) {}
57 class TransactionBuilderResult {
59 boost::optional<CTransaction> maybeTx;
60 boost::optional<std::string> maybeError;
62 TransactionBuilderResult() = delete;
63 TransactionBuilderResult(const CTransaction& tx);
64 TransactionBuilderResult(const std::string& error);
67 CTransaction GetTxOrThrow();
68 std::string GetError();
71 class TransactionBuilder
74 Consensus::Params consensusParams;
76 const CKeyStore* keystore;
77 ZCJoinSplit* sproutParams;
78 const CCoinsViewCache* coinsView;
79 CCriticalSection* cs_coinsView;
80 CMutableTransaction mtx;
83 std::vector<SpendDescriptionInfo> spends;
84 std::vector<OutputDescriptionInfo> outputs;
85 std::vector<libzcash::JSInput> jsInputs;
86 std::vector<libzcash::JSOutput> jsOutputs;
87 std::vector<TransparentInputInfo> tIns;
89 boost::optional<std::pair<uint256, libzcash::SaplingPaymentAddress>> saplingChangeAddr;
90 boost::optional<libzcash::SproutPaymentAddress> sproutChangeAddr;
91 boost::optional<CTxDestination> tChangeAddr;
94 TransactionBuilder() {}
96 const Consensus::Params& consensusParams,
99 CKeyStore* keyStore = nullptr,
100 ZCJoinSplit* sproutParams = nullptr,
101 CCoinsViewCache* coinsView = nullptr,
102 CCriticalSection* cs_coinsView = nullptr);
104 void SetFee(CAmount fee);
106 // Throws if the anchor does not match the anchor used by
107 // previously-added Sapling spends.
108 void AddSaplingSpend(
109 libzcash::SaplingExpandedSpendingKey expsk,
110 libzcash::SaplingNote note,
112 SaplingWitness witness);
114 void AddSaplingOutput(
116 libzcash::SaplingPaymentAddress to,
118 std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}});
120 // Throws if the anchor does not match the anchor used by
121 // previously-added Sprout inputs.
123 libzcash::SproutSpendingKey sk,
124 libzcash::SproutNote note,
125 SproutWitness witness);
127 void AddSproutOutput(
128 libzcash::SproutPaymentAddress to,
130 std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}});
132 // Assumes that the value correctly corresponds to the provided UTXO.
133 void AddTransparentInput(COutPoint utxo, CScript scriptPubKey, CAmount value);
135 void AddTransparentOutput(CTxDestination& to, CAmount value);
137 void SendChangeTo(libzcash::SaplingPaymentAddress changeAddr, uint256 ovk);
139 void SendChangeTo(libzcash::SproutPaymentAddress);
141 void SendChangeTo(CTxDestination& changeAddr);
143 TransactionBuilderResult Build();
146 void CreateJSDescriptions();
148 void CreateJSDescription(
151 std::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> vjsin,
152 std::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> vjsout,
153 std::array<size_t, ZC_NUM_JS_INPUTS>& inputMap,
154 std::array<size_t, ZC_NUM_JS_OUTPUTS>& outputMap);
157 #endif /* TRANSACTION_BUILDER_H */