forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.cpp
More file actions
70 lines (56 loc) · 1.89 KB
/
Copy pathTransaction.cpp
File metadata and controls
70 lines (56 loc) · 1.89 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
#include "Transaction.h"
#include <stdexcept>
#include <ctime>
#include "../Hash.h"
#include "../HexCoding.h"
using namespace TW::Bravo;
using json = nlohmann::json;
/// Signature
Signature::Signature(Data sig): data(sig) {
if (sig.size() != Signature::DataSize) {
throw std::invalid_argument("Invalid signature!");
}
}
void Signature::serialize(Data& os) const noexcept {
append(os, data);
}
std::string Signature::string() const noexcept {
return hex(data);
}
Transaction::Transaction(const Data& referenceBlockId, int32_t referenceBlockTime) {
setReferenceBlock(referenceBlockId);
expiration = referenceBlockTime + Transaction::ExpirySeconds;
}
void Transaction::setReferenceBlock(const Data& refBlockId) {
if (refBlockId.size() != Hash::ripemdSize) {
throw std::invalid_argument("Invalid Reference Block Id!");
}
refBlockNumber = decode16BE(refBlockId.data() + 2);
refBlockPrefix = decode32LE(refBlockId.data() + 4);
}
void Transaction::serialize(Data& os) const noexcept {
encode16LE(refBlockNumber, os);
encode32LE(refBlockPrefix, os);
encode32LE(expiration, os);
encodePointerCollection(operations, os);
encodePointerCollection(extensions, os);
}
json Transaction::serialize() const noexcept {
char formattedDate[20];
time_t time = expiration;
if (strftime(formattedDate, 19, "%FT%T", std::gmtime(&time)) != 19) {
std::runtime_error("Error creating a formatted string!");
}
json sigs = json::array();
for (const auto& sig : signatures) {
sigs.push_back(sig.string());
}
json obj;
obj["ref_block_num"] = refBlockNumber;
obj["ref_block_prefix"] = refBlockPrefix;
obj["expiration"] = std::string(formattedDate, 19);
obj["operations"] = encodePointerCollection(operations);
obj["extensions"] = encodePointerCollection(extensions);
obj["signatures"] = sigs;
return obj;
}