forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hsm_control.c
61 lines (53 loc) · 1.44 KB
/
hsm_control.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
#include "hsm_control.h"
#include "lightningd.h"
#include "subd.h"
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/take/take.h>
#include <common/status.h>
#include <common/utils.h>
#include <errno.h>
#include <hsmd/gen_hsm_client_wire.h>
#include <inttypes.h>
#include <lightningd/hsm_control.h>
#include <lightningd/log.h>
#include <string.h>
#include <wally_bip32.h>
#include <wire/wire_sync.h>
u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld)
{
for (;;) {
u8 *msg = wire_sync_read(ctx, ld->hsm_fd);
if (!msg)
fatal("Could not read from HSM: %s", strerror(errno));
if (fromwire_peektype(msg) != STATUS_TRACE)
return msg;
log_debug(ld->log, "HSM TRACE: %.*s",
(int)(tal_len(msg) - sizeof(be16)),
(char *)msg + sizeof(be16));
tal_free(msg);
}
}
void hsm_init(struct lightningd *ld, bool newdir)
{
const tal_t *tmpctx = tal_tmpctx(ld);
u8 *msg;
bool create;
ld->hsm_fd = subd_raw(ld, "lightning_hsmd");
if (ld->hsm_fd < 0)
err(1, "Could not subd hsm");
if (newdir)
create = true;
else
create = (access("hsm_secret", F_OK) != 0);
if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx, create)))
err(1, "Writing init msg to hsm");
ld->wallet->bip32_base = tal(ld->wallet, struct ext_key);
msg = hsm_sync_read(tmpctx, ld);
if (!fromwire_hsm_init_reply(msg, NULL,
&ld->id,
&ld->peer_seed,
ld->wallet->bip32_base))
errx(1, "HSM did not give init reply");
tal_free(tmpctx);
}