-
Notifications
You must be signed in to change notification settings - Fork 0
/
HexWallet.ino
233 lines (191 loc) · 6.88 KB
/
HexWallet.ino
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
#include <Arduino.h>
#include <SPIFFS.h>
#include <ArduinoJson.h>
#include <mbedtls/sha256.h>
#include <mbedtls/sha512.h>
#include <mbedtls/md.h>
#include <mbedtls/pkcs5.h>
#include <mbedtls/ecdsa.h>
#include <mbedtls/ecp.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/entropy.h>
#include "esp_heap_caps.h"
#include "esp_random.h"
#include "base58.h"
#include "local_segwit.h"
#include "local_ripemd160.h"
#include "word_list.h"
#include "keccak256.h"
#include "Utils.h"
#include "BIP39.h"
#include "BIP32.h"
#include "BIP44.h"
void setup() {
Serial.begin(115200);
if (psramFound()) {
Serial.println("PSRAM is available!");
} else {
Serial.println("PSRAM is not available.");
}
const char* my_mnemonic = generate_mnemonic();
const char* passphrase = "";
const char* hmac_keys = "Bitcoin seed";
bool is_bip84 = true;
Serial.print("Mnemonic: ");
Serial.println(my_mnemonic);
uint8_t my_seed[SEED_SIZE];
uint8_t master_private_key[KEY_SIZE];
uint8_t master_chain_code[KEY_SIZE];
mnemonic_to_seed(my_mnemonic, passphrase, my_seed);
const char* hex_char = address_hex(my_seed, SEED_SIZE);
Serial.print("My Seed: ");
Serial.println(hex_char);
Serial.println();
generate_master_private_key_and_chaincode(my_seed, hmac_keys, master_private_key, master_chain_code);
printf("Master Private Key: ");
for (int i = 0; i < KEY_SIZE; i++) {
printf("%02x", master_private_key[i]);
}
printf("\n");
printf("Master Chain Code: ");
for (int i = 0; i < CHAIN_CODE_SIZE; i++) {
printf("%02x", master_chain_code[i]);
}
printf("\n\n");
char xprv[EXTENDED_KEY_SIZE];
char xpub[EXTENDED_KEY_SIZE];
char zprv[EXTENDED_KEY_SIZE];
char zpub[EXTENDED_KEY_SIZE];
size_t xprv_len = EXTENDED_KEY_SIZE;
generate_xprv(0, 0, master_chain_code, master_private_key, xprv, &xprv_len);
printf("Extended Private Key (xprv): %s\n", xprv);
uint8_t public_key[33];
generate_public_key_from_private(master_private_key, public_key);
size_t xpub_len = EXTENDED_KEY_SIZE;
generate_xpub(0, 0, master_chain_code, public_key, xpub, &xpub_len);
printf("Extended Public Key (xpub): %s\n", xpub);
size_t zprv_len = EXTENDED_KEY_SIZE;
generate_zprv(0, 0, master_chain_code, master_private_key, zprv, &zprv_len);
printf("Extended Private Key (zprv): %s\n", zprv);
uint8_t zprv_public_key[33];
generate_public_key_from_private(master_private_key, zprv_public_key);
size_t zpub_len = EXTENDED_KEY_SIZE;
generate_zpub(0, 0, master_chain_code, public_key, zpub, &zpub_len);
printf("Extended Public Key (zpub): %s\n", zpub);
// 存储解析的结果
uint8_t version[4];
uint8_t depth;
uint32_t fingerprint, child_number;
uint8_t local_chain_code[CHAIN_CODE_SIZE];
uint8_t local_private_key[33];
uint8_t local_public_key[33];
uint8_t bip84_local_chain_code[CHAIN_CODE_SIZE];
uint8_t bip84_local_private_key[33];
uint8_t bip84_local_public_key[33];
// 解析扩展私钥 xprv
if (bip32_parse_extended_key(xprv, version, &depth, &fingerprint, &child_number, local_chain_code, local_private_key)) {
printf("\n成功解析 BIP32 Root Key 扩展私钥 (xprv)\n");
printf("版本字节: %02x%02x%02x%02x\n", version[0], version[1], version[2], version[3]);
printf("深度: %d\n", depth);
printf("父密钥指纹: %08x\n", fingerprint);
printf("序号: %u\n", child_number);
printf("私钥: ");
for (int i = 0; i < 33; i++) {
printf("%02x", local_private_key[i]);
}
printf("\n链码: ");
for (int i = 0; i < CHAIN_CODE_SIZE; i++) {
printf("%02x", local_chain_code[i]);
}
printf("\n\n");
} else {
printf("解析 xprv 失败\n");
}
// 解析扩展公钥 xpub
if (bip32_parse_extended_key(xpub, version, &depth, &fingerprint, &child_number, local_chain_code, local_public_key)) {
printf("\n成功解析 BIP32 Root Key 扩展公钥 (xpub)\n");
printf("版本字节: %02x%02x%02x%02x\n", version[0], version[1], version[2], version[3]);
printf("深度: %d\n", depth);
printf("父公钥指纹: %08x\n", fingerprint);
printf("序号: %u\n", child_number);
printf("公钥: ");
for (int i = 0; i < 33; i++) {
printf("%02x", local_public_key[i]);
}
printf("\n链码: ");
for (int i = 0; i < CHAIN_CODE_SIZE; i++) {
printf("%02x", local_chain_code[i]);
}
printf("\n\n");
} else {
printf("解析 xpub 失败\n");
}
if (bip32_parse_extended_key(zprv, version, &depth, &fingerprint, &child_number, bip84_local_chain_code, bip84_local_private_key)) {
printf("\n成功解析 BIP32 Root Key 扩展私钥 (zprv)\n");
printf("版本字节: %02x%02x%02x%02x\n", version[0], version[1], version[2], version[3]);
printf("深度: %d\n", depth);
printf("父密钥指纹: %08x\n", fingerprint);
printf("序号: %u\n", child_number);
printf("私钥: ");
for (int i = 0; i < 33; i++) {
printf("%02x", bip84_local_private_key[i]);
}
printf("\n链码: ");
for (int i = 0; i < CHAIN_CODE_SIZE; i++) {
printf("%02x", bip84_local_chain_code[i]);
}
printf("\n\n");
} else {
printf("解析 zprv 失败\n");
}
// 解析扩展公钥 zpub
if (bip32_parse_extended_key(zpub, version, &depth, &fingerprint, &child_number, bip84_local_chain_code, bip84_local_public_key)) {
printf("\n成功解析 BIP32 Root Key 扩展公钥 (zpub)\n");
printf("版本字节: %02x%02x%02x%02x\n", version[0], version[1], version[2], version[3]);
printf("深度: %d\n", depth);
printf("父公钥指纹: %08x\n", fingerprint);
printf("序号: %u\n", child_number);
printf("公钥: ");
for (int i = 0; i < 33; i++) {
printf("%02x", bip84_local_public_key[i]);
}
printf("\n链码: ");
for (int i = 0; i < CHAIN_CODE_SIZE; i++) {
printf("%02x", bip84_local_chain_code[i]);
}
printf("\n\n");
} else {
printf("解析 zpub 失败\n");
}
if (is_bip84) {
generate_bip44_addresses(bip84_local_public_key, bip84_local_private_key, bip84_local_chain_code, 1, is_bip84);
} else {
generate_bip44_addresses(local_public_key, local_private_key, local_chain_code, 1, !is_bip84);
}
/*
uint8_t child_private_key[KEY_SIZE];
uint8_t child_chain_code[CHAIN_CODE_SIZE];
uint32_t child_index = 0; // 子地址索引
int result = CKDpriv(master_private_key, master_chain_code, child_index, child_private_key, child_chain_code);
if (result) {
// 输出生成的子私钥和链码
print_hex(child_private_key, KEY_SIZE);
print_hex(child_chain_code, CHAIN_CODE_SIZE);
} else {
printf("子密钥生成失败。\n");
}
*/
/*
char WIFOutput[54];
privateKeyToWIF(master_private_key, WIFOutput);
Serial.print("WIF Output: ");
Serial.println(WIFOutput);
char my_address[35];
generate_btc_address(master_private_key, my_address);
*/
//char address[35];
//generate_btc_address(master_private_key, address);
//generate_bip44_address(my_mnemonic, passphrase);
}
void loop() {
}