Skip to content

Commit

Permalink
feat: json web signature 2020 support WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
thackerronak committed Jun 20, 2023
1 parent f1f6a56 commit 62ee442
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.utils.CommonUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.Validate;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
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.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.proof.SignatureType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -423,8 +424,11 @@ public Map<String, Object> credentialsValidation(Map<String, Object> data) {
didDocumentResolverRegistry.register(
new DidWebDocumentResolver(HttpClient.newHttpClient(), new DidWebParser(), miwSettings.enforceHttps()));

LinkedDataProofValidation proofValidation = LinkedDataProofValidation.newInstance(didDocumentResolverRegistry);
Boolean valid = proofValidation.checkProof(verifiableCredential);
// 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);
response.put("vc", verifiableCredential);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
import org.eclipse.tractusx.managedidentitywallets.exception.BadDataException;
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.utils.Validate;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.did.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.did.resolver.OctetKeyPairFactory;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebDocumentResolver;
import org.eclipse.tractusx.ssi.lib.did.web.util.DidWebParser;
import org.eclipse.tractusx.ssi.lib.jwt.SignedJwtFactory;
Expand All @@ -47,9 +50,6 @@
import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentation;
import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentationBuilder;
import org.eclipse.tractusx.ssi.lib.model.verifiable.presentation.VerifiablePresentationType;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistry;
import org.eclipse.tractusx.ssi.lib.resolver.DidDocumentResolverRegistryImpl;
import org.eclipse.tractusx.ssi.lib.resolver.OctetKeyPairFactory;
import org.eclipse.tractusx.ssi.lib.serialization.jsonLd.JsonLdSerializerImpl;
import org.eclipse.tractusx.ssi.lib.serialization.jwt.SerializedJwtPresentationFactory;
import org.eclipse.tractusx.ssi.lib.serialization.jwt.SerializedJwtPresentationFactoryImpl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,12 @@
import org.eclipse.tractusx.managedidentitywallets.exception.ForbiddenException;
import org.eclipse.tractusx.managedidentitywallets.utils.EncryptionUtils;
import org.eclipse.tractusx.managedidentitywallets.utils.Validate;
import org.eclipse.tractusx.ssi.lib.base.MultibaseFactory;
import org.eclipse.tractusx.ssi.lib.crypt.ed25519.Ed25519Key;
import org.eclipse.tractusx.ssi.lib.crypt.ed25519.Ed25519KeySet;
import org.eclipse.tractusx.ssi.lib.crypt.jwk.JsonWebKey;
import org.eclipse.tractusx.ssi.lib.did.web.DidWebFactory;
import org.eclipse.tractusx.ssi.lib.model.MultibaseString;
import org.eclipse.tractusx.ssi.lib.model.base.MultibaseFactory;
import org.eclipse.tractusx.ssi.lib.model.did.*;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredential;
import org.eclipse.tractusx.ssi.lib.model.verifiable.credential.VerifiableCredentialType;
Expand All @@ -66,6 +68,7 @@
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 @@ -220,23 +223,32 @@ private Wallet createWallet(CreateWalletRequest request, boolean authority) {
Did did = DidWebFactory.fromHostname(miwSettings.host() + ":" + request.getBpn());

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

//Building Verification Methods:
List<VerificationMethod> verificationMethods = new ArrayList<>();
Ed25519VerificationKey2020Builder builder = new Ed25519VerificationKey2020Builder();
Ed25519VerificationKey2020 key =
builder
.id(did.toUri())
// 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);
// verificationMethods.add(key);

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

DidDocumentBuilder didDocumentBuilder = new DidDocumentBuilder();
didDocumentBuilder.id(did.toUri());
didDocumentBuilder.verificationMethods(verificationMethods);
didDocumentBuilder.verificationMethods(List.of(jwkVerificationMethod));//ed25519VerificationMethod
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,14 @@

import org.eclipse.tractusx.managedidentitywallets.constant.StringPool;
import org.eclipse.tractusx.managedidentitywallets.dao.entity.HoldersCredential;
import org.eclipse.tractusx.ssi.lib.model.Ed25519Signature2020;
import org.eclipse.tractusx.ssi.lib.model.did.DidDocument;
import org.eclipse.tractusx.ssi.lib.model.proof.jws.JWSSignature2020;
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.LinkedDataProofGenerator;
import org.eclipse.tractusx.ssi.lib.proof.hash.LinkedDataHasher;
import org.eclipse.tractusx.ssi.lib.proof.transform.LinkedDataTransformer;
import org.eclipse.tractusx.ssi.lib.proof.verify.LinkedDataSigner;
import org.eclipse.tractusx.ssi.lib.proof.SignatureType;

import java.net.URI;
import java.net.URLDecoder;
Expand Down Expand Up @@ -117,11 +115,19 @@ private static VerifiableCredential createVerifiableCredential(DidDocument issue


//Ed25519 Proof Builder
LinkedDataProofGenerator generator = new LinkedDataProofGenerator(
new LinkedDataHasher(), new LinkedDataTransformer(), new LinkedDataSigner());
// 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 = generator.createEd25519Signature2020(builder.build(), verificationMethod,
// privateKey);
// Ed25519Signature2020 proof =
// (Ed25519Signature2020) generator.createProof(
// builder.build(), verificationMethod, privateKey);
JWSSignature2020 proof =
(JWSSignature2020) generator.createProof(
builder.build(), verificationMethod, privateKey);


//Adding Proof to VC
builder.proof(proof);
Expand Down

0 comments on commit 62ee442

Please sign in to comment.