-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy patheoskeys.c
82 lines (67 loc) · 1.66 KB
/
eoskeys.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
/*
** (c)maester.xyz
** EOSIO keys and address CLang lib
** No dependence except clib. Can be used on linux, windows, Android or iOS
*/
#include <stdlib.h>
#include <stdio.h>
#include "ecdsa.h"
#include "bignum256.h"
#include "hash.h"
#include "sha256.h"
#include "ripemd160.h"
#include "base58.h"
void eosPrivate2Address(const char privIn[64], char * privStr, char *addrStr)
{
HashState hs;
uint8_t priv[32];
uint8_t x[32];
uint8_t y[32];
uint8_t pub33[33];
uint8_t hash[32];
uint8_t pub37[37];
uint8_t pub37Rev[37];
uint8_t priv37[37];
uint8_t priv37Rev[37];
int i;
bigFromHexString(privIn, priv);
ecPubkey(priv, x, y);
if(y[0] % 2)
pub33[32] = 0x03;
else
pub33[32] = 0x02;
memcpy(pub33, x, 32);
ripemd160Begin(&hs);
for(i=0; i<33; i++)
ripemd160WriteByte(&hs, pub33[32-i]);
ripemd160Finish(&hs);
writeHashToByteArray(hash, &hs, true);
for(i=0; i<4; i++)
{
pub37[i] = hash[3-i];
}
memcpy(pub37+4, pub33, 33);
for(i=36; i>=0; i--)
pub37Rev[i] = pub37[36-i];
size_t len = 256;
b58enc(addrStr, &len, pub37Rev, 37);
addrStr[len] = 0;
//////////////////////////////////////////////
// Priv-WIF
memcpy(priv37+4, priv, 32);
priv37[36] = 0x80;
sha256Begin(&hs);
for(i=0; i<33; i++)
sha256WriteByte(&hs, priv37[36-i]);
sha256FinishDouble(&hs);
writeHashToByteArray(hash, &hs, true);
for(i=0; i<4; i++)
{
priv37[i] = hash[3-i];
}
for(i=36; i>=0; i--)
priv37Rev[i] = priv37[36-i];
len = 256;
b58enc(privStr, &len, priv37Rev, 37);
privStr[len] = 0;
}