Skip to content

Commit

Permalink
fix: Invalid signature integer: negative on JS platform (#158)
Browse files Browse the repository at this point in the history
Signed-off-by: Francisco Javier Ribó Labrador <[email protected]>
Co-authored-by: Ahmed Moussa <[email protected]>
  • Loading branch information
elribonazo and hamada147 committed May 20, 2024
1 parent 8180fa8 commit 08f8516
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
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
)
}
}

0 comments on commit 08f8516

Please sign in to comment.