-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathetherkeys.c
More file actions
59 lines (45 loc) · 1.17 KB
/
etherkeys.c
File metadata and controls
59 lines (45 loc) · 1.17 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
/*
** (C)ELLIPAL, Tong Chen
**
** Ethereum private key to address
** - CLang lib
** - No dependence. Can be used on linux, windows, Android and iOS
*/
#include "ecdsa.h"
#include "bignum256.h"
#include "keccak.h"
#include "etherkeys.h"
void etherPrivate2Address(const char privStr[BN256_STR_LEN], char addrStr[ETH_ADD_STR_LEN])
{
int i;
uint8_t temp;
uint8_t priv[32];
uint8_t x[32];
uint8_t y[32];
uint8_t pub64[64];
uint8_t outBuff[32];
bigFromHexString(privStr, priv);
ecPubkey(priv, x, y);
memcpy(pub64, y, 32);
memcpy(pub64+32, x, 32);
for(i=0; i<32; i++)
{
temp = pub64[i];
pub64[i] = pub64[63-i];
pub64[63-i] = temp;
}
keccak256(outBuff, 32, pub64, 64);
#ifdef __DEBUG
printf("PUB1: %s\n", pubStr128);
printf("PUB: ");
for(i=0; i<64; i++)
printf("%02x", pub64[63-i]);
printf("\n\nKeccak: \n");
for(i=0; i<32; i++)
printf("%02x", outBuff[i]);
printf("\n\n");
#endif
sprintf(addrStr, "0x");
for(i=0; i<20; i++)
sprintf(addrStr+2+i*2, "%02x", outBuff[12+i]);
}