Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
k1_recover.cpp
Go to the documentation of this file.
3#include <secp256k1.h>
5
6namespace fc {
7
12
13 std::variant<k1_recover_error, bytes> k1_recover(const bytes& signature, const bytes& digest) {
15 FC_ASSERT(context != nullptr);
16
17 if (signature.size() != 65 || digest.size() != 32) {
19 }
20
21 int recid = signature[0];
22 if (recid<27 || recid>=35) return k1_recover_error::invalid_signature;
23 recid = (recid - 27) & 3;
24
26 if (!secp256k1_ecdsa_recoverable_signature_parse_compact(context, &sig, (const unsigned char*)&signature[1], recid)) {
28 }
29
30 secp256k1_pubkey pub_key;
31 if (!secp256k1_ecdsa_recover(context, &pub_key, &sig, (const unsigned char*)&digest[0])) {
33 }
34
35 size_t kOutLen{65};
36 bytes out(kOutLen, '\0');
37 secp256k1_ec_pubkey_serialize(context, (unsigned char*)&out[0], &kOutLen, &pub_key, SECP256K1_EC_UNCOMPRESSED);
38 return out;
39 }
40}
Defines exception's used by fc.
#define FC_ASSERT(TEST,...)
Checks a condition and throws an assert_exception if the test is FALSE.
namespace sysio::chain
Definition authority.cpp:3
fc::sha256 digest(const T &value)
Definition digest.hpp:9
const secp256k1_context * k1_recover_context()
Definition k1_recover.cpp:8
std::vector< char > bytes
Definition alt_bn128.hpp:10
bytes signature
Definition pke.hpp:17
std::variant< k1_recover_error, bytes > k1_recover(const bytes &signature, const bytes &digest)
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition secp256k1.c:246
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Definition secp256k1.c:107
#define SECP256K1_EC_UNCOMPRESSED
Definition secp256k1.h:202
#define SECP256K1_CONTEXT_VERIFY
Definition secp256k1.h:195
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *input64, int recid) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Definition main_impl.h:38
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Definition main_impl.h:137