Created
January 26, 2021 13:36
-
-
Save meehow/bb646b216e23608351377a344135e91e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
"os/exec" | |
"strings" | |
"time" | |
"github.com/ethereum/go-ethereum/accounts" | |
hdwallet "github.com/miguelmota/go-ethereum-hdwallet" | |
"github.com/tyler-smith/go-bip39" | |
) | |
var wordlist = bip39.GetWordList() | |
func contains(list []string, item string) bool { | |
for _, listitem := range list { | |
if listitem == item { | |
return true | |
} | |
} | |
return false | |
} | |
func ok() { | |
fmt.Println("OK") | |
time.Sleep(time.Second) | |
cmd := exec.Command("clear") | |
cmd.Stdout = os.Stdout | |
cmd.Run() | |
} | |
func main() { | |
mnemonic := buildMnemonic() | |
wallet, err := hdwallet.NewFromMnemonic(mnemonic) | |
account, err := wallet.Derive(accounts.DefaultBaseDerivationPath, false) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(account.Address.Hex()) | |
} | |
func buildMnemonic() string { | |
fmt.Printf("Wordlist: %v\n", wordlist) | |
fmt.Println( | |
`To build your mnemonic, you can hand pick first 23 words of the mnemonic. | |
List word can be chosen from shorter list, to match correct checksum of your mnemonic.`) | |
words := make([]string, 0, 23) | |
scanner := bufio.NewScanner(os.Stdin) | |
for len(words) < 23 { | |
fmt.Printf("Enter word %d: ", len(words)+1) | |
if !scanner.Scan() { | |
log.Fatal("Can't scan") | |
} | |
word := scanner.Text() | |
if !contains(wordlist, word) { | |
fmt.Printf("Word %q not found on a mnemonic list\n", word) | |
continue | |
} | |
words = append(words, word) | |
ok() | |
} | |
first23 := strings.Join(words, " ") | |
last := make([]string, 0, 8) | |
for _, word := range wordlist { | |
_, err := bip39.MnemonicToByteArray(first23 + " " + word) | |
if err == nil { | |
last = append(last, word) | |
} | |
} | |
fmt.Printf("Possible closing words %v\n", last) | |
for { | |
fmt.Print("Enter last word: ") | |
if !scanner.Scan() { | |
log.Fatal("Can't scan") | |
} | |
word := scanner.Text() | |
_, err := bip39.MnemonicToByteArray(first23 + " " + word) | |
if err != nil { | |
fmt.Println(err) | |
continue | |
} | |
ok() | |
return first23 + " " + word | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment