]> Git Repo - VerusCoin.git/blob - src/wallet/asyncrpcoperation_shieldcoinbase.cpp
527f810bc6b8cd1f7941ae8b5fb4857f1b3859ad
[VerusCoin.git] / src / wallet / asyncrpcoperation_shieldcoinbase.cpp
1 // Copyright (c) 2017 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.
4
5 #include "asyncrpcqueue.h"
6 #include "amount.h"
7 #include "consensus/upgrades.h"
8 #include "core_io.h"
9 #include "init.h"
10 #include "main.h"
11 #include "net.h"
12 #include "netbase.h"
13 #include "rpcserver.h"
14 #include "timedata.h"
15 #include "util.h"
16 #include "utilmoneystr.h"
17 #include "wallet.h"
18 #include "walletdb.h"
19 #include "script/interpreter.h"
20 #include "utiltime.h"
21 #include "rpcprotocol.h"
22 #include "zcash/IncrementalMerkleTree.hpp"
23 #include "sodium.h"
24 #include "miner.h"
25
26 #include <iostream>
27 #include <chrono>
28 #include <thread>
29 #include <string>
30
31 #include "asyncrpcoperation_shieldcoinbase.h"
32
33 #include "paymentdisclosure.h"
34 #include "paymentdisclosuredb.h"
35
36 using namespace libzcash;
37
38 static int find_output(UniValue obj, int n) {
39     UniValue outputMapValue = find_value(obj, "outputmap");
40     if (!outputMapValue.isArray()) {
41         throw JSONRPCError(RPC_WALLET_ERROR, "Missing outputmap for JoinSplit operation");
42     }
43
44     UniValue outputMap = outputMapValue.get_array();
45     assert(outputMap.size() == ZC_NUM_JS_OUTPUTS);
46     for (size_t i = 0; i < outputMap.size(); i++) {
47         if (outputMap[i].get_int() == n) {
48             return i;
49         }
50     }
51
52     throw std::logic_error("n is not present in outputmap");
53 }
54
55 AsyncRPCOperation_shieldcoinbase::AsyncRPCOperation_shieldcoinbase(
56         CMutableTransaction contextualTx,
57         std::vector<ShieldCoinbaseUTXO> inputs,
58         std::string toAddress,
59         CAmount fee,
60         UniValue contextInfo) :
61         tx_(contextualTx), inputs_(inputs), fee_(fee), contextinfo_(contextInfo)
62 {
63     assert(contextualTx.nVersion >= 2);  // transaction format version must support vjoinsplit
64
65     if (fee < 0 || fee > MAX_MONEY) {
66         throw JSONRPCError(RPC_INVALID_PARAMETER, "Fee is out of range");
67     }
68
69     if (inputs.size() == 0) {
70         throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Empty inputs");
71     }
72
73     //  Check the destination address is valid for this network i.e. not testnet being used on mainnet
74     CZCPaymentAddress address(toAddress);
75     try {
76         tozaddr_ = address.Get();
77     } catch (const std::runtime_error& e) {
78         throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("runtime error: ") + e.what());
79     }
80
81     // Log the context info
82     if (LogAcceptCategory("zrpcunsafe")) {
83         LogPrint("zrpcunsafe", "%s: z_shieldcoinbase initialized (context=%s)\n", getId(), contextInfo.write());
84     } else {
85         LogPrint("zrpc", "%s: z_shieldcoinbase initialized\n", getId());
86     }
87
88     // Lock UTXOs
89     lock_utxos();
90
91     // Enable payment disclosure if requested
92     paymentDisclosureMode = fExperimentalMode && GetBoolArg("-paymentdisclosure", false);
93 }
94
95 AsyncRPCOperation_shieldcoinbase::~AsyncRPCOperation_shieldcoinbase() {
96 }
97
98 void AsyncRPCOperation_shieldcoinbase::main() {
99     if (isCancelled()) {
100         unlock_utxos(); // clean up
101         return;
102     }
103
104     set_state(OperationStatus::EXECUTING);
105     start_execution_clock();
106
107     bool success = false;
108
109 #ifdef ENABLE_MINING
110   #ifdef ENABLE_WALLET
111     GenerateBitcoins(false, NULL, 0);
112   #else
113     GenerateBitcoins(false, 0);
114   #endif
115 #endif
116
117     try {
118         success = main_impl();
119     } catch (const UniValue& objError) {
120         int code = find_value(objError, "code").get_int();
121         std::string message = find_value(objError, "message").get_str();
122         set_error_code(code);
123         set_error_message(message);
124     } catch (const runtime_error& e) {
125         set_error_code(-1);
126         set_error_message("runtime error: " + string(e.what()));
127     } catch (const logic_error& e) {
128         set_error_code(-1);
129         set_error_message("logic error: " + string(e.what()));
130     } catch (const exception& e) {
131         set_error_code(-1);
132         set_error_message("general exception: " + string(e.what()));
133     } catch (...) {
134         set_error_code(-2);
135         set_error_message("unknown error");
136     }
137
138 #ifdef ENABLE_MINING
139   #ifdef ENABLE_WALLET
140     GenerateBitcoins(GetBoolArg("-gen",false), pwalletMain, GetArg("-genproclimit", 1));
141   #else
142     GenerateBitcoins(GetBoolArg("-gen",false), GetArg("-genproclimit", 1));
143   #endif
144 #endif
145
146     stop_execution_clock();
147
148     if (success) {
149         set_state(OperationStatus::SUCCESS);
150     } else {
151         set_state(OperationStatus::FAILED);
152     }
153
154     std::string s = strprintf("%s: z_shieldcoinbase finished (status=%s", getId(), getStateAsString());
155     if (success) {
156         s += strprintf(", txid=%s)\n", tx_.GetHash().ToString());
157     } else {
158         s += strprintf(", error=%s)\n", getErrorMessage());
159     }
160     LogPrintf("%s",s);
161
162     unlock_utxos(); // clean up
163
164     // !!! Payment disclosure START
165     if (success && paymentDisclosureMode && paymentDisclosureData_.size()>0) {
166         uint256 txidhash = tx_.GetHash();
167         std::shared_ptr<PaymentDisclosureDB> db = PaymentDisclosureDB::sharedInstance();
168         for (PaymentDisclosureKeyInfo p : paymentDisclosureData_) {
169             p.first.hash = txidhash;
170             if (!db->Put(p.first, p.second)) {
171                 LogPrint("paymentdisclosure", "%s: Payment Disclosure: Error writing entry to database for key %s\n", getId(), p.first.ToString());
172             } else {
173                 LogPrint("paymentdisclosure", "%s: Payment Disclosure: Successfully added entry to database for key %s\n", getId(), p.first.ToString());
174             }
175         }
176     }
177     // !!! Payment disclosure END
178 }
179
180
181 bool AsyncRPCOperation_shieldcoinbase::main_impl() {
182
183     CAmount minersFee = fee_;
184
185     size_t numInputs = inputs_.size();
186
187     // Check mempooltxinputlimit to avoid creating a transaction which the local mempool rejects
188     size_t limit = (size_t)GetArg("-mempooltxinputlimit", 0);
189     if (limit>0 && numInputs > limit) {
190         throw JSONRPCError(RPC_WALLET_ERROR,
191             strprintf("Number of inputs %d is greater than mempooltxinputlimit of %d",
192             numInputs, limit));
193     }
194
195     CAmount targetAmount = 0;
196     for (ShieldCoinbaseUTXO & utxo : inputs_) {
197         targetAmount += utxo.amount;
198     }
199
200     if (targetAmount <= minersFee) {
201         throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS,
202             strprintf("Insufficient coinbase funds, have %s and miners fee is %s",
203             FormatMoney(targetAmount), FormatMoney(minersFee)));
204     }
205
206     CAmount sendAmount = targetAmount - minersFee;
207     LogPrint("zrpc", "%s: spending %s to shield %s with fee %s\n",
208             getId(), FormatMoney(targetAmount), FormatMoney(sendAmount), FormatMoney(minersFee));
209
210     // update the transaction with these inputs
211     CMutableTransaction rawTx(tx_);
212     for (ShieldCoinbaseUTXO & t : inputs_) {
213         CTxIn in(COutPoint(t.txid, t.vout));
214         rawTx.vin.push_back(in);
215     }
216     tx_ = CTransaction(rawTx);
217
218     // Prepare raw transaction to handle JoinSplits
219     CMutableTransaction mtx(tx_);
220     crypto_sign_keypair(joinSplitPubKey_.begin(), joinSplitPrivKey_);
221     mtx.joinSplitPubKey = joinSplitPubKey_;
222     tx_ = CTransaction(mtx);
223
224     // Create joinsplit
225     UniValue obj(UniValue::VOBJ);
226     ShieldCoinbaseJSInfo info;
227     info.vpub_old = sendAmount;
228     info.vpub_new = 0;
229     JSOutput jso = JSOutput(tozaddr_, sendAmount);
230     info.vjsout.push_back(jso);
231     obj = perform_joinsplit(info);
232
233     sign_send_raw_transaction(obj);
234     return true;
235 }
236
237
238 /**
239  * Sign and send a raw transaction.
240  * Raw transaction as hex string should be in object field "rawtxn"
241  */
242 void AsyncRPCOperation_shieldcoinbase::sign_send_raw_transaction(UniValue obj)
243 {
244     // Sign the raw transaction
245     UniValue rawtxnValue = find_value(obj, "rawtxn");
246     if (rawtxnValue.isNull()) {
247         throw JSONRPCError(RPC_WALLET_ERROR, "Missing hex data for raw transaction");
248     }
249     std::string rawtxn = rawtxnValue.get_str();
250
251     UniValue params = UniValue(UniValue::VARR);
252     params.push_back(rawtxn);
253     UniValue signResultValue = signrawtransaction(params, false);
254     UniValue signResultObject = signResultValue.get_obj();
255     UniValue completeValue = find_value(signResultObject, "complete");
256     bool complete = completeValue.get_bool();
257     if (!complete) {
258         // TODO: #1366 Maybe get "errors" and print array vErrors into a string
259         throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Failed to sign transaction");
260     }
261
262     UniValue hexValue = find_value(signResultObject, "hex");
263     if (hexValue.isNull()) {
264         throw JSONRPCError(RPC_WALLET_ERROR, "Missing hex data for signed transaction");
265     }
266     std::string signedtxn = hexValue.get_str();
267
268     // Send the signed transaction
269     if (!testmode) {
270         params.clear();
271         params.setArray();
272         params.push_back(signedtxn);
273         UniValue sendResultValue = sendrawtransaction(params, false);
274         if (sendResultValue.isNull()) {
275             throw JSONRPCError(RPC_WALLET_ERROR, "Send raw transaction did not return an error or a txid.");
276         }
277
278         std::string txid = sendResultValue.get_str();
279
280         UniValue o(UniValue::VOBJ);
281         o.push_back(Pair("txid", txid));
282         set_result(o);
283     } else {
284         // Test mode does not send the transaction to the network.
285
286         CDataStream stream(ParseHex(signedtxn), SER_NETWORK, PROTOCOL_VERSION);
287         CTransaction tx;
288         stream >> tx;
289
290         UniValue o(UniValue::VOBJ);
291         o.push_back(Pair("test", 1));
292         o.push_back(Pair("txid", tx.GetHash().ToString()));
293         o.push_back(Pair("hex", signedtxn));
294         set_result(o);
295     }
296
297     // Keep the signed transaction so we can hash to the same txid
298     CDataStream stream(ParseHex(signedtxn), SER_NETWORK, PROTOCOL_VERSION);
299     CTransaction tx;
300     stream >> tx;
301     tx_ = tx;
302 }
303
304
305 UniValue AsyncRPCOperation_shieldcoinbase::perform_joinsplit(ShieldCoinbaseJSInfo & info) {
306     uint32_t consensusBranchId;
307     uint256 anchor;
308     {
309         LOCK(cs_main);
310         consensusBranchId = CurrentEpochBranchId(chainActive.Height() + 1, Params().GetConsensus());
311         anchor = pcoinsTip->GetBestAnchor();
312     }
313
314
315     if (anchor.IsNull()) {
316         throw std::runtime_error("anchor is null");
317     }
318
319     // Make sure there are two inputs and two outputs
320     while (info.vjsin.size() < ZC_NUM_JS_INPUTS) {
321         info.vjsin.push_back(JSInput());
322     }
323
324     while (info.vjsout.size() < ZC_NUM_JS_OUTPUTS) {
325         info.vjsout.push_back(JSOutput());
326     }
327
328     if (info.vjsout.size() != ZC_NUM_JS_INPUTS || info.vjsin.size() != ZC_NUM_JS_OUTPUTS) {
329         throw runtime_error("unsupported joinsplit input/output counts");
330     }
331
332     CMutableTransaction mtx(tx_);
333
334     LogPrint("zrpcunsafe", "%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",
335             getId(),
336             tx_.vjoinsplit.size(),
337             FormatMoney(info.vpub_old), FormatMoney(info.vpub_new),
338             FormatMoney(info.vjsin[0].note.value), FormatMoney(info.vjsin[1].note.value),
339             FormatMoney(info.vjsout[0].value), FormatMoney(info.vjsout[1].value)
340             );
341
342     // Generate the proof, this can take over a minute.
343     boost::array<libzcash::JSInput, ZC_NUM_JS_INPUTS> inputs
344             {info.vjsin[0], info.vjsin[1]};
345     boost::array<libzcash::JSOutput, ZC_NUM_JS_OUTPUTS> outputs
346             {info.vjsout[0], info.vjsout[1]};
347     boost::array<size_t, ZC_NUM_JS_INPUTS> inputMap;
348     boost::array<size_t, ZC_NUM_JS_OUTPUTS> outputMap;
349
350     uint256 esk; // payment disclosure - secret
351
352     JSDescription jsdesc = JSDescription::Randomized(
353             *pzcashParams,
354             joinSplitPubKey_,
355             anchor,
356             inputs,
357             outputs,
358             inputMap,
359             outputMap,
360             info.vpub_old,
361             info.vpub_new,
362             !this->testmode,
363             &esk); // parameter expects pointer to esk, so pass in address
364     {
365         auto verifier = libzcash::ProofVerifier::Strict();
366         if (!(jsdesc.Verify(*pzcashParams, verifier, joinSplitPubKey_))) {
367             throw std::runtime_error("error verifying joinsplit");
368         }
369     }
370
371     mtx.vjoinsplit.push_back(jsdesc);
372
373     // Empty output script.
374     CScript scriptCode;
375     CTransaction signTx(mtx);
376     uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL, 0, consensusBranchId);
377
378     // Add the signature
379     if (!(crypto_sign_detached(&mtx.joinSplitSig[0], NULL,
380             dataToBeSigned.begin(), 32,
381             joinSplitPrivKey_
382             ) == 0))
383     {
384         throw std::runtime_error("crypto_sign_detached failed");
385     }
386
387     // Sanity check
388     if (!(crypto_sign_verify_detached(&mtx.joinSplitSig[0],
389             dataToBeSigned.begin(), 32,
390             mtx.joinSplitPubKey.begin()
391             ) == 0))
392     {
393         throw std::runtime_error("crypto_sign_verify_detached failed");
394     }
395
396     CTransaction rawTx(mtx);
397     tx_ = rawTx;
398
399     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
400     ss << rawTx;
401
402     std::string encryptedNote1;
403     std::string encryptedNote2;
404     {
405         CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
406         ss2 << ((unsigned char) 0x00);
407         ss2 << jsdesc.ephemeralKey;
408         ss2 << jsdesc.ciphertexts[0];
409         ss2 << jsdesc.h_sig(*pzcashParams, joinSplitPubKey_);
410
411         encryptedNote1 = HexStr(ss2.begin(), ss2.end());
412     }
413     {
414         CDataStream ss2(SER_NETWORK, PROTOCOL_VERSION);
415         ss2 << ((unsigned char) 0x01);
416         ss2 << jsdesc.ephemeralKey;
417         ss2 << jsdesc.ciphertexts[1];
418         ss2 << jsdesc.h_sig(*pzcashParams, joinSplitPubKey_);
419
420         encryptedNote2 = HexStr(ss2.begin(), ss2.end());
421     }
422
423     UniValue arrInputMap(UniValue::VARR);
424     UniValue arrOutputMap(UniValue::VARR);
425     for (size_t i = 0; i < ZC_NUM_JS_INPUTS; i++) {
426         arrInputMap.push_back(inputMap[i]);
427     }
428     for (size_t i = 0; i < ZC_NUM_JS_OUTPUTS; i++) {
429         arrOutputMap.push_back(outputMap[i]);
430     }
431
432     // !!! Payment disclosure START
433     unsigned char buffer[32] = {0};
434     memcpy(&buffer[0], &joinSplitPrivKey_[0], 32); // private key in first half of 64 byte buffer
435     std::vector<unsigned char> vch(&buffer[0], &buffer[0] + 32);
436     uint256 joinSplitPrivKey = uint256(vch);
437     size_t js_index = tx_.vjoinsplit.size() - 1;
438     uint256 placeholder;
439     for (int i = 0; i < ZC_NUM_JS_OUTPUTS; i++) {
440         uint8_t mapped_index = outputMap[i];
441         // placeholder for txid will be filled in later when tx has been finalized and signed.
442         PaymentDisclosureKey pdKey = {placeholder, js_index, mapped_index};
443         JSOutput output = outputs[mapped_index];
444         libzcash::PaymentAddress zaddr = output.addr;  // randomized output
445         PaymentDisclosureInfo pdInfo = {PAYMENT_DISCLOSURE_VERSION_EXPERIMENTAL, esk, joinSplitPrivKey, zaddr};
446         paymentDisclosureData_.push_back(PaymentDisclosureKeyInfo(pdKey, pdInfo));
447
448         CZCPaymentAddress address(zaddr);
449         LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), address.ToString());
450     }
451     // !!! Payment disclosure END
452
453     UniValue obj(UniValue::VOBJ);
454     obj.push_back(Pair("encryptednote1", encryptedNote1));
455     obj.push_back(Pair("encryptednote2", encryptedNote2));
456     obj.push_back(Pair("rawtxn", HexStr(ss.begin(), ss.end())));
457     obj.push_back(Pair("inputmap", arrInputMap));
458     obj.push_back(Pair("outputmap", arrOutputMap));
459     return obj;
460 }
461
462 /**
463  * Override getStatus() to append the operation's context object to the default status object.
464  */
465 UniValue AsyncRPCOperation_shieldcoinbase::getStatus() const {
466     UniValue v = AsyncRPCOperation::getStatus();
467     if (contextinfo_.isNull()) {
468         return v;
469     }
470
471     UniValue obj = v.get_obj();
472     obj.push_back(Pair("method", "z_shieldcoinbase"));
473     obj.push_back(Pair("params", contextinfo_ ));
474     return obj;
475 }
476
477 /**
478  * Lock input utxos
479  */
480  void AsyncRPCOperation_shieldcoinbase::lock_utxos() {
481     LOCK2(cs_main, pwalletMain->cs_wallet);
482     for (auto utxo : inputs_) {
483         COutPoint outpt(utxo.txid, utxo.vout);
484         pwalletMain->LockCoin(outpt);
485     }
486 }
487
488 /**
489  * Unlock input utxos
490  */
491 void AsyncRPCOperation_shieldcoinbase::unlock_utxos() {
492     LOCK2(cs_main, pwalletMain->cs_wallet);
493     for (auto utxo : inputs_) {
494         COutPoint outpt(utxo.txid, utxo.vout);
495         pwalletMain->UnlockCoin(outpt);
496     }
497 }
This page took 0.04205 seconds and 2 git commands to generate.