2 package memstorage provides a simple in-memory key store designed for
3 use in test cases, particularly to isolate them from the filesystem,
4 concurrency, and cleanup issues.
9 "github.com/pkg/errors"
10 keys "github.com/tendermint/go-crypto/keys"
18 type MemStore map[string]data
20 // New creates an instance of file-based key storage with tight permissions
25 // assert MemStore satisfies keys.Storage
26 var _ keys.Storage = MemStore{}
28 // Put adds the given key, returns an error if it another key
29 // is already stored under this name
30 func (s MemStore) Put(name string, key []byte, info keys.Info) error {
31 if _, ok := s[name]; ok {
32 return errors.Errorf("Key named '%s' already exists", name)
34 s[name] = data{info, key}
38 // Get returns the key stored under the name, or returns an error if not present
39 func (s MemStore) Get(name string) ([]byte, keys.Info, error) {
43 err = errors.Errorf("Key named '%s' doesn't exist", name)
45 return d.key, d.info.Format(), err
48 // List returns the public info of all keys in the MemStore in unsorted order
49 func (s MemStore) List() (keys.Infos, error) {
50 res := make([]keys.Info, len(s))
53 res[i] = d.info.Format()
59 // Delete removes the named key from the MemStore, raising an error if it
60 // wasn't present yet.
61 func (s MemStore) Delete(name string) error {
64 return errors.Errorf("Key named '%s' doesn't exist", name)