From 08f851621084bd3873fbfbbfef9c41c9f67ff12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Rib=C3=B3?= Date: Thu, 9 May 2024 13:30:17 +0200 Subject: [PATCH] fix: Invalid signature integer: negative on JS platform (#158) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Francisco Javier Ribó Labrador Co-authored-by: Ahmed Moussa --- .../prism/apollo/secp256k1/Secp256k1Lib.kt | 14 ++++++++--- .../prism/apollo/utils/Secp256k1LibTestJS.kt | 24 +++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/utils/Secp256k1LibTestJS.kt diff --git a/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/secp256k1/Secp256k1Lib.kt b/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/secp256k1/Secp256k1Lib.kt index 619184ad9..d3d662c31 100644 --- a/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/secp256k1/Secp256k1Lib.kt +++ b/apollo/src/jsMain/kotlin/io/iohk/atala/prism/apollo/secp256k1/Secp256k1Lib.kt @@ -67,7 +67,11 @@ actual class Secp256k1Lib actual constructor() { } catch (e: dynamic) { secp256k1.Signature.fromCompact(jsSignatureByteArray) } - return signature.normalizeS() + return if (signature.hasHighS()) { + signature.normalizeS() + } else { + signature + } } /** @@ -88,8 +92,12 @@ actual class Secp256k1Lib actual constructor() { if (secp256k1.verify(normalised, sha.asUint8Array(), publicKey.asUint8Array(), {})) { return true } - val transcoded = transcodeSignatureToBitcoin(normalised.toCompactRawBytes().asByteArray()) - return secp256k1.verify(transcoded, sha.asUint8Array(), publicKey.asUint8Array(), {}) + return try { + val transcoded = transcodeSignatureToBitcoin(normalised.toCompactRawBytes().asByteArray()) + secp256k1.verify(transcoded, sha.asUint8Array(), publicKey.asUint8Array(), {}) + } catch (e: dynamic) { + secp256k1.verify(normalised, sha.asUint8Array(), publicKey.asUint8Array(), {}) + } } private fun transcodeSignatureToBitcoin(signature: ByteArray): SignatureType { diff --git a/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/utils/Secp256k1LibTestJS.kt b/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/utils/Secp256k1LibTestJS.kt new file mode 100644 index 000000000..2022bcfde --- /dev/null +++ b/apollo/src/jsTest/kotlin/io/iohk/atala/prism/apollo/utils/Secp256k1LibTestJS.kt @@ -0,0 +1,24 @@ +package io.iohk.atala.prism.apollo.utils + +import io.iohk.atala.prism.apollo.derivation.Mnemonic +import kotlin.test.Test +import kotlin.test.assertEquals + +class Secp256k1LibTestJS { + + @Test + fun testCreateApolloSignatureAndVerify() { + val mnemonics = Mnemonic.Companion.createRandomMnemonics() + val seed = Mnemonic.Companion.createSeed(mnemonics) + val secret = seed.slice(0..31).toTypedArray() + val sk = KMMECSecp256k1PrivateKey.Companion.secp256k1FromByteArray(secret.toByteArray()) + val pk = sk.getPublicKey() + val data = "Data 0002".encodeToByteArray() + val signature = sk.sign(data) + val verified = pk.verify(signature, data) + assertEquals( + verified, + true + ) + } +}