2020-06-14 09:13:39 +00:00
|
|
|
package lnd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2020-08-06 15:04:06 +00:00
|
|
|
"regexp"
|
2020-06-14 09:13:39 +00:00
|
|
|
"strings"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2022-07-18 06:52:40 +00:00
|
|
|
"github.com/btcsuite/btcd/btcutil/hdkeychain"
|
2020-06-14 09:13:39 +00:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/lightningnetwork/lnd/aezeed"
|
|
|
|
"golang.org/x/crypto/ssh/terminal"
|
|
|
|
)
|
|
|
|
|
2020-10-29 18:47:09 +00:00
|
|
|
const (
|
2021-01-01 11:56:19 +00:00
|
|
|
MnemonicEnvName = "AEZEED_MNEMONIC"
|
|
|
|
PassphraseEnvName = "AEZEED_PASSPHRASE"
|
2020-10-29 18:47:09 +00:00
|
|
|
)
|
|
|
|
|
2020-08-06 15:04:06 +00:00
|
|
|
var (
|
2020-12-15 11:58:32 +00:00
|
|
|
numberDotsRegex = regexp.MustCompile(`[\d.\-\n\r\t]*`)
|
2020-08-06 15:04:06 +00:00
|
|
|
multipleSpaces = regexp.MustCompile(" [ ]+")
|
|
|
|
)
|
|
|
|
|
2020-10-29 18:46:41 +00:00
|
|
|
func ReadAezeed(params *chaincfg.Params) (*hdkeychain.ExtendedKey, time.Time,
|
|
|
|
error) {
|
2020-06-14 09:13:39 +00:00
|
|
|
|
2020-10-29 18:47:09 +00:00
|
|
|
// To automate things with chantools, we also offer reading the seed
|
|
|
|
// from environment variables.
|
2021-01-01 11:56:19 +00:00
|
|
|
mnemonicStr := strings.TrimSpace(os.Getenv(MnemonicEnvName))
|
2020-10-29 18:47:09 +00:00
|
|
|
|
|
|
|
// If nothing is set in the environment, read the seed from the
|
|
|
|
// terminal.
|
|
|
|
if mnemonicStr == "" {
|
|
|
|
var err error
|
|
|
|
// We'll now prompt the user to enter in their 24-word mnemonic.
|
|
|
|
fmt.Printf("Input your 24-word mnemonic separated by spaces: ")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
mnemonicStr, err = reader.ReadString('\n')
|
|
|
|
if err != nil {
|
|
|
|
return nil, time.Unix(0, 0), err
|
|
|
|
}
|
2020-06-14 09:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// We'll trim off extra spaces, and ensure the mnemonic is all
|
2020-08-06 15:04:06 +00:00
|
|
|
// lower case.
|
2020-06-14 09:13:39 +00:00
|
|
|
mnemonicStr = strings.TrimSpace(mnemonicStr)
|
|
|
|
mnemonicStr = strings.ToLower(mnemonicStr)
|
|
|
|
|
2020-08-06 15:04:06 +00:00
|
|
|
// To allow the tool to also accept the copy/pasted version of the
|
|
|
|
// backup text (which contains numbers and dots and multiple spaces),
|
|
|
|
// we do some more cleanup with regex.
|
|
|
|
mnemonicStr = numberDotsRegex.ReplaceAllString(mnemonicStr, "")
|
|
|
|
mnemonicStr = multipleSpaces.ReplaceAllString(mnemonicStr, " ")
|
|
|
|
mnemonicStr = strings.TrimSpace(mnemonicStr)
|
2020-08-08 10:17:01 +00:00
|
|
|
|
2020-06-14 09:13:39 +00:00
|
|
|
cipherSeedMnemonic := strings.Split(mnemonicStr, " ")
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
if len(cipherSeedMnemonic) != 24 {
|
|
|
|
return nil, time.Unix(0, 0), fmt.Errorf("wrong cipher seed "+
|
|
|
|
"mnemonic length: got %v words, expecting %v words",
|
|
|
|
len(cipherSeedMnemonic), 24)
|
|
|
|
}
|
|
|
|
|
2020-10-29 18:47:09 +00:00
|
|
|
// Additionally, the user may have a passphrase, that will also need to
|
|
|
|
// be provided so the daemon can properly decipher the cipher seed.
|
|
|
|
// Try the environment variable first.
|
2021-01-01 11:56:19 +00:00
|
|
|
passphrase := strings.TrimSpace(os.Getenv(PassphraseEnvName))
|
2020-10-29 18:47:09 +00:00
|
|
|
|
|
|
|
// Because we cannot differentiate between an empty and a non-existent
|
|
|
|
// environment variable, we need a special character that indicates that
|
|
|
|
// no passphrase should be used. We use a single dash (-) for that as
|
|
|
|
// that would be too short for a passphrase anyway.
|
|
|
|
var passphraseBytes []byte
|
|
|
|
switch {
|
|
|
|
// The user indicated in the environment variable that no passphrase
|
|
|
|
// should be used. We don't set any value.
|
|
|
|
case passphrase == "-":
|
|
|
|
|
|
|
|
// The environment variable didn't contain anything, we'll read the
|
|
|
|
// passphrase from the terminal.
|
|
|
|
case passphrase == "":
|
|
|
|
fmt.Printf("Input your cipher seed passphrase (press enter " +
|
|
|
|
"if your seed doesn't have a passphrase): ")
|
|
|
|
var err error
|
|
|
|
passphraseBytes, err = terminal.ReadPassword(
|
|
|
|
int(syscall.Stdin), // nolint
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, time.Unix(0, 0), err
|
|
|
|
}
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
// There was a password in the environment, just convert it to bytes.
|
|
|
|
default:
|
|
|
|
passphraseBytes = []byte(passphrase)
|
2020-06-14 09:13:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var mnemonic aezeed.Mnemonic
|
|
|
|
copy(mnemonic[:], cipherSeedMnemonic)
|
|
|
|
|
|
|
|
// If we're unable to map it back into the ciphertext, then either the
|
|
|
|
// mnemonic is wrong, or the passphrase is wrong.
|
2020-10-29 18:47:09 +00:00
|
|
|
cipherSeed, err := mnemonic.ToCipherSeed(passphraseBytes)
|
2020-06-14 09:13:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, time.Unix(0, 0), fmt.Errorf("failed to decrypt "+
|
2022-07-18 07:50:27 +00:00
|
|
|
"seed with passphrase: %w", err)
|
2020-06-14 09:13:39 +00:00
|
|
|
}
|
|
|
|
rootKey, err := hdkeychain.NewMaster(cipherSeed.Entropy[:], params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, time.Unix(0, 0), fmt.Errorf("failed to derive " +
|
|
|
|
"master extended key")
|
|
|
|
}
|
|
|
|
return rootKey, cipherSeed.BirthdayTime(), nil
|
|
|
|
}
|