forked from celestiaorg/celestia-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_keystore.go
71 lines (55 loc) · 1.2 KB
/
map_keystore.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
60
61
62
63
64
65
66
67
68
69
70
71
package keystore
import (
"fmt"
"sync"
)
// mapKeystore is a simple in-memory Keystore implementation.
type mapKeystore struct {
keys map[KeyName]PrivKey
keysLk sync.Mutex
}
// NewMapKeystore constructs in-memory Keystore.
func NewMapKeystore() Keystore {
return &mapKeystore{keys: make(map[KeyName]PrivKey)}
}
func (m *mapKeystore) Put(n KeyName, k PrivKey) error {
m.keysLk.Lock()
defer m.keysLk.Unlock()
_, ok := m.keys[n]
if ok {
return fmt.Errorf("keystore: key '%s' already exists", n)
}
m.keys[n] = k
return nil
}
func (m *mapKeystore) Get(n KeyName) (PrivKey, error) {
m.keysLk.Lock()
defer m.keysLk.Unlock()
k, ok := m.keys[n]
if !ok {
return PrivKey{}, fmt.Errorf("%w: %s", ErrNotFound, n)
}
return k, nil
}
func (m *mapKeystore) Delete(n KeyName) error {
m.keysLk.Lock()
defer m.keysLk.Unlock()
_, ok := m.keys[n]
if !ok {
return fmt.Errorf("keystore: key '%s' not found", n)
}
delete(m.keys, n)
return nil
}
func (m *mapKeystore) List() ([]KeyName, error) {
m.keysLk.Lock()
defer m.keysLk.Unlock()
keys := make([]KeyName, 0, len(m.keys))
for k := range m.keys {
keys = append(keys, k)
}
return keys, nil
}
func (m *mapKeystore) Path() string {
return ""
}