@@ -136,6 +136,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
136
136
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS :
137
137
case WIRE_HSMD_DEV_MEMLEAK :
138
138
case WIRE_HSMD_SIGN_MESSAGE :
139
+ case WIRE_HSMD_BIP137_SIGN_MESSAGE :
139
140
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY :
140
141
case WIRE_HSMD_SIGN_BOLT12 :
141
142
case WIRE_HSMD_SIGN_BOLT12_2 :
@@ -182,6 +183,7 @@ bool hsmd_check_client_capabilities(struct hsmd_client *client,
182
183
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY :
183
184
case WIRE_HSMD_DEV_MEMLEAK_REPLY :
184
185
case WIRE_HSMD_SIGN_MESSAGE_REPLY :
186
+ case WIRE_HSMD_BIP137_SIGN_MESSAGE_REPLY :
185
187
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY :
186
188
case WIRE_HSMD_SIGN_BOLT12_REPLY :
187
189
case WIRE_HSMD_SIGN_BOLT12_2_REPLY :
@@ -715,6 +717,51 @@ static u8 *handle_sign_message(struct hsmd_client *c, const u8 *msg_in)
715
717
return towire_hsmd_sign_message_reply (NULL , & rsig );
716
718
}
717
719
720
+ /* FIXME: implement BIP0322 signature scheme so that we can support any type of
721
+ * address. */
722
+ /* Sign a message with a private key (see BIP137):
723
+ * signature = base64(SigRec(SHA256(SHA256(
724
+ * "\x18Bitcoin Signed Message:\n" + var_int(len(message)) + message
725
+ * )))) */
726
+ static u8 * handle_bip137_sign_message (struct hsmd_client * c , const u8 * msg_in )
727
+ {
728
+ u8 * msg ;
729
+ u32 keyidx ;
730
+ struct sha256_ctx sctx = SHA256_INIT ;
731
+ struct sha256_double shad ;
732
+ secp256k1_ecdsa_recoverable_signature rsig ;
733
+ struct privkey privkey ;
734
+ struct pubkey pubkey ;
735
+
736
+ if (!fromwire_hsmd_bip137_sign_message (tmpctx , msg_in , & msg , & keyidx ))
737
+ return hsmd_status_malformed_request (c , msg_in );
738
+
739
+ /* double sha256 the message */
740
+ const char header [] = "\x18"
741
+ "Bitcoin Signed Message:\n" ;
742
+ sha256_update (& sctx , (const u8 * )header , strlen (header ));
743
+
744
+ u8 vt [VARINT_MAX_LEN ];
745
+ size_t msg_len = tal_count (msg );
746
+ size_t vtlen = varint_put (vt , msg_len );
747
+ sha256_update (& sctx , vt , vtlen );
748
+
749
+ sha256_update (& sctx , msg , msg_len );
750
+ sha256_double_done (& sctx , & shad );
751
+
752
+ /* get the private key BIP32 */
753
+ bitcoin_key (& privkey , & pubkey , keyidx );
754
+
755
+ if (!secp256k1_ecdsa_sign_recoverable (
756
+ secp256k1_ctx , & rsig , shad .sha .u .u8 , privkey .secret .data , NULL ,
757
+ NULL )) {
758
+ return hsmd_status_bad_request (c , msg_in ,
759
+ "Failed to sign message" );
760
+ }
761
+
762
+ return towire_hsmd_bip137_sign_message_reply (NULL , & rsig );
763
+ }
764
+
718
765
/*~ lightningd asks us to sign a liquidity ad offer */
719
766
static u8 * handle_sign_option_will_fund_offer (struct hsmd_client * c ,
720
767
const u8 * msg_in )
@@ -2181,6 +2228,8 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
2181
2228
return handle_preapprove_keysend (client , msg );
2182
2229
case WIRE_HSMD_SIGN_MESSAGE :
2183
2230
return handle_sign_message (client , msg );
2231
+ case WIRE_HSMD_BIP137_SIGN_MESSAGE :
2232
+ return handle_bip137_sign_message (client , msg );
2184
2233
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS :
2185
2234
return handle_get_channel_basepoints (client , msg );
2186
2235
case WIRE_HSMD_CANNOUNCEMENT_SIG_REQ :
@@ -2263,6 +2312,7 @@ u8 *hsmd_handle_client_message(const tal_t *ctx, struct hsmd_client *client,
2263
2312
case WIRE_HSMD_GET_CHANNEL_BASEPOINTS_REPLY :
2264
2313
case WIRE_HSMD_DEV_MEMLEAK_REPLY :
2265
2314
case WIRE_HSMD_SIGN_MESSAGE_REPLY :
2315
+ case WIRE_HSMD_BIP137_SIGN_MESSAGE_REPLY :
2266
2316
case WIRE_HSMD_GET_OUTPUT_SCRIPTPUBKEY_REPLY :
2267
2317
case WIRE_HSMD_SIGN_BOLT12_REPLY :
2268
2318
case WIRE_HSMD_SIGN_BOLT12_2_REPLY :
@@ -2297,6 +2347,7 @@ u8 *hsmd_init(struct secret hsm_secret, const u64 hsmd_version,
2297
2347
WIRE_HSMD_FORGET_CHANNEL ,
2298
2348
WIRE_HSMD_REVOKE_COMMITMENT_TX ,
2299
2349
WIRE_HSMD_SIGN_BOLT12_2 ,
2350
+ WIRE_HSMD_BIP137_SIGN_MESSAGE ,
2300
2351
WIRE_HSMD_PREAPPROVE_INVOICE_CHECK ,
2301
2352
WIRE_HSMD_PREAPPROVE_KEYSEND_CHECK ,
2302
2353
};
0 commit comments