forked from anda0109/blockchain-privatekey-to-address
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathetherkeys.c
51 lines (40 loc) · 950 Bytes
/
etherkeys.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
#include "etherkeys.h"
#include "keccak.h"
#include "ecdsa.h"
#include "bignum256.h"
#include <stdio.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);
keccak(pub64, 64, outBuff);
#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
snprintf(addrStr, 3, "0x");
for (i = 0; i<20; i++)
snprintf(addrStr + 2 + i * 2, 3, "%02x", outBuff[12 + i]);
}