Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Invalid signature integer: negative on JS platform #158

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

/**
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
)
}
}
Loading