walletinfo: allow reading wallet passphrase from env variable

pull/17/head
Oliver Gugger 4 years ago
parent ce0964a3bd
commit c19f0af7dd
No known key found for this signature in database
GPG Key ID: 8E4256593F177720

@ -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
}

Loading…
Cancel
Save