Skip to content

METADIUM/verifiable-credential-java

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 

Repository files navigation

verifiable-credential-java

Get it

Maven

Add the JitPack repository to build file

<repositories>
    <repository>
        <id>jitpack.io</id>
        <url>https://jitpack.io</url>
    </repository>
</repositories>

Add dependency

<dependency>
    <groupId>com.github.METADIUM</groupId>
    <artifactId>verifiable-credential-java</artifactId>
    <version>0.2.2</version>
</dependency>

Gradle

Add root build.gradle

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

Add dependency

dependencies {
    implementation 'com.github.METADIUM:verifiable-credential-java:0.2.2'
}

If use Google service plug-in in android, add code

googleServices {
    disableVersionCheck = true
}

Use it

Make Verifiable Credential

VerifiableCredential vc = new VerifiableCredential();
vc.setId(URI.create("http://aa.metadium.com/credential/343"));  // Set id of verifiable credential
vc.addTypes(Collections.singletonList("NameCredential"));       // Add Credential type name
vc.setIssuer(URI.create("did:meta:0x3489384932859420"));        // Set did of issuer
vc.setIssuanceDate(issuedDate);                                 // Set issued date
vc.setExpirationDate(expireDate);                               // Set expire date

// Add credential subject
LinkedHashMap<String, String> subject = new LinkedHashMap<>();
subject.put("id", "did:meta:0x11111111120");
subject.put("name", "mansud");
vc.setCredentialSubject(subject);

Sign VerifiableCredential

SignedJWT signedVc = vc.sign(
    "did:meta:0x348938499420#managementKey#4358",   // key id of signer
    "0d8mf03",                                      // nonce
    new ECDSASigner(privateKey)
);
String signedVcString = signedVc.serialize();

Verify VerifiableCredential

SignedJWT signedVc = SignedJWT.parse(signedVcString);

// verifying
if (signedVc.verify(new ECDSAVerifier(publicKey))) {
	VerifiableCredential verifiedVc = new VerifiableCredential(signedVc);
	if (verifiedVc == null) {
		// invalid vc
		return;
	}
   // Get subject
   Map<String, Object> credentialSubject = (Map<String, Object>)verifiedVc.getCredentialSubject();
}
else {
	// not verified
}

Make Verifiable Presentation

VerifiablePresentation vp = new VerifiablePresentation();
vp.setId(URI.create("http://aa.metadium.com/presentation/343"));    // Set id of verifiable presentatil
vp.setHolder(URI.create("did:meta:0x3489384932859420"));            // Set did of holder
vp.addTypes(Collections.singletonList("TestPresentation"));         // Add presentation type name
vp.addVerifiableCredential(verifiableCredential_1);                 // Add signed verifiable credential
vp.addVerifiableCredential(verifiableCredential_2);

Sign Verifiable Presentation

SignedJWT signedVp = vp.sign(
    "did:meta:0x348938499420#managementKey#4358",   // key id of holder
    "0d8mf03",                                      // nonce
    new ECDSASigner(privateKey)
);
String signedVpString = jwsObject.serialize();

Verify Verifiable Presentation

SignedJWT signedVp = Signed.parse(signedVpString);

// Verify verifiable presentation
if (signedVp.verify(new ECDSAVerifier(publicKey))) {
	VerifiablePresentation verifiedVp = new VerifiablePresentation(signedVp);
	if (verifiedVp == null) {
		// invalid vp
		return;
	}
   // Get verifiable credential
   for (Object vc : verifiedVp.getVerifiableCredentials()) {
       VerifiableCredential = (String)vc;
   }
}