forked from miguelmota/go-ethereum-hdwallet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mnemonic.go
56 lines (48 loc) · 1 KB
/
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
package hdwallet
import (
"fmt"
"strings"
"github.com/tyler-smith/go-bip39"
)
type Mnemonic struct {
bitSize int
passphrase string
data string
}
func NewMenonic(bitSize int, passphrase string) (m *Mnemonic, err error) {
if bitSize != 128 && bitSize != 256 {
return nil, fmt.Errorf("invalid bit size: %d", bitSize)
}
m = &Mnemonic{
passphrase: passphrase,
bitSize: bitSize,
}
entropy, err := bip39.NewEntropy(bitSize)
if err != nil {
return nil, err
}
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return nil, err
}
m.data = mnemonic
return
}
func (m *Mnemonic) Words() []string {
return strings.Split(m.data, " ")
}
func (m *Mnemonic) Data() string {
return m.data
}
func (m *Mnemonic) Seed() []byte {
return bip39.NewSeed(m.data, m.passphrase)
}
func (m *Mnemonic) String() string {
s := strings.Builder{}
s.WriteString("mnemonic: ")
s.WriteString(m.data)
s.WriteRune('\n')
s.WriteString("passphrase: ")
s.WriteString(m.passphrase)
return s.String()
}