TransWikia.com

Smart contract error while calling recover_key

EOS.IO Asked by Riccardo Persiani on February 6, 2021

Specs

I am using the function recover_key from the library crypto.h at the following link: https://github.com/EOSIO/eos/blob/master/contracts/eosiolib/crypto.h

Code

int compressed_size = recover_key(&digest, (char *)signature, sizeof(signature),(char *)compressed, sizeof(compressed));

Error

I call the function multiple times but at a certain point, like the third time, the following function fails, with the following error:

2018-09-27T09:01:48.492 thread-0 main.cpp:3091 main ] Failed with error: Assert Exception (10)
secp256k1_ecdsa_recover_compact( detail::_get_context(), (unsigned char*) digest.data(), (unsigned char*) c.begin() + 1, (unsigned char*) my->_key.begin(), (int*) &pk_len, 1, (*c.begin() – 27) & 3 ):

Additional Info

The problem is that when the function fails the parameters provided are exactly at the same format than the other two times.

What I want

How can I solve this issue? How can I make the key recovery work everytime?

One Answer

Here's one way of doing it:

#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
#include <eosiolib/crypto.h>
using namespace eosio;

class ec: public eosio::contract {
  public:
      using contract::contract;

  ///@abi action
  void ecverify(std::string data, const signature &sig, const public_key &pk)
  {
    checksum256 digest;
    sha256(&data[0], data.size(), &digest);

    assert_recover_key(&digest, (const char *)&sig, sizeof(sig), (const char *)&pk, sizeof(pk));
    print("VALID");
  }
};

EOSIO_ABI( ec, (ecverify) )

Answered by Miguel Mota on February 6, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP