From a5b62ebe5c3346efae2c25895f9b3f37f54b5ccb Mon Sep 17 00:00:00 2001 From: Cristian G Date: Fri, 22 Sep 2023 12:20:24 -0400 Subject: [PATCH] fix PR requested changes --- .../walletsdk/apollo/utils/JVMBigIntegerExt.kt | 3 ++- .../walletsdk/apollo/utils/Secp256k1PublicKey.kt | 6 +++--- .../ec/{_ECCoordinate.kt => KMMECCoordinate.kt} | 2 +- .../prism/walletsdk/apollo/utils/ec/KMMECPoint.kt | 15 +++++++++++++++ .../prism/walletsdk/apollo/utils/ec/_ECPoint.kt | 15 --------------- .../domain/models/keyManagement/PrivateKey.kt | 10 ++++++++++ .../domain/models/keyManagement/PublicKey.kt | 10 ++++++++++ 7 files changed, 41 insertions(+), 20 deletions(-) rename atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/{_ECCoordinate.kt => KMMECCoordinate.kt} (86%) create mode 100644 atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECPoint.kt delete mode 100644 atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECPoint.kt diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/JVMBigIntegerExt.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/JVMBigIntegerExt.kt index 09181265a..53c6c2557 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/JVMBigIntegerExt.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/JVMBigIntegerExt.kt @@ -4,7 +4,8 @@ import com.ionspin.kotlin.bignum.integer.Sign import java.math.BigInteger fun BigInteger.toUnsignedByteArray(): ByteArray { - return toByteArray().dropWhile { it == 0.toByte() }.toByteArray() + val comparedValue = 0.toByte() + return toByteArray().dropWhile { it == comparedValue }.toByteArray() } fun BigInteger.toKotlinBigInteger(): com.ionspin.kotlin.bignum.integer.BigInteger { diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt index c82061011..335b1459f 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/Secp256k1PublicKey.kt @@ -9,7 +9,7 @@ import io.iohk.atala.prism.apollo.utils.KMMECPoint import io.iohk.atala.prism.apollo.utils.KMMECSecp256k1PublicKey import io.iohk.atala.prism.apollo.utils.KMMEllipticCurve import io.iohk.atala.prism.walletsdk.apollo.config.ECConfig -import io.iohk.atala.prism.walletsdk.apollo.utils.ec._ECPoint +import io.iohk.atala.prism.walletsdk.apollo.utils.ec.KMMECPoint import io.iohk.atala.prism.walletsdk.domain.models.Curve import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.CurveKey import io.iohk.atala.prism.walletsdk.domain.models.keyManagement.CustomKey @@ -93,9 +93,9 @@ class Secp256k1PublicKey(nativeValue: ByteArray) : PublicKey(), VerifiableKey { } companion object { - fun computeCurvePoint(key: BCECPublicKey): _ECPoint { + fun computeCurvePoint(key: BCECPublicKey): io.iohk.atala.prism.walletsdk.apollo.utils.ec.KMMECPoint { val javaPoint = key.w - return _ECPoint(javaPoint.affineX.toKotlinBigInteger(), javaPoint.affineY.toKotlinBigInteger()) + return KMMECPoint(javaPoint.affineX.toKotlinBigInteger(), javaPoint.affineY.toKotlinBigInteger()) } fun secp256k1FromCompressed(compressed: ByteArray): Secp256k1PublicKey { diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECCoordinate.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECCoordinate.kt similarity index 86% rename from atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECCoordinate.kt rename to atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECCoordinate.kt index 130834734..385f8a666 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECCoordinate.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECCoordinate.kt @@ -3,7 +3,7 @@ package io.iohk.atala.prism.walletsdk.apollo.utils.ec import com.ionspin.kotlin.bignum.integer.BigInteger import io.iohk.atala.prism.walletsdk.apollo.helpers.padStart -class _ECCoordinate(val coordinate: BigInteger) { +class KMMECCoordinate(val coordinate: BigInteger) { fun bytes(): ByteArray = coordinate.toByteArray().padStart(PRIVATE_KEY_BYTE_SIZE, 0) diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECPoint.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECPoint.kt new file mode 100644 index 000000000..c0cb2a251 --- /dev/null +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/KMMECPoint.kt @@ -0,0 +1,15 @@ +package io.iohk.atala.prism.walletsdk.apollo.utils.ec + +import com.ionspin.kotlin.bignum.integer.BigInteger + +class KMMECPoint(val x: KMMECCoordinate, val y: KMMECCoordinate) { + constructor(x: String, y: String) : this( + KMMECCoordinate(BigInteger.parseString(x)), + KMMECCoordinate(BigInteger.parseString(y)) + ) + + constructor(x: BigInteger, y: BigInteger) : this( + KMMECCoordinate(x), + KMMECCoordinate(y) + ) +} diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECPoint.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECPoint.kt deleted file mode 100644 index 1ade3f708..000000000 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/apollo/utils/ec/_ECPoint.kt +++ /dev/null @@ -1,15 +0,0 @@ -package io.iohk.atala.prism.walletsdk.apollo.utils.ec - -import com.ionspin.kotlin.bignum.integer.BigInteger - -class _ECPoint(val x: _ECCoordinate, val y: _ECCoordinate) { - constructor(x: String, y: String) : this( - _ECCoordinate(BigInteger.parseString(x)), - _ECCoordinate(BigInteger.parseString(y)) - ) - - constructor(x: BigInteger, y: BigInteger) : this( - _ECCoordinate(x), - _ECCoordinate(y) - ) -} diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PrivateKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PrivateKey.kt index d7f917afa..0714abecf 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PrivateKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PrivateKey.kt @@ -1,11 +1,21 @@ package io.iohk.atala.prism.walletsdk.domain.models.keyManagement +import io.iohk.atala.prism.walletsdk.domain.models.Curve + abstract class PrivateKey : Key() { fun getCurve(): String { return this.getProperty(CurveKey().property) } + fun getCurveInstance(): Curve? { + return try { + Curve.valueOf(this.getProperty(CurveKey().property)) + } catch (e: Exception) { + null + } + } + fun getIndex(): String { return this.getProperty(IndexKey().property) } diff --git a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PublicKey.kt b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PublicKey.kt index a26156440..3aacdc57a 100644 --- a/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PublicKey.kt +++ b/atala-prism-sdk/src/commonMain/kotlin/io/iohk/atala/prism/walletsdk/domain/models/keyManagement/PublicKey.kt @@ -1,11 +1,21 @@ package io.iohk.atala.prism.walletsdk.domain.models.keyManagement +import io.iohk.atala.prism.walletsdk.domain.models.Curve + abstract class PublicKey : Key() { fun getCurve(): String { return this.getProperty(CurveKey().property) } + fun getCurveInstance(): Curve? { + return try { + Curve.valueOf(this.getProperty(CurveKey().property)) + } catch (e: Exception) { + null + } + } + fun getValue(): ByteArray { return this.raw }