Skip to content

Commit 6be965f

Browse files
committed
5/25 15:06
1 parent f1426ba commit 6be965f

File tree

4 files changed

+160
-0
lines changed

4 files changed

+160
-0
lines changed

hsmd/hsm_wire.csv

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ msgtype,hsm_init_reply,111
2121
msgdata,hsm_init_reply,node_id,node_id,
2222
msgdata,hsm_init_reply,bip32,ext_key,
2323

24+
# Declare a new channel.
25+
msgtype,hsm_new_channel,25
26+
# Which identity to use for requests
27+
msgdata,hsm_new_channel,id,node_id,
28+
# Database id for this client.
29+
msgdata,hsm_new_channel,dbid,u64,
30+
31+
# No value returned.
32+
msgtype,hsm_new_channel_reply,125
33+
2434
# Get a new HSM FD, with the specified capabilities
2535
msgtype,hsm_client_hsmfd,9
2636
# Which identity to use for requests
@@ -42,6 +52,26 @@ msgtype,hsm_get_channel_basepoints_reply,110
4252
msgdata,hsm_get_channel_basepoints_reply,basepoints,basepoints,
4353
msgdata,hsm_get_channel_basepoints_reply,funding_pubkey,pubkey,
4454

55+
# Provide channel parameters.
56+
msgtype,hsm_ready_channel,24
57+
msgdata,hsm_ready_channel,is_outbound,bool,
58+
msgdata,hsm_ready_channel,channel_value,amount_sat,
59+
msgdata,hsm_ready_channel,push_value,amount_msat,
60+
msgdata,hsm_ready_channel,funding_txid,bitcoin_txid,
61+
msgdata,hsm_ready_channel,funding_txout,u16,
62+
msgdata,hsm_ready_channel,local_to_self_delay,u16,
63+
msgdata,hsm_ready_channel,local_shutdown_script_len,u16,
64+
msgdata,hsm_ready_channel,local_shutdown_script,u8,local_shutdown_script_len
65+
msgdata,hsm_ready_channel,remote_basepoints,basepoints,
66+
msgdata,hsm_ready_channel,remote_funding_pubkey,pubkey,
67+
msgdata,hsm_ready_channel,remote_to_self_delay,u16,
68+
msgdata,hsm_ready_channel,remote_shutdown_script_len,u16,
69+
msgdata,hsm_ready_channel,remote_shutdown_script,u8,remote_shutdown_script_len
70+
msgdata,hsm_ready_channel,option_static_remotekey,bool,
71+
72+
# No value returned.
73+
msgtype,hsm_ready_channel_reply,124
74+
4575
# Return signature for a funding tx.
4676
#include <common/utxo.h>
4777
# FIXME: This should also take their commit sig & details, to verify.

hsmd/hsmd.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1491,6 +1491,74 @@ static struct io_plan *pass_client_hsmfd(struct io_conn *conn,
14911491
send_pending_client_fd, c);
14921492
}
14931493

