forked from lurcury/c-lurcurysign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.c
152 lines (131 loc) · 3.82 KB
/
go.c
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
#include "base58.h"
#include "common.h"
#include "ec.h"
//#include "hex.c"
struct KeyData {
char* privateKey;
char* uncompressed_publicKey;
char* uncompressed_address;
char* compressed_publicKey;
char* compressed_address;
};
char* toHex(uint8_t vpriv_bytes[],int size){
char buffer [100];
static char pubKey [2048];
char target[2048] = "";
for(int i =0;i < size; i++){
sprintf (buffer, "%02X", vpriv_bytes[i]);
strcat(target, buffer);
}
strcpy(pubKey,target);
return pubKey;
}
char* publickeytoaddrsss(uint8_t pub_bytes[],size_t size){
uint8_t address_bytes[210];
char *address;
static char addressSta [2048];
const char address_exp[] = "mqMi3XYqsPvBWtrJTk8euPWDVmFTZ5jHuK";
address_bytes[0] = 0x00;
bbp_hash160(address_bytes+1, pub_bytes, size);
address = bbp_base58check(address_bytes, 21);
printf("address : %s\n", address);
strcpy(addressSta, address);
free(address);
return addressSta;
}
struct KeyData keyPublish(uint8_t privateKeyV[]) {
struct KeyData r;
char privateKey[2048];
char uncompressed_publicKey[2048];
char uncompressed_address[2048];
char compressed_publicKey[2048];
char compressed_address[2048];
strcpy(privateKey, toHex(privateKeyV,32));
r.privateKey = privateKey;
EC_KEY *key;
uint8_t priv[32];
uint8_t *pub;
const BIGNUM *priv_bn;
point_conversion_form_t conv_forms[] = {
POINT_CONVERSION_UNCOMPRESSED,
POINT_CONVERSION_COMPRESSED
};
const char *conv_forms_desc[] = {
"uncompressed",
"compressed"
};
int i;
key = bbp_ec_new_keypair(privateKeyV);
if (!key) {
puts("Unable to create keypair");
//return -1;
}
bbp_print_hex("privateKey:", privateKeyV, sizeof(priv));
priv_bn = EC_KEY_get0_private_key(key);
if (!priv_bn) {
puts("Unable to decode private key");
//return -1;
}
BN_bn2bin(priv_bn, priv);
for (i = 0; i < sizeof(conv_forms) / sizeof(point_conversion_form_t); ++i) {
size_t pub_len;
uint8_t *pub_copy;
EC_KEY_set_conv_form(key, conv_forms[i]);
pub_len = i2o_ECPublicKey(key, NULL);
pub = calloc(pub_len, sizeof(uint8_t));
pub_copy = pub;
if (i2o_ECPublicKey(key, &pub_copy) != pub_len) {
puts("Unable to decode public key");
//return -1;
}
if(i==0){
strcpy(uncompressed_publicKey, toHex(pub,pub_len));
r.uncompressed_publicKey = uncompressed_publicKey;
strcpy(uncompressed_address, publickeytoaddrsss(pub,pub_len));
r.uncompressed_address = uncompressed_address;
}
else{
strcpy(compressed_publicKey, toHex(pub,pub_len));
r.compressed_publicKey = compressed_publicKey;
strcpy(compressed_address, publickeytoaddrsss(pub,pub_len));
r.compressed_address = compressed_address;
}
free(pub);
}
EC_KEY_free(key);
return r;
}
struct KeyData test(){
struct KeyData result;
uint8_t priv_bytes[32] = {
0x16, 0x26, 0x07, 0x83, 0xe4, 0x0b, 0x16, 0x73,
0x16, 0x73, 0x62, 0x2a, 0xc8, 0xa5, 0xb0, 0x45,
0xfc, 0x3e, 0xa4, 0xaf, 0x70, 0xf7, 0x27, 0xf3,
0xf9, 0xe9, 0x2b, 0xdd, 0x3a, 0x1d, 0x01, 0x09
};
// uint8_t priv_bytes[32] = {0x16260783e40b16731673622ac8a5b045fc3ea4af70f727f3f9e92bdd3a1d0109};
result = keyPublish(priv_bytes);
return result;
}
int main(){
struct KeyData result;
result = test();
printf("\n%s",result.privateKey);
printf("\n%s",result.uncompressed_address);
printf("\n%s",result.compressed_address);
printf("\n%s",result.compressed_publicKey);
}
/*
struct KeyData go(){
struct KeyData r;
r.privateKey = "abc";
printf("%s",r.privateKey);
return r;
}
int main(){
struct KeyData r;
go();
//printf("%s",r.privateKey);
return 0;
}
*/