Skip to content

Commit 50df1cd

Browse files
committed
HSMD: add new wire api to sign messages ...
with custom keys. Changelog-Added: HSMD: add new wire api to sign messages with bitcoin wallet keys. Signed-off-by: Lagrang3 <lagrang3@protonmail.com>
1 parent 9da6487 commit 50df1cd

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

common/hsm_version.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* v5 with preapprove_check: 0ed6dd4ea2c02b67c51b1420b3d07ab2227a4c06ce7e2942d946967687e9baf7
2828
* v6 no secret from get_per_commitment_point: 0cad1790beb3473d64355f4cb4f64daa80c28c8a241998b7ef0223385d7ffff9
2929
* v6 with sign_bolt12_2 (tweak using node id): 8fcb731279a10af3f95aeb8be1da6b2ced76a1984afa18c5f46a03515d70ea0e
30+
* v7 with sign_message_with_key: e919c58fb80be639feb8502a740f0ed80a87c5885154c5632a0dacf2d96b9899
3031
*/
3132
#define HSM_MIN_VERSION 5
3233
#define HSM_MAX_VERSION 6

hsmd/hsmd.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
689689
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS:
690690
case WIRE_HSMD_SIGN_INVOICE:
691691
case WIRE_HSMD_SIGN_MESSAGE:
692+
case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY:
692693
case WIRE_HSMD_SIGN_OPTION_WILL_FUND_OFFER:
693694
case WIRE_HSMD_SIGN_BOLT12:
694695
case WIRE_HSMD_SIGN_BOLT12_2:
@@ -745,6 +746,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
745746
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY:
746747
case WIRE_HSMD_DEV_MEMLEAK_REPLY:
747748
case WIRE_HSMD_SIGN_MESSAGE_REPLY:
749+
case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY_REPLY:
748750
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY:
749751
case WIRE_HSMD_SIGN_BOLT12_REPLY:
750752
case WIRE_HSMD_SIGN_BOLT12_2_REPLY:

hsmd/hsmd_wire.csv

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,15 @@ msgdata,hsmd_sign_message,msg,u8,len
356356
msgtype,hsmd_sign_message_reply,123
357357
msgdata,hsmd_sign_message_reply,sig,secp256k1_ecdsa_recoverable_signature,
358358

359+
# sign a raw message with a derived key
360+
msgtype,hsmd_sign_message_with_key,45
361+
msgdata,hsmd_sign_message_with_key,len,u16,
362+
msgdata,hsmd_sign_message_with_key,msg,u8,len
363+
msgdata,hsmd_sign_message_with_key,keyidx,u32,
364+
365+
msgtype,hsmd_sign_message_with_key_reply,145
366+
msgdata,hsmd_sign_message_with_key_reply,sig,secp256k1_ecdsa_recoverable_signature,
367+
359368
# lightningd needs to get a scriptPubkey for a utxo with closeinfo
360369
msgtype,hsmd_get_output_scriptpubkey,24
361370
msgdata,hsmd_get_output_scriptpubkey,channel_id,u64,

hsmd/libhsmd.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
135135
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS:
136136
case WIRE_HSMD_DEV_MEMLEAK:
137137
case WIRE_HSMD_SIGN_MESSAGE:
138+
case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY:
138139
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY:
139140
case WIRE_HSMD_SIGN_BOLT12:
140141
case WIRE_HSMD_SIGN_BOLT12_2:
@@ -181,6 +182,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
181182
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY:
182183
case WIRE_HSMD_DEV_MEMLEAK_REPLY:
183184
case WIRE_HSMD_SIGN_MESSAGE_REPLY:
185+
case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY_REPLY:
184186
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY:
185187
case WIRE_HSMD_SIGN_BOLT12_REPLY:
186188
case WIRE_HSMD_SIGN_BOLT12_2_REPLY:
@@ -701,6 +703,51 @@ static u8 *handle_sign_message(struct hsmd_client *c, const u8 *msg_in)
701703
return towire_hsmd_sign_message_reply(NULL, &rsig);
702704
}
703705

706+
/* FIXME: implement BIP0322 signature scheme so that we can support any type of
707+
* address. */
708+
/* Sign a message with a private key (see BIP137):
709+
* signature = base64(SigRec(SHA256(SHA256(
710+
* "\x18Bitcoin Signed Message:\n" + var_int(len(message)) + message
711+
* )))) */
712+
static u8 *handle_sign_message_with_key(struct hsmd_client *c, const u8 *msg_in)
713+
{
714+
u8 *msg;
715+
u32 keyidx;
716+
struct sha256_ctx sctx = SHA256_INIT;
717+
struct sha256_double shad;
718+
secp256k1_ecdsa_recoverable_signature rsig;
719+
struct privkey privkey;
720+
struct pubkey pubkey;
721+
722+
if (!fromwire_hsmd_sign_message_with_key(tmpctx, msg_in, &msg, &keyidx))
723+
return hsmd_status_malformed_request(c, msg_in);
724+
725+
/* double sha256 the message */
726+
const char header[] = "\x18"
727+
"Bitcoin Signed Message:\n";
728+
sha256_update(&sctx, (const u8 *)header, strlen(header));
729+
730+
u8 vt[VARINT_MAX_LEN];
731+
size_t msg_len = tal_count(msg);
732+
size_t vtlen = varint_put(vt, msg_len);
733+
sha256_update(&sctx, vt, vtlen);
734+
735+
sha256_update(&sctx, msg, msg_len);
736+
sha256_double_done(&sctx, &shad);
737+
738+
/* get the private key BIP32 */
739+
bitcoin_key(&privkey, &pubkey, keyidx);
740+
741+
if (!secp256k1_ecdsa_sign_recoverable(
742+
secp256k1_ctx, &rsig, shad.sha.u.u8, privkey.secret.data, NULL,
743+
NULL)) {
744+
return hsmd_status_bad_request(c, msg_in,
745+
"Failed to sign message");
746+
}
747+
748+
return towire_hsmd_sign_message_with_key_reply(NULL, &rsig);
749+
}
750+
704751
/*~ lightningd asks us to sign a liquidity ad offer */
705752
static u8 *handle_sign_option_will_fund_offer(struct hsmd_client *c,
706753
const u8 *msg_in)
@@ -2167,6 +2214,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
21672214
return handle_preapprove_keysend(client, msg);
21682215
case WIRE_HSMD_SIGN_MESSAGE:
21692216
return handle_sign_message(client, msg);
2217+
case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY:
2218+
return handle_sign_message_with_key(client, msg);
21702219
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS:
21712220
return handle_get_channel_basepoints(client, msg);
21722221
case WIRE_HSMD_CANNOUNCEMENT_SIG_REQ:
@@ -2249,6 +2298,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
22492298
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY:
22502299
case WIRE_HSMD_DEV_MEMLEAK_REPLY:
22512300
case WIRE_HSMD_SIGN_MESSAGE_REPLY:
2301+
case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY_REPLY:
22522302
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY:
22532303
case WIRE_HSMD_SIGN_BOLT12_REPLY:
22542304
case WIRE_HSMD_SIGN_BOLT12_2_REPLY:

0 commit comments

Comments
 (0)