-
Notifications
You must be signed in to change notification settings - Fork 3
/
transaction.cpp
141 lines (120 loc) · 4.14 KB
/
transaction.cpp
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
#include "transaction.h"
#include <sodium.h>
#include "lisk.h"
#include "utils.h"
Transaction::Transaction(
std::uint8_t _type,
std::int32_t _timestamp,
std::vector<unsigned char> _senderPublicKey,
std::uint64_t _recipientId,
std::uint64_t _amount,
std::uint64_t _fee,
std::vector<unsigned char> _assetData,
std::uint64_t _dappId
)
: type(_type)
, timestamp(_timestamp)
, senderAddress(addressFromPubkey(_senderPublicKey))
, senderPublicKey(_senderPublicKey)
, recipientAddress(_recipientId)
, amount(_amount)
, fee(_fee)
, assetData(_assetData)
, dappId(_dappId)
, type3Votes(_type == 3
? Transaction::parseType3Votes(std::string(_assetData.begin(), _assetData.end()))
: VotesUpdate())
, type4Pubkeys(_type == 4
? Transaction::parseType4Pubkeys(std::string(_assetData.begin(), _assetData.end()))
: std::vector<std::vector<unsigned char>>())
{
}
std::vector<unsigned char> Transaction::serialize() const
{
std::size_t size = 1 // type
+ 4 // timestamp
+ 32 // sender pubkey
+ 8 // recipient
+ 8 // amount
+ assetData.size()
;
auto out = std::vector<unsigned char>(size);
out[0] = type;
out[1] = (timestamp >> 0) & 0xFF;
out[2] = (timestamp >> 8) & 0xFF;
out[3] = (timestamp >> 16) & 0xFF;
out[4] = (timestamp >> 24) & 0xFF;
for (int i = 0; i < senderPublicKey.size(); ++i) {
out[5+i] = senderPublicKey[i];
}
for (int i = 0; i < 8; ++i) {
out[37+i] = (recipientAddress >> (7-i)*8) & 0xFF;
}
for (int i = 0; i < 8; ++i) {
out[45+i] = (amount >> i*8) & 0xFF;
}
for (int i = 0; i < assetData.size(); ++i) {
out[53+i] = assetData[i];
}
return out;
}
std::vector<unsigned char> Transaction::hash(std::vector<unsigned char> signature, std::vector<unsigned char> secondSignature) const
{
auto out = std::vector<unsigned char>(crypto_hash_sha256_BYTES);
crypto_hash_sha256_state state;
crypto_hash_sha256_init(&state);
auto message = serialize();
crypto_hash_sha256_update(&state, message.data(), message.size());
crypto_hash_sha256_update(&state, signature.data(), signature.size());
crypto_hash_sha256_update(&state, secondSignature.data(), secondSignature.size());
crypto_hash_sha256_final(&state, out.data());
return out;
}
std::uint64_t Transaction::id(std::vector<unsigned char> signature, std::vector<unsigned char> secondSignature) const
{
return idFromEightBytes(firstEightBytesReversed(hash(signature, secondSignature)));
}
VotesUpdate Transaction::parseType3Votes(const std::string transactionAsset)
{
VotesUpdate out;
auto iterator = transactionAsset.cbegin();
while (iterator != transactionAsset.cend()) {
if (*iterator == ',') ++iterator;
auto prefix = *iterator;
++iterator;
auto pubkey = hex2Bytes(std::string(iterator, iterator+64));
iterator += 64;
if (prefix == '+') {
out.added.push_back(pubkey);
} else if (prefix == '-') {
out.removed.push_back(pubkey);
} else {
throw std::runtime_error("Invalid prefix found in votes attset data: " + std::string(1, prefix));
}
}
return out;
}
std::vector<std::vector<unsigned char>> Transaction::parseType4Pubkeys(const std::string transactionAsset)
{
std::vector<std::vector<unsigned char>> out;
auto iterator = transactionAsset.cbegin();
iterator += 2; // skip min, lifetime
while (iterator != transactionAsset.cend()) {
if (*iterator == ',') ++iterator;
++iterator; // skip "+"
auto pubkeyHex = std::string(iterator, iterator+64);
out.push_back(hex2Bytes(pubkeyHex));
iterator += 64;
}
return out;
}
std::ostream& operator<<(std::ostream& os, const Transaction& trx)
{
os << std::to_string(trx.type)
<< "/" << trx.timestamp
<< "/" << trx.senderPublicKey.size()
<< "/" << trx.recipientAddress << "L"
<< "/" << trx.amount
;
return os;
}