-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.cpp
254 lines (218 loc) · 7.63 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
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
#include "transaction.hpp"
#include "helpers.hpp"
#include "block.hpp"
#include "openssl/sha.h"
#include <sstream>
#include <iostream>
#include <cstring>
#include <ctime>
using namespace std;
TransactionInfo transactionInfoFromBuffer(const char* buffer) {
TransactionInfo t;
readNetworkNBytes(buffer, t.signature, 64);
readNetworkNBytes(buffer, t.signingKey, 32);
t.timestamp = readNetworkUint64(buffer);
t.to = readNetworkPublicWalletAddress(buffer);
t.amount = readNetworkUint64(buffer);
t.fee = readNetworkUint64(buffer);
t.isTransactionFee = readNetworkUint32(buffer) > 0;
if (!t.isTransactionFee) {
PublicKey pub;
memcpy(pub.data(), t.signingKey, 32);
t.from = walletAddressFromPublicKey(pub);
} else {
t.from = NULL_ADDRESS;
}
return t;
}
void transactionInfoToBuffer(TransactionInfo& t, char* buffer) {
writeNetworkNBytes(buffer, t.signature, 64);
writeNetworkNBytes(buffer, t.signingKey, 32);
writeNetworkUint64(buffer, t.timestamp);
writeNetworkPublicWalletAddress(buffer, t.to);
writeNetworkUint64(buffer, t.amount);
writeNetworkUint64(buffer, t.fee);
uint32_t flag = 0;
if (t.isTransactionFee) flag = 1;
writeNetworkUint32(buffer, flag);
}
Transaction::Transaction(PublicWalletAddress from, PublicWalletAddress to, TransactionAmount amount, PublicKey signingKey, TransactionAmount fee) {
this->from = from;
this->to = to;
this->amount = amount;
this->isTransactionFee = false;
this->timestamp = std::time(0);
this->fee = fee;
this->signingKey = signingKey;
}
Transaction::Transaction(PublicWalletAddress from, PublicWalletAddress to, TransactionAmount amount, PublicKey signingKey, TransactionAmount fee, uint64_t timestamp) {
this->from = from;
this->to = to;
this->amount = amount;
this->isTransactionFee = false;
this->timestamp = timestamp;
this->fee = fee;
this->signingKey = signingKey;
}
Transaction::Transaction() {
}
Transaction::Transaction(const TransactionInfo& t) {
this->to = t.to;
if (!t.isTransactionFee) this->from = t.from;
memcpy((void*)this->signature.data(), (void*)t.signature, 64);
memcpy((void*)this->signingKey.data(), (void*)t.signingKey, 32);
this->amount = t.amount;
this->isTransactionFee = t.isTransactionFee;
this->timestamp = t.timestamp;
this->fee = t.fee;
}
TransactionInfo Transaction::serialize() {
TransactionInfo t;
memcpy((void*)t.signature, (void*)this->signature.data(), 64);
memcpy((void*)t.signingKey, (void*)this->signingKey.data(), 32);
t.timestamp = this->timestamp;
t.to = this->to;
t.from = this->from;
t.amount = this->amount;
t.fee = this->fee;
t.isTransactionFee = this->isTransactionFee;
return t;
}
Transaction::Transaction(const Transaction & t) {
this->to = t.to;
this->from = t.from;
this->signature = t.signature;
this->amount = t.amount;
this->isTransactionFee = t.isTransactionFee;
this->timestamp = t.timestamp;
this->fee = t.fee;
this->signingKey = t.signingKey;
}
Transaction::Transaction(PublicWalletAddress to, TransactionAmount fee) {
this->to = to;
this->amount = fee;
this->isTransactionFee = true;
this->timestamp = getCurrentTime();
this->fee = 0;
}
Transaction::Transaction(json data) {
PublicWalletAddress to;
this->timestamp = stringToUint64(data["timestamp"]);
this->to = stringToWalletAddress(data["to"]);
this->fee = data["fee"];
if(data["from"] == "") {
this->amount = data["amount"];
this->isTransactionFee = true;
} else {
this->from = stringToWalletAddress(data["from"]);
this->signature = stringToSignature(data["signature"]);
this->amount = data["amount"];
this->isTransactionFee = false;
this->signingKey = stringToPublicKey(data["signingKey"]);
}
}
void Transaction::setTransactionFee(TransactionAmount amount) {
this->fee = amount;
}
TransactionAmount Transaction::getTransactionFee() const {
return this->fee;
}
json Transaction::toJson() {
json result;
result["to"] = walletAddressToString(this->toWallet());
result["amount"] = this->amount;
result["timestamp"] = uint64ToString(this->timestamp);
result["fee"] = this->fee;
if (!this->isTransactionFee) {
result["from"] = walletAddressToString(this->fromWallet());
result["signingKey"] = publicKeyToString(this->signingKey);
result["signature"] = signatureToString(this->signature);
} else {
result["from"] = "";
}
return result;
}
bool Transaction::isFee() const {
return this->isTransactionFee;
}
void Transaction::setTimestamp(uint64_t t) {
this->timestamp = t;
}
uint64_t Transaction::getTimestamp() {
return this->timestamp;
}
TransactionSignature Transaction::getSignature() const {
return this->signature;
}
void Transaction::setAmount(TransactionAmount amt) {
this->amount = amt;
}
bool Transaction::signatureValid() const {
if (this->isFee()) return true;
SHA256Hash hash = this->hashContents();
return checkSignature((const char*)hash.data(), hash.size(), this->signature, this->signingKey);
}
PublicKey Transaction::getSigningKey() {
return this->signingKey;
}
SHA256Hash Transaction::getHash() const {
SHA256Hash ret;
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256Hash contentHash = this->hashContents();
SHA256_Update(&sha256, (unsigned char*)contentHash.data(), contentHash.size());
if (!this->isTransactionFee) {
SHA256_Update(&sha256, (unsigned char*)this->signature.data(), this->signature.size());
}
SHA256_Final(ret.data(), &sha256);
return ret;
}
PublicWalletAddress Transaction::fromWallet() const {
return this->from;
}
PublicWalletAddress Transaction::toWallet() const {
return this->to;
}
TransactionAmount Transaction::getAmount() const {
return this->amount;
}
TransactionAmount Transaction::getFee() const {
return this->fee;
}
SHA256Hash Transaction::hashContents() const {
SHA256Hash ret;
SHA256_CTX sha256;
SHA256_Init(&sha256);
PublicWalletAddress wallet = this->toWallet();
SHA256_Update(&sha256, (unsigned char*)wallet.data(), wallet.size());
if (!this->isTransactionFee) {
wallet = this->fromWallet();
SHA256_Update(&sha256, (unsigned char*)wallet.data(), wallet.size());
}
SHA256_Update(&sha256, (unsigned char*)&this->fee, sizeof(TransactionAmount));
SHA256_Update(&sha256, (unsigned char*)&this->amount, sizeof(TransactionAmount));
SHA256_Update(&sha256, (unsigned char*)&this->timestamp, sizeof(uint64_t));
SHA256_Final(ret.data(), &sha256);
return ret;
}
void Transaction::sign(PublicKey pubKey, PrivateKey signingKey) {
SHA256Hash hash = this->hashContents();
TransactionSignature signature = signWithPrivateKey((const char*)hash.data(), hash.size(), pubKey, signingKey);
this->signature = signature;
}
bool operator<(const Transaction& a, const Transaction& b) {
return a.signature < b.signature;
}
bool operator==(const Transaction& a, const Transaction& b) {
if(a.timestamp != b.timestamp) return false;
if(a.toWallet() != b.toWallet()) return false;
if(a.getTransactionFee() != b.getTransactionFee()) return false;
if( a.amount != b.amount) return false;
if( a.isTransactionFee != b.isTransactionFee) return false;
if (!a.isTransactionFee) {
if( a.fromWallet() != b.fromWallet()) return false;
if(signatureToString(a.signature) != signatureToString(b.signature)) return false;
if (publicKeyToString(a.signingKey) != publicKeyToString(b.signingKey)) return false;
}
return true;
}