Skip to content

Commit ed0ab57

Browse files
sleepdefic1tfaustbrian
authored andcommitted
fix(transaction): Improve Numeric Json Serialization (#103)
Current implementation sometimes fails to properly serialize numbers in the `toJson()` method. This is especially detrimental to some Linux builds and all Arduino IDE builds (Arduino IDE Library version (Cpp-Crypto-Arduino-v.0.3.0)). Specifically, this PR does the following: - moves `<cinttypes>` to `crypto_helpers.h` for non-IoT builds. - forces type serialization for: - `amount` (llu/uint64_t). - `fee` (llu/uint64_t). - `network` (uint8_t/int). - `timestamp` (lu/uint32_t). - `type` (uint8_t/int). - `version` (uint8_t/int). - updates the changelog.
1 parent 81647da commit ed0ab57

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1313
- improved formatting and maintainability ([#98])
1414
- improved Slots implementations ([#92])
1515

16+
### Fixed
17+
18+
- fixed Transaction Json numeric serialization ([#103])
19+
1620
## [0.5.0] - 2019-02-20
1721

1822
### Changed

src/helpers/crypto_helpers.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const auto WIF_SIZE = 52U;
2929

3030
#endif
3131

32+
#ifndef USE_IOT
33+
34+
#define __STDC_FORMAT_MACROS 1
35+
#include <cinttypes>
36+
37+
#endif
38+
3239
// Write data into dst
3340
template <typename T>
3441
inline void pack(std::vector<uint8_t>& dst, T& data) {

src/transactions/transaction.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
#include "bcl/Sha256.hpp"
1414

15+
#include <cstdlib>
1516
#include <map>
1617
#include <string>
1718
#include <vector>
1819

19-
#define __STDC_FORMAT_MACROS 1
20-
#include <cinttypes>
21-
2220
using namespace Ark::Crypto::Identities;
2321

2422
std::string Ark::Crypto::Transactions::Transaction::getId() const {
@@ -326,7 +324,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() {
326324
DynamicJsonDocument doc(docCapacity);
327325

328326
// Amount
329-
doc["amount"] = txArray["amount"];
327+
doc["amount"] = strtoull(txArray["amount"].c_str(), nullptr, 10);
330328

331329
// Asset
332330
if (this->type == 0) {
@@ -342,7 +340,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() {
342340
JsonObject dAsset = doc.createNestedObject("asset");
343341
JsonObject delegate = dAsset.createNestedObject("delegate");
344342
delegate["username"] = txArray["username"];
345-
}else if (this->type == 3) {
343+
} else if (this->type == 3) {
346344
// Vote
347345
JsonObject vAsset = doc.createNestedObject("asset");
348346
JsonArray votes = vAsset.createNestedArray("votes");
@@ -368,14 +366,14 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() {
368366
};
369367

370368
// Fee
371-
doc["fee"] = txArray["fee"];
369+
doc["fee"] = strtoull(txArray["fee"].c_str(), nullptr, 10);
372370

373371
// Id
374372
doc["id"] = txArray["id"];
375373

376374
// Network
377375
if (txArray["network"] != "0") {
378-
doc["network"] = txArray["network"];
376+
doc["network"] = atoi(txArray["network"].c_str());
379377
};
380378

381379
// RecipientId
@@ -410,10 +408,10 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() {
410408
};
411409

412410
// Timestamp
413-
doc["timestamp"] = txArray["timestamp"];
411+
doc["timestamp"] = strtoul(txArray["timestamp"].c_str(), nullptr, 10);
414412

415413
// Type
416-
doc["type"] = txArray["type"];
414+
doc["type"] = atoi(txArray["type"].c_str());
417415

418416
// VendorField
419417
if (std::strlen(txArray["vendorField"].c_str()) > 0) {
@@ -422,7 +420,7 @@ std::string Ark::Crypto::Transactions::Transaction::toJson() {
422420

423421
// Version
424422
if (txArray["version"] != "0") {
425-
doc["version"] = txArray["version"];
423+
doc["version"] = atoi(txArray["version"].c_str());
426424
};
427425

428426
char jsonChar[docCapacity];

0 commit comments

Comments
 (0)