1494+
/*~ This is used to declare a new channel. */
1495+
static struct io_plan *handle_new_channel(struct io_conn *conn,
1496+
struct client *c,
1497+
const u8 *msg_in)
1498+
{
1499+
struct node_id peer_id;
1500+
u64 dbid;
1501+
1502+
if (!fromwire_hsm_new_channel(msg_in, &peer_id, &dbid))
1503+
return bad_req(conn, c, msg_in);
1504+
1505+
return req_reply(conn, c,
1506+
take(towire_hsm_new_channel_reply(NULL)));
1507+
}
1508+
1509+
static bool mem_is_zero(const void *mem, size_t len)
1510+
{
1511+
size_t i;
1512+
for (i = 0; i < len; ++i)
1513+
if (((const unsigned char *)mem)[i])
1514+
return false;
1515+
return true;
1516+
}
1517+
1518+
/*~ This is used to provide all unchanging public channel parameters. */
1519+
static struct io_plan *handle_ready_channel(struct io_conn *conn,
1520+
struct client *c,
1521+
const u8 *msg_in)
1522+
{
1523+
bool is_outbound;
1524+
struct amount_sat channel_value;
1525+
struct amount_msat push_value;
1526+
struct bitcoin_txid funding_txid;
1527+
u16 funding_txout;
1528+
u16 local_to_self_delay;
1529+
u8 *local_shutdown_script;
1530+
struct basepoints remote_basepoints;
1531+
struct pubkey remote_funding_pubkey;
1532+
u16 remote_to_self_delay;
1533+
u8 *remote_shutdown_script;
1534+
bool option_static_remotekey;
1535+
struct amount_msat value_msat;
1536+
1537+
if (!fromwire_hsm_ready_channel(tmpctx, msg_in, &is_outbound,
1538+
&channel_value, &push_value, &funding_txid,
1539+
&funding_txout, &local_to_self_delay,
1540+
&local_shutdown_script,
1541+
&remote_basepoints,
1542+
&remote_funding_pubkey,
1543+
&remote_to_self_delay,
1544+
&remote_shutdown_script,
1545+
&option_static_remotekey))
1546+
return bad_req(conn, c, msg_in);
1547+
1548+
/* Fail fast if any values are obviously uninitialized. */
1549+
assert(amount_sat_greater(channel_value, AMOUNT_SAT(0)));
1550+
assert(amount_sat_to_msat(&value_msat, channel_value));
1551+
assert(amount_msat_less_eq(push_value, value_msat));
1552+
assert(!mem_is_zero(&funding_txid, sizeof(funding_txid)));
1553+
assert(local_to_self_delay > 0);
1554+
assert(!mem_is_zero(&remote_basepoints, sizeof(remote_basepoints)));
1555+
assert(!mem_is_zero(&remote_funding_pubkey, sizeof(remote_funding_pubkey)));
1556+
assert(remote_to_self_delay > 0);
1557+
1558+
return req_reply(conn, c,
1559+
take(towire_hsm_ready_channel_reply(NULL)));
1560+
}
1561+
14941562
/*~ For almost every wallet tx we use the BIP32 seed, but not for onchain
14951563
* unilateral closes from a peer: they (may) have an output to us using a
14961564
* public key based on the channel basepoints. It's a bit spammy to spend
@@ -1891,6 +1959,7 @@ static bool check_client_capabilities(struct client *client,
18911959

18921960
case WIRE_HSM_GET_PER_COMMITMENT_POINT:
18931961
case WIRE_HSM_CHECK_FUTURE_SECRET:
1962+
case WIRE_HSM_READY_CHANNEL:
18941963
return (client->capabilities & HSM_CAP_COMMITMENT_POINT) != 0;
18951964

18961965
case WIRE_HSM_SIGN_REMOTE_COMMITMENT_TX:
@@ -1901,6 +1970,7 @@ static bool check_client_capabilities(struct client *client,
19011970
return (client->capabilities & HSM_CAP_SIGN_CLOSING_TX) != 0;
19021971

19031972
case WIRE_HSM_INIT:
1973+
case WIRE_HSM_NEW_CHANNEL:
19041974
case WIRE_HSM_CLIENT_HSMFD:
19051975
case WIRE_HSM_SIGN_FUNDING:
19061976
case WIRE_HSM_SIGN_WITHDRAWAL:
@@ -1918,6 +1988,8 @@ static bool check_client_capabilities(struct client *client,
19181988
case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY:
19191989
case WIRE_HSM_CUPDATE_SIG_REPLY:
19201990
case WIRE_HSM_CLIENT_HSMFD_REPLY:
1991+
case WIRE_HSM_NEW_CHANNEL_REPLY:
1992+
case WIRE_HSM_READY_CHANNEL_REPLY:
19211993
case WIRE_HSM_SIGN_FUNDING_REPLY:
19221994
case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REPLY:
19231995
case WIRE_HSM_SIGN_WITHDRAWAL_REPLY:
@@ -1957,6 +2029,12 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
19572029
case WIRE_HSM_CLIENT_HSMFD:
19582030
return pass_client_hsmfd(conn, c, c->msg_in);
19592031

2032+
case WIRE_HSM_NEW_CHANNEL:
2033+
return handle_new_channel(conn, c, c->msg_in);
2034+
2035+
case WIRE_HSM_READY_CHANNEL:
2036+
return handle_ready_channel(conn, c, c->msg_in);
2037+
19602038
case WIRE_HSM_GET_CHANNEL_BASEPOINTS:
19612039
return handle_get_channel_basepoints(conn, c, c->msg_in);
19622040

@@ -2023,6 +2101,8 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
20232101
case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY:
20242102
case WIRE_HSM_CUPDATE_SIG_REPLY:
20252103
case WIRE_HSM_CLIENT_HSMFD_REPLY:
2104+
case WIRE_HSM_NEW_CHANNEL_REPLY:
2105+
case WIRE_HSM_READY_CHANNEL_REPLY:
20262106
case WIRE_HSM_SIGN_FUNDING_REPLY:
20272107
case WIRE_HSM_NODE_ANNOUNCEMENT_SIG_REPLY:
20282108
case WIRE_HSM_SIGN_WITHDRAWAL_REPLY:

lightningd/opening_control.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ new_uncommitted_channel(struct peer *peer)
644644
{
645645
struct lightningd *ld = peer->ld;
646646
struct uncommitted_channel *uc = tal(ld, struct uncommitted_channel);
647+
u8 *msg;
647648

648649
uc->peer = peer;
649650
assert(!peer->uncommitted_channel);
@@ -657,6 +658,15 @@ new_uncommitted_channel(struct peer *peer)
657658
uc->fc = NULL;
658659
uc->our_config.id = 0;
659660

661+
/* Declare the new channel to the HSM. */
662+
msg = towire_hsm_new_channel(NULL, &uc->peer->id, uc->dbid);
663+
if (!wire_sync_write(ld->hsm_fd, take(msg)))
664+
fatal("Could not write to HSM: %s", strerror(errno));
665+
msg = wire_sync_read(tmpctx, ld->hsm_fd);
666+
if (!fromwire_hsm_new_channel_reply(msg))
667+
fatal("HSM gave bad hsm_new_channel_reply %s",
668+
tal_hex(msg, msg));
669+
660670
get_channel_basepoints(ld, &uc->peer->id, uc->dbid,
661671
&uc->local_basepoints, &uc->local_funding_pubkey);
662672

