Skip to content

Commit

Permalink
hsmd: method to sign liquidity ad offer
Browse files Browse the repository at this point in the history
When we accept a bid to create a channel lease, we send back a signature
committing to our max channel lease amounts.
  • Loading branch information
niftynei committed Jul 20, 2021
1 parent b1982f0 commit 3a7b376
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 2 deletions.
1 change: 1 addition & 0 deletions hsmd/capabilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define HSM_CAP_COMMITMENT_POINT 8
#define HSM_CAP_SIGN_REMOTE_TX 16
#define HSM_CAP_SIGN_CLOSING_TX 32
#define HSM_CAP_SIGN_WILL_FUND_OFFER 64

#define HSM_CAP_MASTER 1024
#endif /* LIGHTNING_HSMD_CAPABILITIES_H */
2 changes: 2 additions & 0 deletions hsmd/hsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS:
case WIRE_HSMD_SIGN_INVOICE:
case WIRE_HSMD_SIGN_MESSAGE:
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER:
case WIRE_HSMD_SIGN_BOLT12:
case WIRE_HSMD_ECDH_REQ:
case WIRE_HSMD_CHECK_FUTURE_SECRET:
Expand All @@ -689,6 +690,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
case WIRE_HSMD_SIGN_TX_REPLY:
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER_REPLY:
case WIRE_HSMD_GET_PER_COMMITMENT_POINT_REPLY:
case WIRE_HSMD_CHECK_FUTURE_SECRET_REPLY:
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY:
Expand Down
9 changes: 9 additions & 0 deletions hsmd/hsmd_wire.csv
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,12 @@ msgdata,hsmd_sign_bolt12,publictweak,u8,publictweaklen
msgtype,hsmd_sign_bolt12_reply,125
msgdata,hsmd_sign_bolt12_reply,sig,bip340sig,

# Sign an option_will_fund offer hash
msgtype,hsmd_sign_option_will_fund_offer,26
msgdata,hsmd_sign_option_will_fund_offer,funding_pubkey,pubkey,
msgdata,hsmd_sign_option_will_fund_offer,blockheight,u32,
msgdata,hsmd_sign_option_will_fund_offer,channel_fee_base_max_msat,u32,
msgdata,hsmd_sign_option_will_fund_offer,channel_fee_proportional_basis_max,u16,

msgtype,hsmd_sign_option_will_fund_offer_reply,126
msgdata,hsmd_sign_option_will_fund_offer_reply,rsig,secp256k1_ecdsa_signature,
55 changes: 54 additions & 1 deletion hsmd/hsmd_wiregen.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion hsmd/hsmd_wiregen.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions hsmd/libhsmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
case WIRE_HSMD_SIGN_MUTUAL_CLOSE_TX:
return (client->capabilities & HSM_CAP_SIGN_CLOSING_TX) != 0;

case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER:
return (client->capabilities & HSM_CAP_SIGN_WILL_FUND_OFFER) != 0;

case WIRE_HSMD_INIT:
case WIRE_HSMD_CLIENT_HSMFD:
case WIRE_HSMD_SIGN_WITHDRAWAL:
Expand All @@ -119,6 +122,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
case WIRE_HSMD_SIGN_TX_REPLY:
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER_REPLY:
case WIRE_HSMD_GET_PER_COMMITMENT_POINT_REPLY:
case WIRE_HSMD_CHECK_FUTURE_SECRET_REPLY:
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY:
Expand Down Expand Up @@ -468,6 +472,53 @@ static u8 *handle_sign_message(struct hsmd_client *c, const u8 *msg_in)
return towire_hsmd_sign_message_reply(NULL, &rsig);
}

/*~ lightningd asks us to sign a liquidity ad offer */
static u8 *handle_sign_option_will_fund_offer(struct hsmd_client *c,
const u8 *msg_in)
{
struct pubkey funding_pubkey;
u32 blockheight, channel_fee_base_max_msat;
u16 channel_fee_proportional_basis_max;
struct sha256_ctx sctx = SHA256_INIT;
struct sha256 sha;
secp256k1_ecdsa_signature sig;
struct privkey node_pkey;

if (!fromwire_hsmd_sign_option_will_fund_offer(msg_in,
&funding_pubkey,
&blockheight,
&channel_fee_base_max_msat,
&channel_fee_proportional_basis_max))
return hsmd_status_malformed_request(c, msg_in);

/* BOLT- #2:
* - MUST set `signature` to the ECDSA signature of
* SHA256("option_will_fund" || `funding_pubkey`|| `blockheight` ||
* `channel_fee_base_max_msat` ||
* `channel_fee_proportional_basis_max`)
*/
sha256_update(&sctx, "option_will_fund",
strlen("option_will_fund"));
sha256_update(&sctx, &funding_pubkey, sizeof(funding_pubkey));
sha256_update(&sctx, &blockheight, sizeof(blockheight));
sha256_update(&sctx, &channel_fee_base_max_msat,
sizeof(channel_fee_base_max_msat));
sha256_update(&sctx, &channel_fee_base_max_msat,
sizeof(channel_fee_base_max_msat));
sha256_done(&sctx, &sha);

node_key(&node_pkey, NULL);

if (!secp256k1_ecdsa_sign(secp256k1_ctx, &sig,
sha.u.u8,
node_pkey.secret.data,
NULL, NULL))
return hsmd_status_bad_request(c, msg_in,
"Failed to sign message");

return towire_hsmd_sign_option_will_fund_offer_reply(NULL, &sig);
}

/*~ lightningd asks us to sign a bolt12 (e.g. offer). */
static u8 *handle_sign_bolt12(struct hsmd_client *c, const u8 *msg_in)
{
Expand Down Expand Up @@ -1352,6 +1403,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
return handle_ecdh(client, msg);
case WIRE_HSMD_SIGN_INVOICE:
return handle_sign_invoice(client, msg);
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER:
return handle_sign_option_will_fund_offer(client, msg);
case WIRE_HSMD_SIGN_BOLT12:
return handle_sign_bolt12(client, msg);
case WIRE_HSMD_SIGN_MESSAGE:
Expand Down Expand Up @@ -1397,6 +1450,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST:
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY:
case WIRE_HSMD_SIGN_TX_REPLY:
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER_REPLY:
case WIRE_HSMD_GET_PER_COMMITMENT_POINT_REPLY:
case WIRE_HSMD_CHECK_FUTURE_SECRET_REPLY:
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY:
Expand Down

0 comments on commit 3a7b376

Please sign in to comment.