Skip to content

Commit

Permalink
feat: json web signature 2020 support added for did document, vc and vp
Browse files Browse the repository at this point in the history
  • Loading branch information
thackerronak committed Jun 21, 2023
1 parent 62ee442 commit a9838ce
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,8 @@ public Map<String, Object> credentialsValidation(Map<String, Object> data) {
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps()));

// LinkedDataProofValidation proofValidation = LinkedDataProofValidation.newInstance(didDocumentResolverRegistry);
LinkedDataProofValidation proofValidation = LinkedDataProofValidation.newInstance(SignatureType.JWS,
didDocumentResolverRegistry);
// Boolean valid = proofValidation.checkProof(verifiableCredential);
Boolean valid = proofValidation.verifiyProof(verifiableCredential);
Map<String, Object> response = new HashMap<>();
response.put(StringPool.VALID, valid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
import org.bouncycastle.crypto.params.Ed25519KeyGenerationParameters;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import org.bouncycastle.crypto.util.PrivateKeyInfoFactory;
import org.bouncycastle.crypto.util.SubjectPublicKeyInfoFactory;
import org.bouncycastle.openssl.jcajce.JcaPEMWriter;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
import org.eclipse.tractusx.managedidentitywallets.config.MIWSettings;
import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential;
Expand Down Expand Up @@ -68,7 +67,6 @@
import org.springframework.transaction.annotation.Transactional;

import java.io.StringWriter;
import java.net.URI;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -222,33 +220,20 @@ private Wallet createWallet(CreateWalletRequest request, boolean authority) {
//create did json
Did did = DidWebFactory.fromHostname(miwSettings.host() + ":" + request.getBpn());

//Extracting keys
// Ed25519KeySet keySet = new Ed25519KeySet(keyPair.getPrivateKey(), keyPair.getPublicKey());
//Extracting keys
Ed25519Key privateKey = Ed25519Key.asPrivateKey(keyPair.getPrivateKey());
Ed25519Key publicKey = Ed25519Key.asPrivateKey(keyPair.getPublicKey());
Ed25519Key publicKey = Ed25519Key.asPublicKey(keyPair.getPublicKey());
MultibaseString publicKeyBase = MultibaseFactory.create(publicKey.getEncoded());

//Building Verification Methods:
// List<VerificationMethod> verificationMethods = new ArrayList<>();
// Ed25519VerificationKey2020Builder builder = new Ed25519VerificationKey2020Builder();
Ed25519VerificationMethodBuilder ed25519VerificationKey2020Builder =
new Ed25519VerificationMethodBuilder();
Ed25519VerificationMethod ed25519VerificationMethod =
ed25519VerificationKey2020Builder
.id(URI.create(did + "#key-1"))
.controller(did.toUri())
.publicKeyMultiBase(publicKeyBase)
.build();
// verificationMethods.add(key);

// JWK
JsonWebKey jwk = JsonWebKey.fromED21559("", publicKey.getEncoded(), privateKey.getEncoded());//#key-2
JsonWebKey jwk = JsonWebKey.fromED21559("", publicKey.getEncoded(), privateKey.getEncoded());
JWKVerificationMethod jwkVerificationMethod =
new JWKVerificationMethodBuilder().did(did).jwk(jwk).build();

DidDocumentBuilder didDocumentBuilder = new DidDocumentBuilder();
didDocumentBuilder.id(did.toUri());
didDocumentBuilder.verificationMethods(List.of(jwkVerificationMethod));//ed25519VerificationMethod
didDocumentBuilder.verificationMethods(List.of(jwkVerificationMethod));
DidDocument didDocument = didDocumentBuilder.build();
didDocument = DidDocument.fromJson(URLDecoder.decode(didDocument.toJson(), StandardCharsets.UTF_8));
log.debug("did document created for bpn ->{}", request.getBpn());
Expand Down Expand Up @@ -322,21 +307,22 @@ private Ed25519KeySet createKeyPair() {
return new Ed25519KeySet(privateKeyBytes, publicKeyBytes);
}


@SneakyThrows
private String getPrivateKeyString(byte[] privateKeyBytes) {
StringWriter stringWriter = new StringWriter();
JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter);
pemWriter.writeObject(PrivateKeyInfoFactory.createPrivateKeyInfo(new Ed25519PrivateKeyParameters(privateKeyBytes, 0)));
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(new PemObject("PRIVATE KEY", privateKeyBytes));
pemWriter.flush();
pemWriter.close();
return stringWriter.toString();
}

@SneakyThrows
private String getPublicKeyString(byte[] publicKeyBytes) {
StringWriter stringWriter = new StringWriter();
JcaPEMWriter pemWriter = new JcaPEMWriter(stringWriter);
pemWriter.writeObject(SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(new Ed25519PublicKeyParameters(publicKeyBytes, 0)));
PemWriter pemWriter = new PemWriter(stringWriter);
pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKeyBytes));
pemWriter.flush();
pemWriter.close();
return stringWriter.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,9 @@ private static VerifiableCredential createVerifiableCredential(DidDocument issue
.credentialSubject(verifiableCredentialSubject);


//Ed25519 Proof Builder
// LinkedDataProofGenerator generator = new LinkedDataProofGenerator(
// new LinkedDataHasher(), new LinkedDataTransformer(), new LinkedDataSigner());
LinkedDataProofGenerator generator = LinkedDataProofGenerator.newInstance(SignatureType.JWS);
URI verificationMethod = issuerDoc.getVerificationMethods().get(0).getId();
// Ed25519Signature2020 proof = generator.createEd25519Signature2020(builder.build(), verificationMethod,
// privateKey);
// Ed25519Signature2020 proof =
// (Ed25519Signature2020) generator.createProof(
// builder.build(), verificationMethod, privateKey);

JWSSignature2020 proof =
(JWSSignature2020) generator.createProof(
builder.build(), verificationMethod, privateKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@
import org.eclipse.tractusx.managedidentitywallets.dto.IssueFrameworkCredentialRequest;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialBuilder;
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.LinkedDataProofValidation;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.proof.SignatureType;
import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -214,9 +215,9 @@ void validateCredentialsWithInvalidVC() throws com.fasterxml.jackson.core.JsonPr
//mock setup
LinkedDataProofValidation mock = Mockito.mock(LinkedDataProofValidation.class);
utils.when(() -> {
LinkedDataProofValidation.newInstance(Mockito.any(DidDocumentResolverRegistryImpl.class));
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidDocumentResolverRegistryImpl.class));
}).thenReturn(mock);
Mockito.when(mock.checkProof(Mockito.any(VerifiableCredential.class))).thenReturn(false);
Mockito.when(mock.verifiyProof(Mockito.any(VerifiableCredential.class))).thenReturn(false);

