diff --git a/pkg/service/keystore/service.go b/pkg/service/keystore/service.go index 0e42795d2..e1f18b308 100644 --- a/pkg/service/keystore/service.go +++ b/pkg/service/keystore/service.go @@ -46,6 +46,22 @@ 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) { + if keyStoreStorage == nil { + return nil, errors.New("no storage provided") + } + + 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) diff --git a/pkg/service/keystore/service_test.go b/pkg/service/keystore/service_test.go index 170ee6c02..284348031 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(bolt, 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) +}