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