1 // Copyright (c) 2016 The Zcash developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef ASYNCRPCOPERATION_SENDMANY_H
6 #define ASYNCRPCOPERATION_SENDMANY_H
8 #include "asyncrpcoperation.h"
11 #include "primitives/transaction.h"
12 #include "zcash/JoinSplit.hpp"
13 #include "zcash/Address.hpp"
14 #include "json/json_spirit_value.h"
19 // TODO: Compute fee based on a heuristic, e.g. (num tx output * dust threshold) + joinsplit bytes * ?
20 #define ASYNC_RPC_OPERATION_DEFAULT_MINERS_FEE 10000
22 using namespace libzcash;
23 using namespace json_spirit;
25 // A recipient is a tuple of address, amount, memo (optional if zaddr)
26 typedef std::tuple<std::string, CAmount, std::string> SendManyRecipient;
28 // Input UTXO is a tuple (quadruple) of txid, vout, amount, coinbase)
29 typedef std::tuple<uint256, int, CAmount, bool> SendManyInputUTXO;
31 // Input JSOP is a tuple of JSOutpoint, note and amount
32 typedef std::tuple<JSOutPoint, Note, CAmount> SendManyInputJSOP;
34 // Package of info which is passed to perform_joinsplit methods.
35 struct AsyncJoinSplitInfo
37 std::vector<JSInput> vjsin;
38 std::vector<JSOutput> vjsout;
39 std::vector<Note> notes;
44 class AsyncRPCOperation_sendmany : public AsyncRPCOperation {
46 AsyncRPCOperation_sendmany(std::string fromAddress, std::vector<SendManyRecipient> tOutputs, std::vector<SendManyRecipient> zOutputs, int minDepth);
47 virtual ~AsyncRPCOperation_sendmany();
49 // We don't want to be copied or moved around
50 AsyncRPCOperation_sendmany(AsyncRPCOperation_sendmany const&) = delete; // Copy construct
51 AsyncRPCOperation_sendmany(AsyncRPCOperation_sendmany&&) = delete; // Move construct
52 AsyncRPCOperation_sendmany& operator=(AsyncRPCOperation_sendmany const&) = delete; // Copy assign
53 AsyncRPCOperation_sendmany& operator=(AsyncRPCOperation_sendmany &&) = delete; // Move assign
57 bool testmode = false; // Set to true to disable sending txs and generating proofs
60 friend class TEST_FRIEND_AsyncRPCOperation_sendmany; // class for unit testing
63 std::string fromaddress_;
66 CBitcoinAddress fromtaddr_;
67 PaymentAddress frompaymentaddress_;
68 SpendingKey spendingkey_;
70 uint256 joinSplitPubKey_;
71 unsigned char joinSplitPrivKey_[crypto_sign_SECRETKEYBYTES];
72 ZCJoinSplit *zcashParams_;
74 std::vector<SendManyRecipient> t_outputs_;
75 std::vector<SendManyRecipient> z_outputs_;
76 std::vector<SendManyInputUTXO> t_inputs_;
77 std::vector<SendManyInputJSOP> z_inputs_;
81 void add_taddr_change_output_to_tx(CAmount amount);
82 void add_taddr_outputs_to_tx();
83 bool find_unspent_notes();
84 bool find_utxos(bool fAcceptCoinbase);
85 boost::array<unsigned char, ZC_MEMO_SIZE> get_memo_from_hex_string(std::string s);
88 // JoinSplit without any input notes to spend
89 Object perform_joinsplit(AsyncJoinSplitInfo &);
91 // JoinSplit with input notes to spend (JSOutPoints))
92 Object perform_joinsplit(AsyncJoinSplitInfo &, std::vector<JSOutPoint> & );
94 // JoinSplit where you have the witnesses and anchor
95 Object perform_joinsplit(
96 AsyncJoinSplitInfo & info,
97 std::vector<boost::optional < ZCIncrementalWitness>> witnesses,
100 void sign_send_raw_transaction(Object obj); // throws exception if there was an error
105 // To test private methods, a friend class can act as a proxy
106 class TEST_FRIEND_AsyncRPCOperation_sendmany {
108 std::shared_ptr<AsyncRPCOperation_sendmany> delegate;
110 TEST_FRIEND_AsyncRPCOperation_sendmany(std::shared_ptr<AsyncRPCOperation_sendmany> ptr) : delegate(ptr) {}
112 CTransaction getTx() {
113 return delegate->tx_;
116 void setTx(CTransaction tx) {
122 void add_taddr_change_output_to_tx(CAmount amount) {
123 delegate->add_taddr_change_output_to_tx(amount);
126 void add_taddr_outputs_to_tx() {
127 delegate->add_taddr_outputs_to_tx();
130 bool find_unspent_notes() {
131 return delegate->find_unspent_notes();
134 bool find_utxos(bool fAcceptCoinbase) {
135 return delegate->find_utxos(fAcceptCoinbase);
138 boost::array<unsigned char, ZC_MEMO_SIZE> get_memo_from_hex_string(std::string s) {
139 return delegate->get_memo_from_hex_string(s);
143 return delegate->main_impl();
146 Object perform_joinsplit(AsyncJoinSplitInfo &info) {
147 return delegate->perform_joinsplit(info);
150 Object perform_joinsplit(AsyncJoinSplitInfo &info, std::vector<JSOutPoint> &v ) {
151 return delegate->perform_joinsplit(info, v);
154 Object perform_joinsplit(
155 AsyncJoinSplitInfo & info,
156 std::vector<boost::optional < ZCIncrementalWitness>> witnesses,
159 return delegate->perform_joinsplit(info, witnesses, anchor);
162 void sign_send_raw_transaction(Object obj) {
163 delegate->sign_send_raw_transaction(obj);
168 #endif /* ASYNCRPCOPERATION_SENDMANY_H */