-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnative.cc
More file actions
226 lines (176 loc) · 5.39 KB
/
Copy pathnative.cc
File metadata and controls
226 lines (176 loc) · 5.39 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <stdio.h>
#include <v8.h>
#include <node.h>
#include <node_buffer.h>
#include <openssl/bn.h>
#include <openssl/buffer.h>
#include <openssl/ecdsa.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/sha.h>
#include <openssl/ripemd.h>
using namespace std;
using namespace v8;
using namespace node;
static Handle<Value> VException(const char *msg) {
HandleScope scope;
return ThrowException(Exception::Error(String::New(msg)));
}
static Handle<Value>
new_keypair (const Arguments& args)
{
HandleScope scope;
EC_KEY* pkey;
// Generate
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
if (pkey == NULL) {
return VException("Error from EC_KEY_new_by_curve_name");
}
if (!EC_KEY_generate_key(pkey)) {
return VException("Error from EC_KEY_generate_key");
}
// Export private
unsigned int priv_size = i2d_ECPrivateKey(pkey, NULL);
if (!priv_size) {
return VException("Error from i2d_ECPrivateKey(pkey, NULL)");
}
unsigned char *priv = (unsigned char *)malloc(priv_size);
if (i2d_ECPrivateKey(pkey, &priv) != priv_size) {
return VException("Error from i2d_ECPrivateKey(pkey, &priv)");
}
// Export public
unsigned int pub_size = i2o_ECPublicKey(pkey, NULL);
if (!pub_size) {
return VException("Error from i2o_ECPublicKey(pkey, NULL)");
}
unsigned char *pub = (unsigned char *)malloc(pub_size);
if (i2o_ECPublicKey(pkey, &pub) != pub_size) {
return VException("Error from i2o_ECPublicKey(pkey, &pub)");
}
// Return [priv_buf, pub_buf]
Local<Array> a = Array::New(2);
Buffer *priv_buf = Buffer::New(priv_size);
memcpy(Buffer::Data(priv_buf), priv, priv_size);
a->Set(Integer::New(0), priv_buf->handle_);
Buffer *pub_buf = Buffer::New(pub_size);
memcpy(Buffer::Data(pub_buf), pub, pub_size);
a->Set(Integer::New(1), pub_buf->handle_);
return scope.Close(a);
}
static Handle<Value>
pubkey_to_address256 (const Arguments& args)
{
HandleScope scope;
if (args.Length() != 1) {
return VException("One argument expected: pubkey Buffer");
}
if (!Buffer::HasInstance(args[0])) {
return VException("One argument expected: pubkey Buffer");
}
v8::Handle<v8::Object> pub_buf = args[0]->ToObject();
unsigned char *pub_data = (unsigned char *) Buffer::Data(pub_buf);
// sha256(pubkey)
unsigned char hash1[SHA256_DIGEST_LENGTH];
SHA256_CTX c;
SHA256_Init(&c);
SHA256_Update(&c, pub_data, Buffer::Length(pub_buf));
SHA256_Final(hash1, &c);
// ripemd160(sha256(pubkey))
unsigned char hash2[RIPEMD160_DIGEST_LENGTH];
RIPEMD160_CTX c2;
RIPEMD160_Init(&c2);
RIPEMD160_Update(&c2, hash1, SHA256_DIGEST_LENGTH);
RIPEMD160_Final(hash2, &c2);
// x = '\x00' + ripemd160(sha256(pubkey))
// LATER: make the version an optional argument
unsigned char address256[1 + RIPEMD160_DIGEST_LENGTH + 4];
address256[0] = 0;
memcpy(address256 + 1, hash2, RIPEMD160_DIGEST_LENGTH);
// sha256(x)
unsigned char hash3[SHA256_DIGEST_LENGTH];
SHA256_CTX c3;
SHA256_Init(&c3);
SHA256_Update(&c3, address256, 1 + RIPEMD160_DIGEST_LENGTH);
SHA256_Final(hash3, &c3);
// address256 = (x + sha256(x)[:4])
memcpy(
address256 + (1 + RIPEMD160_DIGEST_LENGTH),
hash3,
4);
Buffer *address256_buf = Buffer::New(1 + RIPEMD160_DIGEST_LENGTH + 4);
memcpy(Buffer::Data(address256_buf), address256, 1 + RIPEMD160_DIGEST_LENGTH + 4);
return scope.Close(address256_buf->handle_);
}
static const char* BASE58_ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static Handle<Value>
base58_encode (const Arguments& args)
{
HandleScope scope;
if (args.Length() != 1) {
return VException("One argument expected: a Buffer");
}
if (!Buffer::HasInstance(args[0])) {
return VException("One argument expected: a Buffer");
}
v8::Handle<v8::Object> buf = args[0]->ToObject();
unsigned char *buf_data = (unsigned char *) Buffer::Data(buf);
int buf_length = Buffer::Length(buf);
BN_CTX *ctx = BN_CTX_new();
BIGNUM *bn = BN_bin2bn(buf_data, buf_length, NULL);
BIGNUM *bn58 = BN_new();
BN_set_word(bn58, 58);
BIGNUM *bn0 = BN_new();
BN_set_word(bn0, 0);
BIGNUM *dv = BN_new();
BIGNUM *rem = BN_new();
// TODO: compute safe length
char *str = new char[100];
unsigned int c;
int i, j, j2;
i = 0;
while (BN_cmp(bn, bn0) > 0) {
if (!BN_div(dv, rem, bn, bn58, ctx)) {
return VException("BN_div failed");
}
bn = dv;
c = BN_get_word(rem);
str[i] = BASE58_ALPHABET[c];
i++;
}
// Leading zeros
for (j = 0; j < buf_length; j++) {
if (buf_data[j] != 0) {
break;
}
str[i] = BASE58_ALPHABET[0];
i++;
}
// Terminator
str[i] = 0;
// Reverse string
int numSwaps = (i / 2);
char tmp;
for (j = 0; j < numSwaps; j++) {
j2 = i - 1 - j;
tmp = str[j];
str[j] = str[j2];
str[j2] = tmp;
}
BN_free(bn);
BN_free(bn58);
BN_free(bn0);
Local<String> ret = String::New(str);
delete [] str;
return scope.Close(ret);
}
extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
target->Set(String::New("new_keypair"), FunctionTemplate::New(new_keypair)->GetFunction());
target->Set(String::New("pubkey_to_address256"), FunctionTemplate::New(pubkey_to_address256)->GetFunction());
target->Set(String::New("base58_encode"), FunctionTemplate::New(base58_encode)->GetFunction());
}