From c19f0af7dd94c5fe1ceb5f104f68046aedea6723 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 29 Oct 2020 20:04:25 +0100 Subject: [PATCH] walletinfo: allow reading wallet passphrase from env variable --- cmd/chantools/walletinfo.go | 39 ++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/cmd/chantools/walletinfo.go b/cmd/chantools/walletinfo.go index 374df4e..9459e37 100644 --- a/cmd/chantools/walletinfo.go +++ b/cmd/chantools/walletinfo.go @@ -23,6 +23,10 @@ import ( _ "github.com/btcsuite/btcwallet/walletdb/bdb" ) +const ( + passwordEnvName = "WALLET_PASSWORD" +) + var ( // Namespace from github.com/btcsuite/btcwallet/wallet/wallet.go waddrmgrNamespaceKey = []byte("waddrmgr") @@ -50,6 +54,7 @@ func (c *walletInfoCommand) Execute(_ []string) error { var ( publicWalletPw = lnwallet.DefaultPublicPassphrase privateWalletPw = lnwallet.DefaultPrivatePassphrase + err error ) // Check that we have a wallet DB. @@ -57,13 +62,33 @@ func (c *walletInfoCommand) Execute(_ []string) error { return fmt.Errorf("wallet DB is required") } - // Ask the user for the wallet password. If it's empty, the default - // password will be used, since the lnd wallet is always encrypted. - pw, err := passwordFromConsole("Input wallet password: ") - if err != nil { - return err - } - if len(pw) > 0 { + // To automate things with chantools, we also offer reading the wallet + // password from environment variables. + pw := []byte(strings.TrimSpace(os.Getenv(passwordEnvName))) + + // Because we cannot differentiate between an empty and a non-existent + // environment variable, we need a special character that indicates that + // no password should be used. We use a single dash (-) for that as that + // would be too short for an explicit password anyway. + switch { + // The user indicated in the environment variable that no passphrase + // should be used. We don't set any value. + case string(pw) == "-": + + // The environment variable didn't contain anything, we'll read the + // passphrase from the terminal. + case len(pw) == 0: + pw, err = passwordFromConsole("Input wallet password: ") + if err != nil { + return err + } + if len(pw) > 0 { + publicWalletPw = pw + privateWalletPw = pw + } + + // There was a password in the environment, just use it directly. + default: publicWalletPw = pw privateWalletPw = pw }