@@ -272,6 +272,23 @@ static struct io_plan *req_reply(struct io_conn *conn,
272272 return io_write_wire (conn , msg_out , client_read_next , c );
273273}
274274
275+ /* Send an init reply failure message to lightningd and then call status_failed */
276+ static void hsmd_send_init_reply_failure (enum hsm_secret_error error_code , enum status_failreason reason , const char * error_msg , ...)
277+ {
278+ u8 * msg ;
279+
280+ /* Send the init reply failure first */
281+ msg = towire_hsmd_init_reply_failure (NULL , error_code , error_msg );
282+ if (msg ) {
283+ /* Send directly to lightningd via REQ_FD */
284+ write_all (REQ_FD , msg , tal_bytelen (msg ));
285+ tal_free (msg );
286+ }
287+
288+ /* Then call status_failed with the error message */
289+ status_failed (reason , "%s" , error_msg );
290+ }
291+
275292static void create_hsm (int fd , const char * passphrase )
276293{
277294 u8 * hsm_secret_data ;
@@ -300,23 +317,22 @@ static void create_hsm(int fd, const char *passphrase)
300317
301318 if (ret != WALLY_OK ) {
302319 unlink_noerr ("hsm_secret" );
303- status_failed ( STATUS_FAIL_INTERNAL_ERROR ,
304- "Failed to generate mnemonic from entropy" );
320+ hsmd_send_init_reply_failure ( HSM_SECRET_ERR_SEED_DERIVATION_FAILED , STATUS_FAIL_INTERNAL_ERROR ,
321+ "Failed to generate mnemonic from entropy" );
305322 }
306323 status_debug ("HSM: Generated mnemonic from entropy" );
307324
308325 if (!mnemonic ) {
309326 unlink_noerr ("hsm_secret" );
310- //TODO: Add passphrase error message, add new codes
311- status_failed (STATUS_FAIL_INTERNAL_ERROR ,
312- "Failed to get generated mnemonic" );
327+ hsmd_send_init_reply_failure (HSM_SECRET_ERR_SEED_DERIVATION_FAILED , STATUS_FAIL_INTERNAL_ERROR ,
328+ "Failed to get generated mnemonic" );
313329 }
314330
315331 /* Derive seed hash from mnemonic + passphrase (or zero if no passphrase) */
316332 if (!derive_seed_hash (mnemonic , passphrase , & seed_hash )) {
317333 unlink_noerr ("hsm_secret" );
318- status_failed ( STATUS_FAIL_INTERNAL_ERROR ,
319- "Failed to derive seed hash from mnemonic" );
334+ hsmd_send_init_reply_failure ( HSM_SECRET_ERR_SEED_DERIVATION_FAILED , STATUS_FAIL_INTERNAL_ERROR ,
335+ "Failed to derive seed hash from mnemonic" );
320336 }
321337 status_debug ("HSM: Derived seed hash from mnemonic" );
322338
@@ -334,10 +350,13 @@ static void create_hsm(int fd, const char *passphrase)
334350 u8 bip32_seed [BIP39_SEED_LEN_512 ];
335351 size_t bip32_seed_len ;
336352
337- if (bip39_mnemonic_to_seed (mnemonic , passphrase , bip32_seed , sizeof (bip32_seed ), & bip32_seed_len ) != WALLY_OK ) {
353+ tal_wally_start ();
354+ ret = bip39_mnemonic_to_seed (mnemonic , passphrase , bip32_seed , sizeof (bip32_seed ), & bip32_seed_len );
355+ tal_wally_end (tmpctx );
356+ if (ret != WALLY_OK ) {
338357 unlink_noerr ("hsm_secret" );
339- status_failed ( STATUS_FAIL_INTERNAL_ERROR ,
340- "Failed to derive seed from mnemonic" );
358+ hsmd_send_init_reply_failure ( HSM_SECRET_ERR_SEED_DERIVATION_FAILED , STATUS_FAIL_INTERNAL_ERROR ,
359+ "Failed to derive seed from mnemonic" );
341360 }
342361 status_debug ("HSM: Derived BIP32 seed from mnemonic" );
343362
@@ -425,8 +444,8 @@ static void load_hsm(const char *passphrase)
425444 /* Read the hsm_secret file */
426445 hsm_secret_contents = grab_file (tmpctx , "hsm_secret" );
427446 if (!hsm_secret_contents ) {
428- status_failed ( STATUS_FAIL_INTERNAL_ERROR ,
429- "Could not read hsm_secret: %s" , strerror (errno ));
447+ hsmd_send_init_reply_failure ( HSM_SECRET_ERR_INVALID_FORMAT , STATUS_FAIL_INTERNAL_ERROR ,
448+ "Could not read hsm_secret: %s" , strerror (errno ));
430449 }
431450
432451 /* Remove the NUL terminator that grab_file adds */
@@ -439,8 +458,8 @@ static void load_hsm(const char *passphrase)
439458 passphrase , & err );
440459 tal_wally_end (tmpctx );
441460 if (!hsms ) {
442- status_failed ( STATUS_FAIL_INTERNAL_ERROR ,
443- "Failed to load hsm_secret: %s" , hsm_secret_error_str (err ));
461+ hsmd_send_init_reply_failure ( err , STATUS_FAIL_INTERNAL_ERROR ,
462+ "Failed to load hsm_secret: %s" , hsm_secret_error_str (err ));
444463 }
445464
446465 /* Copy the extracted secret to our global hsm_secret */
@@ -769,6 +788,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
769788 case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY :
770789 case WIRE_HSMD_SIGN_INVOICE_REPLY :
771790 case WIRE_HSMD_INIT_REPLY_V4 :
791+ case WIRE_HSMD_INIT_REPLY_FAILURE :
772792 case WIRE_HSMD_DERIVE_SECRET_REPLY :
773793 case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST :
774794 case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY :
0 commit comments