Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest version #4

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
val kotlin_version: String by project
val waltid_version: String = "0.5.0"
val waltid_version: String = "1.0.2407162254-SNAPSHOT"


plugins {
kotlin("jvm") version "2.0.0"
kotlin("jvm") version "2.0.20-Beta1"
}

group = "identity"
Expand All @@ -16,7 +16,7 @@ tasks.withType<Test>().configureEach {
repositories {
mavenLocal()
mavenCentral()
maven { url = uri("https://maven.waltid.dev/releases") }
maven { url = uri("https://maven.waltid.dev/snapshots") }

}

Expand Down
39 changes: 26 additions & 13 deletions src/main/java/waltid/KeysExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import id.walt.crypto.keys.Key;
import id.walt.crypto.keys.KeyType;
import id.walt.crypto.keys.jwk.JWKKey;
import kotlin.Result;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.concurrent.ExecutionException;
import kotlin.Result;

@SuppressWarnings({"RedundantThrows", "DuplicateThrows"})
public class KeysExamples {

private static final byte[] plaintext = "< this is my plaintext>".getBytes(StandardCharsets.UTF_8);
Expand All @@ -18,12 +18,13 @@ public class KeysExamples {
// demonstrating the Java (async) CompletableFuture API.
// Replace KeyType.Ed25519 with a different KeyType if desired.

public static void signBlocking() {
public static void signBlocking() throws Exception {
System.out.println("Generating key synchronous...");
JWKKey k = (JWKKey) JWKKey.Companion.generateBlocking(KeyType.Ed25519, null);
System.out.println("Sync generated key: " + k);
System.out.println("Signing with key synchronous...");
var signed = (byte[]) k.signRawBlocking(plaintext);
byte[] signed;
signed = (byte[]) k.signRawBlocking(plaintext);

System.out.println("Signed synchronous: " + Arrays.toString(signed));

Expand All @@ -45,23 +46,35 @@ public static void signAsync() {
System.out.println("Async generated key: " + key);
System.out.println("Signing with key asynchronous...");

key.signRawAsync(plaintext).thenAccept(signed -> {
System.out.println("Signed asynchronous: " + Arrays.toString((byte[]) signed));

verifyAsync(key, (byte[]) signed, plaintext, "Test async verification");
});
try {
key.signRawAsync(plaintext).thenAccept(signed -> {
System.out.println("Signed asynchronous: " + Arrays.toString((byte[]) signed));

try {
verifyAsync(key, (byte[]) signed, plaintext, "Test async verification");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}

public static void verifyAsync(Key key, byte[] signed, byte[] plaintext, String message) {
public static void verifyAsync(Key key, byte[] signed, byte[] plaintext, String message) throws Exception {
key.getPublicKeyAsync().thenAccept(publicKey -> {
try {
publicKey.verifyRawAsync(signed, plaintext).thenAccept(result -> {
System.out.println("Verification result (" + message + "): " + result);
}).join();
} catch (Exception e) {
throw new RuntimeException(e);
}
}).join();
}

public static String exportKey() {
public static String exportKey() throws Exception {
// Other KeyTypes:
var key2 = JWKKey.Companion.generateBlocking(KeyType.secp256r1, null);
System.out.println("Export key...");
Expand All @@ -76,7 +89,7 @@ public static void importKey(String jwk) throws ExecutionException, InterruptedE
System.out.println("Import result: " + keyImport);
}

public static void runKeyExample() throws ExecutionException, InterruptedException {
public static void runKeyExample() throws ExecutionException, InterruptedException, Exception {
KeysExamples.signAsync();
KeysExamples.signBlocking();

Expand All @@ -85,7 +98,7 @@ public static void runKeyExample() throws ExecutionException, InterruptedExcepti
}


public static void main(String[] args) throws ExecutionException, InterruptedException {
public static void main(String[] args) throws Exception {
runKeyExample();
}
}
2 changes: 1 addition & 1 deletion src/main/java/waltid/RunAll.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static waltid.VcExamples.runVcExample;

public class RunAll {
public static void main(String[] args) throws ExecutionException, InterruptedException {
public static void main(String[] args) throws Exception {
runKeyExample();
runDidExample();
runVcExample();
Expand Down