-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore_hd.cpp
More file actions
207 lines (161 loc) · 5.85 KB
/
core_hd.cpp
File metadata and controls
207 lines (161 loc) · 5.85 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
#include <argh.h>
#include <data/crypto/random.hpp>
#include <gigamonkey/wif.hpp>
#include <gigamonkey/schema/bip_39.hpp>
#include <gigamonkey/schema/bip_44.hpp>
#include <data/net/JSON.hpp>
#include <data/io/unimplemented.hpp>
using namespace data;
void run (const argh::parser &);
int main (int arg_count, char *arg_values[]) {
try {
run (argh::parser {arg_count, arg_values});
} catch (const std::exception &e) {
std::cout << "Error: " << e.what () << std::endl;
return 1;
} catch (...) {
std::cout << "Unknown error" << std::endl;
return 1;
}
return 0;
}
void version ();
void help ();
void generate (const argh::parser &);
void restore (const argh::parser &);
struct command {
enum name {
unset,
help,
version,
generate,
restore
};
name Method;
static name read (const argh::parser &, uint32 position = 1);
};
void run (const argh::parser & p) {
if (p["version"]) version ();
else if (p["help"]) help ();
else {
command::name cmd = command::read (p);
switch (cmd) {
case command::version: {
version ();
break;
}
case command::help: {
help ();
break;
}
case command::generate: {
generate (p);
break;
}
case command::restore: {
restore (p);
break;
}
default: {
std::cout << "Error: could not read user's command." << std::endl;
help ();
}
}
}
}
template <typename X> struct read_from_stream {
void operator () (std::istringstream &o, X &m) {
o >> m;
}
};
template <> struct read_from_stream<std::string> {
void operator () (std::istringstream &o, std::string &m) {
m = o.str ();
}
};
template <typename X>
void inline read_option (const argh::parser &p, uint32 index, const std::string &option, maybe<X> &m) {
X x;
if (auto positional = p (index); positional) read_from_stream<X> {} (positional, x);
else if (auto opt = p (option); opt) read_from_stream<X> {} (opt, x);
else return;
m = x;
}
template <typename X>
void inline read_option (const argh::parser &p, uint32 index, maybe<X> &m) {
X x;
if (auto positional = p (index); positional) read_from_stream<X> {} (positional, x);
else return;
m = x;
}
template <typename X>
void inline read_option (const argh::parser &p, const std::string &option, maybe<X> &m) {
X x;
if (auto opt = p (option); opt) read_from_stream<X> {} (opt, x);
else return;
m = x;
}
command::name command::read (const argh::parser &p, uint32 position) {
maybe<std::string> m;
read_option (p, position, m);
if (!bool (m)) return unset;
std::transform (m->begin (), m->end (), m->begin (),
[] (unsigned char c) { return std::tolower (c); });
if (*m == "help") return help;
if (*m == "version") return version;
if (*m == "generate") return generate;
if (*m == "restore") return restore;
return unset;
}
void version () {
std::cout << "Welcome to Core HD version 0.0" << std::endl;
}
void help () {
version ();
std::cout <<
"\n\tCoreHD generate -- create a new HD wallet." <<
"\n\tCoreHD restore \"12 or 24 words\" -- restore a wallet from words." << std::endl;
}
void generate (const argh::parser &) {
throw method::unimplemented {"generate words"};
}
void restore (const argh::parser &p) {
using namespace Gigamonkey::HD;
std::cout << "attempting to restore wallet \n" << std::endl;
maybe<std::string> words;
read_option (p, 2, words);
if (!bool (words)) throw exception {} << "No words provided";
std::cout << "reading words \"" << *words << "\"" << std::endl;
seed x;
maybe<std::string> arg_passphrase;
read_option (p, 3, arg_passphrase);
seed (*read_words) (std::string, const string &, BIP_39::language);
if (BIP_39::valid (*words)) {
std::cout << "words have been read as BIP 39" << std::endl;
read_words = BIP_39::read;/*
x = BIP_39::read (*words);
} else if (Electrum_SV::valid (*words)) {
std::cout << "words have been read as Electrum SV" << std::endl;
read_words = &Electrum_SV::read;
x = Eletrum_SV::read (*words);*/
} else {
throw exception {} << "invalid words";
}
string passphrase = arg_passphrase ? *arg_passphrase : "";
x = BIP_39::read (*words, passphrase, BIP_39::english);
BIP_32::secret master_sk = BIP_32::secret::from_seed (x);
BIP_32::pubkey master_pk = master_sk.to_public ();
std::cout << "read master key: " << "\n\tsecret: " << master_sk << "\n\tpubkey: " << master_pk << "\n" << std::endl;
std::cout << "if your wallet uses BIP 44, there is a derivation path to the true master key. \n" <<
"There is a parameter in this derivation called coin type. Different wallets inconsistently \n" <<
"use 1 of 3 possible parameters. All three derived master keys are given below. " << std::endl;
BIP_44::root bip44_root {master_sk};
BIP_44::master_secret bitcoin = bip44_root.master (BIP_44::coin_type_Bitcoin);
BIP_44::master_secret bitcoin_cash = bip44_root.master (BIP_44::coin_type_Bitcoin_Cash);
BIP_44::master_secret bitcoin_sv = bip44_root.master (BIP_44::coin_type_Bitcoin_SV);
std::cout <<
"\ncoin type Bitcoin: \n\tsecret: " << bitcoin.Secret << "\n\tpubkey: " << bitcoin.Secret.to_public () << "\n" <<
"\ncoin type Bitcoin Cash: \n\tsecret: " << bitcoin_cash.Secret << "\n\tpubkey: " << bitcoin_cash.Secret.to_public () << "\n" <<
"\ncoin type Bitcoin SV: \n\tsecret: " << bitcoin_sv.Secret << "\n\tpubkey: " << bitcoin_sv.Secret.to_public () << "\n" <<
std::endl;
}