Is there a specific sample code of using recover_key()? I got an error with recover_key() in contract.

string msg="charlie";
checksum256 m_hash;
sha256( const_cast<char*>(msg.c_str()), msg.length(), &m_hash );
char* pub;
string sig="SIG_K1_K5cpBg7XHRfgdedEAR9w74bXQZk4TUQk9QDMfzNbttY3eZ5WhmpNuWKiRyidskkdjBH9L9pKKe29RiiELNLCNYzFtFyTqJ";

Error Information:

assert_exception: Assert Exception
w < count():
thread-0  static_variant.hpp:339 set_which
Error unpacking field _storage
thread-0  raw.hpp:368 operator()
error unpacking fc::crypto::signature
thread-0  raw.hpp:616 unpack
  • Can u give me a example about how you call ecverify action by cleos? Thanks!! – Vitor Jan 12 at 11:39

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 {
      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));

EOSIO_ABI( ec, (ecverify) )
void assert_recover_key2(const checksum256* digest, const signature* sig, const eosio::public_key* pub) {
     public_key pub2;
     pub2.data[0] = char(pub->type.value);
     memcpy(&pub2.data[1], pub->data.data(), 33);
     assert_recover_key(digest, (const char*)sig, sizeof(signature), (const char*)&pub2, sizeof(public_key));


that's my code, take care the public_key and eosio::public_key is not same

  • what's the format of sig? my signature is string like "SIG_K1_K5cpBg7XHRfgdedEAR9w74bXQZk4TUQk9QDMfzNbttY3eZ5WhmpNuWKiRyidskkdjBH9L9pKKe29RiiELNLCNYzFtFyTqJ",How to converto to signature type? – BruceLingKing Aug 15 '18 at 6:00
  • @BruceLingKing just pass the string in your action, and eos will convert it to eosio::public_key directly – Jimmy Guo Aug 15 '18 at 7:01
  • I did it as u said .But when send action,the errors occured "Error: Non-base58 character order.sig = "SIG_K1_K5cpBg7XHRfgdedEAR9w74bXQZk4TUQk9QDMfzNbttY3eZ5WhmpNuWKiRyidskkdjBH9L9pKKe29RiiELNLCNYzFtFyTqJ"" – BruceLingKing Aug 15 '18 at 7:20
  • According to my test, assert_recover_key2 would failed!When I modify it as bellow,it success. assert_recover_key(digest, (const char*)sig, sizeof(signature), (const char*)&(pub2.data), sizeof(public_key)); – BruceLingKing Aug 15 '18 at 10:48
  • @BruceLingKing any difference with &pub2.data and &pub2? – Jimmy Guo Aug 15 '18 at 11:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.