Skip to content

Commit

Permalink
hsmd: add message to sign the mutual close transaction.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell authored and cdecker committed Jul 23, 2018
1 parent cdc97f5 commit 6b700f9
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions hsmd/capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
55 changes: 55 additions & 0 deletions hsmd/hsm.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,55 @@ static struct io_plan *handle_sign_remote_htlc_tx(struct io_conn *conn,
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)
{
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions hsmd/hsm_client_wire_csv
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6b700f9

Please sign in to comment.