Skip to content

Commit c19f0af

Browse files
committed
walletinfo: allow reading wallet passphrase from env variable
1 parent ce0964a commit c19f0af

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

cmd/chantools/walletinfo.go

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import (
2323
_ "github.com/btcsuite/btcwallet/walletdb/bdb"
2424
)
2525

26+
const (
27+
passwordEnvName = "WALLET_PASSWORD"
28+
)
29+
2630
var (
2731
// Namespace from github.com/btcsuite/btcwallet/wallet/wallet.go
2832
waddrmgrNamespaceKey = []byte("waddrmgr")
@@ -50,20 +54,41 @@ func (c *walletInfoCommand) Execute(_ []string) error {
5054
var (
5155
publicWalletPw = lnwallet.DefaultPublicPassphrase
5256
privateWalletPw = lnwallet.DefaultPrivatePassphrase
57+
err error
5358
)
5459

5560
// Check that we have a wallet DB.
5661
if c.WalletDB == "" {
5762
return fmt.Errorf("wallet DB is required")
5863
}
5964

60-
// Ask the user for the wallet password. If it's empty, the default
61-
// password will be used, since the lnd wallet is always encrypted.
62-
pw, err := passwordFromConsole("Input wallet password: ")
63-
if err != nil {
64-
return err
65-
}
66-
if len(pw) > 0 {
65+
// To automate things with chantools, we also offer reading the wallet
66+
// password from environment variables.
67+
pw := []byte(strings.TrimSpace(os.Getenv(passwordEnvName)))
68+
69+
// Because we cannot differentiate between an empty and a non-existent
70+
// environment variable, we need a special character that indicates that
71+
// no password should be used. We use a single dash (-) for that as that
72+
// would be too short for an explicit password anyway.
73+
switch {
74+
// The user indicated in the environment variable that no passphrase
75+
// should be used. We don't set any value.
76+
case string(pw) == "-":
77+
78+
// The environment variable didn't contain anything, we'll read the
79+
// passphrase from the terminal.
80+
case len(pw) == 0:
81+
pw, err = passwordFromConsole("Input wallet password: ")
82+
if err != nil {
83+
return err
84+
}
85+
if len(pw) > 0 {
86+
publicWalletPw = pw
87+
privateWalletPw = pw
88+
}
89+
90+
// There was a password in the environment, just use it directly.
91+
default:
6792
publicWalletPw = pw
6893
privateWalletPw = pw
6994
}

0 commit comments

Comments
 (0)