From ffc14ee92d17fc4ecfe412cef7e97a9513cde176 Mon Sep 17 00:00:00 2001 From: Andor Kesselman Date: Tue, 21 Feb 2023 12:13:49 +0530 Subject: [PATCH 1/3] added utility on new key store service from keystorage --- pkg/service/keystore/service.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/service/keystore/service.go b/pkg/service/keystore/service.go index 0e42795d2..40a8d8aba 100644 --- a/pkg/service/keystore/service.go +++ b/pkg/service/keystore/service.go @@ -46,6 +46,18 @@ func (s Service) Config() config.KeyStoreServiceConfig { return s.config } +// NewKeyStoreFromKeystoreStorage uses a keystore service directly from storage object +func NewKeyStoreFromKeystoreStorage(config config.KeyStoreServiceConfig, keyStoreStorage *Storage) (*Service, error) { + service := Service{ + storage: keyStoreStorage, + config: config, + } + if !service.Status().IsReady() { + return nil, errors.New(service.Status().Message) + } + return &service, nil +} + func NewKeyStoreService(config config.KeyStoreServiceConfig, s storage.ServiceStorage) (*Service, error) { // First, generate a service key serviceKey, serviceKeySalt, err := GenerateServiceKey(config.ServiceKeyPassword) From abb7b954a0fd4ca70239e2714476ef81b23bd8bd Mon Sep 17 00:00:00 2001 From: Andor Kesselman Date: Tue, 21 Feb 2023 13:12:44 +0530 Subject: [PATCH 2/3] added additional test --- pkg/service/keystore/service_test.go | 62 ++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) diff --git a/pkg/service/keystore/service_test.go b/pkg/service/keystore/service_test.go index 170ee6c02..5da626810 100644 --- a/pkg/service/keystore/service_test.go +++ b/pkg/service/keystore/service_test.go @@ -140,3 +140,65 @@ func TestStoreAndGetKey(t *testing.T) { assert.NoError(t, err) assert.NotEmpty(t, signer) } + +func TestStoreAndGetKeyWithExistingKeystorage(t *testing.T) { + + file, err := os.CreateTemp("", "bolt") + require.NoError(t, err) + name := file.Name() + assert.NoError(t, file.Close()) + bolt, err := storage.NewStorage(storage.Bolt, name) + assert.NoError(t, err) + assert.NotEmpty(t, bolt) + + // remove the db file after the test + t.Cleanup(func() { + _ = bolt.Close() + _ = os.Remove(bolt.URI()) + }) + + config := config.KeyStoreServiceConfig{ + BaseServiceConfig: &config.BaseServiceConfig{ + Name: "test-keyStore", + }, + ServiceKeyPassword: "test-password", + } + + serviceKey, serviceKeySalt, err := GenerateServiceKey(config.ServiceKeyPassword) + + // Next, instantiate the key storage + keyStoreStorage, err := NewKeyStoreStorage(s, ServiceKey{ + Base58Key: serviceKey, + Base58Salt: serviceKeySalt, + }) + + keyStore, err := NewKeyStoreFromKeystoreStorage( + config, + keyStoreStorage, + ) + + assert.NoError(t, err) + assert.NotEmpty(t, keyStore) + + // store the key + _, privKey, err := crypto.GenerateEd25519Key() + assert.NoError(t, err) + err = keyStore.StoreKey(context.Background(), StoreKeyRequest{ + ID: "test-id", + Type: crypto.Ed25519, + Controller: "test-controller", + PrivateKeyBase58: base58.Encode(privKey), + }) + assert.NoError(t, err) + + // get it back + keyResponse, err := keyStore.GetKey(context.Background(), GetKeyRequest{ID: "test-id"}) + assert.NoError(t, err) + assert.NotEmpty(t, keyResponse) + assert.Equal(t, privKey, keyResponse.Key) + + // make sure can create a signer properly + signer, err := crypto.NewJWTSigner("kid", keyResponse.Key) + assert.NoError(t, err) + assert.NotEmpty(t, signer) +} From b6631802866251bccb80fe947566d154a474effb Mon Sep 17 00:00:00 2001 From: Andor Kesselman Date: Tue, 21 Feb 2023 13:14:34 +0530 Subject: [PATCH 3/3] fixed test. check nil --- pkg/service/keystore/service.go | 4 ++++ pkg/service/keystore/service_test.go | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/service/keystore/service.go b/pkg/service/keystore/service.go index 40a8d8aba..e1f18b308 100644 --- a/pkg/service/keystore/service.go +++ b/pkg/service/keystore/service.go @@ -48,6 +48,10 @@ func (s Service) Config() config.KeyStoreServiceConfig { // NewKeyStoreFromKeystoreStorage uses a keystore service directly from storage object func NewKeyStoreFromKeystoreStorage(config config.KeyStoreServiceConfig, keyStoreStorage *Storage) (*Service, error) { + if keyStoreStorage == nil { + return nil, errors.New("no storage provided") + } + service := Service{ storage: keyStoreStorage, config: config, diff --git a/pkg/service/keystore/service_test.go b/pkg/service/keystore/service_test.go index 5da626810..284348031 100644 --- a/pkg/service/keystore/service_test.go +++ b/pkg/service/keystore/service_test.go @@ -167,7 +167,7 @@ func TestStoreAndGetKeyWithExistingKeystorage(t *testing.T) { serviceKey, serviceKeySalt, err := GenerateServiceKey(config.ServiceKeyPassword) // Next, instantiate the key storage - keyStoreStorage, err := NewKeyStoreStorage(s, ServiceKey{ + keyStoreStorage, err := NewKeyStoreStorage(bolt, ServiceKey{ Base58Key: serviceKey, Base58Salt: serviceKeySalt, })