diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/Ed25519KeyJpaRepository.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/EncryptionKeyJpaRepository.java similarity index 87% rename from managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/Ed25519KeyJpaRepository.java rename to managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/EncryptionKeyJpaRepository.java index db65a2318..2359ef963 100644 --- a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/Ed25519KeyJpaRepository.java +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/EncryptionKeyJpaRepository.java @@ -21,12 +21,12 @@ package org.eclipse.tractusx.managedidentitywallets.repository.database; -import org.eclipse.tractusx.managedidentitywallets.repository.entity.Ed25519KeyEntity; +import org.eclipse.tractusx.managedidentitywallets.repository.entity.EncryptionKeyEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; @Repository -public interface Ed25519KeyJpaRepository extends CrudRepository, - JpaRepository { +public interface EncryptionKeyJpaRepository extends CrudRepository, + JpaRepository { } diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/WalletRepository.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/WalletRepository.java index 73d514415..dd8794cda 100644 --- a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/WalletRepository.java +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/database/WalletRepository.java @@ -35,7 +35,8 @@ import org.eclipse.tractusx.managedidentitywallets.models.WalletId; import org.eclipse.tractusx.managedidentitywallets.repository.database.predicate.WalletWithVerifiableCredentialPredicate; import org.eclipse.tractusx.managedidentitywallets.repository.database.query.WalletWithVerifiableCredentialQuery; -import org.eclipse.tractusx.managedidentitywallets.repository.entity.Ed25519KeyEntity; +import org.eclipse.tractusx.managedidentitywallets.repository.entity.EncryptionKeyEntity; +import org.eclipse.tractusx.managedidentitywallets.repository.entity.EncryptionKeyEntityType; import org.eclipse.tractusx.managedidentitywallets.repository.entity.VerifiableCredentialEntity; import org.eclipse.tractusx.managedidentitywallets.repository.entity.VerifiableCredentialWalletIntersectionEntity; import org.eclipse.tractusx.managedidentitywallets.repository.entity.WalletEntity; @@ -62,7 +63,7 @@ public class WalletRepository { private final WalletJpaRepository walletJpaRepository; private final VerifiableCredentialJpaRepository verifiableCredentialJpaRepository; private final VerifiableCredentialWalletIntersectionJpaRepository verifiableCredentialWalletIntersectionJpaRepository; - private final Ed25519KeyJpaRepository ed25519KeyJpaRepository; + private final EncryptionKeyJpaRepository encryptionKeyJpaRepository; private final WalletMap walletMap; @Transactional @@ -76,9 +77,9 @@ public void create(@NonNull final Wallet wallet) { walletEntity.setId(walletId); walletEntity.setName(walletName); - final List ed25519KeyEntities = wallet.getStoredEd25519Keys().stream() + final List ed25519KeyEntities = wallet.getStoredEd25519Keys().stream() .map(k -> { - final Ed25519KeyEntity keyEntity = new Ed25519KeyEntity(); + final EncryptionKeyEntity keyEntity = new EncryptionKeyEntity(); keyEntity.setId(UUID.randomUUID().toString()); keyEntity.setWallet(walletEntity); keyEntity.setDidFragment(k.getDidFragment().getText()); @@ -86,7 +87,7 @@ public void create(@NonNull final Wallet wallet) { keyEntity.setPrivateKeyCypherTextBase64(k.getPrivateKey().getBase64()); return keyEntity; }).collect(Collectors.toList()); - walletEntity.setEd25519Keys(ed25519KeyEntities); + walletEntity.setEncryptionKeys(ed25519KeyEntities); /* Assert Wallet Does Not Exist */ if (walletJpaRepository.existsById(walletId)) { @@ -99,7 +100,7 @@ public void create(@NonNull final Wallet wallet) { } walletJpaRepository.save(walletEntity); - ed25519KeyJpaRepository.saveAll(ed25519KeyEntities); + encryptionKeyJpaRepository.saveAll(ed25519KeyEntities); } @Transactional @@ -117,15 +118,16 @@ public void update(@NonNull final Wallet wallet) { final String newWalletName = wallet.getWalletName().getText(); walletEntity.setName(newWalletName); - final List ed25519KeyEntities = new ArrayList<>(); + final List ed25519KeyEntities = new ArrayList<>(); for (final PersistedEd25519VerificationMethod storedEd25519Key : wallet.getStoredEd25519Keys()) { // keep keys that are already in db or generate new ones // it should not be possible to update the key itself - walletEntity.getEd25519Keys().stream().filter( + walletEntity.getEncryptionKeys().stream().filter( k -> k.getId().equals(storedEd25519Key.getId().getText()) ).findFirst() .ifPresentOrElse(ed25519KeyEntities::add, () -> { - final Ed25519KeyEntity keyEntity = new Ed25519KeyEntity(); + final EncryptionKeyEntity keyEntity = new EncryptionKeyEntity(); + keyEntity.setKeyType(EncryptionKeyEntityType.ED25519); keyEntity.setId(storedEd25519Key.getId().getText()); keyEntity.setWallet(walletEntity); keyEntity.setDidFragment(storedEd25519Key.getDidFragment().getText()); @@ -136,15 +138,15 @@ public void update(@NonNull final Wallet wallet) { }); } - walletEntity.getEd25519Keys().clear(); - walletEntity.getEd25519Keys().addAll(ed25519KeyEntities); + walletEntity.getEncryptionKeys().clear(); + walletEntity.getEncryptionKeys().addAll(ed25519KeyEntities); /* Write to DB */ if (log.isTraceEnabled()) { log.trace("update: wallet={}", wallet); } - ed25519KeyJpaRepository.saveAll(ed25519KeyEntities); + encryptionKeyJpaRepository.saveAll(ed25519KeyEntities); walletJpaRepository.save(walletEntity); } @@ -154,7 +156,7 @@ public void deleteAll() { log.trace("delete all"); } verifiableCredentialWalletIntersectionJpaRepository.deleteAll(); - ed25519KeyJpaRepository.deleteAll(); + encryptionKeyJpaRepository.deleteAll(); walletJpaRepository.deleteAll(); } diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/Ed25519KeyEntity.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/EncryptionKeyEntity.java similarity index 85% rename from managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/Ed25519KeyEntity.java rename to managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/EncryptionKeyEntity.java index 334bb2cea..8a47a3596 100644 --- a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/Ed25519KeyEntity.java +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/EncryptionKeyEntity.java @@ -30,14 +30,15 @@ @Data @NoArgsConstructor @EqualsAndHashCode(of = "id", callSuper = false) -@Entity(name = Ed25519KeyEntity.TABLE_NAME) -@Table(name = Ed25519KeyEntity.TABLE_NAME) +@Entity(name = EncryptionKeyEntity.TABLE_NAME) +@Table(name = EncryptionKeyEntity.TABLE_NAME) @ToString -public class Ed25519KeyEntity extends AbstractEntity { +public class EncryptionKeyEntity extends AbstractEntity { - public static final String TABLE_NAME = "key_ed25519"; + public static final String TABLE_NAME = "encryption_key"; public static final String COLUMN_ID = "id"; public static final String COLUMN_WALLET_ID = "wallet_id"; + public static final String COLUMN_KEY_TYPE = "key_type"; public static final String COLUMN_DID_FRAGMENT = "did_fragment"; public static final String COLUMN_PUBLIC_KEY_CYPHER_TEXT_BASE_64 = "public_key_cypher_text_base64"; public static final String COLUMN_PRIVATE_KEY_CYPHER_TEXT_BASE_64 = "private_key_cypher_text_base64"; @@ -47,6 +48,9 @@ public class Ed25519KeyEntity extends AbstractEntity { @Column(name = COLUMN_ID, nullable = false, updatable = false) private String id; + @Column(name = COLUMN_KEY_TYPE, nullable = false, updatable = false) + private String keyType; + @Column(name = COLUMN_DID_FRAGMENT, nullable = false, updatable = false) private String didFragment; diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/EncryptionKeyEntityType.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/EncryptionKeyEntityType.java new file mode 100644 index 000000000..f6348a0fc --- /dev/null +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/EncryptionKeyEntityType.java @@ -0,0 +1,5 @@ +package org.eclipse.tractusx.managedidentitywallets.repository.entity; + +public class EncryptionKeyEntityType { + public static final String ED25519 = "ed25519"; +} diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/WalletEntity.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/WalletEntity.java index 76a183e35..582739630 100644 --- a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/WalletEntity.java +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/entity/WalletEntity.java @@ -26,7 +26,6 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.ToString; -import org.apache.commons.lang3.builder.ToStringExclude; import org.checkerframework.common.aliasing.qual.Unique; import java.util.List; @@ -54,7 +53,7 @@ public class WalletEntity extends AbstractEntity { @OneToMany(fetch = FetchType.EAGER, mappedBy = "wallet", cascade = CascadeType.ALL, orphanRemoval = true) @ToString.Exclude - private List ed25519Keys; + private List encryptionKeys; @OneToMany(mappedBy = "id.wallet", cascade = CascadeType.ALL, orphanRemoval = true) @ToString.Exclude diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/map/WalletMap.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/map/WalletMap.java index fdb4743f9..60257f971 100644 --- a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/map/WalletMap.java +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/map/WalletMap.java @@ -25,6 +25,7 @@ import lombok.NonNull; import org.eclipse.tractusx.managedidentitywallets.exception.MappingException; import org.eclipse.tractusx.managedidentitywallets.models.*; +import org.eclipse.tractusx.managedidentitywallets.repository.entity.EncryptionKeyEntityType; import org.eclipse.tractusx.managedidentitywallets.repository.entity.WalletEntity; import org.springframework.stereotype.Component; @@ -40,8 +41,10 @@ public Wallet map(@NonNull WalletEntity entity) throws MappingException { final WalletId walletId = new WalletId(entity.getId()); final WalletName walletName = new WalletName(entity.getName()); - final List keys = entity.getEd25519Keys() - .stream().map( + final List keys = entity.getEncryptionKeys() + .stream() + .filter(key -> EncryptionKeyEntityType.ED25519.equals(key.getKeyType())) + .map( key -> PersistedEd25519VerificationMethod.builder() .id(new Ed25519KeyId(key.getId())) .didFragment(new DidFragment(key.getDidFragment())) diff --git a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/vault/VaultRepositoryImpl.java b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/vault/VaultRepositoryImpl.java index f1426368f..9074fac1b 100644 --- a/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/vault/VaultRepositoryImpl.java +++ b/managed-identity-wallets-app/src/main/java/org/eclipse/tractusx/managedidentitywallets/repository/vault/VaultRepositoryImpl.java @@ -78,7 +78,6 @@ public PersistedEd25519VerificationMethod storeKey(@NonNull final WalletId walle .build(); } - private PublicKeyPlainText decrypt(@NonNull VaultIdentifier vaultIdentifier, @NonNull PublicKeyCypherText PrivateKeyCypherText) { final String value = prepareEncryptKey(vaultIdentifier) .decrypt(vaultIdentifier.getIdentifier(), PrivateKeyCypherText.getBase64()); diff --git a/managed-identity-wallets-app/src/main/resources/db/changelog/changes/init.sql b/managed-identity-wallets-app/src/main/resources/db/changelog/changes/init.sql index 69c457dcf..4031747f9 100644 --- a/managed-identity-wallets-app/src/main/resources/db/changelog/changes/init.sql +++ b/managed-identity-wallets-app/src/main/resources/db/changelog/changes/init.sql @@ -33,7 +33,7 @@ CREATE TABLE IF NOT EXISTS wallet CREATE INDEX wallet_name ON wallet (name); /* Key */ -CREATE TABLE IF NOT EXISTS key_ed25519 +CREATE TABLE IF NOT EXISTS encryption_key ( id varchar(255) NOT NULL, version varchar(8) DEFAULT 'v1', @@ -41,6 +41,7 @@ CREATE TABLE IF NOT EXISTS key_ed25519 wallet_id varchar(255) NOT NULL, public_key_cypher_text_base64 text NOT NULL, private_key_cypher_text_base64 text NOT NULL, + key_type varchar(255) NOT NULL, created_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP, modified_at timestamp with time zone DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), @@ -116,4 +117,4 @@ CREATE TABLE IF NOT EXISTS verifiable_credential_issuer_intersection PRIMARY KEY (verifiable_credential_id, verifiable_credential_issuer_id), FOREIGN KEY (verifiable_credential_id) REFERENCES verifiable_credential (id) ON DELETE CASCADE, FOREIGN KEY (verifiable_credential_issuer_id) REFERENCES verifiable_credential_issuer (issuer) ON DELETE CASCADE -); \ No newline at end of file +);