-
Notifications
You must be signed in to change notification settings - Fork 0
/
BTG_Wallet.h
46 lines (39 loc) · 1.35 KB
/
BTG_Wallet.h
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
#ifndef __BTG_WALLET_H_
#define __BTG_WALLET_H_
void generate_btg_address(const uint8_t *private_key, char *btg_address, size_t btg_address_len) {
if (!is_private_key_valid(private_key)) {
Serial.println("Error: Private key is not Vaild.");
return;
}
uint8_t pubkey[33];
uint8_t sha256_hash[32];
uint8_t ripemd160_hash[20];
uint8_t versioned_payload[21];
// 1. 生成压缩公钥
if (generate_compressed_pubkey(private_key, pubkey) != 0) {
Serial.println("Error: Generated Compressed PublicKey failed.");
return;
}
if (pubkey[0] == 0x00) {
Serial.println("Error: Public Key is zero.");
return;
}
size_t pubkey_len = sizeof(pubkey);
mbedtls_sha256(pubkey, pubkey_len, sha256_hash, 0);
local_ripemd160(sha256_hash, 32, ripemd160_hash);
// 4. 添加版本字节(0x00 for Bitcoin)
versioned_payload[0] = 0x26;
memcpy(versioned_payload + 1, ripemd160_hash, 20);
if (sizeof(versioned_payload) > 21) {
Serial.println("Error: Payload is over memory.");
return;
}
if (base58checkEncode(versioned_payload, sizeof(versioned_payload), btg_address, &btg_address_len)) {
btg_address[btg_address_len] = '\0'; // 确保字符串终止
//Serial.print("Bitcoin Address (Base58): ");
//Serial.println(btc_address);
} else {
Serial.println("Error: BTG Base58 check encoding failed.");
}
}
#endif