-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransaction.cpp
More file actions
163 lines (139 loc) · 4.8 KB
/
transaction.cpp
File metadata and controls
163 lines (139 loc) · 4.8 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
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
#include "transaction.h"
#include "wallet.h"
#include <sstream>
#include <iomanip>
#include <openssl/sha.h>
#include <chrono>
#include <ctime>
#include <iostream>
using json = nlohmann::json;
TransactionInput::TransactionInput(const std::string& txId, int outputIndex, const std::string& signature)
: txId_(txId)
, outputIndex_(outputIndex)
, signature_(signature)
{
}
TransactionOutput::TransactionOutput(double amount, const std::string& owner)
: amount_(amount)
, owner_(owner)
{
}
Transaction::Transaction(const std::string& from, const std::string& to, double amount)
: from_(from)
, to_(to)
, amount_(amount)
{
std::cout << "Transaction::Transaction: " << from_ << " " << to_ << " " << amount_ << std::endl;
transactionId_ = calculateTransactionId();
}
Transaction Transaction::createSystemTransaction(const std::string& to, double amount) {
Transaction tx("SYSTEM", to, amount);
// 系统交易使用特殊的签名机制
tx.signature_ = "SYSTEM_SIGNATURE_" + std::to_string(amount) + "_" + to;
tx.addOutput(TransactionOutput(amount, to));
return tx;
}
void Transaction::addInput(const TransactionInput& input) {
std::cout << "Transaction::addInput: " << transactionId_ << " input: " << input.getTxId() << " " << input.getOutputIndex() << std::endl;
inputs_.push_back(input);
transactionId_ = calculateTransactionId();
}
void Transaction::addOutput(const TransactionOutput& output) {
std::cout << "Transaction::addOutput: " << transactionId_ << " output: " << output.getAmount() << " " << output.getOwner() << std::endl;
outputs_.push_back(output);
transactionId_ = calculateTransactionId();
}
bool Transaction::verifySignature() const {
// 系统交易使用特殊的验证逻辑
if (from_ == "SYSTEM") {
// 验证系统交易的签名格式
std::string expectedSignature = "SYSTEM_SIGNATURE_" + std::to_string(amount_) + "_" + to_;
return signature_ == expectedSignature;
}
// 普通交易使用标准的签名验证
return Wallet::verify(transactionId_, signature_, from_);
}
std::string Transaction::calculateTransactionId() const {
std::stringstream ss;
ss << from_ << to_ << amount_;
// 添加输入和输出的信息
for (const auto& input : inputs_) {
ss << input.getTxId() << input.getOutputIndex();
}
for (const auto& output : outputs_) {
ss << output.getAmount() << output.getOwner();
}
// 计算SHA256哈希
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, ss.str().c_str(), ss.str().size());
SHA256_Final(hash, &sha256);
std::stringstream hashStream;
for(int i = 0; i < SHA256_DIGEST_LENGTH; i++) {
hashStream << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
}
return hashStream.str();
}
// 检查交易是否有效(包括余额检查)
bool Transaction::isValid() const {
return !from_.empty() && !to_.empty() && amount_ > 0 && !signature_.empty();
}
std::string Transaction::toJson() const {
json j;
j["from"] = from_;
j["to"] = to_;
j["amount"] = amount_;
j["timestamp"] = timestamp_;
j["transactionId"] = transactionId_;
j["signature"] = signature_;
// 添加输入
json inputs = json::array();
for (const auto& input : inputs_) {
json inputJson;
inputJson["txId"] = input.getTxId();
inputJson["outputIndex"] = input.getOutputIndex();
inputJson["signature"] = input.getSignature();
inputs.push_back(inputJson);
}
j["inputs"] = inputs;
// 添加输出
json outputs = json::array();
for (const auto& output : outputs_) {
json outputJson;
outputJson["amount"] = output.getAmount();
outputJson["owner"] = output.getOwner();
outputs.push_back(outputJson);
}
j["outputs"] = outputs;
return j.dump();
}
Transaction::Transaction(const json& json) {
from_ = json["from"];
to_ = json["to"];
amount_ = json["amount"];
timestamp_ = json["timestamp"];
transactionId_ = json["transactionId"];
signature_ = json["signature"];
// 解析输入
if (json.contains("inputs")) {
for (const auto& inputJson : json["inputs"]) {
TransactionInput input(
inputJson["txId"],
inputJson["outputIndex"],
inputJson["signature"]
);
inputs_.push_back(input);
}
}
// 解析输出
if (json.contains("outputs")) {
for (const auto& outputJson : json["outputs"]) {
TransactionOutput output(
outputJson["amount"],
outputJson["owner"]
);
outputs_.push_back(output);
}
}
}