-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclightning-dumpkeys.c
284 lines (229 loc) · 7.15 KB
/
clightning-dumpkeys.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "short_types.h"
#include "hkdf.h"
#include "compiler.h"
#include "secp256k1.h"
#include "bip32.h"
#include "hash.h"
#include "base58.h"
#include "descriptor.h"
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <assert.h>
#define BIP32_ENTROPY_LEN_256 32
#define fatal(fmt, ...) do { fprintf(stderr, fmt "\n", __VA_ARGS__); exit(1); } while (0)
#define fatal1(...) do { fprintf(stderr, __VA_ARGS__); exit(1); } while (0)
enum networks {
mainnet,
testnet
};
/* General 256-bit secret, which must be private. Used in various places. */
struct secret {
u8 data[32];
};
static struct {
struct secret hsm_secret;
struct ext_key master;
struct ext_key parent_ext;
struct ext_key child_ext;
} secretstuff;
struct bip32_key_version {
u32 bip32_pubkey_version;
u32 bip32_privkey_version;
};
/* Version codes for BIP32 extended keys in libwally-core.
* It's not suitable to add this struct into client struct,
* so set it static.*/
static struct bip32_key_version bip32_key_version;
bool read_all(int fd, void *data, size_t size)
{
while (size) {
ssize_t done;
done = read(fd, data, size);
if (done < 0 && errno == EINTR)
continue;
if (done <= 0)
return false;
data = (char *)data + done;
size -= done;
}
return true;
}
static void populate_secretstuff(enum networks network)
{
u8 bip32_seed[BIP32_ENTROPY_LEN_256];
u32 salt = 0;
/* struct ext_key master_extkey, child_extkey; */
const u32 flags = SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN;
secp256k1_context *ctx = secp256k1_context_create(flags);
if (network == mainnet) {
bip32_key_version = (struct bip32_key_version) {
.bip32_pubkey_version = BIP32_VER_MAIN_PUBLIC,
.bip32_privkey_version = BIP32_VER_MAIN_PRIVATE
};
} else {
bip32_key_version = (struct bip32_key_version) {
.bip32_pubkey_version = BIP32_VER_TEST_PUBLIC,
.bip32_privkey_version = BIP32_VER_TEST_PRIVATE
};
}
assert(bip32_key_version.bip32_pubkey_version == BIP32_VER_MAIN_PUBLIC
|| bip32_key_version.bip32_pubkey_version == BIP32_VER_TEST_PUBLIC);
assert(bip32_key_version.bip32_privkey_version == BIP32_VER_MAIN_PRIVATE
|| bip32_key_version.bip32_privkey_version == BIP32_VER_TEST_PRIVATE);
/* Fill in the BIP32 tree for bitcoin addresses. */
/* In libwally-core, the version BIP32_VER_TEST_PRIVATE is for testnet/regtest,
* and BIP32_VER_MAIN_PRIVATE is for mainnet. For litecoin, we also set it like
* bitcoin else.*/
do {
hkdf_sha256(bip32_seed, sizeof(bip32_seed),
&salt, sizeof(salt),
&secretstuff.hsm_secret,
sizeof(secretstuff.hsm_secret),
"bip32 seed", strlen("bip32 seed"));
salt++;
} while (bip32_key_from_seed(ctx, bip32_seed, sizeof(bip32_seed),
bip32_key_version.bip32_privkey_version,
0, &secretstuff.master) != WALLY_OK);
/* BIP 32:
*
* The default wallet layout
*
* An HDW is organized as several 'accounts'. Accounts are numbered,
* the default account ("") being number 0. Clients are not required
* to support more than one account - if not, they only use the
* default account.
*
* Each account is composed of two keypair chains: an internal and an
* external one. The external keychain is used to generate new public
* addresses, while the internal keychain is used for all other
* operations (change addresses, generation addresses, ..., anything
* that doesn't need to be communicated). Clients that do not support
* separate keychains for these should use the external one for
* everything.
*
* - m/iH/0/k corresponds to the k'th keypair of the external chain of
* account number i of the HDW derived from master m.
*/
/* Hence child 0, then child 0 again to get extkey to derive from. */
if (bip32_key_from_parent(ctx, &secretstuff.master, 0,
BIP32_FLAG_KEY_PRIVATE,
&secretstuff.child_ext) != WALLY_OK)
/*~ status_failed() is a helper which exits and sends lightningd
* a message about what happened. For hsmd, that's fatal to
* lightningd. */
fatal1("Can't derive child bip32 key");
if (bip32_key_from_parent(ctx, &secretstuff.child_ext, 0,
BIP32_FLAG_KEY_PRIVATE,
&secretstuff.parent_ext) != WALLY_OK)
fatal1("Can't derive private bip32 key");
}
static void load_hsm(const char *secretfile, enum networks network)
{
int fd = open(secretfile ? secretfile : "hsm_secret", O_RDONLY);
if (fd < 0)
fatal("opening: %s", strerror(errno));
if (!read_all(fd, &secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret)))
fatal("reading: %s", strerror(errno));
close(fd);
populate_secretstuff(network);
}
static int wally_free_string(char *str)
{
if (!str)
return WALLY_EINVAL;
wally_clear(str, strlen(str));
free(str);
return WALLY_OK;
}
static inline size_t hex_str_size(size_t bytes)
{
return 2 * bytes + 1;
}
static char hexchar(unsigned int val)
{
if (val < 10)
return '0' + val;
if (val < 16)
return 'a' + val - 10;
abort();
}
UNUSED static bool hex_encode(const void *buf, size_t bufsize, char *dest,
size_t destsize)
{
size_t i;
if (destsize < hex_str_size(bufsize))
return false;
for (i = 0; i < bufsize; i++) {
unsigned int c = ((const unsigned char *)buf)[i];
*(dest++) = hexchar(c >> 4);
*(dest++) = hexchar(c & 0xF);
}
*dest = '\0';
return true;
}
static void print_key(struct ext_key *key, const char *name, int key_type,
const char *key_type_name) {
static u8 buf[128];
static char cbuf[512];
static char cbuf2[512];
char *out;
int ret = bip32_key_serialize(key, key_type, buf, BIP32_SERIALIZED_LEN);
assert(ret == WALLY_OK);
wally_base58_from_bytes(buf, BIP32_SERIALIZED_LEN,
BASE58_FLAG_CHECKSUM, &out);
printf("%s\t%s %s\n", out, name, key_type_name);
if (!streq(name, "root")) {
snprintf(cbuf, sizeof(cbuf), "combo(%s/*)", out);
descriptor_checksum(cbuf, strlen(cbuf), cbuf2, sizeof(cbuf2));
printf("%s#%s\t%s %s descriptor\n", cbuf, cbuf2, name, key_type_name);
}
wally_free_string(out);
}
static void dump_bip32(struct ext_key *key, const char *name) {
print_key(key, name, BIP32_FLAG_KEY_PRIVATE, "private");
print_key(key, name, BIP32_FLAG_KEY_PUBLIC, "public");
}
static int dump_xpriv(const char *secretfile, enum networks network) {
if (network == mainnet) {
bip32_key_version = (struct bip32_key_version)
{ .bip32_pubkey_version = BIP32_VER_MAIN_PUBLIC
, .bip32_privkey_version = BIP32_VER_MAIN_PRIVATE
};
} else {
bip32_key_version = (struct bip32_key_version)
{ .bip32_pubkey_version = BIP32_VER_TEST_PUBLIC
, .bip32_privkey_version = BIP32_VER_TEST_PRIVATE
};
}
load_hsm(secretfile, network);
dump_bip32(&secretstuff.master, "root");
printf("\n");
dump_bip32(&secretstuff.parent_ext, "extended");
/* printf("\n"); */
/* dump_bip32(&secretstuff.child_ext, "extended internal"); */
return 0;
}
void usage()
{
fprintf(stderr, "usage: clightning-dumpkeys <hsmd_secretfile> [--testnet]\n");
exit(42);
}
int main(int argc, char *argv[])
{
if (argc != 2 && argc != 3)
usage();
enum networks network = mainnet;
if (argc == 3) {
if (strcmp(argv[2], "--testnet") == 0) {
network = testnet;
} else {
usage();
}
}
const char *secretfile = argv[1];
dump_xpriv(secretfile, network);
return 0;
}