-
Notifications
You must be signed in to change notification settings - Fork 13
/
mnemonic.go
47 lines (40 loc) · 1.35 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
package core
import "github.com/tyler-smith/go-bip39"
// GenerateNewEntropy generates a new entropy
func GenerateNewEntropy() ([]byte, error) {
entropy, err := bip39.NewEntropy(256)
if err != nil {
return nil, err
}
return entropy, nil
}
// EntropyToMnemonic creates the mnemonic passphrase.
func EntropyToMnemonic(entropy []byte) (string, error) {
mnemonic, err := bip39.NewMnemonic(entropy)
if err != nil {
return "", err
}
return mnemonic, nil
}
// SeedFromMnemonic generates a new seed.
// The seed is the product of applying a key derivation algo (PBKDF2) on the mnemonic (as the entropy)
// and the password as salt.
// Please see https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
func SeedFromMnemonic(mnemonic string, password string) ([]byte, error) {
seed, err := bip39.NewSeedWithErrorChecking(mnemonic, password)
if err != nil {
return nil, err
}
return seed, nil
}
// SeedFromEntropy creates seed from the given entropy
// the seed is the product of applying a key derivation algo (PBKDF2) on the mnemonic (as the entropy)
// and the password as salt.
// please see https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki
func SeedFromEntropy(entropy []byte, password string) ([]byte, error) {
mnemonic, err := EntropyToMnemonic(entropy)
if err != nil {
return nil, err
}
return bip39.NewSeed(mnemonic, password), nil
}