From 09c42764e3e907e101231403a942776b259d20af Mon Sep 17 00:00:00 2001 From: Yuriy Glukhov Date: Wed, 21 Mar 2018 16:45:52 +0200 Subject: [PATCH] Added signature serialization procs (#9) --- src/backend_libsecp256k1/libsecp256k1.nim | 19 +++++++++++++++++++ src/datatypes_interface.nim | 1 + 2 files changed, 20 insertions(+) diff --git a/src/backend_libsecp256k1/libsecp256k1.nim b/src/backend_libsecp256k1/libsecp256k1.nim index 1c89ef9..820477d 100644 --- a/src/backend_libsecp256k1/libsecp256k1.nim +++ b/src/backend_libsecp256k1/libsecp256k1.nim @@ -48,6 +48,25 @@ proc private_key_to_public_key*(key: PrivateKey): PublicKey {.noInit.}= if not success: raise newException(ValueError, "Private key is invalid") +proc serialize*(s: Signature, output: var openarray[byte], fromIdx: int = 0) = + ## Serialize an ECDSA signature in compact format, 65 bytes long + ## (64 bytes + recovery id). The output is written starting from `fromIdx`. + assert(output.len - fromIdx >= 65) + var v: cint + discard secp256k1_ecdsa_recoverable_signature_serialize_compact(ctx, + cast[ptr cuchar](addr output[fromIdx]), addr v, s.asPtrRecoverableSignature) + output[fromIdx + 64] = byte(v) + +proc parseSignature*(data: openarray[byte], fromIdx: int = 0): Signature = + ## Parse a compact ECDSA signature. Bytes [fromIdx .. fromIdx + 63] of `data` + ## should contain the signature, byte [fromIdx + 64] should contain the recovery id. + assert(data.len - fromIdx >= 65) + if secp256k1_ecdsa_recoverable_signature_parse_compact(ctx, + result.asPtrRecoverableSignature, + cast[ptr cuchar](unsafeAddr data[fromIdx]), + cint(data[fromIdx + 64])) != 1: + raise newException(ValueError, "Signature data is invalid") + proc serialize*(key: PublicKey, output: var openarray[byte], addPrefix = false) = ## Exports a publicKey to `output` buffer so that it can be var diff --git a/src/datatypes_interface.nim b/src/datatypes_interface.nim index ea20d7d..a349153 100644 --- a/src/datatypes_interface.nim +++ b/src/datatypes_interface.nim @@ -23,6 +23,7 @@ when defined(backend_native): else: import ./backend_libsecp256k1/libsecp256k1 export libsecp256k1.serialize + export libsecp256k1.parseSignature export libsecp256k1.`$` export libsecp256k1.parsePublicKey