@@ -272,6 +272,23 @@ static struct io_plan *req_reply(struct io_conn *conn,
272
272
return io_write_wire (conn , msg_out , client_read_next , c );
273
273
}
274
274
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
+
275
292
static void create_hsm (int fd , const char * passphrase )
276
293
{
277
294
u8 * hsm_secret_data ;
@@ -300,23 +317,22 @@ static void create_hsm(int fd, const char *passphrase)
300
317
301
318
if (ret != WALLY_OK ) {
302
319
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" );
305
322
}
306
323
status_debug ("HSM: Generated mnemonic from entropy" );
307
324
308
325
if (!mnemonic ) {
309
326
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" );
313
329
}
314
330
315
331
/* Derive seed hash from mnemonic + passphrase (or zero if no passphrase) */
316
332
if (!derive_seed_hash (mnemonic , passphrase , & seed_hash )) {
317
333
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" );
320
336
}
321
337
status_debug ("HSM: Derived seed hash from mnemonic" );
322
338
@@ -334,10 +350,13 @@ static void create_hsm(int fd, const char *passphrase)
334
350
u8 bip32_seed [BIP39_SEED_LEN_512 ];
335
351
size_t bip32_seed_len ;
336
352
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 ) {
338
357
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" );
341
360
}
342
361
status_debug ("HSM: Derived BIP32 seed from mnemonic" );
343
362
@@ -425,8 +444,8 @@ static void load_hsm(const char *passphrase)
425
444
/* Read the hsm_secret file */
426
445
hsm_secret_contents = grab_file (tmpctx , "hsm_secret" );
427
446
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 ));
430
449
}
431
450
432
451
/* Remove the NUL terminator that grab_file adds */
@@ -439,8 +458,8 @@ static void load_hsm(const char *passphrase)
439
458
passphrase , & err );
440
459
tal_wally_end (tmpctx );
441
460
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 ));
444
463
}
445
464
446
465
/* Copy the extracted secret to our global hsm_secret */
@@ -764,6 +783,7 @@ static struct io_plan *handle_client(struct io_conn *conn, struct client *c)
764
783
case WIRE_HSMD_SIGN_WITHDRAWAL_REPLY :
765
784
case WIRE_HSMD_SIGN_INVOICE_REPLY :
766
785
case WIRE_HSMD_INIT_REPLY_V4 :
786
+ case WIRE_HSMD_INIT_REPLY_FAILURE :
767
787
case WIRE_HSMD_DERIVE_SECRET_REPLY :
768
788
case WIRE_HSMSTATUS_CLIENT_BAD_REQUEST :
769
789
case WIRE_HSMD_SIGN_COMMITMENT_TX_REPLY :
0 commit comments