Skip to content

Commit ce0964a

Browse files
committed
lnd: allow reading aezeed mnemonic and passphrase from env variables
1 parent c797965 commit ce0964a

File tree

1 file changed

+53
-16
lines changed

1 file changed

+53
-16
lines changed

lnd/aezeed.go

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import (
1515
"golang.org/x/crypto/ssh/terminal"
1616
)
1717

18+
const (
19+
memonicEnvName = "AEZEED_MNEMONIC"
20+
passphraseEnvName = "AEZEED_PASSPHRASE"
21+
)
22+
1823
var (
1924
numberDotsRegex = regexp.MustCompile("[\\d.\\-\\n\\r\\t]*")
2025
multipleSpaces = regexp.MustCompile(" [ ]+")
@@ -23,12 +28,21 @@ var (
2328
func ReadAezeed(params *chaincfg.Params) (*hdkeychain.ExtendedKey, time.Time,
2429
error) {
2530

26-
// We'll now prompt the user to enter in their 24-word mnemonic.
27-
fmt.Printf("Input your 24-word mnemonic separated by spaces: ")
28-
reader := bufio.NewReader(os.Stdin)
29-
mnemonicStr, err := reader.ReadString('\n')
30-
if err != nil {
31-
return nil, time.Unix(0, 0), err
31+
// To automate things with chantools, we also offer reading the seed
32+
// from environment variables.
33+
mnemonicStr := strings.TrimSpace(os.Getenv(memonicEnvName))
34+
35+
// If nothing is set in the environment, read the seed from the
36+
// terminal.
37+
if mnemonicStr == "" {
38+
var err error
39+
// We'll now prompt the user to enter in their 24-word mnemonic.
40+
fmt.Printf("Input your 24-word mnemonic separated by spaces: ")
41+
reader := bufio.NewReader(os.Stdin)
42+
mnemonicStr, err = reader.ReadString('\n')
43+
if err != nil {
44+
return nil, time.Unix(0, 0), err
45+
}
3246
}
3347

3448
// We'll trim off extra spaces, and ensure the mnemonic is all
@@ -53,23 +67,46 @@ func ReadAezeed(params *chaincfg.Params) (*hdkeychain.ExtendedKey, time.Time,
5367
len(cipherSeedMnemonic), 24)
5468
}
5569

56-
// Additionally, the user may have a passphrase, that will also
57-
// need to be provided so the daemon can properly decipher the
58-
// cipher seed.
59-
fmt.Printf("Input your cipher seed passphrase (press enter if " +
60-
"your seed doesn't have a passphrase): ")
61-
passphrase, err := terminal.ReadPassword(int(syscall.Stdin)) // nolint
62-
if err != nil {
63-
return nil, time.Unix(0, 0), err
70+
// Additionally, the user may have a passphrase, that will also need to
71+
// be provided so the daemon can properly decipher the cipher seed.
72+
// Try the environment variable first.
73+
passphrase := strings.TrimSpace(os.Getenv(passphraseEnvName))
74+
75+
// Because we cannot differentiate between an empty and a non-existent
76+
// environment variable, we need a special character that indicates that
77+
// no passphrase should be used. We use a single dash (-) for that as
78+
// that would be too short for a passphrase anyway.
79+
var passphraseBytes []byte
80+
switch {
81+
// The user indicated in the environment variable that no passphrase
82+
// should be used. We don't set any value.
83+
case passphrase == "-":
84+
85+
// The environment variable didn't contain anything, we'll read the
86+
// passphrase from the terminal.
87+
case passphrase == "":
88+
fmt.Printf("Input your cipher seed passphrase (press enter " +
89+
"if your seed doesn't have a passphrase): ")
90+
var err error
91+
passphraseBytes, err = terminal.ReadPassword(
92+
int(syscall.Stdin), // nolint
93+
)
94+
if err != nil {
95+
return nil, time.Unix(0, 0), err
96+
}
97+
fmt.Println()
98+
99+
// There was a password in the environment, just convert it to bytes.
100+
default:
101+
passphraseBytes = []byte(passphrase)
64102
}
65-
fmt.Println()
66103

67104
var mnemonic aezeed.Mnemonic
68105
copy(mnemonic[:], cipherSeedMnemonic)
69106

70107
// If we're unable to map it back into the ciphertext, then either the
71108
// mnemonic is wrong, or the passphrase is wrong.
72-
cipherSeed, err := mnemonic.ToCipherSeed(passphrase)
109+
cipherSeed, err := mnemonic.ToCipherSeed(passphraseBytes)
73110
if err != nil {
74111
return nil, time.Unix(0, 0), fmt.Errorf("failed to decrypt "+
75112
"seed with passphrase: %v", err)

0 commit comments

Comments
 (0)