2017-01-10 15:38:33 +10:30
|
|
|
#include "hsm_control.h"
|
|
|
|
#include "lightningd.h"
|
2017-03-10 21:19:43 +10:30
|
|
|
#include "subd.h"
|
2017-01-10 15:38:33 +10:30
|
|
|
#include <ccan/err/err.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/take/take.h>
|
2017-08-29 01:35:01 +09:30
|
|
|
#include <common/status.h>
|
2017-08-29 01:32:01 +09:30
|
|
|
#include <common/utils.h>
|
2017-06-24 16:20:23 +09:30
|
|
|
#include <errno.h>
|
2017-11-30 17:07:38 +01:00
|
|
|
#include <hsmd/gen_hsm_client_wire.h>
|
2017-01-10 15:38:33 +10:30
|
|
|
#include <inttypes.h>
|
2017-11-22 10:55:39 +10:30
|
|
|
#include <lightningd/hsm_control.h>
|
2017-08-29 01:34:01 +09:30
|
|
|
#include <lightningd/log.h>
|
2018-02-05 14:39:27 +10:30
|
|
|
#include <lightningd/log_status.h>
|
2017-06-24 16:20:23 +09:30
|
|
|
#include <string.h>
|
2017-02-21 15:15:29 +10:30
|
|
|
#include <wally_bip32.h>
|
2017-06-24 16:20:23 +09:30
|
|
|
#include <wire/wire_sync.h>
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2017-06-24 16:20:23 +09:30
|
|
|
u8 *hsm_sync_read(const tal_t *ctx, struct lightningd *ld)
|
2017-01-10 15:38:33 +10:30
|
|
|
{
|
2017-06-24 16:20:23 +09:30
|
|
|
for (;;) {
|
|
|
|
u8 *msg = wire_sync_read(ctx, ld->hsm_fd);
|
2018-02-05 14:39:27 +10:30
|
|
|
|
2017-06-24 16:20:23 +09:30
|
|
|
if (!msg)
|
2017-10-25 19:47:08 +10:30
|
|
|
fatal("Could not read from HSM: %s", strerror(errno));
|
2018-02-05 14:39:27 +10:30
|
|
|
if (log_status_msg(ld->log, msg))
|
|
|
|
tal_free(msg);
|
|
|
|
else
|
2017-06-24 16:20:23 +09:30
|
|
|
return msg;
|
2017-01-10 15:38:33 +10:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 21:50:28 +09:30
|
|
|
void hsm_init(struct lightningd *ld)
|
2017-01-10 15:38:33 +10:30
|
|
|
{
|
2017-08-18 14:13:52 +09:30
|
|
|
u8 *msg;
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2017-08-29 13:42:04 +09:30
|
|
|
ld->hsm_fd = subd_raw(ld, "lightning_hsmd");
|
2017-06-24 16:20:23 +09:30
|
|
|
if (ld->hsm_fd < 0)
|
2017-03-10 21:19:43 +10:30
|
|
|
err(1, "Could not subd hsm");
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2018-02-05 14:39:27 +10:30
|
|
|
ld->hsm_log = new_log(ld, ld->log_book, "hsmd:");
|
2018-05-03 21:50:28 +09:30
|
|
|
if (!wire_sync_write(ld->hsm_fd, towire_hsm_init(tmpctx)))
|
2017-06-24 16:20:23 +09:30
|
|
|
err(1, "Writing init msg to hsm");
|
2017-01-10 15:38:33 +10:30
|
|
|
|
2017-08-29 01:39:01 +09:30
|
|
|
ld->wallet->bip32_base = tal(ld->wallet, struct ext_key);
|
2017-06-24 16:20:23 +09:30
|
|
|
msg = hsm_sync_read(tmpctx, ld);
|
2018-02-21 07:29:09 +10:30
|
|
|
if (!fromwire_hsm_init_reply(msg,
|
2017-08-29 01:39:01 +09:30
|
|
|
&ld->id,
|
2017-06-24 16:20:23 +09:30
|
|
|
&ld->peer_seed,
|
2017-08-29 01:39:01 +09:30
|
|
|
ld->wallet->bip32_base))
|
2017-06-24 16:20:23 +09:30
|
|
|
errx(1, "HSM did not give init reply");
|
|
|
|
}
|