2017-01-10 06:08:33 +01:00
|
|
|
#include "hsm_control.h"
|
|
|
|
#include "lightningd.h"
|
2017-01-10 06:08:33 +01:00
|
|
|
#include "peer_control.h"
|
2017-03-10 11:49:43 +01:00
|
|
|
#include "subd.h"
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/take/take.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <daemon/log.h>
|
2017-06-24 08:50:23 +02:00
|
|
|
#include <errno.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <inttypes.h>
|
2017-03-10 11:49:43 +01:00
|
|
|
#include <lightningd/hsm/gen_hsm_wire.h>
|
2017-06-24 08:50:23 +02:00
|
|
|
#include <lightningd/status.h>
|
|
|
|
#include <string.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <wally_bip32.h>
|
2017-06-24 08:50:23 +02:00
|
|
|
#include <wire/wire_sync.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-06-24 08:50:23 +02:00
|
|
|
u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-06-24 08:50:23 +02:00
|
|
|
for (;;) {
|
|
|
|
u8 *msg = wire_sync_read(ctx, ld->hsm_fd);
|
|
|
|
if (!msg)
|
|
|
|
fatal("Could not write 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);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
void hsm_init(struct lightningd *ld, bool newdir)
|
|
|
|
{
|
2017-06-24 08:50:23 +02:00
|
|
|
const tal_t *tmpctx = tal_tmpctx(ld);
|
2017-08-18 06:43:52 +02:00
|
|
|
u8 *msg;
|
2017-01-10 06:08:33 +01:00
|
|
|
bool create;
|
|
|
|
|
2017-06-24 08:50:23 +02:00
|
|
|
ld->hsm_fd = subd_raw(ld, "lightningd_hsm");
|
|
|
|
if (ld->hsm_fd < 0)
|
2017-03-10 11:49:43 +01:00
|
|
|
err(1, "Could not subd hsm");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
if (newdir)
|
|
|
|
create = true;
|
|
|
|
else
|
|
|
|
create = (access("hsm_secret", F_OK) != 0);
|
|
|
|
|
2017-06-24 08:50:23 +02:00
|
|
|
if (!wire_sync_write(ld->hsm_fd, towire_hsmctl_init(tmpctx, create)))
|
|
|
|
err(1, "Writing init msg to hsm");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-08-18 06:43:52 +02:00
|
|
|
ld->bip32_base = tal(ld, struct ext_key);
|
2017-06-24 08:50:23 +02:00
|
|
|
msg = hsm_sync_read(tmpctx, ld);
|
2017-08-18 06:43:52 +02:00
|
|
|
if (!fromwire_hsmctl_init_reply(msg, NULL,
|
2017-06-24 08:50:23 +02:00
|
|
|
&ld->dstate.id,
|
|
|
|
&ld->peer_seed,
|
2017-08-18 06:43:52 +02:00
|
|
|
ld->bip32_base))
|
2017-06-24 08:50:23 +02:00
|
|
|
errx(1, "HSM did not give init reply");
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-08-18 06:43:52 +02:00
|
|
|
/* FIXME... */
|
2017-06-24 08:50:23 +02:00
|
|
|
ld->wallet->bip32_base = ld->bip32_base;
|
2017-08-18 06:43:52 +02:00
|
|
|
tal_free(tmpctx);
|
2017-06-24 08:50:23 +02:00
|
|
|
}
|