Skip to content

Commit b33076e

Browse files
committed
Minor.
1 parent 3b2bf34 commit b33076e

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

keystore/storage/file.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package storage
2+
3+
import (
4+
"context"
5+
"os"
6+
)
7+
8+
const readWritePerms = os.FileMode(0600)
9+
10+
var _ Storage = &FileStorage{}
11+
12+
// FileStorage implements Storage using a file
13+
type FileStorage struct {
14+
name string
15+
}
16+
17+
func NewFileStorage(name string) *FileStorage {
18+
return &FileStorage{
19+
name: name,
20+
}
21+
}
22+
23+
func (f *FileStorage) GetEncryptedKeystore(ctx context.Context) ([]byte, error) {
24+
return os.ReadFile(f.name)
25+
}
26+
27+
func (f *FileStorage) PutEncryptedKeystore(ctx context.Context, encryptedKeystore []byte) error {
28+
return os.WriteFile(f.name, encryptedKeystore, readWritePerms)
29+
}

keystore/storage/file_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package storage_test
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/smartcontractkit/chainlink-common/keystore/storage"
10+
)
11+
12+
func TestFileStorage(t *testing.T) {
13+
t.Parallel()
14+
storage := storage.NewFileStorage(filepath.Join(t.TempDir(), "out.txt"))
15+
_, err := storage.GetEncryptedKeystore(t.Context())
16+
require.ErrorContains(t, err, "no such file or directory")
17+
require.NoError(t, storage.PutEncryptedKeystore(t.Context(), []byte("test")))
18+
got, err := storage.GetEncryptedKeystore(t.Context())
19+
require.NoError(t, err)
20+
require.Equal(t, []byte("test"), got)
21+
}

keystore/storage/memory.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"sync"
66
)
77

8+
var _ Storage = &MemoryStorage{}
9+
810
// MemoryStorage implements Storage using in-memory storage
911
type MemoryStorage struct {
1012
mu sync.RWMutex

keystore/storage/memory_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package storage_test
22

33
import (
4-
"context"
54
"testing"
65

7-
"github.com/smartcontractkit/chainlink-common/keystore/storage"
86
"github.com/stretchr/testify/require"
7+
8+
"github.com/smartcontractkit/chainlink-common/keystore/storage"
99
)
1010

1111
func TestMemoryStorage(t *testing.T) {
12+
t.Parallel()
1213
storage := storage.NewMemoryStorage()
13-
require.NoError(t, storage.PutEncryptedKeystore(context.Background(), []byte("test")))
14-
got, err := storage.GetEncryptedKeystore(context.Background())
14+
require.NoError(t, storage.PutEncryptedKeystore(t.Context(), []byte("test")))
15+
got, err := storage.GetEncryptedKeystore(t.Context())
1516
require.NoError(t, err)
1617
require.Equal(t, []byte("test"), got)
1718
}

0 commit comments

Comments
 (0)