-
Notifications
You must be signed in to change notification settings - Fork 62
/
memkeystore.go
55 lines (43 loc) · 991 Bytes
/
memkeystore.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
package keystore
import ci "gx/ipfs/QmNiCwBNA8MWDADTFVq1BonUEJbS2SvjAoNkZZrhEwcuUi/go-libp2p-crypto"
type MemKeystore struct {
keys map[string]ci.PrivKey
}
func NewMemKeystore() *MemKeystore {
return &MemKeystore{make(map[string]ci.PrivKey)}
}
func (mk *MemKeystore) Put(name string, k ci.PrivKey) error {
if err := validateName(name); err != nil {
return err
}
_, ok := mk.keys[name]
if ok {
return ErrKeyExists
}
mk.keys[name] = k
return nil
}
func (mk *MemKeystore) Get(name string) (ci.PrivKey, error) {
if err := validateName(name); err != nil {
return nil, err
}
k, ok := mk.keys[name]
if !ok {
return nil, ErrNoSuchKey
}
return k, nil
}
func (mk *MemKeystore) Delete(name string) error {
if err := validateName(name); err != nil {
return err
}
delete(mk.keys, name)
return nil
}
func (mk *MemKeystore) List() ([]string, error) {
out := make([]string, 0, len(mk.keys))
for k, _ := range mk.keys {
out = append(out, k)
}
return out, nil
}