Skip to content

Commit

Permalink
fix: fix did creation in did document
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandra-bel committed Mar 21, 2024
1 parent ebee250 commit c21f7af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.constant.SupportedAlgorithms;
Expand All @@ -47,10 +48,11 @@
import org.eclipse.tractusx.managedidentitywallets.exception.SignatureFailureException;
import org.eclipse.tractusx.managedidentitywallets.exception.UnsupportedAlgorithmException;
import org.eclipse.tractusx.managedidentitywallets.utils.EncryptionUtils;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.did.Did;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocumentBuilder;
import org.eclipse.tractusx.ssi.lib.model.did.DidMethod;
import org.eclipse.tractusx.ssi.lib.model.did.DidMethodIdentifier;
import org.eclipse.tractusx.ssi.lib.model.did.JWKVerificationMethod;
import org.eclipse.tractusx.ssi.lib.model.did.VerificationMethod;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
Expand All @@ -75,6 +77,7 @@
import java.util.UUID;
import java.util.stream.Collectors;

import static org.eclipse.tractusx.managedidentitywallets.constant.StringPool.COLON_SEPARATOR;
import static org.eclipse.tractusx.managedidentitywallets.constant.StringPool.PRIVATE_KEY;
import static org.eclipse.tractusx.managedidentitywallets.constant.StringPool.PUBLIC_KEY;
import static org.eclipse.tractusx.managedidentitywallets.constant.StringPool.REFERENCE_KEY;
Expand All @@ -94,6 +97,7 @@
public class JwtPresentationES256KService {

public static final String JWK_Y = "y";

private JsonLdSerializer jsonLdSerializer;
private Did agentDid;
private WalletRepository walletRepository;
Expand Down Expand Up @@ -136,7 +140,8 @@ public Wallet storeWalletKeyES256K(Wallet wallet, String keyId) {
.provider(BouncyCastleProviderSingleton.getInstance())
.generate();

Did did = DidWebFactory.fromHostnameAndPath(miwSettings.host(), wallet.getBpn());
Did did = getDidFromDidString(wallet.getDid());

JWKVerificationMethod jwkVerificationMethod = getJwkVerificationMethod(ecKey, did);
DidDocument didDocument = wallet.getDidDocument();
List<VerificationMethod> verificationMethods = didDocument.getVerificationMethods();
Expand Down Expand Up @@ -164,6 +169,14 @@ public Wallet storeWalletKeyES256K(Wallet wallet, String keyId) {
return wallet;
}

private Did getDidFromDidString(String didString) {
int index = StringUtils.ordinalIndexOf(didString, COLON_SEPARATOR, 2);
String identifier = didString.substring(index + 1);
DidMethod didMethod = new DidMethod("web");
DidMethodIdentifier methodIdentifier = new DidMethodIdentifier(identifier);
return new Did(didMethod, methodIdentifier, null);
}

private JWKVerificationMethod getJwkVerificationMethod(ECKey ecKey, Did did) {
Map<String, Object> verificationMethodJson = new HashMap<>();
Map<String, String> publicKeyJwk = Map.of(JWK_KEK_TYPE, ecKey.getKeyType().toString(), JWK_CURVE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
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.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.constant.SupportedAlgorithms;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.Wallet;
Expand All @@ -42,8 +41,6 @@
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialSubject;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Expand All @@ -55,7 +52,11 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ContextConfiguration;

import java.net.URI;
Expand Down Expand Up @@ -213,22 +214,7 @@ void createWalletTest201() throws JsonProcessingException, JSONException {
ResponseEntity<String> getWalletResponse = restTemplate.exchange(RestURI.API_WALLETS_IDENTIFIER + "?withCredentials={withCredentials}", HttpMethod.GET, entity, String.class, bpn, "true");
Assertions.assertEquals(getWalletResponse.getStatusCode().value(), HttpStatus.OK.value());
Wallet body = TestUtils.getWalletFromString(getWalletResponse.getBody());
Assertions.assertEquals(2, body.getVerifiableCredentials().size());

VerifiableCredential verifiableCredential = body.getVerifiableCredentials().stream()
.filter(vp -> vp.getTypes().contains(MIWVerifiableCredentialType.BPN_CREDENTIAL))
.findFirst()
.orElse(null);
Assertions.assertEquals(verifiableCredential.getCredentialSubject().get(0).get(StringPool.ID), wallet.getDid());
Assertions.assertEquals(verifiableCredential.getCredentialSubject().get(0).get(StringPool.BPN), wallet.getBpn());
Assertions.assertEquals(MIWVerifiableCredentialType.BPN_CREDENTIAL, verifiableCredential.getCredentialSubject().get(0).get(StringPool.TYPE));

VerifiableCredential summaryVerifiableCredential = body.getVerifiableCredentials().stream()
.filter(vc -> vc.getTypes().contains(MIWVerifiableCredentialType.SUMMARY_CREDENTIAL)).findFirst()
.orElse(null);
VerifiableCredentialSubject subject = summaryVerifiableCredential.getCredentialSubject().get(0);
List<String> list = (List<String>) subject.get(StringPool.ITEMS);
Assertions.assertTrue(list.contains(MIWVerifiableCredentialType.BPN_CREDENTIAL));
Assertions.assertEquals(0, body.getVerifiableCredentials().size());
}


Expand Down

0 comments on commit c21f7af

Please sign in to comment.