openingd/openingd.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,26 @@ static bool funder_finalize_channel_setup(struct state *state,
669669
char *err_reason;
670670
struct wally_tx_output *direct_outputs[NUM_SIDES];
671671

672+
/*~ Channel is ready; Report the channel parameters to the signer. */
673+
msg = towire_hsm_ready_channel(NULL,
674+
true, /* is_outbound */
675+
state->funding,
676+
state->push_msat,
677+
&state->funding_txid,
678+
state->funding_txout,
679+
state->localconf.to_self_delay,
680+
state->upfront_shutdown_script[LOCAL],
681+
&state->their_points,
682+
&state->their_funding_pubkey,
683+
state->remoteconf.to_self_delay,
684+
state->upfront_shutdown_script[REMOTE],
685+
state->option_static_remotekey);
686+
wire_sync_write(HSM_FD, take(msg));
687+
msg = wire_sync_read(tmpctx, HSM_FD);
688+
if (!fromwire_hsm_ready_channel_reply(msg))
689+
status_failed(STATUS_FAIL_HSM_IO, "Bad ready_channel_reply %s",
690+
tal_hex(tmpctx, msg));
691+
672692
/*~ Now we can initialize the `struct channel`. This represents
673693
* the current channel state and is how we can generate the current
674694
* commitment transaction.
@@ -1168,6 +1188,26 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
11681188
&state->channel_id),
11691189
type_to_string(msg, struct channel_id, &id_in));
11701190

1191+
/*~ Channel is ready; Report the channel parameters to the signer. */
1192+
msg = towire_hsm_ready_channel(NULL,
1193+
false, /* is_outbound */
1194+
state->funding,
1195+
state->push_msat,
1196+
&state->funding_txid,
1197+
state->funding_txout,
1198+
state->localconf.to_self_delay,
1199+
state->upfront_shutdown_script[LOCAL],
1200+
&theirs,
1201+
&their_funding_pubkey,
1202+
state->remoteconf.to_self_delay,
1203+
state->upfront_shutdown_script[REMOTE],
1204+
state->option_static_remotekey);
1205+
wire_sync_write(HSM_FD, take(msg));
1206+
msg = wire_sync_read(tmpctx, HSM_FD);
1207+
if (!fromwire_hsm_ready_channel_reply(msg))
1208+
status_failed(STATUS_FAIL_HSM_IO, "Bad ready_channel_reply %s",
1209+
tal_hex(tmpctx, msg));
1210+
11711211
/* Now we can create the channel structure. */
11721212
state->channel = new_initial_channel(state,
11731213
&state->funding_txid,

0 commit comments

Comments
 (0)