diff --git a/hsmd/capabilities.h b/hsmd/capabilities.h index 565356b40..3a306e778 100644 --- a/hsmd/capabilities.h +++ b/hsmd/capabilities.h @@ -6,6 +6,7 @@ #define HSM_CAP_SIGN_ONCHAIN_TX 4 #define HSM_CAP_COMMITMENT_POINT 8 #define HSM_CAP_SIGN_REMOTE_TX 16 +#define HSM_CAP_SIGN_CLOSING_TX 32 #define HSM_CAP_MASTER 1024 #endif /* LIGHTNING_HSMD_CAPABILITIES_H */ diff --git a/hsmd/hsm.c b/hsmd/hsm.c index 214c5b8cd..02e6dc4ea 100644 --- a/hsmd/hsm.c +++ b/hsmd/hsm.c @@ -683,6 +683,55 @@ fail: return io_close(conn); } +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); +} + static bool check_client_capabilities(struct client *client, enum hsm_client_wire_type t) { @@ -708,6 +757,9 @@ static bool check_client_capabilities(struct client *client, case WIRE_HSM_SIGN_REMOTE_HTLC_TX: return (client->capabilities & HSM_CAP_SIGN_REMOTE_TX) != 0; + case WIRE_HSM_SIGN_MUTUAL_CLOSE_TX: + return (client->capabilities & HSM_CAP_SIGN_CLOSING_TX) != 0; + case WIRE_HSM_INIT: case WIRE_HSM_CLIENT_HSMFD: case WIRE_HSM_SIGN_FUNDING: @@ -812,6 +864,9 @@ static struct io_plan *handle_client(struct io_conn *conn, case WIRE_HSM_SIGN_REMOTE_HTLC_TX: return handle_sign_remote_htlc_tx(conn, c); + case WIRE_HSM_SIGN_MUTUAL_CLOSE_TX: + return handle_sign_mutual_close_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 1de135a12..1757caf9f 100644 --- a/hsmd/hsm_client_wire_csv +++ b/hsmd/hsm_client_wire_csv @@ -149,6 +149,12 @@ hsm_sign_remote_htlc_tx,,wscript,len*u8 hsm_sign_remote_htlc_tx,,amounts_satoshi,u64 hsm_sign_remote_htlc_tx,,remote_per_commit_point,struct pubkey +# closingd asks HSM to sign mutual close tx. +hsm_sign_mutual_close_tx,21 +hsm_sign_mutual_close_tx,,tx,struct bitcoin_tx +hsm_sign_mutual_close_tx,,remote_funding_key,struct pubkey +hsm_sign_mutual_close_tx,,funding_amount,u64 + # Reply for all the above requests. hsm_sign_tx_reply,112 hsm_sign_tx_reply,,sig,secp256k1_ecdsa_signature