Skip to content

Commit

Permalink
hsmd: routine to sign HTLC tx merged with our own tx.
Browse files Browse the repository at this point in the history
Since HTLC txs when using anchors are
SIGHASH_SINGLE|SIGHASH_ANYONECANPAY, we can attach other inputs to
give it a higher feerate.  But we need the HSMd to actually sign the
combo.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Jun 29, 2023
1 parent a9d8f84 commit 662b268
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/hsm_version.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* v4 with check_pubkey: 48b3992745aa3c6ab6ce5cdaee9082cb7d70017f523d322015e9710bf49fd193
* v4 with sign_any_penalty_to_us: ead7963185194a515d1f14d2c44401392575299d68ce9a13d8a12baff3cf4f35
* v4 with sign_anchorspend: 8a30722e38b56e82af566b9629ff18da01fcebd1e80ec67f04d8b3a2fa66d81c
* v4 with sign_htlc_tx_mingle: b9247e75d41ee1b3fc2f7db0bac8f4e92d544ab2f017d430ae3a000589c384e5
*/
#define HSM_MIN_VERSION 3
#define HSM_MAX_VERSION 4
Expand Down
2 changes: 2 additions & 0 deletions hsmd/hsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_SIGN_ANY_REMOTE_HTLC_TO_US:
case WIRE_HSMD_SIGN_ANY_LOCAL_HTLC_TX:
case WIRE_HSMD_SIGN_ANCHORSPEND:
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE:
/* Hand off to libhsmd for processing */
return req_reply(conn, c,
take(hsmd_handle_client_message(
Expand Down Expand Up @@ -720,6 +721,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_PREAPPROVE_KEYSEND_REPLY:
case WIRE_HSMD_CHECK_PUBKEY_REPLY:
case WIRE_HSMD_SIGN_ANCHORSPEND_REPLY:
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE_REPLY:
return bad_req_fmt(conn, c, c->msg_in,
"Received an incoming message of type %s, "
"which is not a request",
Expand Down
10 changes: 10 additions & 0 deletions hsmd/hsmd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,13 @@ msgdata,hsmd_sign_any_local_htlc_tx,option_anchor_outputs,bool,
msgdata,hsmd_sign_any_local_htlc_tx,input,u32,
msgdata,hsmd_sign_any_local_htlc_tx,peerid,node_id,
msgdata,hsmd_sign_any_local_htlc_tx,channel_dbid,u64,

msgtype,hsmd_sign_htlc_tx_mingle,149
msgdata,hsmd_sign_htlc_tx_mingle,peerid,node_id,
msgdata,hsmd_sign_htlc_tx_mingle,channel_dbid,u64,
msgdata,hsmd_sign_htlc_tx_mingle,num_inputs,u16,
msgdata,hsmd_sign_htlc_tx_mingle,inputs,utxo,num_inputs
msgdata,hsmd_sign_htlc_tx_mingle,psbt,wally_psbt,

msgtype,hsmd_sign_htlc_tx_mingle_reply,150
msgdata,hsmd_sign_htlc_tx_mingle_reply,psbt,wally_psbt,
26 changes: 26 additions & 0 deletions hsmd/libhsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
case WIRE_HSMD_SIGN_ANY_REMOTE_HTLC_TO_US:
case WIRE_HSMD_SIGN_ANY_LOCAL_HTLC_TX:
case WIRE_HSMD_SIGN_ANCHORSPEND:
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE:
return (client->capabilities & HSM_CAP_MASTER) != 0;

/*~ These are messages sent by the HSM so we should never receive them. */
Expand Down Expand Up @@ -163,6 +164,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
case WIRE_HSMD_DERIVE_SECRET_REPLY:
case WIRE_HSMD_CHECK_PUBKEY_REPLY:
case WIRE_HSMD_SIGN_ANCHORSPEND_REPLY:
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE_REPLY:
break;
}
return false;
Expand Down Expand Up @@ -1504,6 +1506,26 @@ static u8 *handle_sign_anchorspend(struct hsmd_client *c, const u8 *msg_in)
return towire_hsmd_sign_anchorspend_reply(NULL, psbt);
}

/*~ Called from lightningd */
static u8 *handle_sign_htlc_tx_mingle(struct hsmd_client *c, const u8 *msg_in)
{
struct node_id peer_id;
u64 dbid;
struct utxo **utxos;
struct wally_psbt *psbt;

/* FIXME: Check output goes to us. */
if (!fromwire_hsmd_sign_htlc_tx_mingle(tmpctx, msg_in,
&peer_id, &dbid, &utxos, &psbt))
return hsmd_status_malformed_request(c, msg_in);

/* Sign all the UTXOs (htlc_inout input is already signed with
* SIGHASH_SINGLE|SIGHASH_ANYONECANPAY) */
sign_our_inputs(utxos, psbt);

return towire_hsmd_sign_htlc_tx_mingle_reply(NULL, psbt);
}

/*~ This is another lightningd-only interface; signing a commit transaction.
* This is dangerous, since if we sign a revoked commitment tx we'll lose
* funds, thus it's only available to lightningd.
Expand Down Expand Up @@ -1909,6 +1931,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
return handle_sign_any_penalty_to_us(client, msg);
case WIRE_HSMD_SIGN_ANCHORSPEND:
return handle_sign_anchorspend(client, msg);
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE:
return handle_sign_htlc_tx_mingle(client, msg);

case WIRE_HSMD_DEV_MEMLEAK:
case WIRE_HSMD_ECDH_RESP:
Expand Down Expand Up @@ -1940,6 +1964,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
case WIRE_HSMD_PREAPPROVE_KEYSEND_REPLY:
case WIRE_HSMD_CHECK_PUBKEY_REPLY:
case WIRE_HSMD_SIGN_ANCHORSPEND_REPLY:
case WIRE_HSMD_SIGN_HTLC_TX_MINGLE_REPLY:
break;
}
return hsmd_status_bad_request(client, msg, "Unknown request");
Expand All @@ -1957,6 +1982,7 @@ u8 *hsmd_init(struct secret hsm_secret,
WIRE_HSMD_CHECK_PUBKEY,
WIRE_HSMD_SIGN_ANY_DELAYED_PAYMENT_TO_US,
WIRE_HSMD_SIGN_ANCHORSPEND,
WIRE_HSMD_SIGN_HTLC_TX_MINGLE,
};

/*~ Don't swap this. */
Expand Down

0 comments on commit 662b268

Please sign in to comment.