keystore interface docs
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jan 22, 2018 at 11:20 UTC
76907bb4022da64a18d45a7f6ee37d04f9536757
2 files changed
+16
-10
keystore/keystore.go
+13
-9
@@ -10,22 +10,25 @@ import (
10
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
11
)
12
13
+// Keystore provides a key management interface
14
type Keystore interface {
14
- // Has return whether or not a key exist in the Keystore
15
+ // Has returns whether or not a key exist in the Keystore
16
Has(string) (bool, error)
16
- // Put store a key in the Keystore
17
+ // Put stores a key in the Keystore, if a key with the same name already exists, returns ErrKeyExists
18
Put(string, ci.PrivKey) error
18
- // Get retrieve a key from the Keystore
19
+ // Get retrieves a key from the Keystore if it exists, and returns ErrNoSuchKey
20
+ // otherwise.
21
Get(string) (ci.PrivKey, error)
20
- // Delete remove a key from the Keystore
22
+ // Delete removes a key from the Keystore
23
Delete(string) error
22
- // List return a list of key identifier
24
+ // List returns a list of key identifier
25
List() ([]string, error)
26
}
27
28
var ErrNoSuchKey = fmt.Errorf("no key by the given name was found")
29
var ErrKeyExists = fmt.Errorf("key by that name already exists, refusing to overwrite")
30
31
+// FSKeystore is a keystore backed by files in a given directory stored on disk.
32
type FSKeystore struct {
33
dir string
34
}
@@ -60,7 +63,7 @@ func NewFSKeystore(dir string) (*FSKeystore, error) {
63
return &FSKeystore{dir}, nil
64
}
65
63
-// Has return whether or not a key exist in the Keystore
66
+// Has returns whether or not a key exist in the Keystore
67
func (ks *FSKeystore) Has(name string) (bool, error) {
68
kp := filepath.Join(ks.dir, name)
69
@@ -77,7 +80,7 @@ func (ks *FSKeystore) Has(name string) (bool, error) {
80
return true, nil
81
}
82
80
-// Put store a key in the Keystore
83
+// Put stores a key in the Keystore, if a key with the same name already exists, returns ErrKeyExists
84
func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
85
if err := validateName(name); err != nil {
86
return err
@@ -108,7 +111,8 @@ func (ks *FSKeystore) Put(name string, k ci.PrivKey) error {
111
return err
112
}
113
111
-// Get retrieve a key from the Keystore
114
+// Get retrieves a key from the Keystore if it exists, and returns ErrNoSuchKey
115
+// otherwise.
116
func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) {
117
if err := validateName(name); err != nil {
118
return nil, err
@@ -127,7 +131,7 @@ func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) {
131
return ci.UnmarshalPrivateKey(data)
132
}
133
130
-// Delete remove a key from the Keystore
134
+// Delete removes a key from the Keystore
135
func (ks *FSKeystore) Delete(name string) error {
136
if err := validateName(name); err != nil {
137
return err
keystore/memkeystore.go
+3
-1
@@ -2,6 +2,8 @@ package keystore
2
3
import ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
4
5
+// MemKeystore is an in memory keystore implementation that is not persisted to
6
+// any backing storage.
7
type MemKeystore struct {
8
keys map[string]ci.PrivKey
9
}
@@ -58,7 +60,7 @@ func (mk *MemKeystore) Delete(name string) error {
60
// List return a list of key identifier
61
func (mk *MemKeystore) List() ([]string, error) {
62
out := make([]string, 0, len(mk.keys))
61
- for k, _ := range mk.keys {
63
+ for k := range mk.keys {
64
out = append(out, k)
65
}
66
return out, nil