-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
59 lines (49 loc) · 1.73 KB
/
utils.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
package krab
import (
"fmt"
"strings"
mnemonics "github.com/RTradeLtd/entropy-mnemonics"
ci "github.com/libp2p/go-libp2p-core/crypto"
)
// all of this is taken from https://github.com/ipfs/go-ipfs-keystore
var (
// ErrNoSuchKey is returned if a key of the given name is not found in the store
ErrNoSuchKey = "no key by the given name was found"
// ErrKeyExists is returned when writing a key would overwrite an existing key
ErrKeyExists = "key by that name already exists, refusing to overwrite"
// ErrKeyFmt is returned when the key's format is invalid
ErrKeyFmt = "key has invalid format"
)
// ExportKeyAsMnemonic is used to take an IPFS key, and return a human-readable friendly version.
// The idea is to allow users to easily export the keys they create, allowing them to take control of their records (ipns, tns, etc..)
func ExportKeyAsMnemonic(pk ci.PrivKey) (string, error) {
pkBytes, err := pk.Bytes()
if err != nil {
return "", err
}
phrase, err := mnemonics.ToPhrase(pkBytes, mnemonics.English)
if err != nil {
return "", err
}
return phrase.String(), nil
}
// MnemonicToKey takes an exported mnemonic phrase, and converts it to a private key
func MnemonicToKey(phrase string) (ci.PrivKey, error) {
mnemonicBytes, err := mnemonics.FromString(phrase, mnemonics.English)
if err != nil {
return nil, err
}
return ci.UnmarshalPrivateKey(mnemonicBytes)
}
func validateName(name string) error {
if name == "" {
return fmt.Errorf("%s: key names must be at least one character", ErrKeyFmt)
}
if strings.Contains(name, "/") {
return fmt.Errorf("%s: key names may not contain slashes", ErrKeyFmt)
}
if strings.HasPrefix(name, ".") {
return fmt.Errorf("%s: key names may not begin with a period", ErrKeyFmt)
}
return nil
}