File tree Expand file tree Collapse file tree 4 files changed +57
-4
lines changed
Expand file tree Collapse file tree 4 files changed +57
-4
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 55 "sync"
66)
77
8+ var _ Storage = & MemoryStorage {}
9+
810// MemoryStorage implements Storage using in-memory storage
911type MemoryStorage struct {
1012 mu sync.RWMutex
Original file line number Diff line number Diff line change 11package storage_test
22
33import (
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
1111func 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}
You can’t perform that action at this time.
0 commit comments