prompt: ask birthday for user provided seed

This commit is contained in:
Roy Lee 2022-08-19 16:16:46 -07:00
parent d9d257a9f6
commit 40f7d7cefe
2 changed files with 60 additions and 17 deletions

View file

@ -13,7 +13,9 @@ import (
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"os" "os"
"strconv"
"strings" "strings"
"time"
"github.com/lbryio/lbcutil/hdkeychain" "github.com/lbryio/lbcutil/hdkeychain"
"github.com/lbryio/lbcwallet/internal/legacy/keystore" "github.com/lbryio/lbcwallet/internal/legacy/keystore"
@ -120,6 +122,32 @@ func promptListBool(reader *bufio.Reader, prefix string,
return response == "yes" || response == "y", nil return response == "yes" || response == "y", nil
} }
// promptBirhdayUnixTimeStamp prompts the user a Unix timestamp in second.
func promptUnixTimestamp(reader *bufio.Reader, prefix string,
defaultEntry string) (time.Time, error) { // nolint:unparam
prompt := fmt.Sprintf("%s [%s]: ", prefix, defaultEntry)
for {
fmt.Print(prompt)
reply, err := reader.ReadString('\n')
if err != nil {
return time.Time{}, err
}
reply = strings.TrimSpace(strings.ToLower(reply))
if reply == "" {
reply = defaultEntry
}
ts, err := strconv.ParseInt(reply, 10, 64)
if err != nil {
fmt.Print(prompt)
continue
}
return time.Unix(ts, 0), nil
}
}
// promptPass prompts the user for a passphrase with the given prefix. The // promptPass prompts the user for a passphrase with the given prefix. The
// function will ask the user to confirm the passphrase and will repeat the // function will ask the user to confirm the passphrase and will repeat the
// prompts until they enter a matching response. // prompts until they enter a matching response.
@ -158,6 +186,12 @@ func promptPass(_ *bufio.Reader, prefix string, confirm bool) ([]byte, error) {
} }
} }
func birthday(reader *bufio.Reader) (time.Time, error) {
prompt := "Enter the birthday of the seed in Unix timestamp " +
"(the walllet will scan the chain from this time)"
return promptUnixTimestamp(reader, prompt, "0")
}
// PrivatePass prompts the user for a private passphrase with varying behavior // PrivatePass prompts the user for a private passphrase with varying behavior
// depending on whether the passed legacy keystore exists. When it does, the // depending on whether the passed legacy keystore exists. When it does, the
// user is prompted for the existing passphrase which is then used to unlock it. // user is prompted for the existing passphrase which is then used to unlock it.
@ -248,34 +282,31 @@ func PublicPass(reader *bufio.Reader, privPass []byte,
// the user along with prompting them for confirmation. When the user answers // the user along with prompting them for confirmation. When the user answers
// yes, a the user is prompted for it. All prompts are repeated until the user // yes, a the user is prompted for it. All prompts are repeated until the user
// enters a valid response. // enters a valid response.
func Seed(reader *bufio.Reader) ([]byte, error) { func Seed(reader *bufio.Reader) ([]byte, time.Time, error) {
bday := time.Now()
// Ascertain the wallet generation seed. // Ascertain the wallet generation seed.
useUserSeed, err := promptListBool(reader, "Do you have an "+ useUserSeed, err := promptListBool(reader, "Do you have an "+
"existing wallet seed you want to use?", "no") "existing wallet seed you want to use?", "no")
if err != nil { if err != nil {
return nil, err return nil, bday, err
} }
if !useUserSeed { if !useUserSeed {
seed, err := hdkeychain.GenerateSeed(hdkeychain.RecommendedSeedLen) seed, err := hdkeychain.GenerateSeed(hdkeychain.RecommendedSeedLen)
if err != nil { if err != nil {
return nil, err return nil, bday, err
} }
fmt.Println("Your wallet generation seed is:") fmt.Printf("Your wallet generation seed is: %x\n", seed)
fmt.Printf("%x\n", seed)
fmt.Println("IMPORTANT: Keep the seed in a safe place as you\n" + fmt.Printf("\nIMPORTANT: Keep the seed in a safe place as you " +
"will NOT be able to restore your wallet without it.") "will NOT be able to restore your wallet without it.\n\n")
fmt.Println("Please keep in mind that anyone who has access\n" +
"to the seed can also restore your wallet thereby\n" +
"giving them access to all your funds, so it is\n" +
"imperative that you keep it in a secure location.")
for { for {
fmt.Print(`Once you have stored the seed in a safe ` + fmt.Print(`Once you have stored the seed in a safe ` +
`and secure location, enter "OK" to continue: `) `and secure location, enter "OK" to continue: `)
confirmSeed, err := reader.ReadString('\n') confirmSeed, err := reader.ReadString('\n')
if err != nil { if err != nil {
return nil, err return nil, bday, err
} }
confirmSeed = strings.TrimSpace(confirmSeed) confirmSeed = strings.TrimSpace(confirmSeed)
confirmSeed = strings.Trim(confirmSeed, `"`) confirmSeed = strings.Trim(confirmSeed, `"`)
@ -284,8 +315,18 @@ func Seed(reader *bufio.Reader) ([]byte, error) {
} }
} }
return seed, nil return seed, bday, nil
} }
return provideSeed(reader) seed, err := provideSeed(reader)
if err != nil {
return nil, bday, err
}
bday, err = birthday(reader)
if err != nil {
return nil, bday, err
}
return seed, bday, nil
} }

View file

@ -182,19 +182,21 @@ func createWallet(cfg *config) error {
// Ascertain the wallet generation seed. This will either be an // Ascertain the wallet generation seed. This will either be an
// automatically generated value the user has already confirmed or a // automatically generated value the user has already confirmed or a
// value the user has entered which has already been validated. // value the user has entered which has already been validated.
seed, err := prompt.Seed(reader) seed, bday, err := prompt.Seed(reader)
if err != nil { if err != nil {
return err return err
} }
fmt.Println("Creating the wallet...") fmt.Println("Creating the wallet...")
w, err := loader.CreateNewWallet(pubPass, privPass, seed, time.Now()) w, err := loader.CreateNewWallet(pubPass, privPass, seed, bday)
if err != nil { if err != nil {
return err return err
} }
w.Manager.Close() w.Manager.Close()
fmt.Println("The wallet has been created successfully.")
fmt.Println("The wallet has been created successfully with birthday:", bday.Format(time.UnixDate))
return nil return nil
} }