]> Git Repo - VerusCoin.git/blob - src/transaction_builder.h
Replace http with https: in links to the MIT license.
[VerusCoin.git] / src / transaction_builder.h
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 .
4
5 #ifndef TRANSACTION_BUILDER_H
6 #define TRANSACTION_BUILDER_H
7
8 #include "coins.h"
9 #include "consensus/params.h"
10 #include "keystore.h"
11 #include "primitives/transaction.h"
12 #include "script/script.h"
13 #include "script/standard.h"
14 #include "uint256.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"
20
21 #include <boost/optional.hpp>
22
23 struct SpendDescriptionInfo {
24     libzcash::SaplingExpandedSpendingKey expsk;
25     libzcash::SaplingNote note;
26     uint256 alpha;
27     uint256 anchor;
28     SaplingWitness witness;
29
30     SpendDescriptionInfo(
31         libzcash::SaplingExpandedSpendingKey expsk,
32         libzcash::SaplingNote note,
33         uint256 anchor,
34         SaplingWitness witness);
35 };
36
37 struct OutputDescriptionInfo {
38     uint256 ovk;
39     libzcash::SaplingNote note;
40     std::array<unsigned char, ZC_MEMO_SIZE> memo;
41
42     OutputDescriptionInfo(
43         uint256 ovk,
44         libzcash::SaplingNote note,
45         std::array<unsigned char, ZC_MEMO_SIZE> memo) : ovk(ovk), note(note), memo(memo) {}
46 };
47
48 struct TransparentInputInfo {
49     CScript scriptPubKey;
50     CAmount value;
51
52     TransparentInputInfo(
53         CScript scriptPubKey,
54         CAmount value) : scriptPubKey(scriptPubKey), value(value) {}
55 };
56
57 class TransactionBuilderResult {
58 private:
59     boost::optional<CTransaction> maybeTx;
60     boost::optional<std::string> maybeError;
61 public:
62     TransactionBuilderResult() = delete;
63     TransactionBuilderResult(const CTransaction& tx);
64     TransactionBuilderResult(const std::string& error);
65     bool IsTx();
66     bool IsError();
67     CTransaction GetTxOrThrow();
68     std::string GetError();
69 };
70
71 class TransactionBuilder
72 {
73 private:
74     Consensus::Params consensusParams;
75     int nHeight;
76     const CKeyStore* keystore;
77     ZCJoinSplit* sproutParams;
78     const CCoinsViewCache* coinsView;
79     CCriticalSection* cs_coinsView;
80     CMutableTransaction mtx;
81     CAmount fee = 10000;
82
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;
88
89     boost::optional<std::pair<uint256, libzcash::SaplingPaymentAddress>> saplingChangeAddr;
90     boost::optional<libzcash::SproutPaymentAddress> sproutChangeAddr;
91     boost::optional<CTxDestination> tChangeAddr;
92
93 public:
94     TransactionBuilder() {}
95     TransactionBuilder(
96         const Consensus::Params& consensusParams,
97         int nHeight,
98         int nExpiryDelta,
99         CKeyStore* keyStore = nullptr,
100         ZCJoinSplit* sproutParams = nullptr,
101         CCoinsViewCache* coinsView = nullptr,
102         CCriticalSection* cs_coinsView = nullptr);
103
104     void SetFee(CAmount fee);
105
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,
111         uint256 anchor,
112         SaplingWitness witness);
113
114     void AddSaplingOutput(
115         uint256 ovk,
116         libzcash::SaplingPaymentAddress to,
117         CAmount value,
118         std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}});
119
120     // Throws if the anchor does not match the anchor used by
121     // previously-added Sprout inputs.
122     void AddSproutInput(
123         libzcash::SproutSpendingKey sk,
124         libzcash::SproutNote note,
125         SproutWitness witness);
126
127     void AddSproutOutput(
128         libzcash::SproutPaymentAddress to,
129         CAmount value,
130         std::array<unsigned char, ZC_MEMO_SIZE> memo = {{0xF6}});
131
132     // Assumes that the value correctly corresponds to the provided UTXO.
133     void AddTransparentInput(COutPoint utxo, CScript scriptPubKey, CAmount value);
134
135     void AddTransparentOutput(CTxDestination& to, CAmount value);
136
137     void SendChangeTo(libzcash::SaplingPaymentAddress changeAddr, uint256 ovk);
138
139     void SendChangeTo(libzcash::SproutPaymentAddress);
140
141     void SendChangeTo(CTxDestination& changeAddr);
142
143     TransactionBuilderResult Build();
144
145 private:
146     void CreateJSDescriptions();
147
148     void CreateJSDescription(
149         uint64_t vpub_old,
150         uint64_t vpub_new,
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);
155 };
156
157 #endif /* TRANSACTION_BUILDER_H */
This page took 0.034598 seconds and 4 git commands to generate.