Skip to content

Commit

Permalink
feat: improve vanity account generation experience
Browse files Browse the repository at this point in the history
* Vanity account generator buffers up to 5 results and asks you if you want to save them
* Fix the AskYes function

closes #129
  • Loading branch information
randomshinichi authored and noandrea committed Nov 4, 2019
1 parent bd8ba2a commit 51440e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 11 deletions.
29 changes: 24 additions & 5 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,23 +239,42 @@ func vanityFunc(cmd *cobra.Command, args []string) {
}
fmt.Println("The search for your account matching", searchString, "has begun")

foundAccounts := make(chan *account.Account, 5)
var wg sync.WaitGroup
wg.Add(runtime.NumCPU())
for i := 0; i < runtime.NumCPU(); i++ {
go func() {
for {
a, _ := account.New()

if r.MatchString(a.Address[3:]) {
fmt.Println("FOUND!")
fmt.Println("Secret: ", a.SigningKeyToHexString())
fmt.Println("Address", a.Address)
foundAccounts <- a
}
}
}()
}
wg.Wait()
for a := range foundAccounts {
fmt.Printf("Found account! %s \n", a.Address)
filename := fmt.Sprintf("account.%s.json", a.Address)
yes := AskYes(fmt.Sprintf("Save it to %s?", filename), false)
if yes {
pw, err := AskPassword("To encrypt the keystore, please provide a password")
if err != nil {
fmt.Println(err)
}

path, err := account.StoreToKeyStoreFile(a, pw, filename)
if err != nil {
fmt.Println(err)
}
fmt.Println("Account saved in", path)

continueSearch := AskYes("Would you like to continue searching?", false)
if !continueSearch {
return
}
}
}
wg.Wait()
}

func init() {
Expand Down
11 changes: 5 additions & 6 deletions cmd/text_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ func DefaultIfEmptyStr(s *string, defaultS string) {

// AskYes prompt a yes/no question to the prompt
func AskYes(question string, defaultYes bool) (isYes bool) {
fmt.Print(question)
if defaultYes {
fmt.Print(" [yes]: ")
} else {
fmt.Print(" [no]: ")
defaultStrVal := "yes"
if !defaultYes {
defaultStrVal = "no"
}
fmt.Print(question, " [", defaultStrVal, "]: ")
reader := bufio.NewReader(os.Stdin)
reply, _ := reader.ReadString('\n')
DefaultIfEmptyStr(&reply, "yes")
DefaultIfEmptyStr(&reply, defaultStrVal)
if IsEqStr(reply, "yes") {
return true
}
Expand Down

0 comments on commit 51440e8

Please sign in to comment.