Skip to content

Commit

Permalink
Added RunAll config for Java
Browse files Browse the repository at this point in the history
  • Loading branch information
philpotisk committed Jul 15, 2024
1 parent d356c84 commit 9ca96d8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 47 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<div align="center">
<h1>walt.id identity examples</h1>
<p>This repository contains a few examples to quickly get you started with the waltid-identity code API.
<p>This repository contains a few examples to quickly get you started with the waltid-identity SDK.
</p>

<a href="https://walt.id/community">
Expand Down Expand Up @@ -30,22 +30,26 @@ For a selection of examples, please take a look at the following table:

Follow the link to view the associated code example.

You can also view samples in the test directories of the [identity repository](https://github.com/walt-id/waltid-identity).
You can also view samples in the test directories of
the [identity repository](https://github.com/walt-id/waltid-identity).

## Running the examples

We're writing our projects with Gradle in mind; however, you can absolutely also use Apache Maven.
Use the repository `https://maven.waltid.dev/releases`. For a web interface to list available artifacts and their current versions,
Use the repository `https://maven.waltid.dev/releases`. For a web interface to list available artifacts and their
current versions,
visit https://maven.waltid.dev/#/releases/id/walt in your web browser.

If you use Gradle with e.g., IntelliJ IDEA, you can simply run the examples by right-clicking them.

## Join the community

* Connect and get the latest updates: <a href="https://discord.gg/AW8AgqJthZ">Discord</a> | <a href="https://walt.id/newsletter">
* Connect and get the latest updates: <a href="https://discord.gg/AW8AgqJthZ">
Discord</a> | <a href="https://walt.id/newsletter">
Newsletter</a> | <a href="https://www.youtube.com/channel/UCXfOzrv3PIvmur_CmwwmdLA">
YouTube</a> | <a href="https://mobile.twitter.com/walt_id" target="_blank">Twitter</a>
* Get help, request features and report bugs: <a href="https://github.com/walt-id/.github/discussions" target="_blank">GitHub
* Get help, request features and report bugs: <a href="https://github.com/walt-id/.github/discussions" target="_blank">
GitHub
Discussions</a>

## License
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
rootProject.name = "identity-test"
rootProject.name = "waltid-examples"
37 changes: 18 additions & 19 deletions src/main/java/waltid/CustomKeyExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public CustomKeyExample(String xyz) {
this._xyz = xyz;
}

public static void runCustomKeyExample() {
var key = new CustomKeyExample("abc");
System.out.println("A type: " + key.getKeyType());

var plaintext = "plaintext".getBytes(StandardCharsets.UTF_8);
System.out.println("Plaintext: " + new String(plaintext, StandardCharsets.UTF_8));

var signed = (byte[]) key.javaSignRaw(plaintext);
System.out.println("Stub signed: " + new String(signed, StandardCharsets.UTF_8));

var verify = key.javaVerifyRaw(signed, plaintext);
System.out.println("Stub verified: " + new String(verify, StandardCharsets.UTF_8));
}

public static void main(String[] args) {
runCustomKeyExample();
}

private byte[] reverseArray(byte[] arr) {
for (int i = 0; i < arr.length / 2; i++) {
Expand Down Expand Up @@ -94,7 +111,7 @@ public String javaSignJws(@NotNull byte[] plaintext, @NotNull Map<String, String

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(plaintext));

return myHeaders + "." + myPayload + "." + mySignature;
}
Expand Down Expand Up @@ -155,23 +172,5 @@ public KeyType javaGetKeyType() {
};
}

public static void main(String[] args) {
var key = new CustomKeyExample("abc");
System.out.println("A type: " + key.getKeyType());

var plaintext = "plaintext".getBytes(StandardCharsets.UTF_8);
System.out.println("Plaintext: " + new String(plaintext , StandardCharsets.UTF_8));

var signed = (byte[]) key.javaSignRaw(plaintext);
System.out.println("Stub signed: " + new String(signed , StandardCharsets.UTF_8));

var verify = key.javaVerifyRaw(signed, plaintext);
System.out.println("Stub verified: " + new String(verify , StandardCharsets.UTF_8));


}




}
11 changes: 8 additions & 3 deletions src/main/java/waltid/DidExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
import id.walt.did.dids.registrar.dids.DidJwkCreateOptions;
import id.walt.did.dids.registrar.dids.DidKeyCreateOptions;
import id.walt.did.helpers.WaltidServices;
import java.util.concurrent.ExecutionException;
import kotlin.Result;

import java.util.concurrent.ExecutionException;

public class DidExamples {

private static final DidService didService = DidService.INSTANCE;

public static String generateDidSync(Key key) {
DidCreateOptions options = new DidJwkCreateOptions();

DidResult result = didService.javaRegisterByKeyBlocking("jwk", key, options);
DidResult result = didService.javaRegisterByKeyBlocking("jwk", key, options);

System.out.println("(sync) DID: " + result.getDid());
System.out.println("(sync) DID document: " + result.getDidDocument());
Expand All @@ -42,7 +43,7 @@ private static void resolveDid(String did) throws ExecutionException, Interrupte
System.out.println("Resolved: " + x);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
public static void runDidExample() throws ExecutionException, InterruptedException {
WaltidServices.INSTANCE.minimalInitBlocking();
var key = JWKKey.Companion.generateBlocking(KeyType.Ed25519, null);

Expand All @@ -52,4 +53,8 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc
resolveDid(did);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
runDidExample();
}

}
11 changes: 8 additions & 3 deletions src/main/java/waltid/KeysExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
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.Collections;
import java.util.concurrent.ExecutionException;
import kotlin.Result;

public class KeysExamples {

Expand Down Expand Up @@ -76,11 +76,16 @@ public static void importKey(String jwk) throws ExecutionException, InterruptedE
System.out.println("Import result: " + keyImport);
}

public static void main(String[] args) throws ExecutionException, InterruptedException {
public static void runKeyExample() throws ExecutionException, InterruptedException {
KeysExamples.signAsync();
KeysExamples.signBlocking();

var jwk = exportKey();
importKey(jwk);
}


public static void main(String[] args) throws ExecutionException, InterruptedException {
runKeyExample();
}
}
17 changes: 17 additions & 0 deletions src/main/java/waltid/RunAll.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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 {
runKeyExample();
runDidExample();
runVcExample();
runCustomKeyExample();
}
}
8 changes: 5 additions & 3 deletions src/main/java/waltid/VcExamples.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

public class VcExamples {

Expand Down Expand Up @@ -76,12 +77,13 @@ private static void verify(String signed) {
}
}


public static void main(String[] args) {
public static void runVcExample() {
String signed = buildAndSignVC();
verify(signed);
}


public static void main(String[] args) throws ExecutionException, InterruptedException {
runVcExample();
}

}
30 changes: 17 additions & 13 deletions src/main/kotlin/RunAll.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@

import crypto.create.create_ed25519
import crypto.create.create_rsa
import crypto.create.create_secp256k1
import crypto.create.create_secp256r1
import crypto.export_key.export_jwk
import crypto.export_key.export_jwk_object
import crypto.import_key.*
import crypto.import_key.import_ed25519_jwk
import crypto.import_key.import_rsa_jwk
import crypto.import_key.import_secp256k1_jwk
import crypto.import_key.import_secp256r1_jwk
import vc.*


suspend fun main() {

println(" _ _ _ _ _ \n" +
" | | | (_) | | | | \n" +
" __ ____ _| | |_ _ __| | _____ ____ _ _ __ ___ _ __ | | ___ ___ \n" +
" \\ \\ /\\ / / _` | | __| | |/ _` | / _ \\ \\/ / _` | '_ ` _ \\| '_ \\| |/ _ \\/ __|\n" +
" \\ V V / (_| | | |_ _| | (_| | | __/> < (_| | | | | | | |_) | | __/\\__ \\\n" +
" \\_/\\_/ \\__,_|_|\\__(_)_|\\__,_| \\___/_/\\_\\__,_|_| |_| |_| .__/|_|\\___||___/\n" +
" | | \n" +
" |_| ")
println(
" _ _ _ _ _ \n" +
" | | | (_) | | | | \n" +
" __ ____ _| | |_ _ __| | _____ ____ _ _ __ ___ _ __ | | ___ ___ \n" +
" \\ \\ /\\ / / _` | | __| | |/ _` | / _ \\ \\/ / _` | '_ ` _ \\| '_ \\| |/ _ \\/ __|\n" +
" \\ V V / (_| | | |_ _| | (_| | | __/> < (_| | | | | | | |_) | | __/\\__ \\\n" +
" \\_/\\_/ \\__,_|_|\\__(_)_|\\__,_| \\___/_/\\_\\__,_|_| |_| |_| .__/|_|\\___||___/\n" +
" | | \n" +
" |_| "
)

// Crypto
println("create_ed25519() ----------------------------------------------------------------------------------------")
Expand All @@ -37,15 +41,15 @@ suspend fun main() {
println("create_ed25519() ----------------------------------------------------------------------------------------")
create_ed25519()

println("import_ed25519_jwk() ----------------------------------------------------------------------------------------")
println("import_ed25519_jwk() ------------------------------------------------------------------------------------")
import_ed25519_jwk()
println("import_rsa_jwk() ----------------------------------------------------------------------------------------")
import_rsa_jwk()
println("import_rsa_raw() ----------------------------------------------------------------------------------------")
// FIXME import_rsa_raw()
println("import_secp256k1_jwk() ----------------------------------------------------------------------------------------")
println("import_secp256k1_jwk() ----------------------------------------------------------------------------------")
import_secp256k1_jwk()
println("import_secp256r1_jwk() ----------------------------------------------------------------------------------------")
println("import_secp256r1_jwk() ----------------------------------------------------------------------------------")
import_secp256r1_jwk()

// DIDs
Expand Down

0 comments on commit 9ca96d8

Please sign in to comment.