From 522b9dc96bd56d1940edd47e7963561b24faf959 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 23 Jul 2018 11:53:02 +0930 Subject: [PATCH] hsmd: add routing to sign a local htlc transaction for onchaind. Signed-off-by: Rusty Russell --- hsmd/hsm.c | 60 ++++++++++++++++++++++++++++++++++++++++ hsmd/hsm_client_wire_csv | 9 ++++++ 2 files changed, 69 insertions(+) diff --git a/hsmd/hsm.c b/hsmd/hsm.c index c36379402..07e8328fa 100644 --- a/hsmd/hsm.c +++ b/hsmd/hsm.c @@ -482,6 +482,62 @@ static struct io_plan *handle_sign_penalty_to_us(struct io_conn *conn, input_amount); } +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); +} + static bool check_client_capabilities(struct client *client, enum hsm_client_wire_type t) { @@ -497,6 +553,7 @@ static bool check_client_capabilities(struct client *client, case WIRE_HSM_SIGN_DELAYED_PAYMENT_TO_US: case WIRE_HSM_SIGN_REMOTE_HTLC_TO_US: case WIRE_HSM_SIGN_PENALTY_TO_US: + case WIRE_HSM_SIGN_LOCAL_HTLC_TX: return (client->capabilities & HSM_CAP_SIGN_ONCHAIN_TX) != 0; case WIRE_HSM_INIT: @@ -590,6 +647,9 @@ static struct io_plan *handle_client(struct io_conn *conn, case WIRE_HSM_SIGN_PENALTY_TO_US: return handle_sign_penalty_to_us(conn, c); + case WIRE_HSM_SIGN_LOCAL_HTLC_TX: + return handle_sign_local_htlc_tx(conn, c); + case WIRE_HSM_ECDH_RESP: case WIRE_HSM_CANNOUNCEMENT_SIG_REPLY: case WIRE_HSM_CUPDATE_SIG_REPLY: diff --git a/hsmd/hsm_client_wire_csv b/hsmd/hsm_client_wire_csv index d79529756..4df759080 100644 --- a/hsmd/hsm_client_wire_csv +++ b/hsmd/hsm_client_wire_csv @@ -127,6 +127,15 @@ hsm_sign_penalty_to_us,,wscript_len,u16 hsm_sign_penalty_to_us,,wscript,wscript_len*u8 hsm_sign_penalty_to_us,,input_amount,u64 +# Onchaind asks HSM to sign a local HTLC success or HTLC timeout tx. +hsm_sign_local_htlc_tx,16 +hsm_sign_local_htlc_tx,,commit_num,u64 +hsm_sign_local_htlc_tx,,tx,struct bitcoin_tx +hsm_sign_local_htlc_tx,,wscript_len,u16 +hsm_sign_local_htlc_tx,,wscript,wscript_len*u8 +hsm_sign_local_htlc_tx,,input_amount,u64 + # Reply for all the above requests from onchaind. hsm_sign_tx_reply,112 hsm_sign_tx_reply,,sig,secp256k1_ecdsa_signature +