waltid-identity icon indicating copy to clipboard operation
waltid-identity copied to clipboard

Java implementation issue with Keys abstract class.

Open saujam opened this issue 10 months ago • 3 comments

Hi Team, I am experiencing this issue when extending the Keys class (id.walt.crypto.keys) from waltid-crypto-jvm-0.2.0.jar. The issue occurs only with the following methods since the java de-compiler adds a postfix (verifyRaw-0E7RQCE) with a dash character that Java does not support. I would appreciate it if you could check and let me know.

  1. public abstract Object verifyRaw_0E7RQCE/* $FF was: verifyRaw-0E7RQCE*/(@NotNull byte[] var1, @Nullable byte[] var2, @NotNull Continuation var3);
  2. public abstract Object verifyJws_gIAlu_s/* $FF was: verifyJws-gIAlu-s*/(@NotNull String var1, @NotNull Continuation var2);

saujam avatar Apr 15 '24 18:04 saujam

Hi @saujam as you see in the picture, I could reproduce the issue. Due to the "inline" Result class the suffix is produced, which can not be inherited from Java. Screenshot_20240417_215723

To solve this issue, I could think of two options: 1.) Change the result type in the Key interface or 2.) Wrap the Result within a subclass for the JVM, as you see in the next screenshot, which at least works in my PoC:

image

I will discuss these options with the team an come back to you.

philpotisk avatar Apr 17 '24 21:04 philpotisk

Hi @saujam,

if you want to implement a Key in Java instead of Kotlin, you will have to extend from JavaKey instead of Key. There you will not have to handle suspend/coroutines or the kotlin.Result return type.

This is a stub you can utilize:

import id.walt.crypto.keys.JavaKey;
import id.walt.crypto.keys.Key;
import id.walt.crypto.keys.KeyMeta;
import id.walt.crypto.keys.KeyType;
import java.util.Map;
import kotlinx.serialization.json.JsonElement;
import kotlinx.serialization.json.JsonObject;
import org.jetbrains.annotations.NotNull;

public class MyKey extends JavaKey {

    private String _xyz;

    public MyKey(String xyz) {
        this._xyz = xyz;
    }

    @NotNull
    @Override
    public KeyMeta javaGetMeta() {
        return null;
    }

    @NotNull
    @Override
    public byte[] javaGetPublicKeyRepresentation() {
        return new byte[0];
    }

    @NotNull
    @Override
    public Key javaGetPublicKey() {
        return null;
    }

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

    @NotNull
    @Override
    public byte[] javaVerifyRaw() {
        return new byte[0];
    }

    @NotNull
    @Override
    public String javaSignJws(@NotNull byte[] plaintext, @NotNull Map<String, String> headers) {
        return "";
    }

    @NotNull
    @Override
    public Object javaSignRaw(@NotNull byte[] plaintext) {
        return null;
    }

    @NotNull
    @Override
    public String javaExportPEM() {
        return "";
    }

    @NotNull
    @Override
    public JsonObject javaExportJWKObject() {
        return null;
    }

    @NotNull
    @Override
    public String javaExportJWK() {
        return "";
    }

    @NotNull
    @Override
    public String javaGetThumbprint() {
        return "";
    }

    @NotNull
    @Override
    public String javaGetKeyId() {
        return "";
    }

    @Override
    public boolean javaHasPrivateKey() {
        return false;
    }

    @NotNull
    @Override
    public KeyType javaGetKeyType() {
        return null;
    }
}

kbrgmn avatar Apr 25 '24 15:04 kbrgmn

What is the release version for java key implementation.

saujam avatar May 06 '24 17:05 saujam