Skip to content

Commit

Permalink
hsmd: Add hsmd_new_channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ksedgwic authored and rustyrussell committed Dec 14, 2021
1 parent e8f43ef commit bb574be
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions hsmd/hsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_DEV_MEMLEAK:
#endif /* DEVELOPER */

case WIRE_HSMD_NEW_CHANNEL:
case WIRE_HSMD_SIGN_COMMITMENT_TX:
case WIRE_HSMD_VALIDATE_REVOCATION:
case WIRE_HSMD_SIGN_PENALTY_TO_US:
Expand Down Expand Up @@ -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_CUPDATE_SIG_REPLY:
case WIRE_HSMD_CLIENT_HSMFD_REPLY:
case WIRE_HSMD_NEW_CHANNEL_REPLY:
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
case WIRE_HSMD_SIGN_INVOICE_REPLY:
Expand Down
8 changes: 8 additions & 0 deletions hsmd/hsmd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ msgdata,hsmd_init_reply,bip32,ext_key,
msgdata,hsmd_init_reply,bolt12,point32,
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
msgtype,hsmd_client_hsmfd,9
# Which identity to use for requests
Expand Down
20 changes: 20 additions & 0 deletions hsmd/libhsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
return (client->capabilities & HSM_CAP_SIGN_WILL_FUND_OFFER) != 0;

case WIRE_HSMD_INIT:
case WIRE_HSMD_NEW_CHANNEL:
case WIRE_HSMD_CLIENT_HSMFD:
case WIRE_HSMD_SIGN_WITHDRAWAL:
case WIRE_HSMD_SIGN_INVOICE:
Expand All @@ -123,6 +124,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
case WIRE_HSMD_CANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_CUPDATE_SIG_REPLY:
case WIRE_HSMD_CLIENT_HSMFD_REPLY:
case WIRE_HSMD_NEW_CHANNEL_REPLY:
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
case WIRE_HSMD_SIGN_INVOICE_REPLY:
Expand Down Expand Up @@ -279,6 +281,21 @@ static void get_channel_seed(const struct node_id *peer_id, u64 dbid,
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
* 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
Expand Down Expand Up @@ -1412,6 +1429,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
"libhsmd",
hsmd_wire_name(t));

case WIRE_HSMD_NEW_CHANNEL:
return handle_new_channel(client, msg);
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
return handle_get_output_scriptpubkey(client, msg);
case WIRE_HSMD_CHECK_FUTURE_SECRET:
Expand Down Expand Up @@ -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_CUPDATE_SIG_REPLY:
case WIRE_HSMD_CLIENT_HSMFD_REPLY:
case WIRE_HSMD_NEW_CHANNEL_REPLY:
case WIRE_HSMD_NODE_ANNOUNCEMENT_SIG_REPLY:
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY:
case WIRE_HSMD_SIGN_INVOICE_REPLY:
Expand Down
9 changes: 9 additions & 0 deletions lightningd/channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ struct channel *new_unsaved_channel(struct peer *peer,
{
struct lightningd *ld = peer->ld;
struct channel *channel = tal(ld, struct channel);
u8 *msg;

channel->peer = peer;
/* Not saved to the database yet! */
Expand Down Expand Up @@ -284,6 +285,14 @@ struct channel *new_unsaved_channel(struct peer *peer,
channel->their_shachain.id = 0;
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,
&channel->local_basepoints,
&channel->local_funding_pubkey);
Expand Down
13 changes: 13 additions & 0 deletions lightningd/opening_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
#include <common/type_to_string.h>
#include <common/wire_error.h>
#include <connectd/connectd_wiregen.h>
#include <errno.h>
#include <hsmd/hsmd_wiregen.h>
#include <lightningd/channel.h>
#include <lightningd/channel_control.h>
#include <lightningd/notification.h>
#include <lightningd/opening_common.h>
#include <lightningd/peer_control.h>
#include <lightningd/subd.h>
#include <openingd/openingd_wiregen.h>
#include <wire/wire_sync.h>

static void destroy_uncommitted_channel(struct uncommitted_channel *uc)
{
Expand All @@ -34,6 +37,7 @@ new_uncommitted_channel(struct peer *peer)
{
struct lightningd *ld = peer->ld;
struct uncommitted_channel *uc = tal(ld, struct uncommitted_channel);
u8 *new_channel_msg;

uc->peer = peer;
assert(!peer->uncommitted_channel);
Expand All @@ -49,6 +53,15 @@ new_uncommitted_channel(struct peer *peer)

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,
&uc->local_basepoints, &uc->local_funding_pubkey);

Expand Down
4 changes: 4 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
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(); }
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 */
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(); }
Expand Down

0 comments on commit bb574be

Please sign in to comment.