]>
Commit | Line | Data |
---|---|---|
0bb3d40f JG |
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. | |
4 | ||
5 | #include "utiltest.h" | |
6 | ||
be126699 JG |
7 | #include "consensus/upgrades.h" |
8 | ||
0bb3d40f JG |
9 | CWalletTx GetValidReceive(ZCJoinSplit& params, |
10 | const libzcash::SpendingKey& sk, CAmount value, | |
11 | bool randomInputs) { | |
12 | CMutableTransaction mtx; | |
13 | mtx.nVersion = 2; // Enable JoinSplits | |
14 | mtx.vin.resize(2); | |
15 | if (randomInputs) { | |
16 | mtx.vin[0].prevout.hash = GetRandHash(); | |
17 | mtx.vin[1].prevout.hash = GetRandHash(); | |
18 | } else { | |
19 | mtx.vin[0].prevout.hash = uint256S("0000000000000000000000000000000000000000000000000000000000000001"); | |
20 | mtx.vin[1].prevout.hash = uint256S("0000000000000000000000000000000000000000000000000000000000000002"); | |
21 | } | |
22 | mtx.vin[0].prevout.n = 0; | |
23 | mtx.vin[1].prevout.n = 0; | |
24 | ||
25 | // Generate an ephemeral keypair. | |
26 | uint256 joinSplitPubKey; | |
27 | unsigned char joinSplitPrivKey[crypto_sign_SECRETKEYBYTES]; | |
28 | crypto_sign_keypair(joinSplitPubKey.begin(), joinSplitPrivKey); | |
29 | mtx.joinSplitPubKey = joinSplitPubKey; | |
30 | ||
31 | boost::array<libzcash::JSInput, 2> inputs = { | |
32 | libzcash::JSInput(), // dummy input | |
33 | libzcash::JSInput() // dummy input | |
34 | }; | |
35 | ||
36 | boost::array<libzcash::JSOutput, 2> outputs = { | |
37 | libzcash::JSOutput(sk.address(), value), | |
38 | libzcash::JSOutput(sk.address(), value) | |
39 | }; | |
40 | ||
41 | boost::array<libzcash::Note, 2> output_notes; | |
42 | ||
43 | // Prepare JoinSplits | |
44 | uint256 rt; | |
45 | JSDescription jsdesc {params, mtx.joinSplitPubKey, rt, | |
46 | inputs, outputs, 2*value, 0, false}; | |
47 | mtx.vjoinsplit.push_back(jsdesc); | |
48 | ||
49 | // Empty output script. | |
be126699 | 50 | uint32_t consensusBranchId = SPROUT_BRANCH_ID; |
0bb3d40f JG |
51 | CScript scriptCode; |
52 | CTransaction signTx(mtx); | |
be126699 | 53 | uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL, 0, consensusBranchId); |
0bb3d40f JG |
54 | |
55 | // Add the signature | |
56 | assert(crypto_sign_detached(&mtx.joinSplitSig[0], NULL, | |
a513ea90 JG |
57 | dataToBeSigned.begin(), 32, |
58 | joinSplitPrivKey | |
59 | ) == 0); | |
0bb3d40f JG |
60 | |
61 | CTransaction tx {mtx}; | |
62 | CWalletTx wtx {NULL, tx}; | |
63 | return wtx; | |
64 | } | |
65 | ||
66 | libzcash::Note GetNote(ZCJoinSplit& params, | |
67 | const libzcash::SpendingKey& sk, | |
68 | const CTransaction& tx, size_t js, size_t n) { | |
642a1caf | 69 | ZCNoteDecryption decryptor {sk.receiving_key()}; |
0bb3d40f JG |
70 | auto hSig = tx.vjoinsplit[js].h_sig(params, tx.joinSplitPubKey); |
71 | auto note_pt = libzcash::NotePlaintext::decrypt( | |
72 | decryptor, | |
73 | tx.vjoinsplit[js].ciphertexts[n], | |
74 | tx.vjoinsplit[js].ephemeralKey, | |
75 | hSig, | |
76 | (unsigned char) n); | |
77 | return note_pt.note(sk.address()); | |
78 | } | |
79 | ||
80 | CWalletTx GetValidSpend(ZCJoinSplit& params, | |
81 | const libzcash::SpendingKey& sk, | |
82 | const libzcash::Note& note, CAmount value) { | |
83 | CMutableTransaction mtx; | |
84 | mtx.vout.resize(2); | |
85 | mtx.vout[0].nValue = value; | |
86 | mtx.vout[1].nValue = 0; | |
87 | ||
88 | // Generate an ephemeral keypair. | |
89 | uint256 joinSplitPubKey; | |
90 | unsigned char joinSplitPrivKey[crypto_sign_SECRETKEYBYTES]; | |
91 | crypto_sign_keypair(joinSplitPubKey.begin(), joinSplitPrivKey); | |
92 | mtx.joinSplitPubKey = joinSplitPubKey; | |
93 | ||
94 | // Fake tree for the unused witness | |
95 | ZCIncrementalMerkleTree tree; | |
96 | ||
97 | libzcash::JSOutput dummyout; | |
98 | libzcash::JSInput dummyin; | |
99 | ||
100 | { | |
101 | if (note.value > value) { | |
102 | libzcash::SpendingKey dummykey = libzcash::SpendingKey::random(); | |
103 | libzcash::PaymentAddress dummyaddr = dummykey.address(); | |
104 | dummyout = libzcash::JSOutput(dummyaddr, note.value - value); | |
105 | } else if (note.value < value) { | |
106 | libzcash::SpendingKey dummykey = libzcash::SpendingKey::random(); | |
107 | libzcash::PaymentAddress dummyaddr = dummykey.address(); | |
108 | libzcash::Note dummynote(dummyaddr.a_pk, (value - note.value), uint256(), uint256()); | |
109 | tree.append(dummynote.cm()); | |
110 | dummyin = libzcash::JSInput(tree.witness(), dummynote, dummykey); | |
111 | } | |
112 | } | |
113 | ||
114 | tree.append(note.cm()); | |
115 | ||
116 | boost::array<libzcash::JSInput, 2> inputs = { | |
117 | libzcash::JSInput(tree.witness(), note, sk), | |
118 | dummyin | |
119 | }; | |
120 | ||
121 | boost::array<libzcash::JSOutput, 2> outputs = { | |
122 | dummyout, // dummy output | |
123 | libzcash::JSOutput() // dummy output | |
124 | }; | |
125 | ||
126 | boost::array<libzcash::Note, 2> output_notes; | |
127 | ||
128 | // Prepare JoinSplits | |
129 | uint256 rt = tree.root(); | |
130 | JSDescription jsdesc {params, mtx.joinSplitPubKey, rt, | |
131 | inputs, outputs, 0, value, false}; | |
132 | mtx.vjoinsplit.push_back(jsdesc); | |
133 | ||
134 | // Empty output script. | |
be126699 | 135 | uint32_t consensusBranchId = SPROUT_BRANCH_ID; |
0bb3d40f JG |
136 | CScript scriptCode; |
137 | CTransaction signTx(mtx); | |
be126699 | 138 | uint256 dataToBeSigned = SignatureHash(scriptCode, signTx, NOT_AN_INPUT, SIGHASH_ALL, 0, consensusBranchId); |
0bb3d40f JG |
139 | |
140 | // Add the signature | |
141 | assert(crypto_sign_detached(&mtx.joinSplitSig[0], NULL, | |
a513ea90 JG |
142 | dataToBeSigned.begin(), 32, |
143 | joinSplitPrivKey | |
144 | ) == 0); | |
0bb3d40f JG |
145 | CTransaction tx {mtx}; |
146 | CWalletTx wtx {NULL, tx}; | |
147 | return wtx; | |
148 | } |