Skip to content

Commit

Permalink
database now contains type instead of own table for ed25519 keys
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Pinsel <[email protected]>
  • Loading branch information
DominikPinsel committed Feb 9, 2024
1 parent e59e245 commit 2f33a65
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Ed25519KeyEntity, String>,
JpaRepository<Ed25519KeyEntity, String> {
public interface EncryptionKeyJpaRepository extends CrudRepository<EncryptionKeyEntity, String>,
JpaRepository<EncryptionKeyEntity, String> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -76,17 +77,17 @@ public void create(@NonNull final Wallet wallet) {
walletEntity.setId(walletId);
walletEntity.setName(walletName);

final List<Ed25519KeyEntity> ed25519KeyEntities = wallet.getStoredEd25519Keys().stream()
final List<EncryptionKeyEntity> 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());
keyEntity.setPublicKeyCypherTextBase64(k.getPublicKey().getBase64());
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)) {
Expand All @@ -99,7 +100,7 @@ public void create(@NonNull final Wallet wallet) {
}

walletJpaRepository.save(walletEntity);
ed25519KeyJpaRepository.saveAll(ed25519KeyEntities);
encryptionKeyJpaRepository.saveAll(ed25519KeyEntities);
}

@Transactional
Expand All @@ -117,15 +118,16 @@ public void update(@NonNull final Wallet wallet) {
final String newWalletName = wallet.getWalletName().getText();
walletEntity.setName(newWalletName);

final List<Ed25519KeyEntity> ed25519KeyEntities = new ArrayList<>();
final List<EncryptionKeyEntity> 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());
Expand All @@ -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);
}

Expand All @@ -154,7 +156,7 @@ public void deleteAll() {
log.trace("delete all");
}
verifiableCredentialWalletIntersectionJpaRepository.deleteAll();
ed25519KeyJpaRepository.deleteAll();
encryptionKeyJpaRepository.deleteAll();
walletJpaRepository.deleteAll();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.eclipse.tractusx.managedidentitywallets.repository.entity;

public class EncryptionKeyEntityType {
public static final String ED25519 = "ed25519";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +53,7 @@ public class WalletEntity extends AbstractEntity {

@OneToMany(fetch = FetchType.EAGER, mappedBy = "wallet", cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
private List<Ed25519KeyEntity> ed25519Keys;
private List<EncryptionKeyEntity> encryptionKeys;

@OneToMany(mappedBy = "id.wallet", cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<PersistedEd25519VerificationMethod> keys = entity.getEd25519Keys()
.stream().map(
final List<PersistedEd25519VerificationMethod> 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()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ 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',
did_fragment varchar(255) NOT NULL,
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),
Expand Down Expand Up @@ -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
);
);

0 comments on commit 2f33a65

Please sign in to comment.