hsmd: Add hsmd_new_channel

This commit is contained in:
Ken Sedgwick 2021-11-04 10:02:12 -07:00 committed by Rusty Russell
parent e8f43ef6ca
commit bb574be839
6 changed files with 56 additions and 0 deletions

View file

@ -640,6 +640,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_DEV_MEMLEAK: case WIRE_HSMD_DEV_MEMLEAK:
#endif /* DEVELOPER */ #endif /* DEVELOPER */
case WIRE_HSMD_NEW_CHANNEL:
case WIRE_HSMD_SIGN_COMMITMENT_TX: case WIRE_HSMD_SIGN_COMMITMENT_TX:
case WIRE_HSMD_VALIDATE_REVOCATION: case WIRE_HSMD_VALIDATE_REVOCATION:
case WIRE_HSMD_SIGN_PENALTY_TO_US: case WIRE_HSMD_SIGN_PENALTY_TO_US:
@ -671,6 +672,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_CUPDATE_SIG_REPLY: case WIRE_HSMD_CUPDATE_SIG_REPLY:
case WIRE_HSMD_CLIENT_HSMFD_REPLY: case WIRE_HSMD_CLIENT_HSMFD_REPLY:
case WIRE_HSMD_NEW_CHANNEL_REPLY:
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY: case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
case WIRE_HSMD_SIGN_INVOICE_REPLY: case WIRE_HSMD_SIGN_INVOICE_REPLY:

View file

@ -23,6 +23,14 @@ msgdata,hsmd_init_reply,bip32,ext_key,
msgdata,hsmd_init_reply,bolt12,point32, msgdata,hsmd_init_reply,bolt12,point32,
msgdata,hsmd_init_reply,onion_reply_secret,secret, msgdata,hsmd_init_reply,onion_reply_secret,secret,
# Declare a new channel.
msgtype,hsmd_new_channel,30
msgdata,hsmd_new_channel,id,node_id,
msgdata,hsmd_new_channel,dbid,u64,
# No value returned.
msgtype,hsmd_new_channel_reply,130
# Get a new HSM FD, with the specified capabilities # Get a new HSM FD, with the specified capabilities
msgtype,hsmd_client_hsmfd,9 msgtype,hsmd_client_hsmfd,9
# Which identity to use for requests # Which identity to use for requests

1 # Clients should not give a bad request but not the HSM's decision to crash.
23 # Get a new HSM FD, with the specified capabilities # Declare a new channel.
24 msgtype,hsmd_client_hsmfd,9 msgtype,hsmd_new_channel,30
25 # Which identity to use for requests msgdata,hsmd_new_channel,id,node_id,
26 msgdata,hsmd_new_channel,dbid,u64,
27 # No value returned.
28 msgtype,hsmd_new_channel_reply,130
29 # Get a new HSM FD, with the specified capabilities
30 msgtype,hsmd_client_hsmfd,9
31 # Which identity to use for requests
32 msgdata,hsmd_client_hsmfd,id,node_id,
33 # Database id for this client, if any.
34 msgdata,hsmd_client_hsmfd,id,node_id, msgdata,hsmd_client_hsmfd,dbid,u64,
35 # Database id for this client, if any. msgdata,hsmd_client_hsmfd,capabilities,u64,
36 msgdata,hsmd_client_hsmfd,dbid,u64, # No content, just an fd.

View file

