-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain-wallet-sqlite.c
166 lines (111 loc) · 3.27 KB
/
brain-wallet-sqlite.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <openssl/bn.h>
#include <openssl/ec.h>
#include <openssl/obj_mac.h>
#include <sqlite3.h>
sqlite3 *db; char *zErrMsg = 0; char sql[512];
char pwbuf[128], ecprot1[128], ecprot2[128], pbuf[1024]; EC_KEY *pkey; int privtype, addrtype;
char *line;
unsigned long i;
static int callback(void *data, int argc, char **argv, char **azColName) {
printf("input: \"%s\" ", line);
vg_encode_privkey(pkey, privtype, ecprot2);
printf("%s %s\n", ecprot1, ecprot2);
sqlite3_close(db);
exit(1);
}
int EC_KEY_regenerate_key(EC_KEY *eckey, BIGNUM *priv_key) {
int ok = 0;
BN_CTX *ctx = NULL;
EC_POINT *pub_key = NULL;
if (!eckey) return 0;
const EC_GROUP *group = EC_KEY_get0_group(eckey);
if ((ctx = BN_CTX_new()) == NULL)
goto err;
pub_key = EC_POINT_new(group);
if (pub_key == NULL)
goto err;
if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx))
goto err;
EC_KEY_set_private_key(eckey,priv_key);
EC_KEY_set_public_key(eckey,pub_key);
ok = 1;
err:
if (pub_key)
EC_POINT_free(pub_key);
if (ctx != NULL)
BN_CTX_free(ctx);
return(ok);
}
#define MINIMUM_CAPACITY 16
size_t read_line(char **buffer, size_t *capacity) {
char *buf = *buffer;
size_t cap = *capacity, pos = 0;
if (cap < MINIMUM_CAPACITY) { cap = MINIMUM_CAPACITY; }
for (;;) {
buf = realloc(buf, cap);
if (buf == NULL) { return pos; }
*buffer = buf;
*capacity = cap;
if (fgets(buf + pos, cap - pos, stdin) == NULL) {
break;
}
pos += strcspn(buf + pos, "\n");
if (buf[pos] == '\n') {
break;
}
cap *= 2;
}
return pos;
}
int main(int argc, char** argv) {
char dbfile[1024];
size_t size = 0;
unsigned char hash[SHA256_DIGEST_LENGTH];
SHA256_CTX sha256;
sprintf(dbfile, "%s/address.sqlite3", getenv("HOME"));
if( sqlite3_open(dbfile, &db) ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
exit(0);
}
OpenSSL_add_all_algorithms();
pkey = EC_KEY_new_by_curve_name(NID_secp256k1);
addrtype = 0; privtype = 128;
size_t end;
for (end = read_line(&line, &size);
line[end] == '\n'; end = read_line(&line, &size)) {
line[end] = '\0';
SHA256_Init(&sha256);
SHA256_Update(&sha256, line, strlen(line));
SHA256_Final(hash, &sha256);
BIGNUM *bn = BN_bin2bn(&hash[0], SHA256_DIGEST_LENGTH, BN_new());
// assert(bn);
EC_KEY_regenerate_key(pkey, bn);
BN_free(bn);
vg_encode_address(EC_KEY_get0_public_key(pkey),
EC_KEY_get0_group(pkey),
addrtype, ecprot1);
sprintf(sql, "SELECT * from address where id = '%s'", ecprot1);
if (i++ % 10000 == 0) {
fprintf(stderr, "%s %s\n", ecprot1, line);
}
if (sqlite3_exec(db, sql, callback, 0, &zErrMsg) != SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
exit(0);
}
// vg_encode_privkey(pkey, privtype, ecprot2);
// printf("%s %s\n", ecprot1, ecprot2);
}
fprintf(stderr, " done.\n");
EC_KEY_free(pkey);
free(line);
return 0;
}