Skip to content

Commit

Permalink
chore: bump waltid dependencies, fix java interface implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeplotean committed Aug 16, 2024
1 parent 9ca96d8 commit 89a57aa
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
val kotlin_version: String by project
val waltid_version: String = "0.5.0"
val waltid_version: String = "0.6.0"


plugins {
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/waltid/CustomKeyExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,11 @@ public byte[] javaVerifyRaw(@NotNull byte[] signed, @Nullable byte[] detachedPla
}

@NotNull
@Override
public JsonElement javaVerifyJws() {
return null;
}

@NotNull
@Override
public byte[] javaVerifyRaw() {
return new byte[0];
}
Expand All @@ -104,14 +102,13 @@ public JsonElement javaVerifyJws(@Language(value = "json") @NotNull String signe
}
}

@NotNull
@Override
public String javaSignJws(@NotNull byte[] plaintext, @NotNull Map<String, String> headers) {
public @NotNull String javaSignJws(@NotNull byte[] bytes, @NotNull Map<String, ? extends JsonElement> map) {
var base64 = Base64.getUrlEncoder();

String myHeaders = base64.encodeToString("{\"my-headers\": \"xyz\"}".getBytes(StandardCharsets.UTF_8));
String myPayload = base64.encodeToString("{\"my-payload\": \"xyz\"}".getBytes(StandardCharsets.UTF_8));
String mySignature = base64.encodeToString(javaSignRaw(plaintext));
String mySignature = base64.encodeToString(javaSignRaw(bytes));

return myHeaders + "." + myPayload + "." + mySignature;
}
Expand Down Expand Up @@ -171,6 +168,4 @@ public KeyType javaGetKeyType() {
default -> throw new IllegalArgumentException("Unknown key type!");
};
}


}
38 changes: 25 additions & 13 deletions src/main/java/waltid/KeysExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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);
Expand All @@ -45,23 +45,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 -> {
publicKey.verifyRawAsync(signed, plaintext).thenAccept(result -> {
System.out.println("Verification result (" + message + "): " + result);
}).join();
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 +88,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 Exception {
KeysExamples.signAsync();
KeysExamples.signBlocking();

Expand All @@ -85,7 +97,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();
}
}
4 changes: 1 addition & 3 deletions src/main/java/waltid/RunAll.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package waltid;

import java.util.concurrent.ExecutionException;

import static waltid.CustomKeyExample.runCustomKeyExample;
import static waltid.DidExamples.runDidExample;
import static waltid.KeysExamples.runKeyExample;
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

0 comments on commit 89a57aa

Please sign in to comment.