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 #include "asyncrpcoperation_sendmany.h"
6 #include "asyncrpcqueue.h"
13 #include "rpcserver.h"
16 #include "utilmoneystr.h"
19 #include "script/interpreter.h"
21 #include "rpcprotocol.h"
22 #include "zcash/IncrementalMerkleTree.hpp"
30 using namespace libzcash;
32 int find_output(Object obj, int n) {
33 Value outputMapValue = find_value(obj, "outputmap");
34 if (outputMapValue.type() != array_type) {
35 throw JSONRPCError(RPC_WALLET_ERROR, "Missing outputmap for JoinSplit operation");
38 Array outputMap = outputMapValue.get_array();
39 assert(outputMap.size() == ZC_NUM_JS_OUTPUTS);
40 for (size_t i = 0; i < outputMap.size(); i++) {
41 if (outputMap[i] == n) {
46 throw std::logic_error("n is not present in outputmap");
49 AsyncRPCOperation_sendmany::AsyncRPCOperation_sendmany(
50 std::string fromAddress,
51 std::vector<SendManyRecipient> tOutputs,
52 std::vector<SendManyRecipient> zOutputs,
56 fromaddress_(fromAddress), t_outputs_(tOutputs), z_outputs_(zOutputs), mindepth_(minDepth), fee_(fee), contextinfo_(contextInfo)
61 throw JSONRPCError(RPC_INVALID_PARAMETER, "Minconf cannot be negative");
64 if (fromAddress.size() == 0) {
65 throw JSONRPCError(RPC_INVALID_PARAMETER, "From address parameter missing");
68 if (tOutputs.size() == 0 && zOutputs.size() == 0) {
69 throw JSONRPCError(RPC_INVALID_PARAMETER, "No recipients");
72 fromtaddr_ = CBitcoinAddress(fromAddress);
73 isfromtaddr_ = fromtaddr_.IsValid();
77 CZCPaymentAddress address(fromAddress);
79 PaymentAddress addr = address.Get();
81 // We don't need to lock on the wallet as spending key related methods are thread-safe
83 if (!pwalletMain->GetSpendingKey(addr, key)) {
84 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address, no spending key found for zaddr");
88 frompaymentaddress_ = addr;
90 } catch (const std::runtime_error& e) {
91 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("runtime error: ") + e.what());
96 AsyncRPCOperation_sendmany::~AsyncRPCOperation_sendmany() {
99 void AsyncRPCOperation_sendmany::main() {
103 set_state(OperationStatus::EXECUTING);
104 start_execution_clock();
106 bool success = false;
109 success = main_impl();
110 } catch (const Object& objError) {
111 int code = find_value(objError, "code").get_int();
112 std::string message = find_value(objError, "message").get_str();
113 set_error_code(code);
114 set_error_message(message);
115 } catch (const runtime_error& e) {
117 set_error_message("runtime error: " + string(e.what()));
118 } catch (const logic_error& e) {
120 set_error_message("logic error: " + string(e.what()));
121 } catch (const exception& e) {
123 set_error_message("general exception: " + string(e.what()));
126 set_error_message("unknown error");
129 stop_execution_clock();
132 set_state(OperationStatus::SUCCESS);
134 set_state(OperationStatus::FAILED);
137 std::string s = strprintf("async rpc %s finished (status=%s", getId(), getStateAsString());
139 s += strprintf(", tx=%s)\n", tx_.ToString());
141 s += strprintf(", error=%s)\n", getErrorMessage());
147 // 1. #1159 Currently there is no limit set on the number of joinsplits, so size of tx could be invalid.
148 // 2. #1360 Note selection is not optimal
149 // 3. #1277 Spendable notes are not locked, so an operation running in parallel could also try to use them
150 bool AsyncRPCOperation_sendmany::main_impl() {
152 assert(isfromtaddr_ != isfromzaddr_);
154 bool isSingleZaddrOutput = (t_outputs_.size()==0 && z_outputs_.size()==1);
155 bool isMultipleZaddrOutput = (t_outputs_.size()==0 && z_outputs_.size()>=1);
156 bool isPureTaddrOnlyTx = (isfromtaddr_ && z_outputs_.size() == 0);
157 CAmount minersFee = fee_;
159 // When spending coinbase utxos, you can only specify a single zaddr as the change must go somewhere
160 // and if there are multiple zaddrs, we don't know where to send it.
162 if (isSingleZaddrOutput) {
163 bool b = find_utxos(true);
165 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds, no UTXOs found for taddr from address.");
168 bool b = find_utxos(false);
170 if (isMultipleZaddrOutput) {
171 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Could not find any non-coinbase UTXOs to spend. Coinbase UTXOs can only be sent to a single zaddr recipient.");
173 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Could not find any non-coinbase UTXOs to spend.");
179 if (isfromzaddr_ && !find_unspent_notes()) {
180 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Insufficient funds, no unspent notes found for zaddr from address.");
183 CAmount t_inputs_total = 0;
184 for (SendManyInputUTXO & t : t_inputs_) {
185 t_inputs_total += std::get<2>(t);
188 CAmount z_inputs_total = 0;
189 for (SendManyInputJSOP & t : z_inputs_) {
190 z_inputs_total += std::get<2>(t);
193 CAmount t_outputs_total = 0;
194 for (SendManyRecipient & t : t_outputs_) {
195 t_outputs_total += std::get<1>(t);
198 CAmount z_outputs_total = 0;
199 for (SendManyRecipient & t : z_outputs_) {
200 z_outputs_total += std::get<1>(t);
203 CAmount sendAmount = z_outputs_total + t_outputs_total;
204 CAmount targetAmount = sendAmount + minersFee;
206 assert(!isfromtaddr_ || z_inputs_total == 0);
207 assert(!isfromzaddr_ || t_inputs_total == 0);
209 if (isfromtaddr_ && (t_inputs_total < targetAmount)) {
210 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
211 strprintf("Insufficient transparent funds, have %s, need %s",
212 FormatMoney(t_inputs_total), FormatMoney(targetAmount)));
215 if (isfromzaddr_ && (z_inputs_total < targetAmount)) {
216 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
217 strprintf("Insufficient protected funds, have %s, need %s",
218 FormatMoney(z_inputs_total), FormatMoney(targetAmount)));
221 // If from address is a taddr, select UTXOs to spend
222 CAmount selectedUTXOAmount = 0;
223 bool selectedUTXOCoinbase = false;
225 // Get dust threshold
227 secret.MakeNewKey(true);
228 CScript scriptPubKey = GetScriptForDestination(secret.GetPubKey().GetID());
229 CTxOut out(CAmount(1), scriptPubKey);
230 CAmount dustThreshold = out.GetDustThreshold(minRelayTxFee);
231 CAmount dustChange = -1;
233 std::vector<SendManyInputUTXO> selectedTInputs;
234 for (SendManyInputUTXO & t : t_inputs_) {
235 bool b = std::get<3>(t);
237 selectedUTXOCoinbase = true;
239 selectedUTXOAmount += std::get<2>(t);
240 selectedTInputs.push_back(t);
241 if (selectedUTXOAmount >= targetAmount) {
242 // Select another utxo if there is change less than the dust threshold.
243 dustChange = selectedUTXOAmount - targetAmount;
244 if (dustChange == 0 || dustChange >= dustThreshold) {
250 // If there is transparent change, is it valid or is it dust?
251 if (dustChange < dustThreshold && dustChange != 0) {
252 throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
253 strprintf("Insufficient transparent funds, have %s, need %s more to avoid creating invalid change output %s (dust threshold is %s)",
254 FormatMoney(t_inputs_total), FormatMoney(dustThreshold - dustChange), FormatMoney(dustChange), FormatMoney(dustThreshold)));
257 t_inputs_ = selectedTInputs;
258 t_inputs_total = selectedUTXOAmount;
260 // update the transaction with these inputs
261 CMutableTransaction rawTx(tx_);
262 for (SendManyInputUTXO & t : t_inputs_) {
263 uint256 txid = std::get<0>(t);
264 int vout = std::get<1>(t);
265 CAmount amount = std::get<2>(t);
266 CTxIn in(COutPoint(txid, vout));
267 rawTx.vin.push_back(in);
269 tx_ = CTransaction(rawTx);
272 LogPrint("zrpc", "%s: spending %s to send %s with fee %s\n",
273 getId().substr(0,10), FormatMoney(targetAmount, false), FormatMoney(sendAmount, false), FormatMoney(minersFee, false));
274 LogPrint("zrpc", " - transparent input: %s (to choose from)\n", FormatMoney(t_inputs_total, false));
275 LogPrint("zrpc", " - private input: %s (to choose from)\n", FormatMoney(z_inputs_total, false));
276 LogPrint("zrpc", " - transparent output: %s\n", FormatMoney(t_outputs_total, false));
277 LogPrint("zrpc", " - private output: %s\n", FormatMoney(z_outputs_total, false));
278 LogPrint("zrpc", " - fee: %s\n", FormatMoney(minersFee, false));
285 * There are no zaddrs or joinsplits involved.
287 if (isPureTaddrOnlyTx) {
288 add_taddr_outputs_to_tx();
290 CAmount funds = selectedUTXOAmount;
291 CAmount fundsSpent = t_outputs_total + minersFee;
292 CAmount change = funds - fundsSpent;
295 add_taddr_change_output_to_tx(change);
297 LogPrint("zrpc", "%s: transparent change in transaction output (amount=%s)\n",
298 getId().substr(0, 10),
299 FormatMoney(change, false)
304 obj.push_back(Pair("rawtxn", EncodeHexTx(tx_)));
305 sign_send_raw_transaction(obj);
313 // Prepare raw transaction to handle JoinSplits
314 CMutableTransaction mtx(tx_);
316 crypto_sign_keypair(joinSplitPubKey_.begin(), joinSplitPrivKey_);
317 mtx.joinSplitPubKey = joinSplitPubKey_;
318 tx_ = CTransaction(mtx);
320 // Copy zinputs and zoutputs to more flexible containers
321 std::deque<SendManyInputJSOP> zInputsDeque;
322 for (auto o : z_inputs_) {
323 zInputsDeque.push_back(o);
325 std::deque<SendManyRecipient> zOutputsDeque;
326 for (auto o : z_outputs_) {
327 zOutputsDeque.push_back(o);
330 // When spending notes, take a snapshot of note witnesses and anchors as the treestate will
331 // change upon arrival of new blocks which contain joinsplit transactions. This is likely
332 // to happen as creating a chained joinsplit transaction can take longer than the block interval.
333 if (z_inputs_.size() > 0) {
334 LOCK2(cs_main, pwalletMain->cs_wallet);
335 for (auto t : z_inputs_) {
336 JSOutPoint jso = std::get<0>(t);
337 std::vector<JSOutPoint> vOutPoints = { jso };
339 std::vector<boost::optional<ZCIncrementalWitness>> vInputWitnesses;
340 pwalletMain->GetNoteWitnesses(vOutPoints, vInputWitnesses, inputAnchor);
341 jsopWitnessAnchorMap[ jso.ToString() ] = WitnessAnchorData{ vInputWitnesses[0], inputAnchor };
352 * Note: Consensus rule states that coinbase utxos can only be sent to a zaddr.
353 * Local wallet rule does not allow any change when sending coinbase utxos
354 * since there is currently no way to specify a change address and we don't
355 * want users accidentally sending excess funds to a recipient.
358 add_taddr_outputs_to_tx();
360 CAmount funds = selectedUTXOAmount;
361 CAmount fundsSpent = t_outputs_total + minersFee + z_outputs_total;
362 CAmount change = funds - fundsSpent;
365 if (selectedUTXOCoinbase) {
366 assert(isSingleZaddrOutput);
367 throw JSONRPCError(RPC_WALLET_ERROR, strprintf(
368 "Change %s not allowed. When protecting coinbase funds, the wallet does not "
369 "allow any change as there is currently no way to specify a change address "
370 "in z_sendmany.", FormatMoney(change)));
372 add_taddr_change_output_to_tx(change);
373 LogPrint("zrpc", "%s: transparent change in transaction output (amount=%s)\n",
374 getId().substr(0, 10),
375 FormatMoney(change, false)
380 // Create joinsplits, where each output represents a zaddr recipient.
382 while (zOutputsDeque.size() > 0) {
383 AsyncJoinSplitInfo info;
387 while (n++<ZC_NUM_JS_OUTPUTS && zOutputsDeque.size() > 0) {
388 SendManyRecipient smr = zOutputsDeque.front();
389 std::string address = std::get<0>(smr);
390 CAmount value = std::get<1>(smr);
391 std::string hexMemo = std::get<2>(smr);
392 zOutputsDeque.pop_front();
394 PaymentAddress pa = CZCPaymentAddress(address).Get();
395 JSOutput jso = JSOutput(pa, value);
396 if (hexMemo.size() > 0) {
397 jso.memo = get_memo_from_hex_string(hexMemo);
399 info.vjsout.push_back(jso);
401 // Funds are removed from the value pool and enter the private pool
402 info.vpub_old += value;
404 obj = perform_joinsplit(info);
406 sign_send_raw_transaction(obj);
422 * Part 1: taddrs and miners fee
428 * Part 1: Add to the transparent value pool.
431 CAmount jsChange = 0; // this is updated after each joinsplit
432 int changeOutputIndex = -1; // this is updated after each joinsplit if jsChange > 0
433 bool minersFeeProcessed = false;
435 if (t_outputs_total > 0) {
436 add_taddr_outputs_to_tx();
437 CAmount taddrTargetAmount = t_outputs_total + minersFee;
438 minersFeeProcessed = true;
439 while (zInputsDeque.size() > 0 && taddrTargetAmount > 0) {
440 AsyncJoinSplitInfo info;
443 std::vector<JSOutPoint> outPoints;
445 while (n++ < ZC_NUM_JS_INPUTS && taddrTargetAmount > 0) {
446 SendManyInputJSOP o = zInputsDeque.front();
447 JSOutPoint outPoint = std::get<0>(o);
448 Note note = std::get<1>(o);
449 CAmount noteFunds = std::get<2>(o);
450 zInputsDeque.pop_front();
452 info.notes.push_back(note);
453 outPoints.push_back(outPoint);
458 LOCK2(cs_main, pwalletMain->cs_wallet);
459 const CWalletTx& wtx = pwalletMain->mapWallet[outPoint.hash];
460 wtxHeight = mapBlockIndex[wtx.hashBlock]->nHeight;
461 wtxDepth = wtx.GetDepthInMainChain();
463 LogPrint("zrpc", "%s: spending note (txid=%s, vjoinsplit=%d, ciphertext=%d, amount=%s, height=%d, confirmations=%d)\n",
464 getId().substr(0, 10),
465 outPoint.hash.ToString().substr(0, 10),
467 int(outPoint.n), // uint8_t
468 FormatMoney(noteFunds, false),
474 // Put value back into the value pool
475 if (noteFunds >= taddrTargetAmount) {
476 jsChange = noteFunds - taddrTargetAmount;
477 info.vpub_new += taddrTargetAmount;
479 info.vpub_new += noteFunds;
482 taddrTargetAmount -= noteFunds;
483 if (taddrTargetAmount <= 0) {
489 info.vjsout.push_back(JSOutput());
490 info.vjsout.push_back(JSOutput(frompaymentaddress_, jsChange));
492 LogPrint("zrpc", "%s: generating note for change (amount=%s)\n",
493 getId().substr(0, 10),
494 FormatMoney(jsChange, false)
498 obj = perform_joinsplit(info, outPoints);
501 changeOutputIndex = find_output(obj, 1);
509 * Part 2: Send to zaddrs by chaining JoinSplits together and immediately consuming any change
511 if (z_outputs_total>0) {
513 // Keep track of treestate within this transaction
514 boost::unordered_map<uint256, ZCIncrementalMerkleTree, CCoinsKeyHasher> intermediates;
515 std::vector<uint256> previousCommitments;
517 while (zOutputsDeque.size() > 0) {
518 AsyncJoinSplitInfo info;
522 CAmount jsInputValue = 0;
524 std::vector<boost::optional<ZCIncrementalWitness>> witnesses;
526 JSDescription prevJoinSplit;
528 // Keep track of previous JoinSplit and its commitments
529 if (tx_.vjoinsplit.size() > 0) {
530 prevJoinSplit = tx_.vjoinsplit.back();
533 // If there is no change, the chain has terminated so we can reset the tracked treestate.
534 if (jsChange==0 && tx_.vjoinsplit.size() > 0) {
535 intermediates.clear();
536 previousCommitments.clear();
540 // Consume change as the first input of the JoinSplit.
543 LOCK2(cs_main, pwalletMain->cs_wallet);
545 // Update tree state with previous joinsplit
546 ZCIncrementalMerkleTree tree;
547 auto it = intermediates.find(prevJoinSplit.anchor);
548 if (it != intermediates.end()) {
550 } else if (!pcoinsTip->GetAnchorAt(prevJoinSplit.anchor, tree)) {
551 throw JSONRPCError(RPC_WALLET_ERROR, "Could not find previous JoinSplit anchor");
554 assert(changeOutputIndex != -1);
555 boost::optional<ZCIncrementalWitness> changeWitness;
557 for (const uint256& commitment : prevJoinSplit.commitments) {
558 tree.append(commitment);
559 previousCommitments.push_back(commitment);
560 if (!changeWitness && changeOutputIndex == n++) {
561 changeWitness = tree.witness();
562 } else if (changeWitness) {
563 changeWitness.get().append(commitment);
567 witnesses.push_back(changeWitness);
569 jsAnchor = tree.root();
570 intermediates.insert(std::make_pair(tree.root(), tree)); // chained js are interstitial (found in between block boundaries)
572 // Decrypt the change note's ciphertext to retrieve some data we need
573 ZCNoteDecryption decryptor(spendingkey_.viewing_key());
574 auto hSig = prevJoinSplit.h_sig(*pzcashParams, tx_.joinSplitPubKey);
576 NotePlaintext plaintext = NotePlaintext::decrypt(
578 prevJoinSplit.ciphertexts[changeOutputIndex],
579 prevJoinSplit.ephemeralKey,
581 (unsigned char) changeOutputIndex);
583 Note note = plaintext.note(frompaymentaddress_);
584 info.notes.push_back(note);
586 jsInputValue += plaintext.value;
588 LogPrint("zrpc", "%s: spending change (amount=%s)\n",
589 getId().substr(0, 10),
590 FormatMoney(plaintext.value, false)
593 } catch (const std::exception& e) {
594 throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error decrypting output note of previous JoinSplit: %s", e.what()));
600 // Consume spendable non-change notes
602 std::vector<Note> vInputNotes;
603 std::vector<JSOutPoint> vOutPoints;
604 std::vector<boost::optional<ZCIncrementalWitness>> vInputWitnesses;
606 int numInputsNeeded = (jsChange>0) ? 1 : 0;
607 while (numInputsNeeded++ < ZC_NUM_JS_INPUTS && zInputsDeque.size() > 0) {
608 SendManyInputJSOP t = zInputsDeque.front();
609 JSOutPoint jso = std::get<0>(t);
610 Note note = std::get<1>(t);
611 CAmount noteFunds = std::get<2>(t);
612 zInputsDeque.pop_front();
614 WitnessAnchorData wad = jsopWitnessAnchorMap[ jso.ToString() ];
615 vInputWitnesses.push_back(wad.witness);
616 if (inputAnchor.IsNull()) {
617 inputAnchor = wad.anchor;
618 } else if (inputAnchor != wad.anchor) {
619 throw JSONRPCError(RPC_WALLET_ERROR, "Selected input notes do not share the same anchor");
622 vOutPoints.push_back(jso);
623 vInputNotes.push_back(note);
625 jsInputValue += noteFunds;
630 LOCK2(cs_main, pwalletMain->cs_wallet);
631 const CWalletTx& wtx = pwalletMain->mapWallet[jso.hash];
632 wtxHeight = mapBlockIndex[wtx.hashBlock]->nHeight;
633 wtxDepth = wtx.GetDepthInMainChain();
635 LogPrint("zrpc", "%s: spending note (txid=%s, vjoinsplit=%d, ciphertext=%d, amount=%s, height=%d, confirmations=%d)\n",
636 getId().substr(0, 10),
637 jso.hash.ToString().substr(0, 10),
639 int(jso.n), // uint8_t
640 FormatMoney(noteFunds, false),
646 // Add history of previous commitments to witness
647 if (vInputNotes.size() > 0) {
649 if (vInputWitnesses.size()==0) {
650 throw JSONRPCError(RPC_WALLET_ERROR, "Could not find witness for note commitment");
653 for (auto & optionalWitness : vInputWitnesses) {
654 if (!optionalWitness) {
655 throw JSONRPCError(RPC_WALLET_ERROR, "Witness for note commitment is null");
657 ZCIncrementalWitness w = *optionalWitness; // could use .get();
659 for (const uint256& commitment : previousCommitments) {
660 w.append(commitment);
662 if (jsAnchor != w.root()) {
663 throw JSONRPCError(RPC_WALLET_ERROR, "Witness for spendable note does not have same anchor as change input");
666 witnesses.push_back(w);
669 // The jsAnchor is null if this JoinSplit is at the start of a new chain
670 if (jsAnchor.IsNull()) {
671 jsAnchor = inputAnchor;
674 // Add spendable notes as inputs
675 std::copy(vInputNotes.begin(), vInputNotes.end(), std::back_inserter(info.notes));
680 // Find recipient to transfer funds to
682 SendManyRecipient smr = zOutputsDeque.front();
683 std::string address = std::get<0>(smr);
684 CAmount value = std::get<1>(smr);
685 std::string hexMemo = std::get<2>(smr);
686 zOutputsDeque.pop_front();
688 // Will we have any change? Has the miners fee been processed yet?
690 CAmount outAmount = value;
691 if (!minersFeeProcessed) {
692 if (jsInputValue < minersFee) {
693 throw JSONRPCError(RPC_WALLET_ERROR, "Not enough funds to pay miners fee");
695 outAmount += minersFee;
698 if (jsInputValue > outAmount) {
699 jsChange = jsInputValue - outAmount;
700 } else if (outAmount > jsInputValue) {
701 // Any amount due is owed to the recipient. Let the miners fee get paid first.
702 CAmount due = outAmount - jsInputValue;
703 SendManyRecipient r = SendManyRecipient(address, due, hexMemo);
704 zOutputsDeque.push_front(r);
706 // reduce the amount being sent right now to the value of all inputs
707 value = jsInputValue;
708 if (!minersFeeProcessed) {
713 if (!minersFeeProcessed) {
714 minersFeeProcessed = true;
715 info.vpub_new += minersFee; // funds flowing back to public pool
718 // create output for recipient
719 PaymentAddress pa = CZCPaymentAddress(address).Get();
720 JSOutput jso = JSOutput(pa, value);
721 if (hexMemo.size() > 0) {
722 jso.memo = get_memo_from_hex_string(hexMemo);
724 info.vjsout.push_back(jso);
726 // create output for any change
728 info.vjsout.push_back(JSOutput(frompaymentaddress_, jsChange));
730 LogPrint("zrpc", "%s: generating note for change (amount=%s)\n",
731 getId().substr(0, 10),
732 FormatMoney(jsChange, false)
736 obj = perform_joinsplit(info, witnesses, jsAnchor);
739 changeOutputIndex = find_output(obj, 1);
744 sign_send_raw_transaction(obj);
750 * Sign and send a raw transaction.
751 * Raw transaction as hex string should be in object field "rawtxn"
753 void AsyncRPCOperation_sendmany::sign_send_raw_transaction(Object obj)
755 // Sign the raw transaction
756 Value rawtxnValue = find_value(obj, "rawtxn");
757 if (rawtxnValue.is_null()) {
758 throw JSONRPCError(RPC_WALLET_ERROR, "Missing hex data for raw transaction");
760 std::string rawtxn = rawtxnValue.get_str();
762 Value signResultValue = signrawtransaction({Value(rawtxn)}, false);
763 Object signResultObject = signResultValue.get_obj();
764 Value completeValue = find_value(signResultObject, "complete");
765 bool complete = completeValue.get_bool();
767 // TODO: #1366 Maybe get "errors" and print array vErrors into a string
768 throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Failed to sign transaction");
771 Value hexValue = find_value(signResultObject, "hex");
772 if (hexValue.is_null()) {
773 throw JSONRPCError(RPC_WALLET_ERROR, "Missing hex data for signed transaction");
775 std::string signedtxn = hexValue.get_str();
777 // Send the signed transaction
779 Value sendResultValue = sendrawtransaction({Value(signedtxn)}, false);
780 if (sendResultValue.is_null()) {
781 throw JSONRPCError(RPC_WALLET_ERROR, "Send raw transaction did not return an error or a txid.");
784 std::string txid = sendResultValue.get_str();
787 o.push_back(Pair("txid", txid));
788 set_result(Value(o));
790 // Test mode does not send the transaction to the network.
792 CDataStream stream(ParseHex(signedtxn), SER_NETWORK, PROTOCOL_VERSION);
797 o.push_back(Pair("test", 1));
798 o.push_back(Pair("txid", tx.GetHash().ToString()));
799 o.push_back(Pair("hex", signedtxn));
800 set_result(Value(o));
803 // Keep the signed transaction so we can hash to the same txid
804 CDataStream stream(ParseHex(signedtxn), SER_NETWORK, PROTOCOL_VERSION);
811 bool AsyncRPCOperation_sendmany::find_utxos(bool fAcceptCoinbase=false) {
812 set<CBitcoinAddress> setAddress = {fromtaddr_};
813 vector<COutput> vecOutputs;
815 LOCK2(cs_main, pwalletMain->cs_wallet);
817 pwalletMain->AvailableCoins(vecOutputs, false, NULL, true, fAcceptCoinbase);
819 BOOST_FOREACH(const COutput& out, vecOutputs) {
820 if (out.nDepth < mindepth_) {
824 if (setAddress.size()) {
825 CTxDestination address;
826 if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) {
830 if (!setAddress.count(address)) {
835 // By default we ignore coinbase outputs
836 bool isCoinbase = out.tx->IsCoinBase();
837 if (isCoinbase && fAcceptCoinbase==false) {
841 CAmount nValue = out.tx->vout[out.i].nValue;
842 SendManyInputUTXO utxo(out.tx->GetHash(), out.i, nValue, isCoinbase);
843 t_inputs_.push_back(utxo);
846 // sort in ascending order, so smaller utxos appear first
847 std::sort(t_inputs_.begin(), t_inputs_.end(), [](SendManyInputUTXO i, SendManyInputUTXO j) -> bool {
848 return ( std::get<2>(i) < std::get<2>(j));
851 return t_inputs_.size() > 0;
855 bool AsyncRPCOperation_sendmany::find_unspent_notes() {
856 std::vector<CNotePlaintextEntry> entries;
858 LOCK2(cs_main, pwalletMain->cs_wallet);
859 pwalletMain->GetFilteredNotes(entries, fromaddress_, mindepth_);
862 for (CNotePlaintextEntry & entry : entries) {
863 z_inputs_.push_back(SendManyInputJSOP(entry.jsop, entry.plaintext.note(frompaymentaddress_), CAmount(entry.plaintext.value)));
864 std::string data(entry.plaintext.memo.begin(), entry.plaintext.memo.end());
865 if (LogAcceptCategory("zrpcunsafe")) {
866 LogPrint("zrpcunsafe", "%s: found unspent note (txid=%s, vjoinsplit=%d, ciphertext=%d, amount=%s, memo=%s)\n",
867 getId().substr(0, 10),
868 entry.jsop.hash.ToString().substr(0, 10),
870 int(entry.jsop.n), // uint8_t
871 FormatMoney(entry.plaintext.value, false),
872 HexStr(data).substr(0, 10)
875 LogPrint("zrpc", "%s: found unspent note (txid=%s, vjoinsplit=%d, ciphertext=%d, amount=%s)\n",
876 getId().substr(0, 10),
877 entry.jsop.hash.ToString().substr(0, 10),
879 int(entry.jsop.n), // uint8_t
880 FormatMoney(entry.plaintext.value, false)
885 if (z_inputs_.size() == 0) {
889 // sort in descending order, so big notes appear first
890 std::sort(z_inputs_.begin(), z_inputs_.end(), [](SendManyInputJSOP i, SendManyInputJSOP j) -> bool {
891 return ( std::get<2>(i) > std::get<2>(j));
897 Object AsyncRPCOperation_sendmany::perform_joinsplit(AsyncJoinSplitInfo & info) {
898 std::vector<boost::optional < ZCIncrementalWitness>> witnesses;
901 LOCK2(cs_main, pwalletMain->cs_wallet);
902 anchor = pcoinsTip->GetBestAnchor(); // As there are no inputs, ask the wallet for the best anchor
904 return perform_joinsplit(info, witnesses, anchor);
908 Object AsyncRPCOperation_sendmany::perform_joinsplit(AsyncJoinSplitInfo & info, std::vector<JSOutPoint> & outPoints) {
909 std::vector<boost::optional < ZCIncrementalWitness>> witnesses;
913 pwalletMain->GetNoteWitnesses(outPoints, witnesses, anchor);
915 return perform_joinsplit(info, witnesses, anchor);
918 Object AsyncRPCOperation_sendmany::perform_joinsplit(
919 AsyncJoinSplitInfo & info,
920 std::vector<boost::optional < ZCIncrementalWitness>> witnesses,
923 if (anchor.IsNull()) {
924 throw std::runtime_error("anchor is null");
927 if (!(witnesses.size() == info.notes.size())) {
928 throw runtime_error("number of notes and witnesses do not match");
931 for (size_t i = 0; i < witnesses.size(); i++) {
933 throw runtime_error("joinsplit input could not be found in tree");
935 info.vjsin.push_back(JSInput(*witnesses[i], info.notes[i], spendingkey_));
938 // Make sure there are two inputs and two outputs
939 while (info.vjsin.size() < ZC_NUM_JS_INPUTS) {
940 info.vjsin.push_back(JSInput());
943 while (info.vjsout.size() < ZC_NUM_JS_OUTPUTS) {
944 info.vjsout.push_back(JSOutput());
947 if (info.vjsout.size() != ZC_NUM_JS_INPUTS || info.vjsin.size() != ZC_NUM_JS_OUTPUTS) {
948 throw runtime_error("unsupported joinsplit input/output counts");
951 CMutableTransaction mtx(tx_);
953 LogPrint("zrpc", "%s: creating joinsplit at index %d (vpub_old=%s, vpub_new=%s, in[0]=%s, in[1]=%s, out[0]=%s, out[1]=%s)\n",
954 getId().substr(0,10),
955 tx_.vjoinsplit.size(),
956 FormatMoney(info.vpub_old, false), FormatMoney(info.vpub_new, false),
957 FormatMoney(info.vjsin[0].note.value, false), FormatMoney(info.vjsin[1].note.value, false),
958 FormatMoney(info.vjsout[0].value, false), FormatMoney(info.vjsout[1].value, false)
961 // Generate the proof, this can take over a minute.
962 boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> inputs
963 {info.vjsin[0], info.vjsin[1]};
964 boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> outputs
965 {info.vjsout[0], info.vjsout[1]};
966 boost::array<size_t, ZC_NUM_JS_INPUTS> inputMap;
967 boost::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap;
968 JSDescription jsdesc = JSDescription::Randomized(
981 auto verifier = libzcash::ProofVerifier::Strict();
982 if (!(jsdesc.Verify(*pzcashParams, verifier, joinSplitPubKey_))) {
983 throw std::runtime_error("error verifying joinsplit");
987 mtx.vjoinsplit.push_back(jsdesc);
989 // Empty output script.
991 CTransaction signTx(mtx);
992 uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL);
995 if (!(crypto_sign_detached(&mtx.joinSplitSig[0], NULL,
996 dataToBeSigned.begin(), 32,
1000 throw std::runtime_error("crypto_sign_detached failed");
1004 if (!(crypto_sign_verify_detached(&mtx.joinSplitSig[0],
1005 dataToBeSigned.begin(), 32,
1006 mtx.joinSplitPubKey.begin()
1009 throw std::runtime_error("crypto_sign_verify_detached failed");
1012 CTransaction rawTx(mtx);
1015 CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
1018 std::string encryptedNote1;
1019 std::string encryptedNote2;
1021 CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
1022 ss2 << ((unsigned char) 0x00);
1023 ss2 << jsdesc.ephemeralKey;
1024 ss2 << jsdesc.ciphertexts[0];
1025 ss2 << jsdesc.h_sig(*pzcashParams, joinSplitPubKey_);
1027 encryptedNote1 = HexStr(ss2.begin(), ss2.end());
1030 CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
1031 ss2 << ((unsigned char) 0x01);
1032 ss2 << jsdesc.ephemeralKey;
1033 ss2 << jsdesc.ciphertexts[1];
1034 ss2 << jsdesc.h_sig(*pzcashParams, joinSplitPubKey_);
1036 encryptedNote2 = HexStr(ss2.begin(), ss2.end());
1041 for (size_t i = 0; i < ZC_NUM_JS_INPUTS; i++) {
1042 arrInputMap.push_back(inputMap[i]);
1044 for (size_t i = 0; i < ZC_NUM_JS_OUTPUTS; i++) {
1045 arrOutputMap.push_back(outputMap[i]);
1049 obj.push_back(Pair("encryptednote1", encryptedNote1));
1050 obj.push_back(Pair("encryptednote2", encryptedNote2));
1051 obj.push_back(Pair("rawtxn", HexStr(ss.begin(), ss.end())));
1052 obj.push_back(Pair("inputmap", arrInputMap));
1053 obj.push_back(Pair("outputmap", arrOutputMap));
1057 void AsyncRPCOperation_sendmany::add_taddr_outputs_to_tx() {
1059 CMutableTransaction rawTx(tx_);
1061 for (SendManyRecipient & r : t_outputs_) {
1062 std::string outputAddress = std::get<0>(r);
1063 CAmount nAmount = std::get<1>(r);
1065 CBitcoinAddress address(outputAddress);
1066 if (!address.IsValid()) {
1067 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid output address, not a valid taddr.");
1070 CScript scriptPubKey = GetScriptForDestination(address.Get());
1072 CTxOut out(nAmount, scriptPubKey);
1073 rawTx.vout.push_back(out);
1076 tx_ = CTransaction(rawTx);
1079 void AsyncRPCOperation_sendmany::add_taddr_change_output_to_tx(CAmount amount) {
1081 LOCK2(cs_main, pwalletMain->cs_wallet);
1083 EnsureWalletIsUnlocked();
1084 CReserveKey keyChange(pwalletMain);
1086 bool ret = keyChange.GetReservedKey(vchPubKey);
1088 throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Could not generate a taddr to use as a change address"); // should never fail, as we just unlocked
1090 CScript scriptPubKey = GetScriptForDestination(vchPubKey.GetID());
1091 CTxOut out(amount, scriptPubKey);
1093 CMutableTransaction rawTx(tx_);
1094 rawTx.vout.push_back(out);
1095 tx_ = CTransaction(rawTx);
1098 boost::array<unsigned char, ZC_MEMO_SIZE> AsyncRPCOperation_sendmany::get_memo_from_hex_string(std::string s) {
1099 boost::array<unsigned char, ZC_MEMO_SIZE> memo = {{0x00}};
1101 std::vector<unsigned char> rawMemo = ParseHex(s.c_str());
1103 // If ParseHex comes across a non-hex char, it will stop but still return results so far.
1104 size_t slen = s.length();
1105 if (slen % 2 !=0 || (slen>0 && rawMemo.size()!=slen/2)) {
1106 throw JSONRPCError(RPC_INVALID_PARAMETER, "Memo must be in hexadecimal format");
1109 if (rawMemo.size() > ZC_MEMO_SIZE) {
1110 throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Memo size of %d is too big, maximum allowed is %d", rawMemo.size(), ZC_MEMO_SIZE));
1113 // copy vector into boost array
1114 int lenMemo = rawMemo.size();
1115 for (int i = 0; i < ZC_MEMO_SIZE && i < lenMemo; i++) {
1116 memo[i] = rawMemo[i];
1122 * Override getStatus() to append the operation's input parameters to the default status object.
1124 Value AsyncRPCOperation_sendmany::getStatus() const {
1125 Value v = AsyncRPCOperation::getStatus();
1126 if (contextinfo_.is_null()) {
1130 Object obj = v.get_obj();
1131 obj.push_back(Pair("method", "z_sendmany"));
1132 obj.push_back(Pair("params", contextinfo_ ));