Skip to content

Commit 660fb0f

Browse files
authored
[ARCH-330] Add keystore file storage (#1633)
1 parent a50d2a6 commit 660fb0f

8 files changed

Lines changed: 80 additions & 27 deletions

File tree

keystore/admin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"sync"
88
"testing"
99

10-
"github.com/smartcontractkit/chainlink-common/keystore"
11-
"github.com/smartcontractkit/chainlink-common/keystore/storage"
1210
"github.com/stretchr/testify/assert"
1311
"github.com/stretchr/testify/require"
12+
13+
"github.com/smartcontractkit/chainlink-common/keystore"
1414
)
1515

1616
func TestKeystore_CreateDeleteReadKeys(t *testing.T) {
@@ -110,7 +110,7 @@ func TestKeystore_CreateDeleteReadKeys(t *testing.T) {
110110

111111
for _, tt := range tt {
112112
t.Run(tt.name, func(t *testing.T) {
113-
storage := storage.NewMemoryStorage()
113+
storage := keystore.NewMemoryStorage()
114114
ks, err := keystore.LoadKeystore(ctx, storage, keystore.EncryptionParams{
115115
Password: "test-password",
116116
ScryptParams: keystore.FastScryptParams,
@@ -162,7 +162,7 @@ func TestKeystore_ConcurrentCreateAndRead(t *testing.T) {
162162
t.Parallel()
163163

164164
ctx := context.Background()
165-
st := storage.NewMemoryStorage()
165+
st := keystore.NewMemoryStorage()
166166
ks, err := keystore.LoadKeystore(ctx, st, keystore.EncryptionParams{
167167
Password: "test",
168168
ScryptParams: keystore.FastScryptParams,

keystore/file.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package keystore
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/file_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package keystore_test
2+
3+
import (
4+
"path/filepath"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
9+
"github.com/smartcontractkit/chainlink-common/keystore"
10+
)
11+
12+
func TestFileStorage(t *testing.T) {
13+
t.Parallel()
14+
storage := keystore.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/keystore.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ import (
1313

1414
gethkeystore "github.com/ethereum/go-ethereum/accounts/keystore"
1515
gethcrypto "github.com/ethereum/go-ethereum/crypto"
16+
"google.golang.org/protobuf/proto"
17+
1618
"github.com/smartcontractkit/chainlink-common/keystore/internal"
1719
"github.com/smartcontractkit/chainlink-common/keystore/serialization"
18-
"github.com/smartcontractkit/chainlink-common/keystore/storage"
19-
"google.golang.org/protobuf/proto"
2020
)
2121

2222
type KeyType string
@@ -162,11 +162,11 @@ func publicKeyFromPrivateKey(privateKeyBytes internal.Raw, keyType KeyType) ([]b
162162
type keystore struct {
163163
mu sync.RWMutex
164164
keystore map[string]key
165-
storage storage.Storage
165+
storage Storage
166166
enc EncryptionParams
167167
}
168168

169-
func LoadKeystore(ctx context.Context, storage storage.Storage, enc EncryptionParams) (Keystore, error) {
169+
func LoadKeystore(ctx context.Context, storage Storage, enc EncryptionParams) (Keystore, error) {
170170
ks := &keystore{
171171
storage: storage,
172172
enc: enc,
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
package storage
1+
package keystore
22

33
import (
44
"context"
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/memory_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package keystore_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/smartcontractkit/chainlink-common/keystore"
9+
)
10+
11+
func TestMemoryStorage(t *testing.T) {
12+
t.Parallel()
13+
storage := keystore.NewMemoryStorage()
14+
require.NoError(t, storage.PutEncryptedKeystore(t.Context(), []byte("test")))
15+
got, err := storage.GetEncryptedKeystore(t.Context())
16+
require.NoError(t, err)
17+
require.Equal(t, []byte("test"), got)
18+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package storage
1+
package keystore
22

33
import "context"
44

keystore/storage/memory_test.go

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)