From 5acd175058bf9d416b7f14aba5b30a46379ab7fc Mon Sep 17 00:00:00 2001 From: Cristian G <113917899+cristianIOHK@users.noreply.github.com> Date: Wed, 27 Sep 2023 08:46:47 -0400 Subject: [PATCH] feature: add publicKey to PrivateKeys (#96) --- .../iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt | 7 +++++++ .../atala/prism/apollo/utils/KMMX25519PrivateKey.kt | 11 ++++++++++- .../iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt | 2 +- .../iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt | 6 ++++++ .../atala/prism/apollo/utils/KMMX25519PrivateKey.kt | 11 ++++++++++- 5 files changed, 34 insertions(+), 3 deletions(-) diff --git a/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt b/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt index f3131e676..acceabf0f 100644 --- a/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt +++ b/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt @@ -4,6 +4,13 @@ import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters import org.bouncycastle.crypto.signers.Ed25519Signer actual class KMMEdPrivateKey(val raw: ByteArray) { + + fun publicKey(): KMMEdPublicKey { + val private = Ed25519PrivateKeyParameters(raw, 0) + val public = private.generatePublicKey() + return KMMEdPublicKey(public.encoded) + } + actual fun sign(message: ByteArray): ByteArray { val privateKeyParameters = Ed25519PrivateKeyParameters(raw, 0) val signer = Ed25519Signer() diff --git a/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 7f33bb119..1067ed504 100644 --- a/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/base-asymmetric-encryption/src/androidMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -1,3 +1,12 @@ package io.iohk.atala.prism.apollo.utils -actual class KMMX25519PrivateKey(val raw: ByteArray) +import org.bouncycastle.crypto.params.X25519PrivateKeyParameters + +actual class KMMX25519PrivateKey(val raw: ByteArray) { + + fun publicKey(): KMMX25519PublicKey { + val private = X25519PrivateKeyParameters(raw, 0) + val public = private.generatePublicKey() + return KMMX25519PublicKey(public.encoded) + } +} diff --git a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt index 988199dca..30eb0ae2c 100644 --- a/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt +++ b/base-asymmetric-encryption/src/iosMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt @@ -14,7 +14,7 @@ public actual class KMMEdPrivateKey(val raw: ByteArray) { } @Throws(RuntimeException::class) - public fun publicKey(): KMMEdPublicKey { + fun publicKey(): KMMEdPublicKey { val result = Ed25519.publicKeyWithPrivateKey(raw.toNSData()) result.failure()?.let { throw RuntimeException(it.localizedDescription()) } val publicRaw = result.success()?.toByteArray() ?: throw RuntimeException("Null result") diff --git a/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt b/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt index 9d98db66d..5092af0a0 100644 --- a/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt +++ b/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMEdPrivateKey.kt @@ -6,6 +6,12 @@ import java.io.ByteArrayInputStream actual class KMMEdPrivateKey(val raw: ByteArray) { + fun publicKey(): KMMEdPublicKey { + val private = Ed25519PrivateKeyParameters(raw, 0) + val public = private.generatePublicKey() + return KMMEdPublicKey(public.encoded) + } + actual fun sign(message: ByteArray): ByteArray { val privateKeyParameters = Ed25519PrivateKeyParameters(ByteArrayInputStream(raw)) val signer = Ed25519Signer() diff --git a/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt b/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt index 7f33bb119..1067ed504 100644 --- a/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt +++ b/base-asymmetric-encryption/src/jvmMain/kotlin/io/iohk/atala/prism/apollo/utils/KMMX25519PrivateKey.kt @@ -1,3 +1,12 @@ package io.iohk.atala.prism.apollo.utils -actual class KMMX25519PrivateKey(val raw: ByteArray) +import org.bouncycastle.crypto.params.X25519PrivateKeyParameters + +actual class KMMX25519PrivateKey(val raw: ByteArray) { + + fun publicKey(): KMMX25519PublicKey { + val private = X25519PrivateKeyParameters(raw, 0) + val public = private.generatePublicKey() + return KMMX25519PublicKey(public.encoded) + } +}