lightningd: support hsm error code

Suggested-by: Rusty Russell
Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
Changelog-Changed: Support hsm specific error error code in lightning-cli
This commit is contained in:
Vincenzo Palazzo
2021-12-14 22:57:21 +01:00
committed by Rusty Russell
parent 9a85b02c6f
commit 43ff949ea7
7 changed files with 109 additions and 59 deletions

View File

@@ -90,6 +90,7 @@ static void get_encrypted_hsm_secret(struct secret *hsm_secret,
struct secret key;
struct encrypted_hsm_secret encrypted_secret;
char *err;
int exit_code = 0;
fd = open(hsm_secret_path, O_RDONLY);
if (fd < 0)
@@ -98,9 +99,9 @@ static void get_encrypted_hsm_secret(struct secret *hsm_secret,
if (!read_all(fd, encrypted_secret.data, ENCRYPTED_HSM_SECRET_LEN))
errx(ERROR_HSM_FILE, "Could not read encrypted hsm_secret");
err = hsm_secret_encryption_key(passwd, &key);
if (err)
errx(ERROR_LIBSODIUM, "%s", err);
exit_code = hsm_secret_encryption_key_with_exitcode(passwd, &key, &err);
if (exit_code > 0)
errx(exit_code, "%s", err);
if (!decrypt_hsm_secret(&key, &encrypted_secret, hsm_secret))
errx(ERROR_LIBSODIUM, "Could not retrieve the seed. Wrong password ?");
@@ -164,15 +165,15 @@ static int decrypt_hsm(const char *hsm_secret_path)
struct secret hsm_secret;
char *passwd, *err;
const char *dir, *backup;
int exit_code = 0;
/* This checks the file existence, too. */
if (!hsm_secret_is_encrypted(hsm_secret_path))
errx(ERROR_USAGE, "hsm_secret is not encrypted");
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass(&err);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
if (sodium_init() == -1)
errx(ERROR_LIBSODIUM,
@@ -221,6 +222,7 @@ static int encrypt_hsm(const char *hsm_secret_path)
struct encrypted_hsm_secret encrypted_hsm_secret;
char *passwd, *passwd_confirmation, *err;
const char *dir, *backup;
int exit_code = 0;
/* This checks the file existence, too. */
if (hsm_secret_is_encrypted(hsm_secret_path))
@@ -228,14 +230,14 @@ static int encrypt_hsm(const char *hsm_secret_path)
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass(&err);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
printf("Confirm hsm_secret password:\n");
fflush(stdout);
passwd_confirmation = read_stdin_pass(&err);
passwd_confirmation = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd_confirmation)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
if (!streq(passwd, passwd_confirmation))
errx(ERROR_USAGE, "Passwords confirmation mismatch.");
get_hsm_secret(&hsm_secret, hsm_secret_path);
@@ -249,9 +251,9 @@ static int encrypt_hsm(const char *hsm_secret_path)
/* Derive the encryption key from the password provided, and try to encrypt
* the seed. */
err = hsm_secret_encryption_key(passwd, &key);
if (err)
errx(ERROR_LIBSODIUM, "%s", err);
exit_code = hsm_secret_encryption_key_with_exitcode(passwd, &key, &err);
if (exit_code > 0)
errx(exit_code, "%s", err);
if (!encrypt_hsm_secret(&key, &hsm_secret, &encrypted_hsm_secret))
errx(ERROR_LIBSODIUM, "Could not encrypt the hsm_secret seed.");
@@ -295,6 +297,7 @@ static int dump_commitments_infos(struct node_id *node_id, u64 channel_id,
struct secret hsm_secret, channel_seed, per_commitment_secret;
struct pubkey per_commitment_point;
char *passwd, *err;
int exit_code = 0;
secp256k1_ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY
| SECP256K1_CONTEXT_SIGN);
@@ -303,9 +306,9 @@ static int dump_commitments_infos(struct node_id *node_id, u64 channel_id,
if (hsm_secret_is_encrypted(hsm_secret_path)) {
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass(&err);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd);
free(passwd);
} else
@@ -357,7 +360,7 @@ static int guess_to_remote(const char *address, struct node_id *node_id,
u8 goal_pubkeyhash[20];
/* See common/bech32.h for buffer size. */
char hrp[strlen(address) - 6];
int witver;
int witver, exit_code = 0;
size_t witlen;
/* Get the hrp to accept addresses from any network. */
@@ -373,9 +376,9 @@ static int guess_to_remote(const char *address, struct node_id *node_id,
if (hsm_secret_is_encrypted(hsm_secret_path)) {
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass(&err);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd);
free(passwd);
} else
@@ -478,6 +481,7 @@ static int generate_hsm(const char *hsm_secret_path)
{
char mnemonic[BIP39_WORDLIST_LEN];
char *passphrase, *err;
int exit_code = 0;
read_mnemonic(mnemonic);
printf("Warning: remember that different passphrases yield different "
@@ -485,9 +489,9 @@ static int generate_hsm(const char *hsm_secret_path)
printf("If left empty, no password is used (echo is disabled).\n");
printf("Enter your passphrase: \n");
fflush(stdout);
passphrase = read_stdin_pass(&err);
passphrase = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passphrase)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
if (strlen(passphrase) == 0) {
free(passphrase);
passphrase = NULL;
@@ -534,14 +538,15 @@ static int dumponchaindescriptors(const char *hsm_secret_path, const char *old_p
struct ext_key master_extkey;
char *enc_xpub, *descriptor;
struct descriptor_checksum checksum;
int exit_code = 0;
/* This checks the file existence, too. */
if (hsm_secret_is_encrypted(hsm_secret_path)) {
printf("Enter hsm_secret password:\n");
fflush(stdout);
passwd = read_stdin_pass(&err);
passwd = read_stdin_pass_with_exit_code(&err, &exit_code);
if (!passwd)
errx(ERROR_TERM, "%s", err);
errx(exit_code, "%s", err);
get_encrypted_hsm_secret(&hsm_secret, hsm_secret_path, passwd);
free(passwd);
} else