-
Notifications
You must be signed in to change notification settings - Fork 893
/
map_keystore.go
84 lines (65 loc) · 1.52 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
72
73
74
75
76
77
78
79
80
81
82
83
84
package keystore
import (
"fmt"
"sync"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/celestiaorg/celestia-app/app"
"github.com/celestiaorg/celestia-app/app/encoding"
)
// mapKeystore is a simple in-memory Keystore implementation.
type mapKeystore struct {
keys map[KeyName]PrivKey
keysLk sync.Mutex
ring keyring.Keyring
}
// NewMapKeystore constructs in-memory Keystore.
func NewMapKeystore() Keystore {
return &mapKeystore{
keys: make(map[KeyName]PrivKey),
ring: keyring.NewInMemory(encoding.MakeConfig(app.ModuleEncodingRegisters...).Codec),
}
}
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 ""
}
func (m *mapKeystore) Keyring() keyring.Keyring {
return m.ring
}