From 662b2687e215abdffbdae8a408a0e6a10ccbbb31 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 26 Jun 2023 08:44:21 +0930 Subject: [PATCH] hsmd: routine to sign HTLC tx merged with our own tx. 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 --- common/hsm_version.h | 1 + hsmd/hsmd.c | 2 ++ hsmd/hsmd_wire.csv | 10 ++++++++++ hsmd/libhsmd.c | 26 ++++++++++++++++++++++++++ 4 files changed, 39 insertions(+) diff --git a/common/hsm_version.h b/common/hsm_version.h index abe7f887bb96..0b58011b3006 100644 --- a/common/hsm_version.h +++ b/common/hsm_version.h @@ -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 diff --git a/hsmd/hsmd.c b/hsmd/hsmd.c index ae7c8d1eae43..f94040aec257 100644 --- a/hsmd/hsmd.c +++ b/hsmd/hsmd.c @@ -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( @@ -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", diff --git a/hsmd/hsmd_wire.csv b/hsmd/hsmd_wire.csv index 96bfe84b6557..2c7afecb92f2 100644 --- a/hsmd/hsmd_wire.csv +++ b/hsmd/hsmd_wire.csv @@ -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, diff --git a/hsmd/libhsmd.c b/hsmd/libhsmd.c index 304521c0acb5..8e12704fc584 100644 --- a/hsmd/libhsmd.c +++ b/hsmd/libhsmd.c @@ -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. */ @@ -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; @@ -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. @@ -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: @@ -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"); @@ -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. */