Map<String, Object> stringObjectMap = credentialController.credentialsValidation(map).getBody();
Assertions.assertFalse(Boolean.parseBoolean(stringObjectMap.get(StringPool.VALID).toString()));
Expand All @@ -237,9 +238,9 @@ void validateCredentials() throws com.fasterxml.jackson.core.JsonProcessingExcep
//mock setup
LinkedDataProofValidation mock = Mockito.mock(LinkedDataProofValidation.class);
utils.when(() -> {
LinkedDataProofValidation.newInstance(Mockito.any(DidDocumentResolverRegistryImpl.class));
LinkedDataProofValidation.newInstance(Mockito.any(SignatureType.class), Mockito.any(DidDocumentResolverRegistryImpl.class));
}).thenReturn(mock);
Mockito.when(mock.checkProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);
Mockito.when(mock.verifiyProof(Mockito.any(VerifiableCredential.class))).thenReturn(true);

Map<String, Object> stringObjectMap = credentialController.credentialsValidation(map).getBody();
Assertions.assertTrue(Boolean.parseBoolean(stringObjectMap.get(StringPool.VALID).toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@
import org.eclipse.tractusx.managedidentitywallets.service.PresentationService;
import org.eclipse.tractusx.managedidentitywallets.utils.AuthenticationUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.TestUtils;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.exception.DidDocumentResolverNotRegisteredException;
import org.eclipse.tractusx.ssi.lib.exception.JwtException;
import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtVerifier;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistry;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand Down

0 comments on commit a9838ce

Please sign in to comment.