From dc1132dce0162f486a67ead01bf2a5e42a218c09 Mon Sep 17 00:00:00 2001 From: Antoine Poinsot Date: Sat, 2 Jan 2021 23:41:24 +0100 Subject: [PATCH] hsmtool: reuse read_stdin_pass for `generatehsm` passphrase Signed-off-by: Antoine Poinsot --- tools/hsmtool.c | 54 ++++++++++--------------------------------------- 1 file changed, 11 insertions(+), 43 deletions(-) diff --git a/tools/hsmtool.c b/tools/hsmtool.c index cc82d17e39ef..eecf54fd9206 100644 --- a/tools/hsmtool.c +++ b/tools/hsmtool.c @@ -504,53 +504,21 @@ static void read_mnemonic(char *mnemonic) { } } -static void read_passphrase(char **passphrase) { - struct termios current_term, temp_term; - printf("Warning: remember that different passphrases yield different " - "bitcoin wallets.\n"); - printf("If left empty, no password is used (echo is " - "disabled now).\n"); - printf("Enter your passphrase: \n"); - - /* Change terminal options so we do not echo the passphrase */ - if (tcgetattr(fileno(stdin), ¤t_term) != 0) - errx(ERROR_USAGE, "Could not get current terminal options."); - temp_term = current_term; - temp_term.c_lflag &= ~ECHO; - if (tcsetattr(fileno(stdin), TCSAFLUSH, &temp_term) != 0) - errx(ERROR_USAGE, "Could not disable passphrase echoing."); - /* If we don't flush we might end up being buffered and we might seem - * to hang while we wait for the password. */ - fflush(stdout); - - size_t passphrase_size = 0; - size_t characters = getline(passphrase, &passphrase_size, stdin); - if (characters < 0) - errx(ERROR_USAGE, "Could not read passphrase from stdin."); - - /* Newline is not part of the valid passphrase */ - if ( (*passphrase)[characters-1] == '\n' ) { - (*passphrase)[characters-1] = '\0'; - } - - /* If the user did not introduce any password, we want to set passphrase - * to NULL not to '\0' for libwally */ - if (strlen(*passphrase) == 0) { - free(*passphrase); - *passphrase = NULL; - } - - if (tcsetattr(fileno(stdin), TCSAFLUSH, ¤t_term) != 0) - errx(ERROR_USAGE, "Could not restore terminal options."); -} - static int generate_hsm(const char *hsm_secret_path) { char mnemonic[BIP39_WORDLIST_LEN]; - read_mnemonic(mnemonic); + char *passphrase; - char *passphrase = NULL; - read_passphrase(&passphrase); + read_mnemonic(mnemonic); + printf("Warning: remember that different passphrases yield different " + "bitcoin wallets.\n"); + printf("If left empty, no password is used (echo is disabled).\n"); + printf("Enter your passphrase: \n"); + passphrase = read_stdin_pass(); + if (strlen(passphrase) == 0) { + free(passphrase); + passphrase = NULL; + } u8 bip32_seed[BIP39_SEED_LEN_512]; size_t bip32_seed_len;