Skip to content

Commit

Permalink
feat: bpnCredential added in wallet creation
Browse files Browse the repository at this point in the history
  • Loading branch information
thackerronak committed May 30, 2023
1 parent 47a6656 commit 8861877
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ public class MIWVerifiableCredentialType extends VerifiableCredentialType {
* The constant DISMANTLER_CREDENTIAL_CX.
*/
public static final String DISMANTLER_CREDENTIAL_CX = "DismantlerCredentialCX";
public static final String DISMANTLER_CREDENTIAL = "DismantlerCredential";

/**
* The constant USE_CASE_FRAMEWORK_CONDITION_CX.
*/
public static final String USE_CASE_FRAMEWORK_CONDITION_CX = "UseCaseFrameworkConditionCX";

public static final String BPN_CREDENTIAL = "BpnCredential";

public static final String BPN_CREDENTIAL_CX = "BpnCredentialCX";

public static final String MEMBERSHIP_CREDENTIAL_CX = "MembershipCredentialCX";
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

package org.eclipse.tractusx.managedidentitywallets.service;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.io.pem.PemReader;
Expand All @@ -44,6 +43,7 @@
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType;
import org.eclipse.tractusx.ssi.lib.proof.LinkedDataProofGenerator;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.io.StringReader;
Expand All @@ -57,17 +57,23 @@
* The type Credential service.
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class CredentialService {

private final CredentialRepository credentialRepository;
private final MIWSettings miwSettings;
private final WalletKeyRepository walletKeyRepository;
private final EncryptionUtils encryptionUtils;

private final WalletService walletService;

public CredentialService(CredentialRepository credentialRepository, MIWSettings miwSettings, WalletKeyRepository walletKeyRepository, EncryptionUtils encryptionUtils, @Lazy WalletService walletService) {
this.credentialRepository = credentialRepository;
this.miwSettings = miwSettings;
this.walletKeyRepository = walletKeyRepository;
this.encryptionUtils = encryptionUtils;
this.walletService = walletService;
}

/**
* Gets credentials.
*
Expand Down Expand Up @@ -127,7 +133,7 @@ public VerifiableCredential issueDismantlerCredential(IssueDismantlerCredentialR
Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());
byte[] privateKeyBytes = getPrivateKeyById(baseWallet.getId());

Map<String, Object> subject = Map.of("type", MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL_CX,
Map<String, Object> subject = Map.of("type", MIWVerifiableCredentialType.DISMANTLER_CREDENTIAL,
"id", holderWallet.getDid(),
"holderIdentifier", holderWallet.getBpn(),
"activityType", request.getActivityType(),
Expand All @@ -154,19 +160,19 @@ public VerifiableCredential issueMembershipCredential(IssueMembershipCredentialR
Wallet holderWallet = walletService.getWalletByIdentifier(issueMembershipCredentialRequest.getBpn());

//check duplicate
isCredentialExit(holderWallet.getId(), VerifiableCredentialType.MEMBERSHIP_CREDENTIAL);
isCredentialExit(holderWallet.getId(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX);

// Fetch Issuer Wallet
Wallet baseWallet = walletService.getWalletByIdentifier(miwSettings.authorityWalletBpn());
byte[] privateKeyBytes = getPrivateKeyById(baseWallet.getId());

//VC Subject
Credential credential = getCredential(Map.of("type", VerifiableCredentialType.MEMBERSHIP_CREDENTIAL,
Credential credential = getCredential(Map.of("type", MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL,
"id", holderWallet.getDid(),
"holderIdentifier", holderWallet.getBpn(),
"memberOf", baseWallet.getName(),
"status", "Active",
"startTime", Instant.now().toString()), VerifiableCredentialType.MEMBERSHIP_CREDENTIAL, baseWallet, privateKeyBytes, holderWallet);
"startTime", Instant.now().toString()), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX, baseWallet, privateKeyBytes, holderWallet);

//Store Credential
credentialRepository.save(credential);
Expand Down Expand Up @@ -200,7 +206,7 @@ private VerifiableCredential createVerifiableCredential(String issuerDid, List<S
}


private Credential getCredential(Map<String, Object> subject, String type, Wallet baseWallet, byte[] privateKeyBytes, Wallet holderWallet) {
public Credential getCredential(Map<String, Object> subject, String type, Wallet baseWallet, byte[] privateKeyBytes, Wallet holderWallet) {
//VC Subject
VerifiableCredentialSubject verifiableCredentialSubject =
new VerifiableCredentialSubject(subject);
Expand All @@ -222,7 +228,7 @@ private Credential getCredential(Map<String, Object> subject, String type, Walle


@SneakyThrows
private byte[] getPrivateKeyById(Long id) {
public byte[] getPrivateKeyById(Long id) {
WalletKey baseWalletKey = walletKeyRepository.getByWalletId(id);
String privateKey = encryptionUtils.decrypt(baseWalletKey.getPrivateKey());
return new PemReader(new StringReader(privateKey)).readPemObject().getContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.constant.ApplicationConstant;
import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Credential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.WalletKey;
Expand Down Expand Up @@ -84,6 +85,8 @@ public class WalletService {

private final CredentialRepository credentialRepository;

private final CredentialService credentialService;


/**
* Store credential map.
Expand Down Expand Up @@ -215,6 +218,18 @@ public Wallet createWallet(CreateWalletRequest request) {
.publicKey(encryptionUtils.encrypt(getPublicKeyString(keyPair.getPublicKey())))
.build());
log.debug("Wallet created for bpn ->{}", request.getBpn());

// Fetch Issuer Wallet
Wallet baseWallet = getWalletByIdentifier(miwSettings.authorityWalletBpn());
byte[] privateKeyBytes = credentialService.getPrivateKeyById(baseWallet.getId());

Credential credential = credentialService.getCredential(Map.of("type", MIWVerifiableCredentialType.BPN_CREDENTIAL,
"id", wallet.getDid(),
"bpn", wallet.getBpn()), MIWVerifiableCredentialType.BPN_CREDENTIAL_CX, baseWallet, privateKeyBytes, wallet);

//Store Credential
credentialRepository.save(credential);

return wallet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.config.TestContextInitializer;
import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Credential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
Expand All @@ -36,7 +37,6 @@
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType;
import org.json.JSONException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -104,10 +104,10 @@ void issueMembershipCredentialTest201() throws JsonProcessingException, JSONExce

TestUtils.checkVC(verifiableCredential, miwSettings);

Assertions.assertTrue(verifiableCredential.getTypes().contains(VerifiableCredentialType.MEMBERSHIP_CREDENTIAL));
Assertions.assertTrue(verifiableCredential.getTypes().contains(MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX));
Assertions.assertEquals(verifiableCredential.getCredentialSubject().get(0).get("holderIdentifier"), bpn);

Credential credential = credentialRepository.getByHolderAndType(wallet.getId(), VerifiableCredentialType.MEMBERSHIP_CREDENTIAL);
Credential credential = credentialRepository.getByHolderAndType(wallet.getId(), MIWVerifiableCredentialType.MEMBERSHIP_CREDENTIAL_CX);
Assertions.assertNotNull(credential);
TestUtils.checkVC(credential.getData(), miwSettings);

Expand All @@ -126,12 +126,8 @@ void issueMembershipCredentialWithDuplicateBpn409() {
ResponseEntity<String> response = issueMembershipVC(bpn, did);
Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());

ResponseEntity<String> duplicateResponse = issueMembershipVC(bpn, did);

HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();
IssueMembershipCredentialRequest request = IssueMembershipCredentialRequest.builder().bpn(bpn).build();
HttpEntity<IssueMembershipCredentialRequest> entity = new HttpEntity<>(request, headers);

ResponseEntity<String> duplicateResponse = restTemplate.exchange(RestURI.CREDENTIALS_ISSUER_MEMBERSHIP, HttpMethod.POST, entity, String.class);
Assertions.assertEquals(HttpStatus.CONFLICT.value(), duplicateResponse.getStatusCode().value());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.eclipse.tractusx.managedidentitywallets.ManagedIdentityWalletsApplication;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.config.TestContextInitializer;
import org.eclipse.tractusx.managedidentitywallets.constant.MIWVerifiableCredentialType;
import org.eclipse.tractusx.managedidentitywallets.constant.RestURI;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Credential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
Expand Down Expand Up @@ -131,6 +132,21 @@ void createWalletTest201() throws JsonProcessingException, JSONException {

Assertions.assertEquals(walletFromDB.getBpn(), bpn);

//check if BPN credentials is issued
HttpHeaders headers = AuthenticationUtils.getValidUserHttpHeaders();

HttpEntity<CreateWalletRequest> entity = new HttpEntity<>(headers);

ResponseEntity<String> getWalletResponse = restTemplate.exchange(RestURI.API_WALLETS_IDENTIFIER + "?withCredentials={withCredentials}", HttpMethod.GET, entity, String.class, bpn, "true");

Wallet body = getWalletFromString(getWalletResponse.getBody());
Assertions.assertEquals(body.getVerifiableCredentials().size(), 1);
VerifiableCredential verifiableCredential = body.getVerifiableCredentials().get(0);

verifiableCredential.getCredentialSubject().get(0).get("id").equals(wallet.getDid());
verifiableCredential.getCredentialSubject().get(0).get("bpn").equals(wallet.getBpn());
verifiableCredential.getCredentialSubject().get(0).get("type").equals(MIWVerifiableCredentialType.BPN_CREDENTIAL);

}


Expand All @@ -146,7 +162,7 @@ void storeCredentialsTest201() throws JsonProcessingException {
Assertions.assertEquals(HttpStatus.CREATED.value(), response.getStatusCode().value());
Wallet byBpn = walletRepository.getByBpn(miwSettings.authorityWalletBpn());
List<Credential> byHolder = credentialRepository.getByHolder(byBpn.getId());
Assertions.assertEquals(1, byHolder.size());
Assertions.assertEquals(2, byHolder.size());

}

Expand Down Expand Up @@ -235,7 +251,7 @@ void getWalletByIdentifierBPNWithCredentialsTest200() throws JsonProcessingExcep
Wallet body = getWalletFromString(getWalletResponse.getBody());
Assertions.assertEquals(HttpStatus.OK.value(), getWalletResponse.getStatusCode().value());
Assertions.assertNotNull(getWalletResponse.getBody());
Assertions.assertEquals(1, body.getVerifiableCredentials().size());
Assertions.assertEquals(2, body.getVerifiableCredentials().size());
Assertions.assertEquals(body.getBpn(), bpn);
}

Expand Down

0 comments on commit 8861877

Please sign in to comment.