Debian Codegraph @

src/serialization/json_object.cpp

raw symbols SCIP
1 // Copyright (c) 2016-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include "json_object.h"
30
31 #include <boost/range/adaptor/transformed.hpp>
32 #include <boost/variant/apply_visitor.hpp>
33 #include <limits>
34 #include <type_traits>
35
36 #include "cryptonote_basic/cryptonote_basic_impl.h"
37 #include "cryptonote_core/cryptonote_tx_utils.h"
38
39 // drop macro from windows.h
40 #ifdef GetObject
41 #undef GetObject
42 #endif
43
44 namespace cryptonote
45 {
46
47 namespace json
48 {
49
50 namespace
51 {
52 template<typename Source, typename Destination>
53 constexpr bool precision_loss()
54 {
55 return
56 std::numeric_limits<Destination>::is_signed != std::numeric_limits<Source>::is_signed ||
57 std::numeric_limits<Destination>::min() > std::numeric_limits<Source>::min() ||
58 std::numeric_limits<Destination>::max() < std::numeric_limits<Source>::max();
59 }
60
61 template<typename Source, typename Type>
62 void convert_numeric(Source source, Type& i)
63 {
64 static_assert(
65 (std::is_same<Type, char>() && std::is_same<Source, int>()) ||
66 std::numeric_limits<Source>::is_signed == std::numeric_limits<Type>::is_signed,
67 "comparisons below may have undefined behavior"
68 );
69 if (source < std::numeric_limits<Type>::min())
70 {
71 throw WRONG_TYPE{"numeric underflow"};
72 }
73 if (std::numeric_limits<Type>::max() < source)
74 {
75 throw WRONG_TYPE{"numeric overflow"};
76 }
77 i = Type(source);
78 }
79
80 template<typename Type>
81 void to_int(const rapidjson::Value& val, Type& i)
82 {
83 if (!val.IsInt())
84 {
85 throw WRONG_TYPE{"integer"};
86 }
87 convert_numeric(val.GetInt(), i);
88 }
89 template<typename Type>
90 void to_int64(const rapidjson::Value& val, Type& i)
91 {
92 if (!val.IsInt64())
93 {
94 throw WRONG_TYPE{"integer"};
95 }
96 convert_numeric(val.GetInt64(), i);
97 }
98
99 template<typename Type>
100 void to_uint(const rapidjson::Value& val, Type& i)
101 {
102 if (!val.IsUint())
103 {
104 throw WRONG_TYPE{"unsigned integer"};
105 }
106 convert_numeric(val.GetUint(), i);
107 }
108 template<typename Type>
109 void to_uint64(const rapidjson::Value& val, Type& i)
110 {
111 if (!val.IsUint64())
112 {
113 throw WRONG_TYPE{"unsigned integer"};
114 }
115 convert_numeric(val.GetUint64(), i);
116 }
117 }
118
119 void read_hex(const rapidjson::Value& val, epee::span<std::uint8_t> dest)
120 {
121 if (!val.IsString())
122 {
123 throw WRONG_TYPE("string");
124 }
125
126 if (!epee::from_hex::to_buffer(dest, {val.GetString(), val.GetStringLength()}))
127 {
128 throw BAD_INPUT();
129 }
130 }
131
132 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rapidjson::Value& src)
133 {
134 src.Accept(dest);
135 }
136
137 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const boost::string_ref i)
138 {
139 dest.String(i.data(), i.size());
140 }
141
142 void fromJsonValue(const rapidjson::Value& val, std::string& str)
143 {
144 if (!val.IsString())
145 {
146 throw WRONG_TYPE("string");
147 }
148
149 str = val.GetString();
150 }
151
152 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const std::vector<std::uint8_t>& src)
153 {
154 const std::string hex = epee::to_hex::string(epee::to_span(src));
155 dest.String(hex.data(), hex.size());
156 }
157
158 void fromJsonValue(const rapidjson::Value& val, std::vector<std::uint8_t>& dest)
159 {
160 if (!val.IsString())
161 {
162 throw WRONG_TYPE("string");
163 }
164
165 dest.resize(val.GetStringLength() / 2);
166 if ((val.GetStringLength() % 2) != 0 || !epee::from_hex::to_buffer(epee::to_mut_span(dest), {val.GetString(), val.GetStringLength()}))
167 {
168 throw BAD_INPUT();
169 }
170 }
171
172 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, bool i)
173 {
174 dest.Bool(i);
175 }
176
177 void fromJsonValue(const rapidjson::Value& val, bool& b)
178 {
179 if (!val.IsBool())
180 {
181 throw WRONG_TYPE("boolean");
182 }
183 b = val.GetBool();
184 }
185
186 void fromJsonValue(const rapidjson::Value& val, unsigned char& i)
187 {
188 to_uint(val, i);
189 }
190
191 void fromJsonValue(const rapidjson::Value& val, char& i)
192 {
193 to_int(val, i);
194 }
195
196 void fromJsonValue(const rapidjson::Value& val, signed char& i)
197 {
198 to_int(val, i);
199 }
200
201 void fromJsonValue(const rapidjson::Value& val, unsigned short& i)
202 {
203 to_uint(val, i);
204 }
205
206 void fromJsonValue(const rapidjson::Value& val, short& i)
207 {
208 to_int(val, i);
209 }
210
211 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned int i)
212 {
213 dest.Uint(i);
214 }
215
216 void fromJsonValue(const rapidjson::Value& val, unsigned int& i)
217 {
218 to_uint(val, i);
219 }
220
221 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const int i)
222 {
223 dest.Int(i);
224 }
225
226 void fromJsonValue(const rapidjson::Value& val, int& i)
227 {
228 to_int(val, i);
229 }
230
231 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const unsigned long long i)
232 {
233 static_assert(!precision_loss<unsigned long long, std::uint64_t>(), "bad uint64 conversion");
234 dest.Uint64(i);
235 }
236
237 void fromJsonValue(const rapidjson::Value& val, unsigned long long& i)
238 {
239 to_uint64(val, i);
240 }
241
242 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const long long i)
243 {
244 static_assert(!precision_loss<long long, std::int64_t>(), "bad int64 conversion");
245 dest.Int64(i);
246 }
247
248 void fromJsonValue(const rapidjson::Value& val, long long& i)
249 {
250 to_int64(val, i);
251 }
252
253 void fromJsonValue(const rapidjson::Value& val, unsigned long& i)
254 {
255 to_uint64(val, i);
256 }
257
258 void fromJsonValue(const rapidjson::Value& val, long& i)
259 {
260 to_int64(val, i);
261 }
262
263 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::transaction& tx)
264 {
265 dest.StartObject();
266
267 INSERT_INTO_JSON_OBJECT(dest, version, tx.version);
268 INSERT_INTO_JSON_OBJECT(dest, unlock_time, tx.unlock_time);
269 INSERT_INTO_JSON_OBJECT(dest, inputs, tx.vin);
270 INSERT_INTO_JSON_OBJECT(dest, outputs, tx.vout);
271 INSERT_INTO_JSON_OBJECT(dest, extra, tx.extra);
272 if (!tx.pruned)
273 {
274 INSERT_INTO_JSON_OBJECT(dest, signatures, tx.signatures);
275 }
276 {
277 dest.Key("ringct");
278 toJsonValue(dest, tx.rct_signatures, tx.pruned);
279 }
280
281 dest.EndObject();
282 }
283
284
285 void fromJsonValue(const rapidjson::Value& val, cryptonote::transaction& tx)
286 {
287 if (!val.IsObject())
288 {
289 throw WRONG_TYPE("json object");
290 }
291
292 GET_FROM_JSON_OBJECT(val, tx.version, version);
293 GET_FROM_JSON_OBJECT(val, tx.unlock_time, unlock_time);
294 GET_FROM_JSON_OBJECT(val, tx.vin, inputs);
295 GET_FROM_JSON_OBJECT(val, tx.vout, outputs);
296 GET_FROM_JSON_OBJECT(val, tx.extra, extra);
297 GET_FROM_JSON_OBJECT(val, tx.rct_signatures, ringct);
298
299 const auto& sigs = val.FindMember("signatures");
300 if (sigs != val.MemberEnd())
301 {
302 fromJsonValue(sigs->value, tx.signatures);
303 }
304
305 const auto& rsig = tx.rct_signatures;
306 if (!cryptonote::is_coinbase(tx) && rsig.p.bulletproofs.empty() && rsig.p.bulletproofs_plus.empty() && rsig.p.rangeSigs.empty() && rsig.p.MGs.empty() && rsig.get_pseudo_outs().empty() && sigs == val.MemberEnd())
307 tx.pruned = true;
308 }
309
310 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::block& b)
311 {
312 dest.StartObject();
313
314 INSERT_INTO_JSON_OBJECT(dest, major_version, b.major_version);
315 INSERT_INTO_JSON_OBJECT(dest, minor_version, b.minor_version);
316 INSERT_INTO_JSON_OBJECT(dest, timestamp, b.timestamp);
317 INSERT_INTO_JSON_OBJECT(dest, prev_id, b.prev_id);
318 INSERT_INTO_JSON_OBJECT(dest, nonce, b.nonce);
319 INSERT_INTO_JSON_OBJECT(dest, miner_tx, b.miner_tx);
320 INSERT_INTO_JSON_OBJECT(dest, tx_hashes, b.tx_hashes);
321
322 dest.EndObject();
323 }
324
325
326 void fromJsonValue(const rapidjson::Value& val, cryptonote::block& b)
327 {
328 if (!val.IsObject())
329 {
330 throw WRONG_TYPE("json object");
331 }
332
333 GET_FROM_JSON_OBJECT(val, b.major_version, major_version);
334 GET_FROM_JSON_OBJECT(val, b.minor_version, minor_version);
335 GET_FROM_JSON_OBJECT(val, b.timestamp, timestamp);
336 GET_FROM_JSON_OBJECT(val, b.prev_id, prev_id);
337 GET_FROM_JSON_OBJECT(val, b.nonce, nonce);
338 GET_FROM_JSON_OBJECT(val, b.miner_tx, miner_tx);
339 GET_FROM_JSON_OBJECT(val, b.tx_hashes, tx_hashes);
340 }
341
342 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_v& txin)
343 {
344 dest.StartObject();
345 struct add_input
346 {
347 using result_type = void;
348
349 rapidjson::Writer<epee::byte_stream>& dest;
350
351 void operator()(cryptonote::txin_to_key const& input) const
352 {
353 INSERT_INTO_JSON_OBJECT(dest, to_key, input);
354 }
355 void operator()(cryptonote::txin_gen const& input) const
356 {
357 INSERT_INTO_JSON_OBJECT(dest, gen, input);
358 }
359 void operator()(cryptonote::txin_to_script const& input) const
360 {
361 INSERT_INTO_JSON_OBJECT(dest, to_script, input);
362 }
363 void operator()(cryptonote::txin_to_scripthash const& input) const
364 {
365 INSERT_INTO_JSON_OBJECT(dest, to_scripthash, input);
366 }
367 };
368 boost::apply_visitor(add_input{dest}, txin);
369 dest.EndObject();
370 }
371
372
373 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_v& txin)
374 {
375 if (!val.IsObject())
376 {
377 throw WRONG_TYPE("json object");
378 }
379
380 if (val.MemberCount() != 1)
381 {
382 throw MISSING_KEY("Invalid input object");
383 }
384
385 for (auto const& elem : val.GetObject())
386 {
387 if (elem.name == "to_key")
388 {
389 cryptonote::txin_to_key tmpVal;
390 fromJsonValue(elem.value, tmpVal);
391 txin = std::move(tmpVal);
392 }
393 else if (elem.name == "gen")
394 {
395 cryptonote::txin_gen tmpVal;
396 fromJsonValue(elem.value, tmpVal);
397 txin = std::move(tmpVal);
398 }
399 else if (elem.name == "to_script")
400 {
401 cryptonote::txin_to_script tmpVal;
402 fromJsonValue(elem.value, tmpVal);
403 txin = std::move(tmpVal);
404 }
405 else if (elem.name == "to_scripthash")
406 {
407 cryptonote::txin_to_scripthash tmpVal;
408 fromJsonValue(elem.value, tmpVal);
409 txin = std::move(tmpVal);
410 }
411 }
412 }
413
414 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_gen& txin)
415 {
416 dest.StartObject();
417
418 INSERT_INTO_JSON_OBJECT(dest, height, txin.height);
419
420 dest.EndObject();
421 }
422
423 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_gen& txin)
424 {
425 if (!val.IsObject())
426 {
427 throw WRONG_TYPE("json object");
428 }
429
430 GET_FROM_JSON_OBJECT(val, txin.height, height);
431 }
432
433 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_script& txin)
434 {
435 dest.StartObject();
436
437 INSERT_INTO_JSON_OBJECT(dest, prev, txin.prev);
438 INSERT_INTO_JSON_OBJECT(dest, prevout, txin.prevout);
439 INSERT_INTO_JSON_OBJECT(dest, sigset, txin.sigset);
440
441 dest.EndObject();
442 }
443
444
445 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_script& txin)
446 {
447 if (!val.IsObject())
448 {
449 throw WRONG_TYPE("json object");
450 }
451
452 GET_FROM_JSON_OBJECT(val, txin.prev, prev);
453 GET_FROM_JSON_OBJECT(val, txin.prevout, prevout);
454 GET_FROM_JSON_OBJECT(val, txin.sigset, sigset);
455 }
456
457
458 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_scripthash& txin)
459 {
460 dest.StartObject();
461
462 INSERT_INTO_JSON_OBJECT(dest, prev, txin.prev);
463 INSERT_INTO_JSON_OBJECT(dest, prevout, txin.prevout);
464 INSERT_INTO_JSON_OBJECT(dest, script, txin.script);
465 INSERT_INTO_JSON_OBJECT(dest, sigset, txin.sigset);
466
467 dest.EndObject();
468 }
469
470
471 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_scripthash& txin)
472 {
473 if (!val.IsObject())
474 {
475 throw WRONG_TYPE("json object");
476 }
477
478 GET_FROM_JSON_OBJECT(val, txin.prev, prev);
479 GET_FROM_JSON_OBJECT(val, txin.prevout, prevout);
480 GET_FROM_JSON_OBJECT(val, txin.script, script);
481 GET_FROM_JSON_OBJECT(val, txin.sigset, sigset);
482 }
483
484 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txin_to_key& txin)
485 {
486 dest.StartObject();
487
488 INSERT_INTO_JSON_OBJECT(dest, amount, txin.amount);
489 INSERT_INTO_JSON_OBJECT(dest, key_offsets, txin.key_offsets);
490 INSERT_INTO_JSON_OBJECT(dest, key_image, txin.k_image);
491
492 dest.EndObject();
493 }
494
495 void fromJsonValue(const rapidjson::Value& val, cryptonote::txin_to_key& txin)
496 {
497 if (!val.IsObject())
498 {
499 throw WRONG_TYPE("json object");
500 }
501
502 GET_FROM_JSON_OBJECT(val, txin.amount, amount);
503 GET_FROM_JSON_OBJECT(val, txin.key_offsets, key_offsets);
504 GET_FROM_JSON_OBJECT(val, txin.k_image, key_image);
505 }
506
507
508 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_script& txout)
509 {
510 dest.StartObject();
511
512 INSERT_INTO_JSON_OBJECT(dest, keys, txout.keys);
513 INSERT_INTO_JSON_OBJECT(dest, script, txout.script);
514
515 dest.EndObject();
516 }
517
518 void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_script& txout)
519 {
520 if (!val.IsObject())
521 {
522 throw WRONG_TYPE("json object");
523 }
524
525 GET_FROM_JSON_OBJECT(val, txout.keys, keys);
526 GET_FROM_JSON_OBJECT(val, txout.script, script);
527 }
528
529
530 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_scripthash& txout)
531 {
532 dest.StartObject();
533
534 INSERT_INTO_JSON_OBJECT(dest, hash, txout.hash);
535
536 dest.EndObject();
537 }
538
539 void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_scripthash& txout)
540 {
541 if (!val.IsObject())
542 {
543 throw WRONG_TYPE("json object");
544 }
545
546 GET_FROM_JSON_OBJECT(val, txout.hash, hash);
547 }
548
549
550 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_key& txout)
551 {
552 dest.StartObject();
553
554 INSERT_INTO_JSON_OBJECT(dest, key, txout.key);
555
556 dest.EndObject();
557 }
558
559 void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_key& txout)
560 {
561 if (!val.IsObject())
562 {
563 throw WRONG_TYPE("json object");
564 }
565
566 GET_FROM_JSON_OBJECT(val, txout.key, key);
567 }
568
569 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::txout_to_tagged_key& txout)
570 {
571 dest.StartObject();
572
573 INSERT_INTO_JSON_OBJECT(dest, key, txout.key);
574 INSERT_INTO_JSON_OBJECT(dest, view_tag, txout.view_tag);
575
576 dest.EndObject();
577 }
578
579 void fromJsonValue(const rapidjson::Value& val, cryptonote::txout_to_tagged_key& txout)
580 {
581 if (!val.IsObject())
582 {
583 throw WRONG_TYPE("json object");
584 }
585
586 GET_FROM_JSON_OBJECT(val, txout.key, key);
587 GET_FROM_JSON_OBJECT(val, txout.view_tag, view_tag);
588 }
589
590 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_out& txout)
591 {
592 dest.StartObject();
593 INSERT_INTO_JSON_OBJECT(dest, amount, txout.amount);
594
595 struct add_output
596 {
597 using result_type = void;
598
599 rapidjson::Writer<epee::byte_stream>& dest;
600
601 void operator()(cryptonote::txout_to_key const& output) const
602 {
603 INSERT_INTO_JSON_OBJECT(dest, to_key, output);
604 }
605 void operator()(cryptonote::txout_to_tagged_key const& output) const
606 {
607 INSERT_INTO_JSON_OBJECT(dest, to_tagged_key, output);
608 }
609 void operator()(cryptonote::txout_to_script const& output) const
610 {
611 INSERT_INTO_JSON_OBJECT(dest, to_script, output);
612 }
613 void operator()(cryptonote::txout_to_scripthash const& output) const
614 {
615 INSERT_INTO_JSON_OBJECT(dest, to_scripthash, output);
616 }
617 };
618 boost::apply_visitor(add_output{dest}, txout.target);
619 dest.EndObject();
620 }
621
622 void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_out& txout)
623 {
624 if (!val.IsObject())
625 {
626 throw WRONG_TYPE("json object");
627 }
628
629 if (val.MemberCount() != 2)
630 {
631 throw MISSING_KEY("Invalid input object");
632 }
633
634 for (auto const& elem : val.GetObject())
635 {
636 if (elem.name == "amount")
637 {
638 fromJsonValue(elem.value, txout.amount);
639 }
640
641 if (elem.name == "to_key")
642 {
643 cryptonote::txout_to_key tmpVal;
644 fromJsonValue(elem.value, tmpVal);
645 txout.target = std::move(tmpVal);
646 }
647 else if (elem.name == "to_tagged_key")
648 {
649 cryptonote::txout_to_tagged_key tmpVal;
650 fromJsonValue(elem.value, tmpVal);
651 txout.target = std::move(tmpVal);
652 }
653 else if (elem.name == "to_script")
654 {
655 cryptonote::txout_to_script tmpVal;
656 fromJsonValue(elem.value, tmpVal);
657 txout.target = std::move(tmpVal);
658 }
659 else if (elem.name == "to_scripthash")
660 {
661 cryptonote::txout_to_scripthash tmpVal;
662 fromJsonValue(elem.value, tmpVal);
663 txout.target = std::move(tmpVal);
664 }
665 }
666 }
667
668 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::connection_info& info)
669 {
670 dest.StartObject();
671
672 INSERT_INTO_JSON_OBJECT(dest, incoming, info.incoming);
673 INSERT_INTO_JSON_OBJECT(dest, localhost, info.localhost);
674 INSERT_INTO_JSON_OBJECT(dest, local_ip, info.local_ip);
675 INSERT_INTO_JSON_OBJECT(dest, address_type, info.address_type);
676
677 INSERT_INTO_JSON_OBJECT(dest, ip, info.ip);
678 INSERT_INTO_JSON_OBJECT(dest, port, info.port);
679 INSERT_INTO_JSON_OBJECT(dest, rpc_port, info.rpc_port);
680 INSERT_INTO_JSON_OBJECT(dest, rpc_credits_per_hash, info.rpc_credits_per_hash);
681
682 INSERT_INTO_JSON_OBJECT(dest, peer_id, info.peer_id);
683
684 INSERT_INTO_JSON_OBJECT(dest, recv_count, info.recv_count);
685 INSERT_INTO_JSON_OBJECT(dest, recv_idle_time, info.recv_idle_time);
686
687 INSERT_INTO_JSON_OBJECT(dest, send_count, info.send_count);
688 INSERT_INTO_JSON_OBJECT(dest, send_idle_time, info.send_idle_time);
689
690 INSERT_INTO_JSON_OBJECT(dest, state, info.state);
691
692 INSERT_INTO_JSON_OBJECT(dest, live_time, info.live_time);
693
694 INSERT_INTO_JSON_OBJECT(dest, avg_download, info.avg_download);
695 INSERT_INTO_JSON_OBJECT(dest, current_download, info.current_download);
696
697 INSERT_INTO_JSON_OBJECT(dest, avg_upload, info.avg_upload);
698 INSERT_INTO_JSON_OBJECT(dest, current_upload, info.current_upload);
699
700 dest.EndObject();
701 }
702
703
704 void fromJsonValue(const rapidjson::Value& val, cryptonote::connection_info& info)
705 {
706 if (!val.IsObject())
707 {
708 throw WRONG_TYPE("json object");
709 }
710
711 GET_FROM_JSON_OBJECT(val, info.incoming, incoming);
712 GET_FROM_JSON_OBJECT(val, info.localhost, localhost);
713 GET_FROM_JSON_OBJECT(val, info.local_ip, local_ip);
714 GET_FROM_JSON_OBJECT(val, info.address_type, address_type);
715
716 GET_FROM_JSON_OBJECT(val, info.ip, ip);
717 GET_FROM_JSON_OBJECT(val, info.port, port);
718 GET_FROM_JSON_OBJECT(val, info.rpc_port, rpc_port);
719 GET_FROM_JSON_OBJECT(val, info.rpc_credits_per_hash, rpc_credits_per_hash);
720
721 GET_FROM_JSON_OBJECT(val, info.peer_id, peer_id);
722
723 GET_FROM_JSON_OBJECT(val, info.recv_count, recv_count);
724 GET_FROM_JSON_OBJECT(val, info.recv_idle_time, recv_idle_time);
725
726 GET_FROM_JSON_OBJECT(val, info.send_count, send_count);
727 GET_FROM_JSON_OBJECT(val, info.send_idle_time, send_idle_time);
728
729 GET_FROM_JSON_OBJECT(val, info.state, state);
730
731 GET_FROM_JSON_OBJECT(val, info.live_time, live_time);
732
733 GET_FROM_JSON_OBJECT(val, info.avg_download, avg_download);
734 GET_FROM_JSON_OBJECT(val, info.current_download, current_download);
735
736 GET_FROM_JSON_OBJECT(val, info.avg_upload, avg_upload);
737 GET_FROM_JSON_OBJECT(val, info.current_upload, current_upload);
738 }
739
740 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_blob_entry& tx)
741 {
742 dest.StartObject();
743
744 INSERT_INTO_JSON_OBJECT(dest, blob, tx.blob);
745 INSERT_INTO_JSON_OBJECT(dest, prunable_hash, tx.prunable_hash);
746
747 dest.EndObject();
748 }
749
750 void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_blob_entry& tx)
751 {
752 if (!val.IsObject())
753 {
754 throw WRONG_TYPE("json object");
755 }
756
757 GET_FROM_JSON_OBJECT(val, tx.blob, blob);
758 GET_FROM_JSON_OBJECT(val, tx.prunable_hash, prunable_hash);
759 }
760
761 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::block_complete_entry& blk)
762 {
763 dest.StartObject();
764
765 INSERT_INTO_JSON_OBJECT(dest, block, blk.block);
766 INSERT_INTO_JSON_OBJECT(dest, transactions, blk.txs);
767
768 dest.EndObject();
769 }
770
771
772 void fromJsonValue(const rapidjson::Value& val, cryptonote::block_complete_entry& blk)
773 {
774 if (!val.IsObject())
775 {
776 throw WRONG_TYPE("json object");
777 }
778
779 GET_FROM_JSON_OBJECT(val, blk.block, block);
780 GET_FROM_JSON_OBJECT(val, blk.txs, transactions);
781 }
782
783 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::block_with_transactions& blk)
784 {
785 dest.StartObject();
786
787 INSERT_INTO_JSON_OBJECT(dest, block, blk.block);
788 INSERT_INTO_JSON_OBJECT(dest, transactions, blk.transactions);
789
790 dest.EndObject();
791 }
792
793
794 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::block_with_transactions& blk)
795 {
796 if (!val.IsObject())
797 {
798 throw WRONG_TYPE("json object");
799 }
800
801 GET_FROM_JSON_OBJECT(val, blk.block, block);
802 GET_FROM_JSON_OBJECT(val, blk.transactions, transactions);
803 }
804
805 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::transaction_info& tx_info)
806 {
807 dest.StartObject();
808
809 INSERT_INTO_JSON_OBJECT(dest, height, tx_info.height);
810 INSERT_INTO_JSON_OBJECT(dest, in_pool, tx_info.in_pool);
811 INSERT_INTO_JSON_OBJECT(dest, transaction, tx_info.transaction);
812
813 dest.EndObject();
814 }
815
816
817 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::transaction_info& tx_info)
818 {
819 if (!val.IsObject())
820 {
821 throw WRONG_TYPE("json object");
822 }
823
824 GET_FROM_JSON_OBJECT(val, tx_info.height, height);
825 GET_FROM_JSON_OBJECT(val, tx_info.in_pool, in_pool);
826 GET_FROM_JSON_OBJECT(val, tx_info.transaction, transaction);
827 }
828
829 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_key_and_amount_index& out)
830 {
831 dest.StartObject();
832
833 INSERT_INTO_JSON_OBJECT(dest, amount_index, out.amount_index);
834 INSERT_INTO_JSON_OBJECT(dest, key, out.key);
835
836 dest.EndObject();
837 }
838
839
840 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_and_amount_index& out)
841 {
842 if (!val.IsObject())
843 {
844 throw WRONG_TYPE("json object");
845 }
846
847 GET_FROM_JSON_OBJECT(val, out.amount_index, amount_index);
848 GET_FROM_JSON_OBJECT(val, out.key, key);
849 }
850
851 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::amount_with_random_outputs& out)
852 {
853 dest.StartObject();
854
855 INSERT_INTO_JSON_OBJECT(dest, amount, out.amount);
856 INSERT_INTO_JSON_OBJECT(dest, outputs, out.outputs);
857
858 dest.EndObject();
859 }
860
861
862 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::amount_with_random_outputs& out)
863 {
864 if (!val.IsObject())
865 {
866 throw WRONG_TYPE("json object");
867 }
868
869 GET_FROM_JSON_OBJECT(val, out.amount, amount);
870 GET_FROM_JSON_OBJECT(val, out.outputs, outputs);
871 }
872
873 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::peer& peer)
874 {
875 dest.StartObject();
876
877 INSERT_INTO_JSON_OBJECT(dest, id, peer.id);
878 INSERT_INTO_JSON_OBJECT(dest, ip, peer.ip);
879 INSERT_INTO_JSON_OBJECT(dest, port, peer.port);
880 INSERT_INTO_JSON_OBJECT(dest, rpc_port, peer.rpc_port);
881 INSERT_INTO_JSON_OBJECT(dest, rpc_credits_per_hash, peer.rpc_credits_per_hash);
882 INSERT_INTO_JSON_OBJECT(dest, last_seen, peer.last_seen);
883 INSERT_INTO_JSON_OBJECT(dest, pruning_seed, peer.pruning_seed);
884
885 dest.EndObject();
886 }
887
888
889 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::peer& peer)
890 {
891 if (!val.IsObject())
892 {
893 throw WRONG_TYPE("json object");
894 }
895
896 GET_FROM_JSON_OBJECT(val, peer.id, id);
897 GET_FROM_JSON_OBJECT(val, peer.ip, ip);
898 GET_FROM_JSON_OBJECT(val, peer.port, port);
899 GET_FROM_JSON_OBJECT(val, peer.rpc_port, rpc_port);
900 GET_FROM_JSON_OBJECT(val, peer.rpc_credits_per_hash, rpc_credits_per_hash);
901 GET_FROM_JSON_OBJECT(val, peer.last_seen, last_seen);
902 GET_FROM_JSON_OBJECT(val, peer.pruning_seed, pruning_seed);
903 }
904
905 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::tx_in_pool& tx)
906 {
907 dest.StartObject();
908
909 INSERT_INTO_JSON_OBJECT(dest, tx, tx.tx);
910 INSERT_INTO_JSON_OBJECT(dest, tx_hash, tx.tx_hash);
911 INSERT_INTO_JSON_OBJECT(dest, blob_size, tx.blob_size);
912 INSERT_INTO_JSON_OBJECT(dest, weight, tx.weight);
913 INSERT_INTO_JSON_OBJECT(dest, fee, tx.fee);
914 INSERT_INTO_JSON_OBJECT(dest, max_used_block_hash, tx.max_used_block_hash);
915 INSERT_INTO_JSON_OBJECT(dest, max_used_block_height, tx.max_used_block_height);
916 INSERT_INTO_JSON_OBJECT(dest, kept_by_block, tx.kept_by_block);
917 INSERT_INTO_JSON_OBJECT(dest, last_failed_block_hash, tx.last_failed_block_hash);
918 INSERT_INTO_JSON_OBJECT(dest, last_failed_block_height, tx.last_failed_block_height);
919 INSERT_INTO_JSON_OBJECT(dest, receive_time, tx.receive_time);
920 INSERT_INTO_JSON_OBJECT(dest, last_relayed_time, tx.last_relayed_time);
921 INSERT_INTO_JSON_OBJECT(dest, relayed, tx.relayed);
922 INSERT_INTO_JSON_OBJECT(dest, do_not_relay, tx.do_not_relay);
923 INSERT_INTO_JSON_OBJECT(dest, double_spend_seen, tx.double_spend_seen);
924
925 dest.EndObject();
926 }
927
928
929 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::tx_in_pool& tx)
930 {
931 if (!val.IsObject())
932 {
933 throw WRONG_TYPE("json object");
934 }
935
936 GET_FROM_JSON_OBJECT(val, tx.tx, tx);
937 GET_FROM_JSON_OBJECT(val, tx.blob_size, blob_size);
938 GET_FROM_JSON_OBJECT(val, tx.weight, weight);
939 GET_FROM_JSON_OBJECT(val, tx.fee, fee);
940 GET_FROM_JSON_OBJECT(val, tx.max_used_block_hash, max_used_block_hash);
941 GET_FROM_JSON_OBJECT(val, tx.max_used_block_height, max_used_block_height);
942 GET_FROM_JSON_OBJECT(val, tx.kept_by_block, kept_by_block);
943 GET_FROM_JSON_OBJECT(val, tx.last_failed_block_hash, last_failed_block_hash);
944 GET_FROM_JSON_OBJECT(val, tx.last_failed_block_height, last_failed_block_height);
945 GET_FROM_JSON_OBJECT(val, tx.receive_time, receive_time);
946 GET_FROM_JSON_OBJECT(val, tx.last_relayed_time, last_relayed_time);
947 GET_FROM_JSON_OBJECT(val, tx.relayed, relayed);
948 GET_FROM_JSON_OBJECT(val, tx.do_not_relay, do_not_relay);
949 GET_FROM_JSON_OBJECT(val, tx.double_spend_seen, double_spend_seen);
950 }
951
952 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::hard_fork_info& info)
953 {
954 dest.StartObject();
955
956 INSERT_INTO_JSON_OBJECT(dest, version, info.version);
957 INSERT_INTO_JSON_OBJECT(dest, enabled, info.enabled);
958 INSERT_INTO_JSON_OBJECT(dest, window, info.window);
959 INSERT_INTO_JSON_OBJECT(dest, votes, info.votes);
960 INSERT_INTO_JSON_OBJECT(dest, threshold, info.threshold);
961 INSERT_INTO_JSON_OBJECT(dest, voting, info.voting);
962 INSERT_INTO_JSON_OBJECT(dest, state, info.state);
963 INSERT_INTO_JSON_OBJECT(dest, earliest_height, info.earliest_height);
964
965 dest.EndObject();
966 }
967
968
969 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::hard_fork_info& info)
970 {
971 if (!val.IsObject())
972 {
973 throw WRONG_TYPE("json object");
974 }
975
976 GET_FROM_JSON_OBJECT(val, info.version, version);
977 GET_FROM_JSON_OBJECT(val, info.enabled, enabled);
978 GET_FROM_JSON_OBJECT(val, info.window, window);
979 GET_FROM_JSON_OBJECT(val, info.votes, votes);
980 GET_FROM_JSON_OBJECT(val, info.threshold, threshold);
981 GET_FROM_JSON_OBJECT(val, info.voting, voting);
982 GET_FROM_JSON_OBJECT(val, info.state, state);
983 GET_FROM_JSON_OBJECT(val, info.earliest_height, earliest_height);
984 }
985
986 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_amount_count& out)
987 {
988 dest.StartObject();
989
990 INSERT_INTO_JSON_OBJECT(dest, amount, out.amount);
991 INSERT_INTO_JSON_OBJECT(dest, total_count, out.total_count);
992 INSERT_INTO_JSON_OBJECT(dest, unlocked_count, out.unlocked_count);
993 INSERT_INTO_JSON_OBJECT(dest, recent_count, out.recent_count);
994
995 dest.EndObject();
996 }
997
998
999 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_count& out)
1000 {
1001 if (!val.IsObject())
1002 {
1003 throw WRONG_TYPE("json object");
1004 }
1005
1006 GET_FROM_JSON_OBJECT(val, out.amount, amount);
1007 GET_FROM_JSON_OBJECT(val, out.total_count, total_count);
1008 GET_FROM_JSON_OBJECT(val, out.unlocked_count, unlocked_count);
1009 GET_FROM_JSON_OBJECT(val, out.recent_count, recent_count);
1010 }
1011
1012 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_amount_and_index& out)
1013 {
1014 dest.StartObject();
1015
1016 INSERT_INTO_JSON_OBJECT(dest, amount, out.amount);
1017 INSERT_INTO_JSON_OBJECT(dest, index, out.index);
1018
1019 dest.EndObject();
1020 }
1021
1022
1023 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_amount_and_index& out)
1024 {
1025 if (!val.IsObject())
1026 {
1027 throw WRONG_TYPE("json object");
1028 }
1029
1030 GET_FROM_JSON_OBJECT(val, out.amount, amount);
1031 GET_FROM_JSON_OBJECT(val, out.index, index);
1032 }
1033
1034 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_key_mask_unlocked& out)
1035 {
1036 dest.StartObject();
1037
1038 INSERT_INTO_JSON_OBJECT(dest, key, out.key);
1039 INSERT_INTO_JSON_OBJECT(dest, mask, out.mask);
1040 INSERT_INTO_JSON_OBJECT(dest, unlocked, out.unlocked);
1041
1042 dest.EndObject();
1043 }
1044
1045 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_key_mask_unlocked& out)
1046 {
1047 if (!val.IsObject())
1048 {
1049 throw WRONG_TYPE("json object");
1050 }
1051
1052 GET_FROM_JSON_OBJECT(val, out.key, key);
1053 GET_FROM_JSON_OBJECT(val, out.mask, mask);
1054 GET_FROM_JSON_OBJECT(val, out.unlocked, unlocked);
1055 }
1056
1057 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::error& err)
1058 {
1059 dest.StartObject();
1060
1061 INSERT_INTO_JSON_OBJECT(dest, code, err.code);
1062 INSERT_INTO_JSON_OBJECT(dest, error_str, err.error_str);
1063 INSERT_INTO_JSON_OBJECT(dest, message, err.message);
1064
1065 dest.EndObject();
1066 }
1067
1068 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::error& error)
1069 {
1070 if (!val.IsObject())
1071 {
1072 throw WRONG_TYPE("json object");
1073 }
1074
1075 GET_FROM_JSON_OBJECT(val, error.code, code);
1076 GET_FROM_JSON_OBJECT(val, error.error_str, error_str);
1077 GET_FROM_JSON_OBJECT(val, error.message, message);
1078 }
1079
1080 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::BlockHeaderResponse& response)
1081 {
1082 dest.StartObject();
1083
1084 INSERT_INTO_JSON_OBJECT(dest, major_version, response.major_version);
1085 INSERT_INTO_JSON_OBJECT(dest, minor_version, response.minor_version);
1086 INSERT_INTO_JSON_OBJECT(dest, timestamp, response.timestamp);
1087 INSERT_INTO_JSON_OBJECT(dest, prev_id, response.prev_id);
1088 INSERT_INTO_JSON_OBJECT(dest, nonce, response.nonce);
1089 INSERT_INTO_JSON_OBJECT(dest, height, response.height);
1090 INSERT_INTO_JSON_OBJECT(dest, depth, response.depth);
1091 INSERT_INTO_JSON_OBJECT(dest, hash, response.hash);
1092 INSERT_INTO_JSON_OBJECT(dest, difficulty, response.difficulty);
1093 INSERT_INTO_JSON_OBJECT(dest, reward, response.reward);
1094
1095 dest.EndObject();
1096 }
1097
1098 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::BlockHeaderResponse& response)
1099 {
1100 if (!val.IsObject())
1101 {
1102 throw WRONG_TYPE("json object");
1103 }
1104
1105 GET_FROM_JSON_OBJECT(val, response.major_version, major_version);
1106 GET_FROM_JSON_OBJECT(val, response.minor_version, minor_version);
1107 GET_FROM_JSON_OBJECT(val, response.timestamp, timestamp);
1108 GET_FROM_JSON_OBJECT(val, response.prev_id, prev_id);
1109 GET_FROM_JSON_OBJECT(val, response.nonce, nonce);
1110 GET_FROM_JSON_OBJECT(val, response.height, height);
1111 GET_FROM_JSON_OBJECT(val, response.depth, depth);
1112 GET_FROM_JSON_OBJECT(val, response.hash, hash);
1113 GET_FROM_JSON_OBJECT(val, response.difficulty, difficulty);
1114 GET_FROM_JSON_OBJECT(val, response.reward, reward);
1115 }
1116
1117 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::rctSig& sig, const bool prune)
1118 {
1119 using boost::adaptors::transform;
1120
1121 dest.StartObject();
1122
1123 const auto just_mask = [] (rct::ctkey const& key) -> rct::key const&
1124 {
1125 return key.mask;
1126 };
1127
1128 INSERT_INTO_JSON_OBJECT(dest, type, sig.type);
1129 if (sig.type != rct::RCTTypeNull)
1130 {
1131 INSERT_INTO_JSON_OBJECT(dest, encrypted, sig.ecdhInfo);
1132 INSERT_INTO_JSON_OBJECT(dest, commitments, transform(sig.outPk, just_mask));
1133 INSERT_INTO_JSON_OBJECT(dest, fee, sig.txnFee);
1134 }
1135
1136 // prunable
1137 if (!prune && (!sig.p.bulletproofs.empty() || !sig.p.bulletproofs_plus.empty() || !sig.p.rangeSigs.empty() || !sig.p.MGs.empty() || !sig.get_pseudo_outs().empty()))
1138 {
1139 dest.Key("prunable");
1140 dest.StartObject();
1141
1142 INSERT_INTO_JSON_OBJECT(dest, range_proofs, sig.p.rangeSigs);
1143 INSERT_INTO_JSON_OBJECT(dest, bulletproofs, sig.p.bulletproofs);
1144 INSERT_INTO_JSON_OBJECT(dest, bulletproofs_plus, sig.p.bulletproofs_plus);
1145 INSERT_INTO_JSON_OBJECT(dest, mlsags, sig.p.MGs);
1146 INSERT_INTO_JSON_OBJECT(dest, clsags, sig.p.CLSAGs);
1147 INSERT_INTO_JSON_OBJECT(dest, pseudo_outs, sig.get_pseudo_outs());
1148
1149 dest.EndObject();
1150 }
1151
1152 dest.EndObject();
1153 }
1154
1155 void fromJsonValue(const rapidjson::Value& val, rct::rctSig& sig)
1156 {
1157 using boost::adaptors::transform;
1158
1159 if (!val.IsObject())
1160 {
1161 throw WRONG_TYPE("json object");
1162 }
1163
1164 GET_FROM_JSON_OBJECT(val, sig.type, type);
1165 if (sig.type != rct::RCTTypeNull)
1166 {
1167 GET_FROM_JSON_OBJECT(val, sig.ecdhInfo, encrypted);
1168 GET_FROM_JSON_OBJECT(val, sig.outPk, commitments);
1169 GET_FROM_JSON_OBJECT(val, sig.txnFee, fee);
1170 }
1171
1172 // prunable
1173 const auto prunable = val.FindMember("prunable");
1174 if (prunable != val.MemberEnd())
1175 {
1176 rct::keyV pseudo_outs = std::move(sig.get_pseudo_outs());
1177
1178 GET_FROM_JSON_OBJECT(prunable->value, sig.p.rangeSigs, range_proofs);
1179 GET_FROM_JSON_OBJECT(prunable->value, sig.p.bulletproofs, bulletproofs);
1180 GET_FROM_JSON_OBJECT(prunable->value, sig.p.bulletproofs_plus, bulletproofs_plus);
1181 GET_FROM_JSON_OBJECT(prunable->value, sig.p.MGs, mlsags);
1182 GET_FROM_JSON_OBJECT(prunable->value, sig.p.CLSAGs, clsags);
1183 GET_FROM_JSON_OBJECT(prunable->value, pseudo_outs, pseudo_outs);
1184
1185 sig.get_pseudo_outs() = std::move(pseudo_outs);
1186 }
1187 else
1188 {
1189 sig.p.rangeSigs.clear();
1190 sig.p.bulletproofs.clear();
1191 sig.p.bulletproofs_plus.clear();
1192 sig.p.MGs.clear();
1193 sig.p.CLSAGs.clear();
1194 sig.get_pseudo_outs().clear();
1195 }
1196 }
1197
1198 void fromJsonValue(const rapidjson::Value& val, rct::ctkey& key)
1199 {
1200 key.dest = {};
1201 fromJsonValue(val, key.mask);
1202 }
1203
1204 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::ecdhTuple& tuple)
1205 {
1206 dest.StartObject();
1207 INSERT_INTO_JSON_OBJECT(dest, mask, tuple.mask);
1208 INSERT_INTO_JSON_OBJECT(dest, amount, tuple.amount);
1209 dest.EndObject();
1210 }
1211
1212 void fromJsonValue(const rapidjson::Value& val, rct::ecdhTuple& tuple)
1213 {
1214 if (!val.IsObject())
1215 {
1216 throw WRONG_TYPE("json object");
1217 }
1218
1219 GET_FROM_JSON_OBJECT(val, tuple.mask, mask);
1220 GET_FROM_JSON_OBJECT(val, tuple.amount, amount);
1221 }
1222
1223 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::rangeSig& sig)
1224 {
1225 dest.StartObject();
1226
1227 INSERT_INTO_JSON_OBJECT(dest, asig, sig.asig);
1228 INSERT_INTO_JSON_OBJECT(dest, Ci, epee::span<const rct::key>{sig.Ci});
1229
1230 dest.EndObject();
1231 }
1232
1233 void fromJsonValue(const rapidjson::Value& val, rct::rangeSig& sig)
1234 {
1235 if (!val.IsObject())
1236 {
1237 throw WRONG_TYPE("json object");
1238 }
1239
1240 const auto ci = val.FindMember("Ci");
1241 if (ci == val.MemberEnd())
1242 {
1243 throw MISSING_KEY("Ci");
1244 }
1245
1246 GET_FROM_JSON_OBJECT(val, sig.asig, asig);
1247
1248 std::vector<rct::key> keyVector;
1249 cryptonote::json::fromJsonValue(ci->value, keyVector);
1250 if (!(keyVector.size() == 64))
1251 {
1252 throw WRONG_TYPE("key64 (rct::key[64])");
1253 }
1254 for (size_t i=0; i < 64; i++)
1255 {
1256 sig.Ci[i] = keyVector[i];
1257 }
1258 }
1259
1260 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::Bulletproof& p)
1261 {
1262 dest.StartObject();
1263
1264 INSERT_INTO_JSON_OBJECT(dest, V, p.V);
1265 INSERT_INTO_JSON_OBJECT(dest, A, p.A);
1266 INSERT_INTO_JSON_OBJECT(dest, S, p.S);
1267 INSERT_INTO_JSON_OBJECT(dest, T1, p.T1);
1268 INSERT_INTO_JSON_OBJECT(dest, T2, p.T2);
1269 INSERT_INTO_JSON_OBJECT(dest, taux, p.taux);
1270 INSERT_INTO_JSON_OBJECT(dest, mu, p.mu);
1271 INSERT_INTO_JSON_OBJECT(dest, L, p.L);
1272 INSERT_INTO_JSON_OBJECT(dest, R, p.R);
1273 INSERT_INTO_JSON_OBJECT(dest, a, p.a);
1274 INSERT_INTO_JSON_OBJECT(dest, b, p.b);
1275 INSERT_INTO_JSON_OBJECT(dest, t, p.t);
1276
1277 dest.EndObject();
1278 }
1279
1280 void fromJsonValue(const rapidjson::Value& val, rct::Bulletproof& p)
1281 {
1282 if (!val.IsObject())
1283 {
1284 throw WRONG_TYPE("json object");
1285 }
1286
1287 GET_FROM_JSON_OBJECT(val, p.V, V);
1288 GET_FROM_JSON_OBJECT(val, p.A, A);
1289 GET_FROM_JSON_OBJECT(val, p.S, S);
1290 GET_FROM_JSON_OBJECT(val, p.T1, T1);
1291 GET_FROM_JSON_OBJECT(val, p.T2, T2);
1292 GET_FROM_JSON_OBJECT(val, p.taux, taux);
1293 GET_FROM_JSON_OBJECT(val, p.mu, mu);
1294 GET_FROM_JSON_OBJECT(val, p.L, L);
1295 GET_FROM_JSON_OBJECT(val, p.R, R);
1296 GET_FROM_JSON_OBJECT(val, p.a, a);
1297 GET_FROM_JSON_OBJECT(val, p.b, b);
1298 GET_FROM_JSON_OBJECT(val, p.t, t);
1299 }
1300
1301 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::BulletproofPlus& p)
1302 {
1303 dest.StartObject();
1304
1305 INSERT_INTO_JSON_OBJECT(dest, V, p.V);
1306 INSERT_INTO_JSON_OBJECT(dest, A, p.A);
1307 INSERT_INTO_JSON_OBJECT(dest, A1, p.A1);
1308 INSERT_INTO_JSON_OBJECT(dest, B, p.B);
1309 INSERT_INTO_JSON_OBJECT(dest, r1, p.r1);
1310 INSERT_INTO_JSON_OBJECT(dest, s1, p.s1);
1311 INSERT_INTO_JSON_OBJECT(dest, d1, p.d1);
1312 INSERT_INTO_JSON_OBJECT(dest, L, p.L);
1313 INSERT_INTO_JSON_OBJECT(dest, R, p.R);
1314
1315 dest.EndObject();
1316 }
1317
1318 void fromJsonValue(const rapidjson::Value& val, rct::BulletproofPlus& p)
1319 {
1320 if (!val.IsObject())
1321 {
1322 throw WRONG_TYPE("json object");
1323 }
1324
1325 GET_FROM_JSON_OBJECT(val, p.V, V);
1326 GET_FROM_JSON_OBJECT(val, p.A, A);
1327 GET_FROM_JSON_OBJECT(val, p.A1, A1);
1328 GET_FROM_JSON_OBJECT(val, p.B, B);
1329 GET_FROM_JSON_OBJECT(val, p.r1, r1);
1330 GET_FROM_JSON_OBJECT(val, p.s1, s1);
1331 GET_FROM_JSON_OBJECT(val, p.d1, d1);
1332 GET_FROM_JSON_OBJECT(val, p.L, L);
1333 GET_FROM_JSON_OBJECT(val, p.R, R);
1334 }
1335
1336 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::boroSig& sig)
1337 {
1338 dest.StartObject();
1339
1340 INSERT_INTO_JSON_OBJECT(dest, s0, epee::span<const rct::key>{sig.s0});
1341 INSERT_INTO_JSON_OBJECT(dest, s1, epee::span<const rct::key>{sig.s1});
1342 INSERT_INTO_JSON_OBJECT(dest, ee, sig.ee);
1343
1344 dest.EndObject();
1345 }
1346
1347 void fromJsonValue(const rapidjson::Value& val, rct::boroSig& sig)
1348 {
1349 if (!val.IsObject())
1350 {
1351 throw WRONG_TYPE("json object");
1352 }
1353
1354 OBJECT_HAS_MEMBER_OR_THROW(val, "s0")
1355 std::vector<rct::key> keyVector;
1356 cryptonote::json::fromJsonValue(val["s0"], keyVector);
1357 if (!(keyVector.size() == 64))
1358 {
1359 throw WRONG_TYPE("key64 (rct::key[64])");
1360 }
1361 for (size_t i=0; i < 64; i++)
1362 {
1363 sig.s0[i] = keyVector[i];
1364 }
1365
1366 OBJECT_HAS_MEMBER_OR_THROW(val, "s1")
1367 keyVector.clear();
1368 cryptonote::json::fromJsonValue(val["s1"], keyVector);
1369 if (!(keyVector.size() == 64))
1370 {
1371 throw WRONG_TYPE("key64 (rct::key[64])");
1372 }
1373 for (size_t i=0; i < 64; i++)
1374 {
1375 sig.s1[i] = keyVector[i];
1376 }
1377
1378 GET_FROM_JSON_OBJECT(val, sig.ee, ee);
1379 }
1380
1381 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::mgSig& sig)
1382 {
1383 dest.StartObject();
1384
1385 INSERT_INTO_JSON_OBJECT(dest, ss, sig.ss);
1386 INSERT_INTO_JSON_OBJECT(dest, cc, sig.cc);
1387
1388 dest.EndObject();
1389 }
1390
1391 void fromJsonValue(const rapidjson::Value& val, rct::mgSig& sig)
1392 {
1393 if (!val.IsObject())
1394 {
1395 throw WRONG_TYPE("key64 (rct::key[64])");
1396 }
1397
1398 GET_FROM_JSON_OBJECT(val, sig.ss, ss);
1399 GET_FROM_JSON_OBJECT(val, sig.cc, cc);
1400 }
1401
1402 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const rct::clsag& sig)
1403 {
1404 dest.StartObject();
1405
1406 INSERT_INTO_JSON_OBJECT(dest, s, sig.s);
1407 INSERT_INTO_JSON_OBJECT(dest, c1, sig.c1);
1408 INSERT_INTO_JSON_OBJECT(dest, D, sig.D);
1409
1410 dest.EndObject();
1411 }
1412
1413 void fromJsonValue(const rapidjson::Value& val, rct::clsag& sig)
1414 {
1415 if (!val.IsObject())
1416 {
1417 throw WRONG_TYPE("key64 (rct::key[64])");
1418 }
1419
1420 GET_FROM_JSON_OBJECT(val, sig.s, s);
1421 GET_FROM_JSON_OBJECT(val, sig.c1, c1);
1422 GET_FROM_JSON_OBJECT(val, sig.D, D);
1423 }
1424
1425 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::DaemonInfo& info)
1426 {
1427 dest.StartObject();
1428
1429 const uint64_t difficulty_top64 = (info.wide_difficulty >> 64).convert_to<std::uint64_t>();
1430 const uint64_t cumulative_difficulty_top64 = (info.wide_cumulative_difficulty >> 64).convert_to<std::uint64_t>();
1431
1432 INSERT_INTO_JSON_OBJECT(dest, height, info.height);
1433 INSERT_INTO_JSON_OBJECT(dest, target_height, info.target_height);
1434 INSERT_INTO_JSON_OBJECT(dest, top_block_height, info.top_block_height);
1435 INSERT_INTO_JSON_OBJECT(dest, difficulty, info.difficulty);
1436 INSERT_INTO_JSON_OBJECT(dest, difficulty_top64, difficulty_top64);
1437 INSERT_INTO_JSON_OBJECT(dest, target, info.target);
1438 INSERT_INTO_JSON_OBJECT(dest, tx_count, info.tx_count);
1439 INSERT_INTO_JSON_OBJECT(dest, tx_pool_size, info.tx_pool_size);
1440 INSERT_INTO_JSON_OBJECT(dest, alt_blocks_count, info.alt_blocks_count);
1441 INSERT_INTO_JSON_OBJECT(dest, outgoing_connections_count, info.outgoing_connections_count);
1442 INSERT_INTO_JSON_OBJECT(dest, incoming_connections_count, info.incoming_connections_count);
1443 INSERT_INTO_JSON_OBJECT(dest, white_peerlist_size, info.white_peerlist_size);
1444 INSERT_INTO_JSON_OBJECT(dest, grey_peerlist_size, info.grey_peerlist_size);
1445 INSERT_INTO_JSON_OBJECT(dest, mainnet, info.mainnet);
1446 INSERT_INTO_JSON_OBJECT(dest, testnet, info.testnet);
1447 INSERT_INTO_JSON_OBJECT(dest, stagenet, info.stagenet);
1448 INSERT_INTO_JSON_OBJECT(dest, nettype, info.nettype);
1449 INSERT_INTO_JSON_OBJECT(dest, top_block_hash, info.top_block_hash);
1450 INSERT_INTO_JSON_OBJECT(dest, cumulative_difficulty, info.cumulative_difficulty);
1451 INSERT_INTO_JSON_OBJECT(dest, cumulative_difficulty_top64, cumulative_difficulty_top64);
1452 INSERT_INTO_JSON_OBJECT(dest, block_size_limit, info.block_size_limit);
1453 INSERT_INTO_JSON_OBJECT(dest, block_weight_limit, info.block_weight_limit);
1454 INSERT_INTO_JSON_OBJECT(dest, block_size_median, info.block_size_median);
1455 INSERT_INTO_JSON_OBJECT(dest, block_weight_median, info.block_weight_median);
1456 INSERT_INTO_JSON_OBJECT(dest, adjusted_time, info.adjusted_time);
1457 INSERT_INTO_JSON_OBJECT(dest, start_time, info.start_time);
1458 INSERT_INTO_JSON_OBJECT(dest, version, info.version);
1459
1460 dest.EndObject();
1461 }
1462
1463 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::DaemonInfo& info)
1464 {
1465 if (!val.IsObject())
1466 {
1467 throw WRONG_TYPE("json object");
1468 }
1469
1470 uint64_t difficulty_top64 = 0;
1471 uint64_t cumulative_difficulty_top64 = 0;
1472
1473 GET_FROM_JSON_OBJECT(val, info.height, height);
1474 GET_FROM_JSON_OBJECT(val, info.target_height, target_height);
1475 GET_FROM_JSON_OBJECT(val, info.top_block_height, top_block_height);
1476 GET_FROM_JSON_OBJECT(val, info.difficulty, difficulty);
1477 GET_FROM_JSON_OBJECT(val, difficulty_top64, difficulty_top64);
1478 GET_FROM_JSON_OBJECT(val, info.target, target);
1479 GET_FROM_JSON_OBJECT(val, info.tx_count, tx_count);
1480 GET_FROM_JSON_OBJECT(val, info.tx_pool_size, tx_pool_size);
1481 GET_FROM_JSON_OBJECT(val, info.alt_blocks_count, alt_blocks_count);
1482 GET_FROM_JSON_OBJECT(val, info.outgoing_connections_count, outgoing_connections_count);
1483 GET_FROM_JSON_OBJECT(val, info.incoming_connections_count, incoming_connections_count);
1484 GET_FROM_JSON_OBJECT(val, info.white_peerlist_size, white_peerlist_size);
1485 GET_FROM_JSON_OBJECT(val, info.grey_peerlist_size, grey_peerlist_size);
1486 GET_FROM_JSON_OBJECT(val, info.mainnet, mainnet);
1487 GET_FROM_JSON_OBJECT(val, info.testnet, testnet);
1488 GET_FROM_JSON_OBJECT(val, info.stagenet, stagenet);
1489 GET_FROM_JSON_OBJECT(val, info.nettype, nettype);
1490 GET_FROM_JSON_OBJECT(val, info.top_block_hash, top_block_hash);
1491 GET_FROM_JSON_OBJECT(val, info.cumulative_difficulty, cumulative_difficulty);
1492 GET_FROM_JSON_OBJECT(val, cumulative_difficulty_top64, cumulative_difficulty_top64);
1493 GET_FROM_JSON_OBJECT(val, info.block_size_limit, block_size_limit);
1494 GET_FROM_JSON_OBJECT(val, info.block_weight_limit, block_weight_limit);
1495 GET_FROM_JSON_OBJECT(val, info.block_size_median, block_size_median);
1496 GET_FROM_JSON_OBJECT(val, info.block_weight_median, block_weight_median);
1497 GET_FROM_JSON_OBJECT(val, info.adjusted_time, adjusted_time);
1498 GET_FROM_JSON_OBJECT(val, info.start_time, start_time);
1499 GET_FROM_JSON_OBJECT(val, info.version, version);
1500
1501 info.wide_difficulty = difficulty_top64;
1502 info.wide_difficulty <<= 64;
1503 info.wide_difficulty += info.difficulty;
1504
1505 info.wide_cumulative_difficulty = cumulative_difficulty_top64;
1506 info.wide_cumulative_difficulty <<= 64;
1507 info.wide_cumulative_difficulty += info.cumulative_difficulty;
1508 }
1509
1510 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::rpc::output_distribution& dist)
1511 {
1512 dest.StartObject();
1513
1514 INSERT_INTO_JSON_OBJECT(dest, distribution, dist.data.distribution);
1515 INSERT_INTO_JSON_OBJECT(dest, amount, dist.amount);
1516 INSERT_INTO_JSON_OBJECT(dest, start_height, dist.data.start_height);
1517 INSERT_INTO_JSON_OBJECT(dest, base, dist.data.base);
1518
1519 dest.EndObject();
1520 }
1521
1522 void fromJsonValue(const rapidjson::Value& val, cryptonote::rpc::output_distribution& dist)
1523 {
1524 if (!val.IsObject())
1525 {
1526 throw WRONG_TYPE("json object");
1527 }
1528
1529 GET_FROM_JSON_OBJECT(val, dist.data.distribution, distribution);
1530 GET_FROM_JSON_OBJECT(val, dist.amount, amount);
1531 GET_FROM_JSON_OBJECT(val, dist.data.start_height, start_height);
1532 GET_FROM_JSON_OBJECT(val, dist.data.base, base);
1533 }
1534
1535 void toJsonValue(rapidjson::Writer<epee::byte_stream>& dest, const cryptonote::tx_block_template_backlog_entry& entry)
1536 {
1537 dest.StartObject();
1538 INSERT_INTO_JSON_OBJECT(dest, id, entry.id);
1539 INSERT_INTO_JSON_OBJECT(dest, weight, entry.weight);
1540 INSERT_INTO_JSON_OBJECT(dest, fee, entry.fee);
1541 dest.EndObject();
1542 }
1543
1544 void fromJsonValue(const rapidjson::Value& val, cryptonote::tx_block_template_backlog_entry& entry)
1545 {
1546 if (!val.IsObject())
1547 {
1548 throw WRONG_TYPE("json object");
1549 }
1550
1551 GET_FROM_JSON_OBJECT(val, entry.id, id);
1552 GET_FROM_JSON_OBJECT(val, entry.weight, weight);
1553 GET_FROM_JSON_OBJECT(val, entry.fee, fee);
1554 }
1555
1556 } // namespace json
1557
1558 } // namespace cryptonote