@ -105,6 +105,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
return (client->capabilities & HSM_CAP_SIGN_WILL_FUND_OFFER) != 0; return (client->capabilities & HSM_CAP_SIGN_WILL_FUND_OFFER) != 0;
case WIRE_HSMD_INIT: case WIRE_HSMD_INIT:
case WIRE_HSMD_NEW_CHANNEL:
case WIRE_HSMD_CLIENT_HSMFD: case WIRE_HSMD_CLIENT_HSMFD:
case WIRE_HSMD_SIGN_WITHDRAWAL: case WIRE_HSMD_SIGN_WITHDRAWAL:
case WIRE_HSMD_SIGN_INVOICE: case WIRE_HSMD_SIGN_INVOICE:
@ -123,6 +124,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_CUPDATE_SIG_REPLY: case WIRE_HSMD_CUPDATE_SIG_REPLY:
case WIRE_HSMD_CLIENT_HSMFD_REPLY: case WIRE_HSMD_CLIENT_HSMFD_REPLY:
case WIRE_HSMD_NEW_CHANNEL_REPLY:
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY: case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
case WIRE_HSMD_SIGN_INVOICE_REPLY: case WIRE_HSMD_SIGN_INVOICE_REPLY:
@ -279,6 +281,21 @@ static void get_channel_seed(const struct node_id *peer_id, u64 dbid,
info, strlen(info)); info, strlen(info));
} }
/* ~This stub implementation is overriden by fully validating signers
* that need to manage per-channel state. */
static u8 *handle_new_channel(struct hsmd_client *c, const u8 *msg_in)
{
struct node_id peer_id;
u64 dbid;
if (!fromwire_hsmd_new_channel(msg_in, &peer_id, &dbid))
return hsmd_status_malformed_request(c, msg_in);
/* Stub implementation */
return towire_hsmd_new_channel_reply(NULL);
}
/*~ For almost every wallet tx we use the BIP32 seed, but not for onchain /*~ For almost every wallet tx we use the BIP32 seed, but not for onchain
* unilateral closes from a peer: they (may) have an output to us using a * unilateral closes from a peer: they (may) have an output to us using a
* public key based on the channel basepoints. It's a bit spammy to spend * public key based on the channel basepoints. It's a bit spammy to spend
@ -1412,6 +1429,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
"libhsmd", "libhsmd",
hsmd_wire_name(t)); hsmd_wire_name(t));
case WIRE_HSMD_NEW_CHANNEL:
return handle_new_channel(client, msg);
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY: case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
return handle_get_output_scriptpubkey(client, msg); return handle_get_output_scriptpubkey(client, msg);
case WIRE_HSMD_CHECK_FUTURE_SECRET: case WIRE_HSMD_CHECK_FUTURE_SECRET:
@ -1462,6 +1481,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_CUPDATE_SIG_REPLY: case WIRE_HSMD_CUPDATE_SIG_REPLY:
case WIRE_HSMD_CLIENT_HSMFD_REPLY: case WIRE_HSMD_CLIENT_HSMFD_REPLY:
case WIRE_HSMD_NEW_CHANNEL_REPLY:
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY: case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY: case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
case WIRE_HSMD_SIGN_INVOICE_REPLY: case WIRE_HSMD_SIGN_INVOICE_REPLY:

View file

@ -218,6 +218,7 @@ struct channel *new_unsaved_channel(struct peer *peer,
{ {
struct lightningd *ld = peer->ld; struct lightningd *ld = peer->ld;
struct channel *channel = tal(ld, struct channel); struct channel *channel = tal(ld, struct channel);
u8 *msg;
channel->peer = peer; channel->peer = peer;
/* Not saved to the database yet! */ /* Not saved to the database yet! */
@ -284,6 +285,14 @@ struct channel *new_unsaved_channel(struct peer *peer,
channel->their_shachain.id = 0; channel->their_shachain.id = 0;
shachain_init(&channel->their_shachain.chain); shachain_init(&channel->their_shachain.chain);
msg = towire_hsmd_new_channel(NULL, &peer->id, channel->unsaved_dbid);
if (!wire_sync_write(ld->hsm_fd, take(msg)))
fatal("Could not write to HSM: %s", strerror(errno));
msg = wire_sync_read(tmpctx, ld->hsm_fd);
if (!fromwire_hsmd_new_channel_reply(msg))
fatal("HSM gave bad hsm_new_channel_reply %s",
tal_hex(msg, msg));
get_channel_basepoints(ld, &peer->id, channel->unsaved_dbid, get_channel_basepoints(ld, &peer->id, channel->unsaved_dbid,
&channel->local_basepoints, &channel->local_basepoints,
&channel->local_funding_pubkey); &channel->local_funding_pubkey);

View file

@ -4,6 +4,8 @@
#include <common/type_to_string.h> #include <common/type_to_string.h>
#include <common/wire_error.h> #include <common/wire_error.h>
#include <connectd/connectd_wiregen.h> #include <connectd/connectd_wiregen.h>
#include <errno.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/channel.h> #include <lightningd/channel.h>
#include <lightningd/channel_control.h> #include <lightningd/channel_control.h>
#include <lightningd/notification.h> #include <lightningd/notification.h>
@ -11,6 +13,7 @@
#include <lightningd/peer_control.h> #include <lightningd/peer_control.h>
#include <lightningd/subd.h> #include <lightningd/subd.h>
#include <openingd/openingd_wiregen.h> #include <openingd/openingd_wiregen.h>
#include <wire/wire_sync.h>
static void destroy_uncommitted_channel(struct uncommitted_channel *uc) static void destroy_uncommitted_channel(struct uncommitted_channel *uc)
{ {
@ -34,6 +37,7 @@ new_uncommitted_channel(struct peer *peer)
{ {
struct lightningd *ld = peer->ld; struct lightningd *ld = peer->ld;
struct uncommitted_channel *uc = tal(ld, struct uncommitted_channel); struct uncommitted_channel *uc = tal(ld, struct uncommitted_channel);
u8 *new_channel_msg;
uc->peer = peer; uc->peer = peer;
assert(!peer->uncommitted_channel); assert(!peer->uncommitted_channel);
@ -49,6 +53,15 @@ new_uncommitted_channel(struct peer *peer)
memset(&uc->cid, 0xFF, sizeof(uc->cid)); memset(&uc->cid, 0xFF, sizeof(uc->cid));
/* Declare the new channel to the HSM. */
new_channel_msg = towire_hsmd_new_channel(NULL, &uc->peer->id, uc->dbid);
if (!wire_sync_write(ld->hsm_fd, take(new_channel_msg)))
fatal("Could not write to HSM: %s", strerror(errno));
new_channel_msg = wire_sync_read(tmpctx, ld->hsm_fd);
if (!fromwire_hsmd_new_channel_reply(new_channel_msg))
fatal("HSM gave bad hsm_new_channel_reply %s",
tal_hex(new_channel_msg, new_channel_msg));
get_channel_basepoints(ld, &uc->peer->id, uc->dbid, get_channel_basepoints(ld, &uc->peer->id, uc->dbid,
&uc->local_basepoints, &uc->local_funding_pubkey); &uc->local_basepoints, &uc->local_funding_pubkey);

View file

@ -138,6 +138,10 @@ bool fromwire_custommsg_in(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u8
/* Generated stub for fromwire_gossipd_get_stripped_cupdate_reply */ /* Generated stub for fromwire_gossipd_get_stripped_cupdate_reply */
bool fromwire_gossipd_get_stripped_cupdate_reply(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u8 **stripped_update UNNEEDED) bool fromwire_gossipd_get_stripped_cupdate_reply(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u8 **stripped_update UNNEEDED)
{ fprintf(stderr, "fromwire_gossipd_get_stripped_cupdate_reply called!\n"); abort(); } { fprintf(stderr, "fromwire_gossipd_get_stripped_cupdate_reply called!\n"); abort(); }
u8 *towire_hsmd_new_channel(const tal_t *ctx UNNEEDED, const struct node_id *id UNNEEDED, u64 dbid UNNEEDED)
{ fprintf(stderr, "towire_hsmd_new_channel called!\n"); abort(); }
bool fromwire_hsmd_new_channel_reply(const void *p UNNEEDED)
{ fprintf(stderr, "fromwire_hsmd_new_channel_reply called!\n"); abort(); }
/* Generated stub for fromwire_hsmd_get_output_scriptpubkey_reply */ /* Generated stub for fromwire_hsmd_get_output_scriptpubkey_reply */
bool fromwire_hsmd_get_output_scriptpubkey_reply(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u8 **script UNNEEDED) bool fromwire_hsmd_get_output_scriptpubkey_reply(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, u8 **script UNNEEDED)
{ fprintf(stderr, "fromwire_hsmd_get_output_scriptpubkey_reply called!\n"); abort(); } { fprintf(stderr, "fromwire_hsmd_get_output_scriptpubkey_reply called!\n"); abort(); }