-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_mnemonic.go
64 lines (51 loc) · 1.05 KB
/
create_mnemonic.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
package wallet
import (
"errors"
"fmt"
"github.com/tyler-smith/go-bip39"
"github.com/spf13/cobra"
)
var length int
func newCreateMnemonic() *cobra.Command {
cmd := &cobra.Command{
Use: "createmnemonic",
Short: "Creates and returns a mnemonic sentence of the length specified",
RunE: runCreateMnemonic(),
}
cmd.Flags().IntVarP(&length, "length", "l", 24, "Number of words in the mnemonic")
return cmd
}
func runCreateMnemonic() runEFunc {
return func(cmd *cobra.Command, args []string) error {
bitSize, err := lengthToBitSize(length)
if err != nil {
return err
}
entropy, err := bip39.NewEntropy(bitSize)
if err != nil {
return err
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return err
}
fmt.Println(mnemonic)
return nil
}
}
func lengthToBitSize(length int) (int, error) {
switch length {
case 12:
return 128, nil
case 15:
return 160, nil
case 18:
return 192, nil
case 21:
return 224, nil
case 24:
return 256, nil
default:
return 0, errors.New("invalid mnemonic length")
}
}