Update IronTests.java

This commit is contained in:
Markus Gylling 2022-08-31 16:57:12 +02:00
parent 7c7d283530
commit ef74f679fc

View File

@ -1,13 +1,17 @@
package org.oneedtech.inspect.vc;
import java.io.StringWriter;
import java.net.URI;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.HashMap;
import java.util.Map;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import com.apicatalog.did.key.DidKey;
import com.apicatalog.ld.signature.ed25519.Ed25519Proof2020Adapter;
import com.apicatalog.ld.signature.key.KeyPair;
import com.apicatalog.ld.signature.proof.ProofOptions;
@ -15,7 +19,11 @@ import com.apicatalog.ld.signature.proof.VerificationMethod;
import com.apicatalog.vc.Vc;
import com.apicatalog.vc.processor.Issuer;
import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonWriter;
import jakarta.json.JsonWriterFactory;
import jakarta.json.stream.JsonGenerator;
public class IronTests {
@ -23,21 +31,26 @@ public class IronTests {
@Test
void testOb_01() {
Assertions.assertDoesNotThrow(()->{
final DidKey didKey = DidKey.from(URI.create("did:key:z6MkpTHR8VNsBxYAAWHut2Geadd9jSwuBV8xRoAnwWsdvktH"));
//https://w3id.org/security#Ed25519KeyPair2020
//https://w3id.org/security#Ed25519Signature2020
URI unsigned = Samples.OB30.JSON.SIMPLE_JSON_NOPROOF.asURL().toURI();
KeyPair kp = Vc.generateKeys("https://w3id.org/security#Ed25519Signature2020").get(URI.create("urn:1"), 256);
ProofOptions options = ProofOptions.create(
Ed25519Proof2020Adapter.TYPE,
new VerificationMethod(URI.create("did:key:z6MkkUD3J14nkYzn46QeuaVSnp7dF85QJKwKvJvfsjx79aXj")),
//new VerificationMethod(URI.create("did:key:z6MkkUD3J14nkYzn46QeuaVSnp7dF85QJKwKvJvfsjx79aXj")),
new VerificationMethod(didKey.toUri()),
URI.create("https://w3id.org/security#assertionMethod")).created(Instant.now().truncatedTo(ChronoUnit.SECONDS));
Issuer issuer = Vc.sign(unsigned, kp, options);
System.err.println(pretty(issuer.getCompacted()));
JsonObject signed = issuer.getCompacted();
JsonObject proof = signed.getJsonObject("sec:proof");
Assertions.assertNotNull(proof);
System.err.println (issuer.getCompacted().toString());
Vc.verify(issuer.getCompacted()).isValid();
});
}
@ -56,9 +69,20 @@ public class IronTests {
Issuer issuer = Vc.sign(unsigned, kp, options);
JsonObject job = issuer.getCompacted().getJsonObject("sec:proof");
//System.err.println (issuer.getCompacted().toString());
//System.err.println (pretty(issuer.getCompacted().toString()));
Assertions.assertNotNull(job);
Vc.verify(issuer.getCompacted()).isValid();
});
}
String pretty(JsonObject jobj) {
Map<String, Object> properties = new HashMap<>(1);
properties.put(JsonGenerator.PRETTY_PRINTING, true);
StringWriter sw = new StringWriter();
JsonWriterFactory writerFactory = Json.createWriterFactory(properties);
JsonWriter jsonWriter = writerFactory.createWriter(sw);
jsonWriter.writeObject(jobj);
jsonWriter.close();
return sw.toString();
}
}