-
Notifications
You must be signed in to change notification settings - Fork 12
/
melt.go
35 lines (29 loc) · 953 Bytes
/
melt.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
// Package melt provides function to create a mnemonic set of keys from a
// ed25519 private key, and restore that key from the same mnemonic set of
// words.
package melt
import (
"crypto/ed25519"
"fmt"
"github.com/tyler-smith/go-bip39"
)
// ToMnemonic takes a ed25519 private key and returns the list of words.
func ToMnemonic(key *ed25519.PrivateKey) (string, error) {
return toMnemonic(key.Seed())
}
func toMnemonic(seed []byte) (string, error) {
words, err := bip39.NewMnemonic(seed)
if err != nil {
return "", fmt.Errorf("could not create a mnemonic set of words: %w", err)
}
return words, nil
}
// FromMnemonic takes a mnemonic list of words and returns an ed25519
// private key.
func FromMnemonic(mnemonic string) (ed25519.PrivateKey, error) {
seed, err := bip39.EntropyFromMnemonic(mnemonic)
if err != nil {
return nil, fmt.Errorf("failed to get seed from mnemonic: %w", err)
}
return ed25519.NewKeyFromSeed(seed), nil
}