Add compact2der() method

This commit is contained in:
sstone
2020-07-02 21:39:33 +02:00
parent 244673b04c
commit 08d1692932
7 changed files with 73 additions and 0 deletions

View File

@@ -52,6 +52,8 @@ public interface Secp256k1 {
public fun ecdsaRecover(sig: ByteArray, message: ByteArray, recid: Int): ByteArray
public fun compact2der(sig: ByteArray): ByteArray
public fun pubKeyCompress(pubkey: ByteArray) : ByteArray {
return when {
pubkey.size == 33 && (pubkey[0] == 2.toByte() || pubkey[0] == 3.toByte()) -> pubkey

View File

@@ -215,6 +215,18 @@ public object Secp256k1Native : Secp256k1 {
return serializePubkey(pubkey)
}
}
public override fun compact2der(sig: ByteArray): ByteArray {
require(sig.size == 64)
memScoped {
val nSig = allocSignature(sig)
val natOutput = allocArray<UByteVar>(73)
val len = alloc<size_tVar>()
len.value = 73.convert()
secp256k1_ecdsa_signature_serialize_der(ctx, natOutput, len.ptr, nSig.ptr).requireSuccess("secp256k1_ecdsa_signature_serialize_der() failed")
return natOutput.readBytes(len.value.toInt())
}
}
}
internal actual fun getSecpk256k1(): Secp256k1 = Secp256k1Native