@@ -135,6 +135,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
135
135
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS :
136
136
case WIRE_HSMD_DEV_MEMLEAK :
137
137
case WIRE_HSMD_SIGN_MESSAGE :
138
+ case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY :
138
139
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY :
139
140
case WIRE_HSMD_SIGN_BOLT12 :
140
141
case WIRE_HSMD_SIGN_BOLT12_2 :
@@ -181,6 +182,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
181
182
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY :
182
183
case WIRE_HSMD_DEV_MEMLEAK_REPLY :
183
184
case WIRE_HSMD_SIGN_MESSAGE_REPLY :
185
+ case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY_REPLY :
184
186
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY :
185
187
case WIRE_HSMD_SIGN_BOLT12_REPLY :
186
188
case WIRE_HSMD_SIGN_BOLT12_2_REPLY :
@@ -701,6 +703,37 @@ static u8 *handle_sign_message(struct hsmd_client *c, const u8 *msg_in)
701
703
return towire_hsmd_sign_message_reply (NULL , & rsig );
702
704
}
703
705
706
+ /* Raw message signing with provided key */
707
+ static u8 * handle_sign_message_with_key (struct hsmd_client * c , const u8 * msg_in )
708
+ {
709
+ u8 * msg ;
710
+ u32 keyidx ;
711
+ struct sha256_ctx sctx = SHA256_INIT ;
712
+ struct sha256_double shad ;
713
+ secp256k1_ecdsa_signature sig ;
714
+ struct privkey privkey ;
715
+ struct pubkey pubkey ;
716
+
717
+ if (!fromwire_hsmd_sign_message_with_key (tmpctx , msg_in , & msg , & keyidx ))
718
+ return hsmd_status_malformed_request (c , msg_in );
719
+
720
+ /* double sha256 the message */
721
+ sha256_update (& sctx , msg , tal_count (msg ));
722
+ sha256_double_done (& sctx , & shad );
723
+
724
+ /* get the private key BIP32 */
725
+ bitcoin_key (& privkey , & pubkey , keyidx );
726
+
727
+ if (!secp256k1_ecdsa_sign (
728
+ secp256k1_ctx , & sig , shad .sha .u .u8 , privkey .secret .data , NULL ,
729
+ NULL )) {
730
+ return hsmd_status_bad_request (c , msg_in ,
731
+ "Failed to sign message" );
732
+ }
733
+
734
+ return towire_hsmd_sign_message_with_key_reply (NULL , & sig );
735
+ }
736
+
704
737
/*~ lightningd asks us to sign a liquidity ad offer */
705
738
static u8 * handle_sign_option_will_fund_offer (struct hsmd_client * c ,
706
739
const u8 * msg_in )
@@ -2167,6 +2200,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
2167
2200
return handle_preapprove_keysend (client , msg );
2168
2201
case WIRE_HSMD_SIGN_MESSAGE :
2169
2202
return handle_sign_message (client , msg );
2203
+ case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY :
2204
+ return handle_sign_message_with_key (client , msg );
2170
2205
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS :
2171
2206
return handle_get_channel_basepoints (client , msg );
2172
2207
case WIRE_HSMD_CANNOUNCEMENT_SIG_REQ :
@@ -2249,6 +2284,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
2249
2284
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY :
2250
2285
case WIRE_HSMD_DEV_MEMLEAK_REPLY :
2251
2286
case WIRE_HSMD_SIGN_MESSAGE_REPLY :
2287
+ case WIRE_HSMD_SIGN_MESSAGE_WITH_KEY_REPLY :
2252
2288
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY :
2253
2289
case WIRE_HSMD_SIGN_BOLT12_REPLY :
2254
2290
case WIRE_HSMD_SIGN_BOLT12_2_REPLY :
0 commit comments