-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (71 loc) · 1.82 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"bufio"
"crypto/rand"
"flag"
"fmt"
"io"
"math/big"
"os"
)
type CliArgs struct {
wordCount int
lang string
verbose bool
}
func init() {
assertAvailablePRNG()
}
func assertAvailablePRNG() {
// Assert that a cryptographically secure PRNG is available.
// Panic otherwise.
buf := make([]byte, 1)
_, err := io.ReadFull(rand.Reader, buf)
if err != nil {
panic(fmt.Sprintf("crypto/rand is unavailable: Read() failed with %#v", err))
}
}
func main() {
args := readCliArgs()
bip39 := readBip39Words(args)
numbers := generateRandomNumbers(args.wordCount, len(bip39))
for _, num := range numbers {
if args.verbose {
fmt.Printf("%4v:", num)
}
fmt.Println(bip39[num])
}
}
func generateRandomNumbers(count int, max int) []int {
var numbers []int
for i := 0; i < count; i++ {
num, err := rand.Int(rand.Reader, big.NewInt(int64(max)))
if err != nil {
panic(err)
}
numbers = append(numbers, int(num.Int64()))
}
return numbers
}
func readCliArgs() CliArgs {
args := CliArgs{}
flag.IntVar(&args.wordCount, "n", 12, "Specify number of BIP39 words to generate. Default is 12.")
flag.StringVar(&args.lang, "l", "english", "Specify language from which BIP39 list to select from. See list of available languages at https://github.com/bitcoin/bips/tree/master/bip-0039. Default is english.")
flag.BoolVar(&args.verbose, "v", false, "Print indexes of each word in the BIP39 alongside the word. Default is false.")
flag.Parse()
return args
}
func readBip39Words(args CliArgs) []string {
readFile, err := os.Open(fmt.Sprintf("bips/bip-0039/%v.txt", args.lang))
if err != nil {
panic(err)
}
fileScanner := bufio.NewScanner(readFile)
fileScanner.Split(bufio.ScanLines)
var bip39 []string
for fileScanner.Scan() {
bip39 = append(bip39, fileScanner.Text())
}
readFile.Close()
return bip39
}