From 1d66cca332600171f41e17c6097b9410df14fa82 Mon Sep 17 00:00:00 2001 From: waltkb <68587968+waltkb@users.noreply.github.com> Date: Mon, 12 Aug 2024 23:19:42 +0200 Subject: [PATCH] feat: make it easier to register key type in KeyManager from Java --- .../walt/crypto/keys/KeyGenerationRequest.kt | 4 ++- .../kotlin/id/walt/crypto/keys/KeyManager.kt | 11 ++++++-- .../crypto/keys/KeyManagerJavaExtensions.kt | 28 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 waltid-libraries/crypto/waltid-crypto/src/jvmMain/kotlin/id/walt/crypto/keys/KeyManagerJavaExtensions.kt diff --git a/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyGenerationRequest.kt b/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyGenerationRequest.kt index 0cc0cc597..f3597f00e 100644 --- a/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyGenerationRequest.kt +++ b/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyGenerationRequest.kt @@ -9,4 +9,6 @@ data class KeyGenerationRequest( val keyType: KeyType = KeyType.Ed25519, var config: JsonObject? = null, -) +) { + fun getConfigAsMap() = config?.toMap() ?: emptyMap() +} diff --git a/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyManager.kt b/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyManager.kt index f01f5cc05..147cf5e63 100644 --- a/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyManager.kt +++ b/waltid-libraries/crypto/waltid-crypto/src/commonMain/kotlin/id/walt/crypto/keys/KeyManager.kt @@ -35,11 +35,15 @@ object KeyManager { } } + fun registerByType(type: KType, typeId: String, createFunction: suspend (KeyGenerationRequest) -> Key) { + types[typeId] = type + keyTypeGeneration[typeId] = createFunction + } + inline fun register(typeId: String, noinline createFunction: suspend (KeyGenerationRequest) -> T) { keyManagerLogger().trace { "Registering key type \"$typeId\" to ${T::class.simpleName}..." } val type = typeOf() - types[typeId] = type - keyTypeGeneration[typeId] = createFunction + registerByType(type, typeId, createFunction) } suspend fun createKey(generationRequest: KeyGenerationRequest): Key { @@ -59,7 +63,8 @@ object KeyManager { Json.decodeFromJsonElement(serializer(type), JsonObject(fields)) as Key }?.apply { init() } ?: error("No type in serialized key") - fun resolveSerializedKeyBlocking(jsonString: String) = resolveSerializedKeyBlocking(json = Json.parseToJsonElement(jsonString).jsonObject) + fun resolveSerializedKeyBlocking(jsonString: String) = + resolveSerializedKeyBlocking(json = Json.parseToJsonElement(jsonString).jsonObject) } diff --git a/waltid-libraries/crypto/waltid-crypto/src/jvmMain/kotlin/id/walt/crypto/keys/KeyManagerJavaExtensions.kt b/waltid-libraries/crypto/waltid-crypto/src/jvmMain/kotlin/id/walt/crypto/keys/KeyManagerJavaExtensions.kt new file mode 100644 index 000000000..9114e0cb4 --- /dev/null +++ b/waltid-libraries/crypto/waltid-crypto/src/jvmMain/kotlin/id/walt/crypto/keys/KeyManagerJavaExtensions.kt @@ -0,0 +1,28 @@ +package id.walt.crypto.keys + +import kotlin.jvm.internal.Reflection + +object KeyManagerJavaExtensions { + + /** + * Use like this from Java: + * ``` + * KeyManagerJavaExtensions.INSTANCE.register(MyJavaBasedKey.class, "theTypeId", + * keyGenerationRequest -> { + * String backend = keyGenerationRequest.getBackend(); + * KeyType keyType = keyGenerationRequest.getKeyType(); + * + * // Return key here + * return null; + * } + * ); + * ``` + */ + fun register(javaClass: Class, typeId: String, createFunction: (KeyGenerationRequest) -> Key) = + KeyManager.registerByType( + type = Reflection.typeOf(javaClass), + typeId = typeId, + createFunction = createFunction + ) + +}