-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTransaction.hpp
More file actions
54 lines (46 loc) · 1.59 KB
/
Copy pathTransaction.hpp
File metadata and controls
54 lines (46 loc) · 1.59 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
#ifndef TRANSACTION_HPP_
#define TRANSACTION_HPP_
#include <string>
#include <chrono>
#include <iostream>
#include "sha256.hpp"
#include "nlohmann/json.hpp"
#include "timestamp_as_string.hpp"
class Transaction
{
private:
double amount;
std::string senderKey;
std::string receiverKey;
std::string timestamp = getTimestampAsString(std::chrono::system_clock::now());
public:
Transaction(double amount, std::string senderKey, std::string receiverKey)
{
this->amount = amount;
this->senderKey = senderKey;
this->receiverKey = receiverKey;
}
Transaction(nlohmann::json transaction_json)
{
this->amount = transaction_json["amount"];
this->senderKey = transaction_json["senderKey"];
this->receiverKey = transaction_json["receiverKey"];
this->timestamp = transaction_json["timestamp"];
}
std::string getHash() const
{
return sha256(std::to_string(amount) + senderKey + receiverKey + timestamp);
}
void dump(nlohmann::json & output_json) const
{
output_json["transactions"].push_back( {{"amount", amount}, {"senderKey", senderKey}, {"receiverKey", receiverKey}, {"timestamp", timestamp}});
return;
std::cout << "\n### Transaction:";
std::cout << "\n\tSender: " << senderKey;
std::cout << "\n\tReceiver: " << receiverKey;
std::cout << "\n\tAmount: " << amount;
std::cout << "\n\tTimestamp: " << timestamp;
std::cout << "\n\tTransaction Hash: " << getHash() << '\n';
}
};
#endif