Repository navigation
Expand file tree
/
Copy pathOutputInputIdentification.cpp
More file actions
executable file
·296 lines (233 loc) · 9.01 KB
/
Copy pathOutputInputIdentification.cpp
File metadata and controls
executable file
·296 lines (233 loc) · 9.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// Created by mwo on 13/02/17.
//
#include "OutputInputIdentification.h"
namespace xmreg
{
OutputInputIdentification::OutputInputIdentification(
const address_parse_info* _a,
const secret_key* _v,
const transaction* _tx,
crypto::hash const& _tx_hash,
bool is_coinbase,
std::shared_ptr<CurrentBlockchainStatus> _current_bc_status)
: current_bc_status {_current_bc_status}
{
matched_pub_key_index = -1;
address_info = _a;
viewkey = _v;
tx = _tx;
tx_pub_key_str = "";
tx_pub_keys = xmreg::get_tx_pub_keys_from_received_outs(*tx);
tx_is_coinbase = is_coinbase;
tx_hash = _tx_hash;
is_rct = (tx->version >= cryptonote::txversion::v2_ringct);
if (is_rct)
{
rct_type = tx->rct_signatures.type;
}
for (const auto& tx_pub_key : tx_pub_keys)
{
derivations.resize(derivations.size() + 1);
if (!generate_key_derivation(tx_pub_key, *viewkey, derivations.back().first))
{
OMERROR << "Cant get derived key for: " << "\n"
<< "pub_tx_key: " << pod_to_hex(tx_pub_key) << " and "
<< "prv_view_key" << viewkey;;
throw OutputInputIdentificationException(
"Cant get derived key for a tx");
}
derivations.back().second = tx_pub_key;
}
}
uint64_t
OutputInputIdentification::get_mixin_no()
{
if (mixin_no == 0 && !tx_is_coinbase)
mixin_no = xmreg::get_mixin_no(*tx);
return mixin_no;
}
void
OutputInputIdentification::identify_outputs()
{
// <public_key , amount , out idx, unlock time>
std::vector<outputs_tuple> outputs = get_outputs_tuple(*tx);
for (auto& out: outputs)
{
if (std::get<0>(out).type() != typeid(txout_to_key))
{
continue;
}
const txout_to_key& txout_k
= boost::get<cryptonote::txout_to_key>(std::get<0>(out));
uint64_t amount = std::get<1>(out);
uint64_t output_idx_in_tx = std::get<2>(out);
uint64_t unlock_time = std::get<3>(out);
// get the tx output public key
// that normally would be generated for us,
// if someone had sent us some xmr.
size_t matched_index = 0;
for (const auto& derivation : derivations)
{
matched_index++;
public_key derived_output_pubkey;
derive_public_key(derivation.first,
output_idx_in_tx,
address_info->address.m_spend_public_key,
derived_output_pubkey);
// check if generated public key matches the current output's key
if (txout_k.key != derived_output_pubkey) continue;
matched_pub_key_index = matched_index - 1;
// placeholder variable for ringct outputs info
// that we need to save in database
string rtc_outpk;
string rtc_mask;
string rtc_amount;
// initialize with regular amount value
// for ringct, except coinbase, it will be 0
uint64_t rct_amount_val = amount;
// cointbase txs have amounts in plain sight.
// so use amount from ringct, only for non-coinbase txs
if (!tx_is_coinbase)
{
bool r;
// for ringct non-coinbase txs, these values are given
// with txs.
// coinbase ringctx dont have this information. we will provide
// them only when needed, in get_unspent_outputs. So go there
// to see how we deal with ringct coinbase txs when we spent
// them
// go to CurrentBlockchainStatus::construct_output_rct_field
// to see how we deal with coinbase ringct that are used
// as mixins
rtc_outpk = pod_to_hex(tx->rct_signatures
.outPk[output_idx_in_tx].mask);
rtc_mask = pod_to_hex(tx->rct_signatures
.ecdhInfo[output_idx_in_tx].mask);
rtc_amount = pod_to_hex(tx->rct_signatures
.ecdhInfo[output_idx_in_tx].amount);
rct::key mask = tx->rct_signatures
.ecdhInfo[output_idx_in_tx].mask;
r = decode_ringct(tx->rct_signatures,
derivation.second,
*viewkey,
output_idx_in_tx,
mask,
rct_amount_val);
if (!r)
{
OMERROR << "Cant decode ringCT!";
throw OutputInputIdentificationException(
"Cant decode ringCT!");
}
amount = rct_amount_val;
} // if (!tx_is_coinbase)
total_received += amount;
identified_outputs.emplace_back(
output_info{
txout_k.key, derivation.second, amount, output_idx_in_tx,
rtc_outpk, rtc_mask, rtc_amount, unlock_time
});
}
} // for (const auto& out: outputs)
}
void
OutputInputIdentification::identify_inputs(
unordered_map<public_key, uint64_t> const& known_outputs_keys)
{
vector<txin_to_key> input_key_imgs = xmreg::get_key_images(*tx);
size_t search_misses {0};
// make timescale maps for mixins in input
for (txin_to_key const& in_key: input_key_imgs)
{
// get absolute offsets of mixins
std::vector<uint64_t> absolute_offsets
= cryptonote::relative_output_offsets_to_absolute(
in_key.key_offsets);
// get public keys of outputs used in the mixins that
// match to the offests
std::vector<cryptonote::output_data_t> mixin_outputs;
if (!current_bc_status->get_output_keys(in_key.amount,
absolute_offsets,
mixin_outputs))
{
OMERROR << "Mixins key images not found";
continue;
}
// indicates whether we found any matching mixin in the current input
bool found_a_match {false};
// for each found output public key check if its ours or not
for (size_t count = 0; count < absolute_offsets.size(); ++count)
{
// get basic information about mixn's output
cryptonote::output_data_t const& output_data
= mixin_outputs[count];
//cout << " - output_public_key_str: "
// << output_public_key_str;
// before going to the mysql, check our known outputs cash
// if the key exists. Its much faster than going to mysql
// for this.
auto it = known_outputs_keys.find(output_data.pubkey);
if (it != known_outputs_keys.end())
{
// this seems to be our mixin.
// save it into identified_inputs vector
identified_inputs.push_back(input_info {
pod_to_hex(in_key.k_image),
it->second, // amount
output_data.pubkey});
//cout << "\n\n" << it->second << endl;
found_a_match = true;
}
} // for (const cryptonote::output_data_t& output_data: outputs)
if (found_a_match == false)
{
// if we didnt find any match, break of the look.
// there is no reason to check remaining key images
// as when we spent something, our outputs should be
// in all inputs in a given txs. Thus, if a single input
// is without our output, we can assume this tx does
// not contain any of our spendings.
// just to be sure before we break out of this loop,
// do it only after two misses
if (++search_misses > 2)
break;
}
} // for (txin_to_key const& in_key: input_key_imgs)
}
string const&
OutputInputIdentification::get_tx_hash_str()
{
if (tx_hash_str.empty())
tx_hash_str = pod_to_hex(tx_hash);
return tx_hash_str;
}
string const&
OutputInputIdentification::get_tx_prefix_hash_str()
{
if (tx_prefix_hash_str.empty())
{
tx_prefix_hash = get_transaction_prefix_hash(*tx);
tx_prefix_hash_str = pod_to_hex(tx_prefix_hash);
}
return tx_prefix_hash_str;
}
// this function should no longer be required, but is kept to give the db
// one of the tx pub keys to associate with the transaction
string const&
OutputInputIdentification::get_tx_pub_key_str()
{
if (tx_pub_key_str == "")
{
if (tx_pub_keys.size() > 0)
{
tx_pub_key_str = pod_to_hex(tx_pub_keys[0]);
}
else
{
tx_pub_key_str = "0000000000000000000000000000000000000000000000000000000000000000";
}
}
return tx_pub_key_str;
}
}