2017-06-20 17:07:51 +02:00
|
|
|
#include <bitcoin/address.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <bitcoin/privkey.h>
|
|
|
|
#include <bitcoin/pubkey.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <bitcoin/script.h>
|
|
|
|
#include <bitcoin/tx.h>
|
2018-02-08 02:24:46 +01:00
|
|
|
#include <ccan/cast/cast.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/container_of/container_of.h>
|
|
|
|
#include <ccan/crypto/hkdf_sha256/hkdf_sha256.h>
|
|
|
|
#include <ccan/endian/endian.h>
|
|
|
|
#include <ccan/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/fdpass/fdpass.h>
|
|
|
|
#include <ccan/io/io.h>
|
|
|
|
#include <ccan/noerr/noerr.h>
|
2017-02-21 05:45:29 +01:00
|
|
|
#include <ccan/ptrint/ptrint.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <ccan/read_write_all/read_write_all.h>
|
2017-03-19 21:32:44 +01:00
|
|
|
#include <ccan/take/take.h>
|
2018-07-23 04:23:02 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/daemon_conn.h>
|
2018-01-02 16:34:08 +01:00
|
|
|
#include <common/derive_basepoints.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/funding_tx.h>
|
2017-10-26 05:03:19 +02:00
|
|
|
#include <common/hash_u5.h>
|
2018-01-02 16:34:08 +01:00
|
|
|
#include <common/key_derive.h>
|
2017-08-28 18:05:01 +02:00
|
|
|
#include <common/status.h>
|
2018-01-08 11:01:09 +01:00
|
|
|
#include <common/subdaemon.h>
|
2017-10-22 15:32:46 +02:00
|
|
|
#include <common/type_to_string.h>
|
2017-08-28 18:02:01 +02:00
|
|
|
#include <common/utils.h>
|
|
|
|
#include <common/version.h>
|
|
|
|
#include <common/withdraw_tx.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2017-11-28 16:46:14 +01:00
|
|
|
#include <hsmd/capabilities.h>
|
2017-08-29 06:12:04 +02:00
|
|
|
#include <hsmd/client.h>
|
|
|
|
#include <hsmd/gen_hsm_client_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <secp256k1_ecdh.h>
|
|
|
|
#include <sodium/randombytes.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
2017-02-21 05:45:28 +01:00
|
|
|
#include <wally_bip32.h>
|
2017-04-03 05:38:26 +02:00
|
|
|
#include <wire/gen_peer_wire.h>
|
2017-01-10 06:08:33 +01:00
|
|
|
#include <wire/wire_io.h>
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
#define REQ_FD 3
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
/* Nobody will ever find it here! */
|
2017-02-21 05:45:28 +01:00
|
|
|
static struct {
|
2017-05-06 04:19:44 +02:00
|
|
|
struct secret hsm_secret;
|
2017-02-21 05:45:28 +01:00
|
|
|
struct ext_key bip32;
|
|
|
|
} secretstuff;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
struct client {
|
2017-03-19 21:32:44 +01:00
|
|
|
struct daemon_conn dc;
|
2017-03-19 21:32:44 +01:00
|
|
|
struct daemon_conn *master;
|
2017-03-19 21:32:44 +01:00
|
|
|
|
2017-10-22 15:32:46 +02:00
|
|
|
struct pubkey id;
|
2018-07-09 13:17:59 +02:00
|
|
|
u64 dbid;
|
2017-03-19 21:32:44 +01:00
|
|
|
struct io_plan *(*handle)(struct io_conn *, struct daemon_conn *);
|
2017-11-28 16:46:14 +01:00
|
|
|
|
|
|
|
/* What is this client allowed to ask for? */
|
|
|
|
u64 capabilities;
|
2017-01-10 06:08:33 +01:00
|
|
|
};
|
|
|
|
|
2017-11-29 12:09:06 +01:00
|
|
|
/* Function declarations for later */
|
|
|
|
static void init_hsm(struct daemon_conn *master, const u8 *msg);
|
|
|
|
static void pass_client_hsmfd(struct daemon_conn *master, const u8 *msg);
|
|
|
|
static void sign_funding_tx(struct daemon_conn *master, const u8 *msg);
|
|
|
|
static void sign_invoice(struct daemon_conn *master, const u8 *msg);
|
|
|
|
static void sign_node_announcement(struct daemon_conn *master, const u8 *msg);
|
|
|
|
static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg);
|
|
|
|
|
2017-05-06 04:19:44 +02:00
|
|
|
static void node_key(struct privkey *node_privkey, struct pubkey *node_id)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
u32 salt = 0;
|
|
|
|
struct privkey unused_s;
|
|
|
|
struct pubkey unused_k;
|
|
|
|
|
2017-05-06 04:19:44 +02:00
|
|
|
if (node_privkey == NULL)
|
|
|
|
node_privkey = &unused_s;
|
2017-01-10 06:08:33 +01:00
|
|
|
else if (node_id == NULL)
|
|
|
|
node_id = &unused_k;
|
|
|
|
|
|
|
|
do {
|
2017-05-06 04:19:44 +02:00
|
|
|
hkdf_sha256(node_privkey, sizeof(*node_privkey),
|
2017-01-10 06:08:33 +01:00
|
|
|
&salt, sizeof(salt),
|
2017-02-21 05:45:28 +01:00
|
|
|
&secretstuff.hsm_secret,
|
|
|
|
sizeof(secretstuff.hsm_secret),
|
2017-01-10 06:08:33 +01:00
|
|
|
"nodeid", 6);
|
|
|
|
salt++;
|
|
|
|
} while (!secp256k1_ec_pubkey_create(secp256k1_ctx, &node_id->pubkey,
|
2017-05-06 04:19:44 +02:00
|
|
|
node_privkey->secret.data));
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
static struct client *new_client(struct daemon_conn *master,
|
2017-10-22 15:32:46 +02:00
|
|
|
const struct pubkey *id,
|
2018-07-09 13:17:59 +02:00
|
|
|
u64 dbid,
|
2017-11-28 16:46:14 +01:00
|
|
|
const u64 capabilities,
|
2017-03-19 21:32:44 +01:00
|
|
|
struct io_plan *(*handle)(struct io_conn *,
|
|
|
|
struct daemon_conn *),
|
|
|
|
int fd)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-03-19 21:32:44 +01:00
|
|
|
struct client *c = tal(master, struct client);
|
2017-11-29 12:09:06 +01:00
|
|
|
|
|
|
|
if (id) {
|
|
|
|
c->id = *id;
|
|
|
|
} else {
|
|
|
|
memset(&c->id, 0, sizeof(c->id));
|
|
|
|
}
|
2018-07-09 13:17:59 +02:00
|
|
|
c->dbid = dbid;
|
2017-11-29 12:09:06 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
c->handle = handle;
|
2017-03-19 21:32:44 +01:00
|
|
|
c->master = master;
|
2017-11-28 16:46:14 +01:00
|
|
|
c->capabilities = capabilities;
|
2017-06-06 05:08:42 +02:00
|
|
|
daemon_conn_init(c, &c->dc, fd, handle, NULL);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-04-12 08:26:00 +02:00
|
|
|
/* Free the connection if we exit everything. */
|
|
|
|
tal_steal(master, c->dc.conn);
|
2017-03-19 21:32:44 +01:00
|
|
|
/* Free client when connection freed. */
|
|
|
|
tal_steal(c->dc.conn, c);
|
2017-01-10 06:08:33 +01:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
/**
|
|
|
|
* hsm_peer_secret_base -- Derive the base secret seed for per-peer seeds
|
|
|
|
*
|
|
|
|
* This secret is shared by all channels/peers for the client. The
|
|
|
|
* per-peer seeds will be generated from it by mixing in the
|
|
|
|
* channel_id and the peer node_id.
|
|
|
|
*/
|
|
|
|
static void hsm_peer_secret_base(struct secret *peer_seed_base)
|
|
|
|
{
|
|
|
|
hkdf_sha256(peer_seed_base, sizeof(struct secret), NULL, 0,
|
|
|
|
&secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret),
|
|
|
|
"peer seed", strlen("peer seed"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void get_channel_seed(const struct pubkey *peer_id, u64 dbid,
|
|
|
|
struct secret *channel_seed)
|
|
|
|
{
|
|
|
|
struct secret peer_base;
|
|
|
|
u8 input[PUBKEY_DER_LEN + sizeof(dbid)];
|
|
|
|
const char *info = "per-peer seed";
|
|
|
|
|
|
|
|
hsm_peer_secret_base(&peer_base);
|
|
|
|
pubkey_to_der(input, peer_id);
|
|
|
|
memcpy(input + PUBKEY_DER_LEN, &dbid, sizeof(dbid));
|
|
|
|
|
|
|
|
hkdf_sha256(channel_seed, sizeof(*channel_seed),
|
|
|
|
input, sizeof(input),
|
|
|
|
&peer_base, sizeof(peer_base),
|
|
|
|
info, strlen(info));
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
static struct io_plan *handle_ecdh(struct io_conn *conn, struct daemon_conn *dc)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
2017-03-19 21:32:44 +01:00
|
|
|
struct client *c = container_of(dc, struct client, dc);
|
2017-01-10 06:08:33 +01:00
|
|
|
struct privkey privkey;
|
|
|
|
struct pubkey point;
|
2017-05-06 04:19:44 +02:00
|
|
|
struct secret ss;
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_ecdh_req(dc->msg_in, &point)) {
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(c->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
2017-10-22 15:32:46 +02:00
|
|
|
&c->id,
|
2017-03-19 21:32:44 +01:00
|
|
|
dc->msg_in)));
|
2017-03-19 21:32:44 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
|
|
|
|
node_key(&privkey, NULL);
|
2017-05-06 04:19:44 +02:00
|
|
|
if (secp256k1_ecdh(secp256k1_ctx, ss.data, &point.pubkey,
|
|
|
|
privkey.secret.data) != 1) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("secp256k1_ecdh fail for client %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(c->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
2017-10-22 15:32:46 +02:00
|
|
|
&c->id,
|
2017-03-19 21:32:44 +01:00
|
|
|
dc->msg_in)));
|
2017-03-19 21:32:44 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
daemon_conn_send(dc, take(towire_hsm_ecdh_resp(NULL, &ss)));
|
2017-03-19 21:32:44 +01:00
|
|
|
return daemon_conn_read_next(conn, dc);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-04-03 02:45:24 +02:00
|
|
|
static struct io_plan *handle_cannouncement_sig(struct io_conn *conn,
|
2018-07-09 13:17:59 +02:00
|
|
|
struct client *c)
|
2017-04-03 02:45:24 +02:00
|
|
|
{
|
2018-07-09 13:17:59 +02:00
|
|
|
struct daemon_conn *dc = &c->dc;
|
2017-04-03 02:45:24 +02:00
|
|
|
/* First 2 + 256 byte are the signatures and msg type, skip them */
|
|
|
|
size_t offset = 258;
|
|
|
|
struct privkey node_pkey;
|
2018-07-09 13:17:59 +02:00
|
|
|
secp256k1_ecdsa_signature node_sig, bitcoin_sig;
|
2017-04-03 02:45:24 +02:00
|
|
|
struct sha256_double hash;
|
|
|
|
u8 *reply;
|
|
|
|
u8 *ca;
|
2018-07-09 13:17:59 +02:00
|
|
|
struct pubkey funding_pubkey;
|
|
|
|
struct privkey funding_privkey;
|
|
|
|
struct secret channel_seed;
|
|
|
|
|
|
|
|
/* FIXME: We should cache these. */
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
derive_funding_key(&channel_seed, &funding_pubkey, &funding_privkey);
|
2017-04-03 02:45:24 +02:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
if (!fromwire_hsm_cannouncement_sig_req(tmpctx, dc->msg_in, &ca)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Failed to parse cannouncement_sig_req: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, dc->msg_in));
|
2017-04-03 02:45:24 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2018-07-28 08:00:16 +02:00
|
|
|
if (tal_count(ca) < offset) {
|
|
|
|
status_broken("bad cannounce length %zu", tal_count(ca));
|
2017-04-03 02:45:24 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO(cdecker) Check that this is actually a valid
|
|
|
|
* channel_announcement */
|
|
|
|
node_key(&node_pkey, NULL);
|
2018-07-28 08:00:16 +02:00
|
|
|
sha256_double(&hash, ca + offset, tal_count(ca) - offset);
|
2017-04-03 02:45:24 +02:00
|
|
|
|
|
|
|
sign_hash(&node_pkey, &hash, &node_sig);
|
2018-07-09 13:17:59 +02:00
|
|
|
sign_hash(&funding_privkey, &hash, &bitcoin_sig);
|
2017-04-03 02:45:24 +02:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
reply = towire_hsm_cannouncement_sig_reply(NULL, &node_sig,
|
|
|
|
&bitcoin_sig);
|
2017-04-03 02:45:24 +02:00
|
|
|
daemon_conn_send(dc, take(reply));
|
|
|
|
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
}
|
|
|
|
|
2017-04-03 05:38:26 +02:00
|
|
|
static struct io_plan *handle_channel_update_sig(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
/* 2 bytes msg type + 64 bytes signature */
|
|
|
|
size_t offset = 66;
|
|
|
|
struct privkey node_pkey;
|
|
|
|
struct sha256_double hash;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct short_channel_id scid;
|
2017-06-06 01:49:10 +02:00
|
|
|
u32 timestamp, fee_base_msat, fee_proportional_mill;
|
|
|
|
u64 htlc_minimum_msat;
|
2017-04-03 05:38:26 +02:00
|
|
|
u16 flags, cltv_expiry_delta;
|
2017-12-18 07:44:10 +01:00
|
|
|
struct bitcoin_blkid chain_hash;
|
2017-04-03 05:38:26 +02:00
|
|
|
u8 *cu;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_cupdate_sig_req(tmpctx, dc->msg_in, &cu)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Failed to parse %s: %s",
|
|
|
|
hsm_client_wire_type_name(fromwire_peektype(dc->msg_in)),
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, dc->msg_in));
|
2017-04-03 05:38:26 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_channel_update(cu, &sig, &chain_hash,
|
2017-08-22 07:25:01 +02:00
|
|
|
&scid, ×tamp, &flags,
|
2017-04-03 05:38:26 +02:00
|
|
|
&cltv_expiry_delta, &htlc_minimum_msat,
|
|
|
|
&fee_base_msat, &fee_proportional_mill)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Failed to parse inner channel_update: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, dc->msg_in));
|
2017-04-03 05:38:26 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
2018-07-28 08:00:16 +02:00
|
|
|
if (tal_count(cu) < offset) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("inner channel_update too short: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, dc->msg_in));
|
2017-04-03 05:38:26 +02:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
node_key(&node_pkey, NULL);
|
2018-07-28 08:00:16 +02:00
|
|
|
sha256_double(&hash, cu + offset, tal_count(cu) - offset);
|
2017-04-03 05:38:26 +02:00
|
|
|
|
|
|
|
sign_hash(&node_pkey, &hash, &sig);
|
|
|
|
|
2017-08-22 07:25:01 +02:00
|
|
|
cu = towire_channel_update(tmpctx, &sig, &chain_hash,
|
|
|
|
&scid, timestamp, flags,
|
2017-04-03 05:38:26 +02:00
|
|
|
cltv_expiry_delta, htlc_minimum_msat,
|
|
|
|
fee_base_msat, fee_proportional_mill);
|
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
daemon_conn_send(dc, take(towire_hsm_cupdate_sig_reply(NULL, cu)));
|
2017-04-03 05:38:26 +02:00
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
}
|
2017-04-03 02:45:24 +02:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
static struct io_plan *handle_get_channel_basepoints(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
struct pubkey peer_id;
|
|
|
|
u64 dbid;
|
|
|
|
struct secret seed;
|
|
|
|
struct basepoints basepoints;
|
|
|
|
struct pubkey funding_pubkey;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_get_channel_basepoints(dc->msg_in, &peer_id, &dbid))
|
|
|
|
master_badmsg(WIRE_HSM_GET_CHANNEL_BASEPOINTS, dc->msg_in);
|
|
|
|
|
|
|
|
get_channel_seed(&peer_id, dbid, &seed);
|
|
|
|
derive_basepoints(&seed, &funding_pubkey, &basepoints, NULL, NULL);
|
|
|
|
|
|
|
|
daemon_conn_send(dc,
|
|
|
|
take(towire_hsm_get_channel_basepoints_reply(NULL,
|
|
|
|
&basepoints,
|
|
|
|
&funding_pubkey)));
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
/* FIXME: Ensure HSM never does this twice for same dbid! */
|
|
|
|
static struct io_plan *handle_sign_commitment_tx(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
struct pubkey peer_id, remote_funding_pubkey, local_funding_pubkey;
|
|
|
|
u64 dbid, funding_amount;
|
|
|
|
struct secret channel_seed;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct secrets secrets;
|
|
|
|
const u8 *funding_wscript;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_commitment_tx(tmpctx, dc->msg_in,
|
|
|
|
&peer_id, &dbid,
|
|
|
|
&tx,
|
|
|
|
&remote_funding_pubkey,
|
|
|
|
&funding_amount))
|
|
|
|
master_badmsg(WIRE_HSM_SIGN_COMMITMENT_TX, dc->msg_in);
|
|
|
|
|
|
|
|
get_channel_seed(&peer_id, dbid, &channel_seed);
|
|
|
|
derive_basepoints(&channel_seed,
|
|
|
|
&local_funding_pubkey, NULL, &secrets, NULL);
|
|
|
|
|
|
|
|
funding_wscript = bitcoin_redeem_2of2(tmpctx,
|
|
|
|
&local_funding_pubkey,
|
|
|
|
&remote_funding_pubkey);
|
|
|
|
/* Need input amount for signing */
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &funding_amount);
|
|
|
|
sign_tx_input(tx, 0, NULL, funding_wscript,
|
|
|
|
&secrets.funding_privkey,
|
|
|
|
&local_funding_pubkey,
|
|
|
|
&sig);
|
|
|
|
|
|
|
|
daemon_conn_send(dc,
|
|
|
|
take(towire_hsm_sign_commitment_tx_reply(NULL, &sig)));
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
static PRINTF_FMT(3,4)
|
|
|
|
struct io_plan *bad_sign_request(struct io_conn *conn,
|
|
|
|
struct client *c,
|
|
|
|
const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
char *str;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
str = tal_fmt(tmpctx, fmt, ap);
|
|
|
|
status_broken("Client %s bad sign request: %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id), str);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
daemon_conn_send(c->master,
|
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
|
|
|
&c->id,
|
|
|
|
c->dc.msg_in)));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
/* FIXME: make sure it meets some criteria? */
|
|
|
|
static struct io_plan *handle_sign_remote_commitment_tx(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
struct daemon_conn *dc = &c->dc;
|
|
|
|
struct pubkey remote_funding_pubkey, local_funding_pubkey;
|
|
|
|
u64 funding_amount;
|
|
|
|
struct secret channel_seed;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct secrets secrets;
|
|
|
|
const u8 *funding_wscript;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_remote_commitment_tx(tmpctx, dc->msg_in,
|
|
|
|
&tx,
|
|
|
|
&remote_funding_pubkey,
|
|
|
|
&funding_amount))
|
|
|
|
master_badmsg(WIRE_HSM_SIGN_REMOTE_COMMITMENT_TX, dc->msg_in);
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
derive_basepoints(&channel_seed,
|
|
|
|
&local_funding_pubkey, NULL, &secrets, NULL);
|
|
|
|
|
|
|
|
funding_wscript = bitcoin_redeem_2of2(tmpctx,
|
|
|
|
&local_funding_pubkey,
|
|
|
|
&remote_funding_pubkey);
|
|
|
|
/* Need input amount for signing */
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &funding_amount);
|
|
|
|
sign_tx_input(tx, 0, NULL, funding_wscript,
|
|
|
|
&secrets.funding_privkey,
|
|
|
|
&local_funding_pubkey,
|
|
|
|
&sig);
|
|
|
|
|
|
|
|
daemon_conn_send(dc, take(towire_hsm_sign_tx_reply(NULL, &sig)));
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
/* FIXME: Derive output address for this client, and check it here! */
|
|
|
|
static struct io_plan *handle_sign_to_us_tx(struct io_conn *conn,
|
|
|
|
struct client *c,
|
|
|
|
struct bitcoin_tx *tx,
|
|
|
|
const struct privkey *privkey,
|
|
|
|
const u8 *wscript,
|
|
|
|
u64 input_amount)
|
|
|
|
{
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct pubkey pubkey;
|
|
|
|
|
|
|
|
if (!pubkey_from_privkey(privkey, &pubkey))
|
|
|
|
return bad_sign_request(conn, c, "bad pubkey_from_privkey");
|
|
|
|
|
|
|
|
if (tal_count(tx->input) != 1)
|
|
|
|
return bad_sign_request(conn, c, "bad txinput count");
|
|
|
|
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &input_amount);
|
|
|
|
sign_tx_input(tx, 0, NULL, wscript, privkey, &pubkey, &sig);
|
|
|
|
|
|
|
|
daemon_conn_send(&c->dc, take(towire_hsm_sign_tx_reply(NULL, &sig)));
|
|
|
|
return daemon_conn_read_next(conn, &c->dc);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *handle_sign_delayed_payment_to_us(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
u64 commit_num, input_amount;
|
|
|
|
struct secret channel_seed, basepoint_secret;
|
|
|
|
struct pubkey basepoint;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
struct sha256 shaseed;
|
|
|
|
struct pubkey per_commitment_point;
|
|
|
|
struct privkey privkey;
|
|
|
|
u8 *wscript;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_delayed_payment_to_us(tmpctx, c->dc.msg_in,
|
|
|
|
&commit_num,
|
|
|
|
&tx, &wscript,
|
|
|
|
&input_amount))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"malformed hsm_sign_delayed_payment");
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
|
|
|
|
if (!derive_shaseed(&channel_seed, &shaseed))
|
|
|
|
return bad_sign_request(conn, c, "bad derive_shaseed");
|
|
|
|
|
|
|
|
if (!per_commit_point(&shaseed, &per_commitment_point, commit_num))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"bad per_commitment_point %"PRIu64,
|
|
|
|
commit_num);
|
|
|
|
|
|
|
|
if (!derive_delayed_payment_basepoint(&channel_seed,
|
|
|
|
&basepoint,
|
|
|
|
&basepoint_secret))
|
|
|
|
return bad_sign_request(conn, c, "failed deriving basepoint");
|
|
|
|
|
|
|
|
if (!derive_simple_privkey(&basepoint_secret,
|
|
|
|
&basepoint,
|
|
|
|
&per_commitment_point,
|
|
|
|
&privkey))
|
|
|
|
return bad_sign_request(conn, c, "failed deriving privkey");
|
|
|
|
|
|
|
|
return handle_sign_to_us_tx(conn, c, tx, &privkey, wscript,
|
|
|
|
input_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *handle_sign_remote_htlc_to_us(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
u64 input_amount;
|
|
|
|
struct secret channel_seed, htlc_basepoint_secret;
|
|
|
|
struct pubkey htlc_basepoint;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
struct pubkey remote_per_commitment_point;
|
|
|
|
struct privkey privkey;
|
|
|
|
u8 *wscript;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_remote_htlc_to_us(tmpctx, c->dc.msg_in,
|
|
|
|
&remote_per_commitment_point,
|
|
|
|
&tx, &wscript,
|
|
|
|
&input_amount))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"malformed hsm_sign_remote_htlc_to_us");
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
|
|
|
|
if (!derive_htlc_basepoint(&channel_seed, &htlc_basepoint,
|
|
|
|
&htlc_basepoint_secret))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed derive_htlc_basepoint");
|
|
|
|
|
|
|
|
if (!derive_simple_privkey(&htlc_basepoint_secret,
|
|
|
|
&htlc_basepoint,
|
|
|
|
&remote_per_commitment_point,
|
|
|
|
&privkey))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed deriving htlc privkey");
|
|
|
|
|
|
|
|
return handle_sign_to_us_tx(conn, c, tx, &privkey, wscript,
|
|
|
|
input_amount);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *handle_sign_penalty_to_us(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
u64 input_amount;
|
|
|
|
struct secret channel_seed, revocation_secret, revocation_basepoint_secret;
|
|
|
|
struct pubkey revocation_basepoint;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
struct pubkey point;
|
|
|
|
struct privkey privkey;
|
|
|
|
u8 *wscript;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_penalty_to_us(tmpctx, c->dc.msg_in,
|
|
|
|
&revocation_secret,
|
|
|
|
&tx, &wscript,
|
|
|
|
&input_amount))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"malformed hsm_sign_penalty_to_us");
|
|
|
|
|
|
|
|
if (!pubkey_from_secret(&revocation_secret, &point))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed deriving pubkey");
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
if (!derive_revocation_basepoint(&channel_seed,
|
|
|
|
&revocation_basepoint,
|
|
|
|
&revocation_basepoint_secret))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed deriving revocation basepoint");
|
|
|
|
|
|
|
|
if (!derive_revocation_privkey(&revocation_basepoint_secret,
|
|
|
|
&revocation_secret,
|
|
|
|
&revocation_basepoint,
|
|
|
|
&point,
|
|
|
|
&privkey))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed deriving revocation privkey");
|
|
|
|
|
|
|
|
return handle_sign_to_us_tx(conn, c, tx, &privkey, wscript,
|
|
|
|
input_amount);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
static struct io_plan *handle_sign_local_htlc_tx(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
u64 commit_num, input_amount;
|
|
|
|
struct secret channel_seed, htlc_basepoint_secret;
|
|
|
|
struct sha256 shaseed;
|
|
|
|
struct pubkey per_commitment_point, htlc_basepoint;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
u8 *wscript;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct privkey htlc_privkey;
|
|
|
|
struct pubkey htlc_pubkey;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_local_htlc_tx(tmpctx, c->dc.msg_in,
|
|
|
|
&commit_num, &tx, &wscript,
|
|
|
|
&input_amount))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"malformed hsm_sign_local_htlc_tx");
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
|
|
|
|
if (!derive_shaseed(&channel_seed, &shaseed))
|
|
|
|
return bad_sign_request(conn, c, "bad derive_shaseed");
|
|
|
|
|
|
|
|
if (!per_commit_point(&shaseed, &per_commitment_point, commit_num))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"bad per_commitment_point %"PRIu64,
|
|
|
|
commit_num);
|
|
|
|
|
|
|
|
if (!derive_htlc_basepoint(&channel_seed,
|
|
|
|
&htlc_basepoint,
|
|
|
|
&htlc_basepoint_secret))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed deriving htlc basepoint");
|
|
|
|
|
|
|
|
if (!derive_simple_privkey(&htlc_basepoint_secret,
|
|
|
|
&htlc_basepoint,
|
|
|
|
&per_commitment_point,
|
|
|
|
&htlc_privkey))
|
|
|
|
return bad_sign_request(conn, c,
|
|
|
|
"Failed deriving htlc privkey");
|
|
|
|
|
|
|
|
if (!pubkey_from_privkey(&htlc_privkey, &htlc_pubkey))
|
|
|
|
return bad_sign_request(conn, c, "bad pubkey_from_privkey");
|
|
|
|
|
|
|
|
if (tal_count(tx->input) != 1)
|
|
|
|
return bad_sign_request(conn, c, "bad txinput count");
|
|
|
|
|
|
|
|
/* FIXME: Check that output script is correct! */
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &input_amount);
|
|
|
|
sign_tx_input(tx, 0, NULL, wscript, &htlc_privkey, &htlc_pubkey, &sig);
|
|
|
|
|
|
|
|
daemon_conn_send(&c->dc, take(towire_hsm_sign_tx_reply(NULL, &sig)));
|
|
|
|
return daemon_conn_read_next(conn, &c->dc);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
static struct io_plan *
|
|
|
|
handle_get_per_commitment_point(struct io_conn *conn, struct client *c)
|
|
|
|
{
|
|
|
|
struct daemon_conn *dc = &c->dc;
|
|
|
|
struct secret channel_seed;
|
|
|
|
struct sha256 shaseed;
|
|
|
|
struct pubkey per_commitment_point;
|
|
|
|
u64 n;
|
|
|
|
struct secret *old_secret;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_get_per_commitment_point(dc->msg_in, &n)) {
|
|
|
|
status_broken("bad get_per_commitment_point for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
if (!derive_shaseed(&channel_seed, &shaseed)) {
|
|
|
|
status_broken("bad derive_shaseed for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!per_commit_point(&shaseed, &per_commitment_point, n)) {
|
|
|
|
status_broken("bad per_commit_point %"PRIu64" for client %s",
|
|
|
|
n, type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n >= 2) {
|
|
|
|
old_secret = tal(tmpctx, struct secret);
|
2018-08-17 07:06:24 +02:00
|
|
|
if (!per_commit_secret(&shaseed, old_secret, n - 2)) {
|
|
|
|
status_broken("Cannot derive secret %"PRIu64
|
|
|
|
" for client %s",
|
|
|
|
n - 1,
|
|
|
|
type_to_string(tmpctx,
|
|
|
|
struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
2018-07-23 04:23:02 +02:00
|
|
|
} else
|
|
|
|
old_secret = NULL;
|
|
|
|
|
|
|
|
daemon_conn_send(&c->dc,
|
|
|
|
take(towire_hsm_get_per_commitment_point_reply(NULL,
|
|
|
|
&per_commitment_point,
|
|
|
|
old_secret)));
|
|
|
|
return daemon_conn_read_next(conn, &c->dc);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
daemon_conn_send(c->master,
|
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
|
|
|
&c->id,
|
|
|
|
c->dc.msg_in)));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2018-08-17 07:06:35 +02:00
|
|
|
static struct io_plan *handle_check_future_secret(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
struct daemon_conn *dc = &c->dc;
|
|
|
|
struct secret channel_seed;
|
|
|
|
struct sha256 shaseed;
|
|
|
|
u64 n;
|
|
|
|
struct secret secret, suggested;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_check_future_secret(dc->msg_in, &n,
|
|
|
|
&suggested)) {
|
|
|
|
status_broken("bad check_future_secret for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
if (!derive_shaseed(&channel_seed, &shaseed)) {
|
|
|
|
status_broken("bad derive_shaseed for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!per_commit_secret(&shaseed, &secret, n)) {
|
|
|
|
status_broken("bad commit secret #%"PRIu64" for client %s",
|
|
|
|
n, type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_send(&c->dc,
|
|
|
|
take(towire_hsm_check_future_secret_reply(NULL,
|
|
|
|
secret_eq_consttime(&secret, &suggested))));
|
|
|
|
return daemon_conn_read_next(conn, &c->dc);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
daemon_conn_send(c->master,
|
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
|
|
|
&c->id,
|
|
|
|
c->dc.msg_in)));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
static struct io_plan *handle_sign_remote_htlc_tx(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
struct daemon_conn *dc = &c->dc;
|
|
|
|
struct secret channel_seed;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct secrets secrets;
|
|
|
|
struct basepoints basepoints;
|
|
|
|
struct pubkey remote_per_commit_point;
|
|
|
|
u64 amount;
|
|
|
|
u8 *wscript;
|
|
|
|
struct privkey htlc_privkey;
|
|
|
|
struct pubkey htlc_pubkey;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_remote_htlc_tx(tmpctx, dc->msg_in,
|
|
|
|
&tx, &wscript, &amount,
|
|
|
|
&remote_per_commit_point)) {
|
|
|
|
status_broken("bad hsm_sign_remote_htlc_tx for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
derive_basepoints(&channel_seed, NULL, &basepoints, &secrets, NULL);
|
|
|
|
|
|
|
|
if (!derive_simple_privkey(&secrets.htlc_basepoint_secret,
|
|
|
|
&basepoints.htlc,
|
|
|
|
&remote_per_commit_point,
|
|
|
|
&htlc_privkey)) {
|
|
|
|
status_broken("Failed deriving htlc privkey for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!derive_simple_key(&basepoints.htlc,
|
|
|
|
&remote_per_commit_point,
|
|
|
|
&htlc_pubkey)) {
|
|
|
|
status_broken("Failed deriving htlc pubkey for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Need input amount for signing */
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &amount);
|
|
|
|
sign_tx_input(tx, 0, NULL, wscript, &htlc_privkey, &htlc_pubkey,
|
|
|
|
&sig);
|
|
|
|
|
|
|
|
daemon_conn_send(dc, take(towire_hsm_sign_tx_reply(NULL, &sig)));
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
daemon_conn_send(c->master,
|
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
|
|
|
&c->id,
|
|
|
|
c->dc.msg_in)));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
static struct io_plan *handle_sign_mutual_close_tx(struct io_conn *conn,
|
|
|
|
struct client *c)
|
|
|
|
{
|
|
|
|
struct daemon_conn *dc = &c->dc;
|
|
|
|
struct secret channel_seed;
|
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
struct pubkey remote_funding_pubkey, local_funding_pubkey;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
struct secrets secrets;
|
|
|
|
u64 funding_amount;
|
|
|
|
const u8 *funding_wscript;
|
|
|
|
|
|
|
|
if (!fromwire_hsm_sign_mutual_close_tx(tmpctx, dc->msg_in,
|
|
|
|
&tx,
|
|
|
|
&remote_funding_pubkey,
|
|
|
|
&funding_amount)) {
|
|
|
|
status_broken("bad hsm_sign_htlc_mutual_close_tx for client %s",
|
|
|
|
type_to_string(tmpctx, struct pubkey, &c->id));
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: We should know dust level, decent fee range and
|
|
|
|
* balances, and final_keyindex, and thus be able to check tx
|
|
|
|
* outputs! */
|
|
|
|
get_channel_seed(&c->id, c->dbid, &channel_seed);
|
|
|
|
derive_basepoints(&channel_seed,
|
|
|
|
&local_funding_pubkey, NULL, &secrets, NULL);
|
|
|
|
|
|
|
|
funding_wscript = bitcoin_redeem_2of2(tmpctx,
|
|
|
|
&local_funding_pubkey,
|
|
|
|
&remote_funding_pubkey);
|
|
|
|
/* Need input amount for signing */
|
|
|
|
tx->input[0].amount = tal_dup(tx->input, u64, &funding_amount);
|
|
|
|
sign_tx_input(tx, 0, NULL, funding_wscript,
|
|
|
|
&secrets.funding_privkey,
|
|
|
|
&local_funding_pubkey,
|
|
|
|
&sig);
|
|
|
|
|
|
|
|
daemon_conn_send(dc, take(towire_hsm_sign_tx_reply(NULL, &sig)));
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
|
|
|
fail:
|
|
|
|
daemon_conn_send(c->master,
|
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
|
|
|
&c->id,
|
|
|
|
dc->msg_in)));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
static bool check_client_capabilities(struct client *client,
|
|
|
|
enum hsm_client_wire_type t)
|
|
|
|
{
|
|
|
|
switch (t) {
|
|
|
|
case WIRE_HSM_ECDH_REQ:
|
|
|
|
return (client->capabilities & HSM_CAP_ECDH) != 0;
|
|
|
|
|
|
|
|
case WIRE_HSM_CANNOUNCEMENT_SIG_REQ:
|
|
|
|
case WIRE_HSM_CUPDATE_SIG_REQ:
|
2017-11-29 12:09:06 +01:00
|
|
|
case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REQ:
|
2017-11-28 16:46:14 +01:00
|
|
|
return (client->capabilities & HSM_CAP_SIGN_GOSSIP) != 0;
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_DELAYED_PAYMENT_TO_US:
|
|
|
|
case WIRE_HSM_SIGN_REMOTE_HTLC_TO_US:
|
|
|
|
case WIRE_HSM_SIGN_PENALTY_TO_US:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_LOCAL_HTLC_TX:
|
2018-07-23 04:23:02 +02:00
|
|
|
return (client->capabilities & HSM_CAP_SIGN_ONCHAIN_TX) != 0;
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_GET_PER_COMMITMENT_POINT:
|
2018-08-17 07:06:35 +02:00
|
|
|
case WIRE_HSM_CHECK_FUTURE_SECRET:
|
2018-07-23 04:23:02 +02:00
|
|
|
return (client->capabilities & HSM_CAP_COMMITMENT_POINT) != 0;
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_SIGN_REMOTE_COMMITMENT_TX:
|
|
|
|
case WIRE_HSM_SIGN_REMOTE_HTLC_TX:
|
|
|
|
return (client->capabilities & HSM_CAP_SIGN_REMOTE_TX) != 0;
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_SIGN_MUTUAL_CLOSE_TX:
|
|
|
|
return (client->capabilities & HSM_CAP_SIGN_CLOSING_TX) != 0;
|
|
|
|
|
2017-11-29 12:09:06 +01:00
|
|
|
case WIRE_HSM_INIT:
|
|
|
|
case WIRE_HSM_CLIENT_HSMFD:
|
|
|
|
case WIRE_HSM_SIGN_FUNDING:
|
|
|
|
case WIRE_HSM_SIGN_WITHDRAWAL:
|
|
|
|
case WIRE_HSM_SIGN_INVOICE:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_COMMITMENT_TX:
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_GET_CHANNEL_BASEPOINTS:
|
2017-11-29 12:09:06 +01:00
|
|
|
return (client->capabilities & HSM_CAP_MASTER) != 0;
|
|
|
|
|
2018-04-20 10:09:50 +02:00
|
|
|
/* These are messages sent by the HSM so we should never receive them */
|
2017-11-28 16:46:14 +01:00
|
|
|
case WIRE_HSM_ECDH_RESP:
|
|
|
|
case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY:
|
|
|
|
case WIRE_HSM_CUPDATE_SIG_REPLY:
|
2017-11-29 12:09:06 +01:00
|
|
|
case WIRE_HSM_CLIENT_HSMFD_REPLY:
|
|
|
|
case WIRE_HSM_SIGN_FUNDING_REPLY:
|
|
|
|
case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REPLY:
|
|
|
|
case WIRE_HSM_SIGN_WITHDRAWAL_REPLY:
|
|
|
|
case WIRE_HSM_SIGN_INVOICE_REPLY:
|
|
|
|
case WIRE_HSM_INIT_REPLY:
|
2017-11-30 17:07:38 +01:00
|
|
|
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_COMMITMENT_TX_REPLY:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_TX_REPLY:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_GET_PER_COMMITMENT_POINT_REPLY:
|
2018-08-17 07:06:35 +02:00
|
|
|
case WIRE_HSM_CHECK_FUTURE_SECRET_REPLY:
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_GET_CHANNEL_BASEPOINTS_REPLY:
|
2017-11-28 16:46:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct io_plan *handle_client(struct io_conn *conn,
|
|
|
|
struct daemon_conn *dc)
|
|
|
|
{
|
|
|
|
struct client *c = container_of(dc, struct client, dc);
|
|
|
|
enum hsm_client_wire_type t = fromwire_peektype(dc->msg_in);
|
|
|
|
|
2018-02-05 05:09:27 +01:00
|
|
|
status_debug("Client: Received message %d from client", t);
|
2017-11-29 12:09:06 +01:00
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
/* Before we do anything else, is this client allowed to do
|
|
|
|
* what he asks for? */
|
|
|
|
if (!check_client_capabilities(c, t)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Client does not have the required capability to run %d", t);
|
2017-11-28 16:46:14 +01:00
|
|
|
daemon_conn_send(c->master,
|
|
|
|
take(towire_hsmstatus_client_bad_request(
|
2018-03-15 07:10:22 +01:00
|
|
|
NULL, &c->id, dc->msg_in)));
|
2017-11-28 16:46:14 +01:00
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now actually go and do what the client asked for */
|
|
|
|
switch (t) {
|
2017-11-29 12:09:06 +01:00
|
|
|
case WIRE_HSM_INIT:
|
|
|
|
init_hsm(dc, dc->msg_in);
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
|
|
|
case WIRE_HSM_CLIENT_HSMFD:
|
|
|
|
pass_client_hsmfd(dc, dc->msg_in);
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_GET_CHANNEL_BASEPOINTS:
|
|
|
|
return handle_get_channel_basepoints(conn, dc);
|
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
case WIRE_HSM_ECDH_REQ:
|
|
|
|
return handle_ecdh(conn, dc);
|
2017-11-29 12:09:06 +01:00
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
case WIRE_HSM_CANNOUNCEMENT_SIG_REQ:
|
2018-07-09 13:17:59 +02:00
|
|
|
return handle_cannouncement_sig(conn, c);
|
2017-11-29 12:09:06 +01:00
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
case WIRE_HSM_CUPDATE_SIG_REQ:
|
|
|
|
return handle_channel_update_sig(conn, dc);
|
|
|
|
|
2017-11-29 12:09:06 +01:00
|
|
|
case WIRE_HSM_SIGN_FUNDING:
|
|
|
|
sign_funding_tx(dc, dc->msg_in);
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
|
|
|
case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REQ:
|
|
|
|
sign_node_announcement(dc, dc->msg_in);
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
|
|
|
case WIRE_HSM_SIGN_INVOICE:
|
|
|
|
sign_invoice(dc, dc->msg_in);
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
|
|
|
case WIRE_HSM_SIGN_WITHDRAWAL:
|
|
|
|
sign_withdrawal_tx(dc, dc->msg_in);
|
|
|
|
return daemon_conn_read_next(conn, dc);
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_COMMITMENT_TX:
|
|
|
|
return handle_sign_commitment_tx(conn, dc);
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_DELAYED_PAYMENT_TO_US:
|
|
|
|
return handle_sign_delayed_payment_to_us(conn, c);
|
|
|
|
|
|
|
|
case WIRE_HSM_SIGN_REMOTE_HTLC_TO_US:
|
|
|
|
return handle_sign_remote_htlc_to_us(conn, c);
|
|
|
|
|
|
|
|
case WIRE_HSM_SIGN_PENALTY_TO_US:
|
|
|
|
return handle_sign_penalty_to_us(conn, c);
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_LOCAL_HTLC_TX:
|
|
|
|
return handle_sign_local_htlc_tx(conn, c);
|
|
|
|
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_GET_PER_COMMITMENT_POINT:
|
|
|
|
return handle_get_per_commitment_point(conn, c);
|
|
|
|
|
2018-08-17 07:06:35 +02:00
|
|
|
case WIRE_HSM_CHECK_FUTURE_SECRET:
|
|
|
|
return handle_check_future_secret(conn, c);
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_SIGN_REMOTE_COMMITMENT_TX:
|
|
|
|
return handle_sign_remote_commitment_tx(conn, c);
|
|
|
|
|
|
|
|
case WIRE_HSM_SIGN_REMOTE_HTLC_TX:
|
|
|
|
return handle_sign_remote_htlc_tx(conn, c);
|
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_SIGN_MUTUAL_CLOSE_TX:
|
|
|
|
return handle_sign_mutual_close_tx(conn, c);
|
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
case WIRE_HSM_ECDH_RESP:
|
|
|
|
case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY:
|
|
|
|
case WIRE_HSM_CUPDATE_SIG_REPLY:
|
2017-11-29 12:09:06 +01:00
|
|
|
case WIRE_HSM_CLIENT_HSMFD_REPLY:
|
|
|
|
case WIRE_HSM_SIGN_FUNDING_REPLY:
|
|
|
|
case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REPLY:
|
|
|
|
case WIRE_HSM_SIGN_WITHDRAWAL_REPLY:
|
|
|
|
case WIRE_HSM_SIGN_INVOICE_REPLY:
|
|
|
|
case WIRE_HSM_INIT_REPLY:
|
2017-11-30 17:07:38 +01:00
|
|
|
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_COMMITMENT_TX_REPLY:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_SIGN_TX_REPLY:
|
2018-07-23 04:23:02 +02:00
|
|
|
case WIRE_HSM_GET_PER_COMMITMENT_POINT_REPLY:
|
2018-08-17 07:06:35 +02:00
|
|
|
case WIRE_HSM_CHECK_FUTURE_SECRET_REPLY:
|
2018-07-23 04:23:03 +02:00
|
|
|
case WIRE_HSM_GET_CHANNEL_BASEPOINTS_REPLY:
|
2017-11-28 16:46:14 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_send(c->master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsmstatus_client_bad_request(NULL,
|
2017-11-28 16:46:14 +01:00
|
|
|
&c->id,
|
|
|
|
dc->msg_in)));
|
|
|
|
return io_close(conn);
|
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
static void send_init_response(struct daemon_conn *master)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
struct pubkey node_id;
|
2017-08-18 06:43:52 +02:00
|
|
|
u8 *msg;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
node_key(NULL, &node_id);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-07-23 04:23:03 +02:00
|
|
|
msg = towire_hsm_init_reply(NULL, &node_id, &secretstuff.bip32);
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(master, take(msg));
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
static void populate_secretstuff(void)
|
|
|
|
{
|
|
|
|
u8 bip32_seed[BIP32_ENTROPY_LEN_256];
|
|
|
|
u32 salt = 0;
|
2017-02-21 05:45:29 +01:00
|
|
|
struct ext_key master_extkey, child_extkey;
|
2017-02-21 05:45:28 +01:00
|
|
|
|
|
|
|
/* Fill in the BIP32 tree for bitcoin addresses. */
|
|
|
|
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(bip32_seed, sizeof(bip32_seed),
|
|
|
|
BIP32_VER_TEST_PRIVATE,
|
2017-02-21 05:45:29 +01:00
|
|
|
0, &master_extkey) != 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(&master_extkey, 0, BIP32_FLAG_KEY_PRIVATE,
|
|
|
|
&child_extkey) != WALLY_OK)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-02-21 05:45:29 +01:00
|
|
|
"Can't derive child bip32 key");
|
|
|
|
|
|
|
|
if (bip32_key_from_parent(&child_extkey, 0, BIP32_FLAG_KEY_PRIVATE,
|
|
|
|
&secretstuff.bip32) != WALLY_OK)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-02-21 05:45:29 +01:00
|
|
|
"Can't derive private bip32 key");
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
static void bitcoin_pubkey(struct pubkey *pubkey, u32 index)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
|
|
|
struct ext_key ext;
|
|
|
|
|
|
|
|
if (index >= BIP32_INITIAL_HARDENED_CHILD)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2017-02-21 05:45:29 +01:00
|
|
|
"Index %u too great", index);
|
|
|
|
|
|
|
|
if (bip32_key_from_parent(&secretstuff.bip32, index,
|
|
|
|
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-02-21 05:45:29 +01:00
|
|
|
"BIP32 of %u failed", index);
|
|
|
|
|
|
|
|
if (!secp256k1_ec_pubkey_parse(secp256k1_ctx, &pubkey->pubkey,
|
|
|
|
ext.pub_key, sizeof(ext.pub_key)))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-02-21 05:45:29 +01:00
|
|
|
"Parse of BIP32 child %u pubkey failed", index);
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
static void bitcoin_keypair(struct privkey *privkey,
|
|
|
|
struct pubkey *pubkey,
|
|
|
|
u32 index)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
|
|
|
struct ext_key ext;
|
|
|
|
|
|
|
|
if (index >= BIP32_INITIAL_HARDENED_CHILD)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_MASTER_IO,
|
2017-02-21 05:45:29 +01:00
|
|
|
"Index %u too great", index);
|
|
|
|
|
|
|
|
if (bip32_key_from_parent(&secretstuff.bip32, index,
|
|
|
|
BIP32_FLAG_KEY_PRIVATE, &ext) != WALLY_OK)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-02-21 05:45:29 +01:00
|
|
|
"BIP32 of %u failed", index);
|
|
|
|
|
|
|
|
/* libwally says: The private key with prefix byte 0 */
|
2017-05-06 04:19:44 +02:00
|
|
|
memcpy(privkey->secret.data, ext.priv_key+1, 32);
|
2017-02-21 05:45:29 +01:00
|
|
|
if (!secp256k1_ec_pubkey_create(secp256k1_ctx, &pubkey->pubkey,
|
2017-05-06 04:19:44 +02:00
|
|
|
privkey->secret.data))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-02-21 05:45:29 +01:00
|
|
|
"BIP32 pubkey %u create failed", index);
|
2017-02-21 05:45:28 +01:00
|
|
|
}
|
|
|
|
|
2018-05-03 14:20:28 +02:00
|
|
|
static void maybe_create_new_hsm(void)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
int fd = open("hsm_secret", O_CREAT|O_EXCL|O_WRONLY, 0400);
|
2018-05-03 14:20:28 +02:00
|
|
|
if (fd < 0) {
|
|
|
|
if (errno == EEXIST)
|
|
|
|
return;
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"creating: %s", strerror(errno));
|
2018-05-03 14:20:28 +02:00
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
randombytes_buf(&secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret));
|
|
|
|
if (!write_all(fd, &secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret))) {
|
2017-01-10 06:08:33 +01:00
|
|
|
unlink_noerr("hsm_secret");
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"writing: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
if (fsync(fd) != 0) {
|
|
|
|
unlink_noerr("hsm_secret");
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"fsync: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
if (close(fd) != 0) {
|
|
|
|
unlink_noerr("hsm_secret");
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"closing: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
fd = open(".", O_RDONLY);
|
2018-01-08 21:09:01 +01:00
|
|
|
if (fd < 0) {
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"opening: %s", strerror(errno));
|
|
|
|
}
|
2017-01-10 06:08:33 +01:00
|
|
|
if (fsync(fd) != 0) {
|
|
|
|
unlink_noerr("hsm_secret");
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"fsyncdir: %s", strerror(errno));
|
|
|
|
}
|
|
|
|
close(fd);
|
2018-05-03 14:20:28 +02:00
|
|
|
status_unusual("HSM: created new hsm_secret file");
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:39:07 +01:00
|
|
|
static void load_hsm(void)
|
2017-01-10 06:08:33 +01:00
|
|
|
{
|
|
|
|
int fd = open("hsm_secret", O_RDONLY);
|
|
|
|
if (fd < 0)
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"opening: %s", strerror(errno));
|
2017-02-21 05:45:28 +01:00
|
|
|
if (!read_all(fd, &secretstuff.hsm_secret, sizeof(secretstuff.hsm_secret)))
|
2017-09-12 06:55:52 +02:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
2017-01-10 06:08:33 +01:00
|
|
|
"reading: %s", strerror(errno));
|
|
|
|
close(fd);
|
|
|
|
|
2017-02-21 05:45:28 +01:00
|
|
|
populate_secretstuff();
|
2017-03-10 11:49:43 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
static void init_hsm(struct daemon_conn *master, const u8 *msg)
|
2017-03-10 11:49:43 +01:00
|
|
|
{
|
|
|
|
|
2018-05-03 14:20:28 +02:00
|
|
|
if (!fromwire_hsm_init(msg))
|
2017-11-30 17:07:38 +01:00
|
|
|
master_badmsg(WIRE_HSM_INIT, msg);
|
2017-09-12 06:55:52 +02:00
|
|
|
|
2018-05-03 14:20:28 +02:00
|
|
|
maybe_create_new_hsm();
|
|
|
|
load_hsm();
|
2017-02-21 05:45:28 +01:00
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
send_init_response(master);
|
2017-01-10 06:08:33 +01:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:46:14 +01:00
|
|
|
static void pass_client_hsmfd(struct daemon_conn *master, const u8 *msg)
|
|
|
|
{
|
|
|
|
int fds[2];
|
2018-07-09 13:17:59 +02:00
|
|
|
u64 dbid, capabilities;
|
2017-11-28 16:46:14 +01:00
|
|
|
struct pubkey id;
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
if (!fromwire_hsm_client_hsmfd(msg, &id, &dbid, &capabilities))
|
2017-11-30 17:07:38 +01:00
|
|
|
master_badmsg(WIRE_HSM_CLIENT_HSMFD, msg);
|
2017-11-28 16:46:14 +01:00
|
|
|
|
|
|
|
if (socketpair(AF_UNIX, SOCK_STREAM, 0, fds) != 0)
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR, "creating fds: %s", strerror(errno));
|
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
new_client(master, &id, dbid, capabilities, handle_client, fds[0]);
|
2017-11-28 16:46:14 +01:00
|
|
|
daemon_conn_send(master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsm_client_hsmfd_reply(NULL)));
|
2017-11-28 16:46:14 +01:00
|
|
|
daemon_conn_send_fd(master, fds[1]);
|
|
|
|
}
|
|
|
|
|
2018-01-02 16:34:08 +01:00
|
|
|
|
2018-07-09 13:17:58 +02:00
|
|
|
static void derive_peer_seed(struct secret *peer_seed, struct secret *peer_seed_base,
|
2018-01-02 16:34:08 +01:00
|
|
|
const struct pubkey *peer_id, const u64 channel_id)
|
|
|
|
{
|
|
|
|
u8 input[PUBKEY_DER_LEN + sizeof(channel_id)];
|
|
|
|
char *info = "per-peer seed";
|
|
|
|
pubkey_to_der(input, peer_id);
|
|
|
|
memcpy(input + PUBKEY_DER_LEN, &channel_id, sizeof(channel_id));
|
|
|
|
|
|
|
|
hkdf_sha256(peer_seed, sizeof(*peer_seed),
|
|
|
|
input, sizeof(input),
|
|
|
|
peer_seed_base, sizeof(*peer_seed_base),
|
|
|
|
info, strlen(info));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hsm_unilateral_close_privkey(struct privkey *dst,
|
|
|
|
struct unilateral_close_info *info)
|
|
|
|
{
|
2018-07-09 13:17:58 +02:00
|
|
|
struct secret peer_seed, peer_seed_base;
|
2018-01-02 16:34:08 +01:00
|
|
|
struct basepoints basepoints;
|
|
|
|
struct secrets secrets;
|
2018-07-09 13:17:58 +02:00
|
|
|
hsm_peer_secret_base(&peer_seed_base);
|
2018-01-02 16:34:08 +01:00
|
|
|
derive_peer_seed(&peer_seed, &peer_seed_base, &info->peer_id, info->channel_id);
|
|
|
|
derive_basepoints(&peer_seed, NULL, &basepoints, &secrets, NULL);
|
|
|
|
|
Check return value of derive_simple_privkey(...) call in hsm_unilateral_close_privkey(...)
All other users of derive_simple_privkey(...) check the return value:
```
channeld/channel.c: if (!derive_simple_privkey(&peer->our_secrets.htlc_basepoint_secret,
lightningd/test/run-commit_tx.c: if (!derive_simple_privkey(&x_remote_htlc_basepoint_secret,
lightningd/test/run-commit_tx.c: if (!derive_simple_privkey(&x_local_delayed_payment_basepoint_secret,
lightningd/test/run-commit_tx.c: if (!derive_simple_privkey(&x_local_htlc_basepoint_secret,
lightningd/test/run-key_derive.c: if (!derive_simple_privkey(&base_secret, &base_point,
onchaind/onchain.c: if (!derive_simple_privkey(&secrets->delayed_payment_basepoint_secret,
onchaind/onchain.c: if (!derive_simple_privkey(&secrets->payment_basepoint_secret,
onchaind/onchain.c: if (!derive_simple_privkey(&secrets->htlc_basepoint_secret,
onchaind/onchain.c: if (!derive_simple_privkey(&secrets->payment_basepoint_secret,
onchaind/onchain.c: if (!derive_simple_privkey(&secrets->htlc_basepoint_secret,
```
2018-01-09 22:33:42 +01:00
|
|
|
if (!derive_simple_privkey(&secrets.payment_basepoint_secret,
|
|
|
|
&basepoints.payment, &info->commitment_point,
|
|
|
|
dst)) {
|
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Deriving unilateral_close_privkey");
|
|
|
|
}
|
2018-01-02 16:34:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hsm_key_for_utxo - generate the keypair matching the utxo
|
|
|
|
*/
|
|
|
|
static void hsm_key_for_utxo(struct privkey *privkey, struct pubkey *pubkey,
|
|
|
|
const struct utxo *utxo)
|
|
|
|
{
|
|
|
|
if (utxo->close_info != NULL) {
|
|
|
|
/* This is a their_unilateral_close/to-us output, so
|
|
|
|
* we need to derive the secret the long way */
|
2018-02-05 05:09:27 +01:00
|
|
|
status_debug("Unilateral close output, deriving secrets");
|
2018-01-02 16:34:08 +01:00
|
|
|
hsm_unilateral_close_privkey(privkey, utxo->close_info);
|
|
|
|
pubkey_from_privkey(privkey, pubkey);
|
2018-03-15 05:30:38 +01:00
|
|
|
status_debug("Derived public key %s from unilateral close", type_to_string(tmpctx, struct pubkey, pubkey));
|
2018-01-02 16:34:08 +01:00
|
|
|
} else {
|
|
|
|
/* Simple case: just get derive via HD-derivation */
|
|
|
|
bitcoin_keypair(privkey, pubkey, utxo->keyindex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
/* Note that it's the main daemon that asks for the funding signature so it
|
|
|
|
* can broadcast it. */
|
2017-03-19 21:32:44 +01:00
|
|
|
static void sign_funding_tx(struct daemon_conn *master, const u8 *msg)
|
2017-02-21 05:45:29 +01:00
|
|
|
{
|
|
|
|
u64 satoshi_out, change_out;
|
|
|
|
u32 change_keyindex;
|
|
|
|
struct pubkey local_pubkey, remote_pubkey;
|
2018-02-08 02:24:46 +01:00
|
|
|
struct utxo **utxomap;
|
2017-02-21 05:45:29 +01:00
|
|
|
struct bitcoin_tx *tx;
|
2017-03-07 02:03:24 +01:00
|
|
|
u16 outnum;
|
2017-02-21 05:45:29 +01:00
|
|
|
size_t i;
|
2018-03-29 09:09:08 +02:00
|
|
|
struct pubkey *changekey;
|
2018-01-05 01:12:07 +01:00
|
|
|
u8 **scriptSigs;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
|
|
|
/* FIXME: Check fee is "reasonable" */
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_sign_funding(tmpctx, msg,
|
2017-11-30 17:07:38 +01:00
|
|
|
&satoshi_out, &change_out,
|
|
|
|
&change_keyindex, &local_pubkey,
|
2018-02-08 02:24:46 +01:00
|
|
|
&remote_pubkey, &utxomap))
|
2017-11-30 17:07:38 +01:00
|
|
|
master_badmsg(WIRE_HSM_SIGN_FUNDING, msg);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-03-29 03:59:27 +02:00
|
|
|
if (change_out) {
|
|
|
|
changekey = tal(tmpctx, struct pubkey);
|
|
|
|
bitcoin_pubkey(changekey, change_keyindex);
|
|
|
|
} else
|
|
|
|
changekey = NULL;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
tx = funding_tx(tmpctx, &outnum,
|
|
|
|
cast_const2(const struct utxo **, utxomap),
|
2017-03-07 01:58:42 +01:00
|
|
|
satoshi_out, &local_pubkey, &remote_pubkey,
|
2018-03-29 03:59:27 +02:00
|
|
|
change_out, changekey,
|
2017-03-07 02:01:55 +01:00
|
|
|
NULL);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
scriptSigs = tal_arr(tmpctx, u8*, tal_count(utxomap));
|
|
|
|
for (i = 0; i < tal_count(utxomap); i++) {
|
2017-02-21 05:45:29 +01:00
|
|
|
struct pubkey inkey;
|
|
|
|
struct privkey inprivkey;
|
2017-03-07 01:58:42 +01:00
|
|
|
const struct utxo *in = utxomap[i];
|
2017-02-21 05:45:29 +01:00
|
|
|
u8 *subscript;
|
2018-01-05 01:12:07 +01:00
|
|
|
secp256k1_ecdsa_signature sig;
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-01-02 16:34:08 +01:00
|
|
|
hsm_key_for_utxo(&inprivkey, &inkey, in);
|
|
|
|
|
2017-02-21 05:45:29 +01:00
|
|
|
if (in->is_p2sh)
|
2017-03-07 02:05:48 +01:00
|
|
|
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
|
2017-02-21 05:45:29 +01:00
|
|
|
else
|
|
|
|
subscript = NULL;
|
2018-03-25 21:51:11 +02:00
|
|
|
u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey);
|
2017-02-21 05:45:29 +01:00
|
|
|
|
2018-01-05 01:12:07 +01:00
|
|
|
sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey,
|
|
|
|
&sig);
|
|
|
|
|
|
|
|
tx->input[i].witness = bitcoin_witness_p2wpkh(tx, &sig, &inkey);
|
|
|
|
|
2018-02-08 02:24:46 +01:00
|
|
|
if (utxomap[i]->is_p2sh)
|
2018-01-05 01:12:07 +01:00
|
|
|
scriptSigs[i] = bitcoin_scriptsig_p2sh_p2wpkh(tx, &inkey);
|
|
|
|
else
|
|
|
|
scriptSigs[i] = NULL;
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
2018-01-05 01:12:07 +01:00
|
|
|
/* Now complete the transaction by attaching the scriptSigs where necessary */
|
2018-02-08 02:24:46 +01:00
|
|
|
for (size_t i=0; i<tal_count(utxomap); i++)
|
2018-01-05 01:12:07 +01:00
|
|
|
tx->input[i].script = scriptSigs[i];
|
|
|
|
|
2017-03-19 21:32:44 +01:00
|
|
|
daemon_conn_send(master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsm_sign_funding_reply(NULL, tx)));
|
2017-02-21 05:45:29 +01:00
|
|
|
}
|
|
|
|
|
2017-06-20 17:07:51 +02:00
|
|
|
/**
|
|
|
|
* sign_withdrawal_tx - Generate and sign a withdrawal transaction from the master
|
|
|
|
*/
|
|
|
|
static void sign_withdrawal_tx(struct daemon_conn *master, const u8 *msg)
|
|
|
|
{
|
|
|
|
u64 satoshi_out, change_out;
|
|
|
|
u32 change_keyindex;
|
2018-02-08 02:24:46 +01:00
|
|
|
struct utxo **utxos;
|
2018-01-05 00:45:11 +01:00
|
|
|
u8 **scriptSigs;
|
2017-06-20 17:07:51 +02:00
|
|
|
struct bitcoin_tx *tx;
|
|
|
|
struct ext_key ext;
|
|
|
|
struct pubkey changekey;
|
2017-12-10 06:48:18 +01:00
|
|
|
u8 *scriptpubkey;
|
2017-06-20 17:07:51 +02:00
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_sign_withdrawal(tmpctx, msg, &satoshi_out,
|
2017-11-30 17:07:38 +01:00
|
|
|
&change_out, &change_keyindex,
|
2018-02-08 02:24:46 +01:00
|
|
|
&scriptpubkey, &utxos)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Failed to parse sign_withdrawal: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg));
|
2017-06-20 17:07:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bip32_key_from_parent(&secretstuff.bip32, change_keyindex,
|
|
|
|
BIP32_FLAG_KEY_PUBLIC, &ext) != WALLY_OK) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Failed to parse sign_withdrawal: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg));
|
2017-06-20 17:07:51 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pubkey_from_der(ext.pub_key, sizeof(ext.pub_key), &changekey);
|
|
|
|
tx = withdraw_tx(
|
2018-02-08 02:24:46 +01:00
|
|
|
tmpctx, cast_const2(const struct utxo **, utxos),
|
|
|
|
scriptpubkey, satoshi_out,
|
2017-06-20 17:07:51 +02:00
|
|
|
&changekey, change_out, NULL);
|
|
|
|
|
2018-01-05 00:45:11 +01:00
|
|
|
scriptSigs = tal_arr(tmpctx, u8*, tal_count(utxos));
|
2017-06-20 17:07:51 +02:00
|
|
|
for (size_t i = 0; i < tal_count(utxos); i++) {
|
|
|
|
struct pubkey inkey;
|
|
|
|
struct privkey inprivkey;
|
2017-12-19 20:34:00 +01:00
|
|
|
const struct utxo *in = utxos[i];
|
2017-06-20 17:07:51 +02:00
|
|
|
u8 *subscript;
|
2018-01-05 00:45:11 +01:00
|
|
|
secp256k1_ecdsa_signature sig;
|
2017-06-20 17:07:51 +02:00
|
|
|
|
2018-01-02 16:34:08 +01:00
|
|
|
hsm_key_for_utxo(&inprivkey, &inkey, in);
|
|
|
|
|
|
|
|
if (in->is_p2sh || in->close_info != NULL)
|
2017-12-19 20:34:00 +01:00
|
|
|
subscript = bitcoin_redeem_p2sh_p2wpkh(tmpctx, &inkey);
|
|
|
|
else
|
|
|
|
subscript = NULL;
|
2018-03-25 21:51:11 +02:00
|
|
|
u8 *wscript = p2wpkh_scriptcode(tmpctx, &inkey);
|
2017-06-20 17:07:51 +02:00
|
|
|
|
2018-01-05 00:45:11 +01:00
|
|
|
sign_tx_input(tx, i, subscript, wscript, &inprivkey, &inkey,
|
|
|
|
&sig);
|
|
|
|
|
|
|
|
tx->input[i].witness = bitcoin_witness_p2wpkh(tx, &sig, &inkey);
|
|
|
|
|
|
|
|
if (utxos[i]->is_p2sh)
|
|
|
|
scriptSigs[i] = bitcoin_scriptsig_p2sh_p2wpkh(tx, &inkey);
|
|
|
|
else
|
|
|
|
scriptSigs[i] = NULL;
|
2017-06-20 17:07:51 +02:00
|
|
|
}
|
|
|
|
|
2018-01-05 00:45:11 +01:00
|
|
|
/* Now complete the transaction by attaching the scriptSigs where necessary */
|
|
|
|
for (size_t i=0; i<tal_count(utxos); i++)
|
|
|
|
tx->input[i].script = scriptSigs[i];
|
|
|
|
|
2017-06-20 17:07:51 +02:00
|
|
|
daemon_conn_send(master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsm_sign_withdrawal_reply(NULL, tx)));
|
2017-06-20 17:07:51 +02:00
|
|
|
}
|
|
|
|
|
2017-10-26 05:03:19 +02:00
|
|
|
/**
|
|
|
|
* sign_invoice - Sign an invoice with our key.
|
|
|
|
*/
|
|
|
|
static void sign_invoice(struct daemon_conn *master, const u8 *msg)
|
|
|
|
{
|
|
|
|
u5 *u5bytes;
|
|
|
|
u8 *hrpu8;
|
|
|
|
char *hrp;
|
|
|
|
struct sha256 sha;
|
|
|
|
secp256k1_ecdsa_recoverable_signature rsig;
|
|
|
|
struct hash_u5 hu5;
|
|
|
|
struct privkey node_pkey;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_sign_invoice(tmpctx, msg, &u5bytes, &hrpu8)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_broken("Failed to parse sign_invoice: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg));
|
2017-10-26 05:03:19 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME: Check invoice! */
|
|
|
|
|
2018-07-28 08:00:16 +02:00
|
|
|
hrp = tal_dup_arr(tmpctx, char, (char *)hrpu8, tal_count(hrpu8), 1);
|
|
|
|
hrp[tal_count(hrpu8)] = '\0';
|
2017-10-26 05:03:19 +02:00
|
|
|
|
|
|
|
hash_u5_init(&hu5, hrp);
|
2018-07-28 08:00:16 +02:00
|
|
|
hash_u5(&hu5, u5bytes, tal_count(u5bytes));
|
2017-10-26 05:03:19 +02:00
|
|
|
hash_u5_done(&hu5, &sha);
|
|
|
|
|
|
|
|
node_key(&node_pkey, NULL);
|
|
|
|
if (!secp256k1_ecdsa_sign_recoverable(secp256k1_ctx, &rsig,
|
|
|
|
(const u8 *)&sha,
|
|
|
|
node_pkey.secret.data,
|
|
|
|
NULL, NULL)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_failed(STATUS_FAIL_INTERNAL_ERROR,
|
|
|
|
"Failed to sign invoice: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg));
|
2017-10-26 05:03:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
daemon_conn_send(master,
|
2018-03-15 07:10:22 +01:00
|
|
|
take(towire_hsm_sign_invoice_reply(NULL, &rsig)));
|
2017-10-26 05:03:19 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 01:59:48 +02:00
|
|
|
static void sign_node_announcement(struct daemon_conn *master, const u8 *msg)
|
|
|
|
{
|
|
|
|
/* 2 bytes msg type + 64 bytes signature */
|
|
|
|
size_t offset = 66;
|
|
|
|
struct sha256_double hash;
|
|
|
|
struct privkey node_pkey;
|
|
|
|
secp256k1_ecdsa_signature sig;
|
|
|
|
u8 *reply;
|
|
|
|
u8 *ann;
|
|
|
|
|
2018-02-20 21:59:09 +01:00
|
|
|
if (!fromwire_hsm_node_announcement_sig_req(msg, msg, &ann)) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
|
|
"Failed to parse node_announcement_sig_req: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg));
|
2017-05-07 01:59:48 +02:00
|
|
|
}
|
|
|
|
|
2018-07-28 08:00:16 +02:00
|
|
|
if (tal_count(ann) < offset) {
|
2018-02-05 05:09:27 +01:00
|
|
|
status_failed(STATUS_FAIL_GOSSIP_IO,
|
|
|
|
"Node announcement too short: %s",
|
2018-03-15 05:30:38 +01:00
|
|
|
tal_hex(tmpctx, msg));
|
2017-05-07 01:59:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* FIXME(cdecker) Check the node announcement's content */
|
|
|
|
node_key(&node_pkey, NULL);
|
2018-07-28 08:00:16 +02:00
|
|
|
sha256_double(&hash, ann + offset, tal_count(ann) - offset);
|
2017-05-07 01:59:48 +02:00
|
|
|
|
|
|
|
sign_hash(&node_pkey, &hash, &sig);
|
|
|
|
|
2018-03-15 07:10:22 +01:00
|
|
|
reply = towire_hsm_node_announcement_sig_reply(NULL, &sig);
|
2017-05-07 01:59:48 +02:00
|
|
|
daemon_conn_send(master, take(reply));
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
#ifndef TESTING
|
2017-09-12 06:56:59 +02:00
|
|
|
/* FIXME: This is used by debug.c, but doesn't apply to us. */
|
|
|
|
extern void dev_disconnect_init(int fd);
|
2018-02-21 16:06:07 +01:00
|
|
|
void dev_disconnect_init(int fd UNUSED)
|
2017-09-12 06:56:59 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-21 16:06:07 +01:00
|
|
|
static void master_gone(struct io_conn *unused UNUSED, struct daemon_conn *dc UNUSED)
|
2017-06-06 05:08:42 +02:00
|
|
|
{
|
|
|
|
/* Can't tell master, it's gone. */
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2018-04-25 12:55:34 +02:00
|
|
|
setup_locale();
|
|
|
|
|
2017-11-29 12:09:06 +01:00
|
|
|
struct client *client;
|
2018-08-23 14:05:20 +02:00
|
|
|
struct daemon_conn *status_conn = tal(NULL, struct daemon_conn);
|
2017-01-10 06:08:33 +01:00
|
|
|
|
2018-01-08 11:01:09 +01:00
|
|
|
subdaemon_setup(argc, argv);
|
2018-08-23 14:05:20 +02:00
|
|
|
|
|
|
|
/* A trivial daemon_conn just for writing. */
|
|
|
|
daemon_conn_init(status_conn, status_conn, STDIN_FILENO,
|
|
|
|
(void *)io_never, NULL);
|
|
|
|
status_setup_async(status_conn);
|
2017-09-12 06:56:59 +02:00
|
|
|
|
2018-07-09 13:17:59 +02:00
|
|
|
client = new_client(NULL, NULL, 0, HSM_CAP_MASTER | HSM_CAP_SIGN_GOSSIP, handle_client, REQ_FD);
|
2017-11-29 12:09:06 +01:00
|
|
|
|
|
|
|
/* We're our own master! */
|
|
|
|
client->master = &client->dc;
|
|
|
|
io_set_finish(client->dc.conn, master_gone, &client->dc);
|
|
|
|
|
2017-03-10 11:49:43 +01:00
|
|
|
/* When conn closes, everything is freed. */
|
2017-11-29 12:09:06 +01:00
|
|
|
tal_steal(client->dc.conn, client);
|
2017-01-10 06:08:33 +01:00
|
|
|
io_loop(NULL, NULL);
|
2018-03-29 04:06:45 +02:00
|
|
|
daemon_shutdown();
|
2018-03-28 04:30:49 +02:00
|
|
|
|
2017-01-10 06:08:33 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|