-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwallet.cpp
More file actions
309 lines (263 loc) · 7.78 KB
/
wallet.cpp
File metadata and controls
309 lines (263 loc) · 7.78 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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#include "wallet.h"
#include "transaction.h"
#include <openssl/sha.h>
#include <openssl/ecdsa.h>
#include <openssl/obj_mac.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <sstream>
#include <iomanip>
#include <iostream>
Wallet::Wallet() : keyPair_(nullptr) {
// 初始化 OpenSSL
OpenSSL_add_all_algorithms();
generateKeyPair();
}
Wallet::~Wallet() {
if (keyPair_) {
EC_KEY_free(keyPair_);
}
// 清理 OpenSSL
EVP_cleanup();
}
void Wallet::generateKeyPair() {
// 创建新的 EC_KEY
keyPair_ = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!keyPair_) {
throw std::runtime_error("Failed to create EC_KEY");
}
// 生成密钥对
if (!EC_KEY_generate_key(keyPair_)) {
EC_KEY_free(keyPair_);
keyPair_ = nullptr;
throw std::runtime_error("Failed to generate key pair");
}
}
std::string Wallet::getPublicKey() const {
if (!keyPair_) {
throw std::runtime_error("No key pair generated");
}
const EC_POINT* pub = EC_KEY_get0_public_key(keyPair_);
const EC_GROUP* group = EC_KEY_get0_group(keyPair_);
BIGNUM* x = BN_new();
BIGNUM* y = BN_new();
if (!EC_POINT_get_affine_coordinates_GFp(group, pub, x, y, nullptr)) {
BN_free(x);
BN_free(y);
throw std::runtime_error("Failed to get public key coordinates");
}
std::string result = keyToHex(x) + ":" + keyToHex(y);
BN_free(x);
BN_free(y);
return result;
}
std::string Wallet::getPrivateKey() const {
if (!keyPair_) {
throw std::runtime_error("No key pair generated");
}
const BIGNUM* priv = EC_KEY_get0_private_key(keyPair_);
return keyToHex(priv);
}
std::string Wallet::sign(const std::string& data) const {
if (!keyPair_) {
throw std::runtime_error("No key pair generated");
}
// 计算数据的 SHA256 哈希
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, data.c_str(), data.size());
SHA256_Final(hash, &sha256);
// 使用私钥签名
ECDSA_SIG* sig = ECDSA_do_sign(hash, SHA256_DIGEST_LENGTH, keyPair_);
if (!sig) {
throw std::runtime_error("Failed to sign data");
}
// 获取签名值
const BIGNUM* r = nullptr;
const BIGNUM* s = nullptr;
ECDSA_SIG_get0(sig, &r, &s);
// 将签名转换为十六进制字符串
std::string result = keyToHex(r) + ":" + keyToHex(s);
// 清理
ECDSA_SIG_free(sig);
return result;
}
std::string Wallet::sign(const std::string& data, const std::string& privateKey) {
// 创建新的 EC_KEY
EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!key) {
throw std::runtime_error("Failed to create EC_KEY");
}
// 从十六进制字符串设置私钥
BIGNUM* priv = hexToKey(privateKey);
if (!priv) {
EC_KEY_free(key);
throw std::runtime_error("Failed to convert private key");
}
if (!EC_KEY_set_private_key(key, priv)) {
BN_free(priv);
EC_KEY_free(key);
throw std::runtime_error("Failed to set private key");
}
// 计算数据的 SHA256 哈希
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, data.c_str(), data.size());
SHA256_Final(hash, &sha256);
// 使用私钥签名
ECDSA_SIG* sig = ECDSA_do_sign(hash, SHA256_DIGEST_LENGTH, key);
if (!sig) {
BN_free(priv);
EC_KEY_free(key);
throw std::runtime_error("Failed to sign data");
}
// 获取签名值
const BIGNUM* r = nullptr;
const BIGNUM* s = nullptr;
ECDSA_SIG_get0(sig, &r, &s);
// 将签名转换为十六进制字符串
std::string result = keyToHex(r) + ":" + keyToHex(s);
// 清理
ECDSA_SIG_free(sig);
BN_free(priv);
EC_KEY_free(key);
return result;
}
bool Wallet::verify(const std::string& data,
const std::string& signature,
const std::string& publicKey) {
// std::cout << "Verifying signature with public key: " << publicKey << std::endl;
// 解析公钥
size_t pos = publicKey.find(':');
if (pos == std::string::npos) {
std::cout << "Invalid public key format" << std::endl;
return false;
}
std::string xStr = publicKey.substr(0, pos);
std::string yStr = publicKey.substr(pos + 1);
// 创建 EC_KEY
EC_KEY* key = EC_KEY_new_by_curve_name(NID_secp256k1);
if (!key) {
std::cout << "Failed to create EC_KEY" << std::endl;
return false;
}
// 设置公钥
BIGNUM* x = hexToKey(xStr);
BIGNUM* y = hexToKey(yStr);
if (!x || !y) {
std::cout << "Failed to convert public key coordinates" << std::endl;
EC_KEY_free(key);
return false;
}
EC_POINT* pub = EC_POINT_new(EC_KEY_get0_group(key));
if (!pub) {
std::cout << "Failed to set public key coordinates" << std::endl;
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return false;
}
if (!EC_POINT_set_affine_coordinates_GFp(EC_KEY_get0_group(key), pub, x, y, nullptr)) {
std::cout << "Failed to set affine coordinates" << std::endl;
EC_POINT_free(pub);
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return false;
}
if (!EC_KEY_set_public_key(key, pub)) {
std::cout << "Failed to set public key" << std::endl;
EC_POINT_free(pub);
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return false;
}
// 解析签名
pos = signature.find(':');
if (pos == std::string::npos) {
std::cout << "Failed to find signature" << std::endl;
EC_POINT_free(pub);
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return false;
}
std::string rStr = signature.substr(0, pos);
std::string sStr = signature.substr(pos + 1);
BIGNUM* r = hexToKey(rStr);
BIGNUM* s = hexToKey(sStr);
if (!r || !s) {
std::cout << "Failed to parse signature" << std::endl;
EC_POINT_free(pub);
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return false;
}
// 创建签名对象
ECDSA_SIG* sig = ECDSA_SIG_new();
if (!sig) {
std::cout << "Failed to create signature" << std::endl;
BN_free(r);
BN_free(s);
EC_POINT_free(pub);
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return false;
}
ECDSA_SIG_set0(sig, r, s);
// 计算数据的 SHA256 哈希
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
SHA256_Init(&sha256);
SHA256_Update(&sha256, data.c_str(), data.size());
SHA256_Final(hash, &sha256);
// 验证签名
int result = ECDSA_do_verify(hash, SHA256_DIGEST_LENGTH, sig, key);
// 清理
ECDSA_SIG_free(sig);
EC_POINT_free(pub);
BN_free(x);
BN_free(y);
EC_KEY_free(key);
return result == 1;
}
std::string Wallet::keyToHex(const BIGNUM* key) {
char* hex = BN_bn2hex(key);
if (!hex) {
throw std::runtime_error("Failed to convert key to hex");
}
std::string result(hex);
OPENSSL_free(hex);
return result;
}
BIGNUM* Wallet::hexToKey(const std::string& hex) {
BIGNUM* key = BN_new();
if (!key) {
return nullptr;
}
if (!BN_hex2bn(&key, hex.c_str())) {
BN_free(key);
return nullptr;
}
return key;
}
void Wallet::processTransaction(const Transaction& tx) {
if (tx.getFrom() == getPublicKey()) {
balance_ -= tx.getAmount();
}
if (tx.getTo() == getPublicKey()) {
balance_ += tx.getAmount();
}
}
// bool Wallet::deductBalance(double amount) {
// if (balance_ >= amount) {
// balance_ -= amount;
// return true;
// }
// return false;
// }