Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions keystore/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"sync"
"testing"

"github.com/smartcontractkit/chainlink-common/keystore"
"github.com/smartcontractkit/chainlink-common/keystore/storage"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/keystore"
)

func TestKeystore_CreateDeleteReadKeys(t *testing.T) {
Expand Down Expand Up @@ -110,7 +110,7 @@ func TestKeystore_CreateDeleteReadKeys(t *testing.T) {

for _, tt := range tt {
t.Run(tt.name, func(t *testing.T) {
storage := storage.NewMemoryStorage()
storage := keystore.NewMemoryStorage()
ks, err := keystore.LoadKeystore(ctx, storage, keystore.EncryptionParams{
Password: "test-password",
ScryptParams: keystore.FastScryptParams,
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestKeystore_ConcurrentCreateAndRead(t *testing.T) {
t.Parallel()

ctx := context.Background()
st := storage.NewMemoryStorage()
st := keystore.NewMemoryStorage()
ks, err := keystore.LoadKeystore(ctx, st, keystore.EncryptionParams{
Password: "test",
ScryptParams: keystore.FastScryptParams,
Expand Down
29 changes: 29 additions & 0 deletions keystore/file.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package keystore

import (
"context"
"os"
)

const readWritePerms = os.FileMode(0600)

var _ Storage = &FileStorage{}

// FileStorage implements Storage using a file
type FileStorage struct {
name string
}

func NewFileStorage(name string) *FileStorage {
return &FileStorage{
name: name,
}
}

func (f *FileStorage) GetEncryptedKeystore(ctx context.Context) ([]byte, error) {
return os.ReadFile(f.name)
}

func (f *FileStorage) PutEncryptedKeystore(ctx context.Context, encryptedKeystore []byte) error {
return os.WriteFile(f.name, encryptedKeystore, readWritePerms)
}
21 changes: 21 additions & 0 deletions keystore/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package keystore_test

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/keystore"
)

func TestFileStorage(t *testing.T) {
t.Parallel()
storage := keystore.NewFileStorage(filepath.Join(t.TempDir(), "out.txt"))
_, err := storage.GetEncryptedKeystore(t.Context())
require.ErrorContains(t, err, "no such file or directory")
require.NoError(t, storage.PutEncryptedKeystore(t.Context(), []byte("test")))
got, err := storage.GetEncryptedKeystore(t.Context())
require.NoError(t, err)
require.Equal(t, []byte("test"), got)
}
8 changes: 4 additions & 4 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (

gethkeystore "github.com/ethereum/go-ethereum/accounts/keystore"
gethcrypto "github.com/ethereum/go-ethereum/crypto"
"google.golang.org/protobuf/proto"

"github.com/smartcontractkit/chainlink-common/keystore/internal"
"github.com/smartcontractkit/chainlink-common/keystore/serialization"
"github.com/smartcontractkit/chainlink-common/keystore/storage"
"google.golang.org/protobuf/proto"
)

type KeyType string
Expand Down Expand Up @@ -162,11 +162,11 @@ func publicKeyFromPrivateKey(privateKeyBytes internal.Raw, keyType KeyType) ([]b
type keystore struct {
mu sync.RWMutex
keystore map[string]key
storage storage.Storage
storage Storage
enc EncryptionParams
}

func LoadKeystore(ctx context.Context, storage storage.Storage, enc EncryptionParams) (Keystore, error) {
func LoadKeystore(ctx context.Context, storage Storage, enc EncryptionParams) (Keystore, error) {
ks := &keystore{
storage: storage,
enc: enc,
Expand Down
4 changes: 3 additions & 1 deletion keystore/storage/memory.go → keystore/memory.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package storage
package keystore

import (
"context"
"sync"
)

var _ Storage = &MemoryStorage{}

// MemoryStorage implements Storage using in-memory storage
type MemoryStorage struct {
mu sync.RWMutex
Expand Down
18 changes: 18 additions & 0 deletions keystore/memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package keystore_test

import (
"testing"

"github.com/stretchr/testify/require"

"github.com/smartcontractkit/chainlink-common/keystore"
)

func TestMemoryStorage(t *testing.T) {
t.Parallel()
storage := keystore.NewMemoryStorage()
require.NoError(t, storage.PutEncryptedKeystore(t.Context(), []byte("test")))
got, err := storage.GetEncryptedKeystore(t.Context())
require.NoError(t, err)
require.Equal(t, []byte("test"), got)
}
2 changes: 1 addition & 1 deletion keystore/storage/storage.go → keystore/storage.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package storage
package keystore

import "context"

Expand Down
17 changes: 0 additions & 17 deletions keystore/storage/memory_test.go

This file was deleted.

Loading