2

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";
recover_key(&m_hash,sig.data(),strlen(maker.sig.data()),pub,sizeof(char));

Error Information:

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

2 Answers 2

1

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) )
0
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

9
  • what's the format of sig? my signature is string like "SIG_K1_K5cpBg7XHRfgdedEAR9w74bXQZk4TUQk9QDMfzNbttY3eZ5WhmpNuWKiRyidskkdjBH9L9pKKe29RiiELNLCNYzFtFyTqJ",How to converto to signature type? Aug 15, 2018 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, 2018 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"" Aug 15, 2018 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)); Aug 15, 2018 at 10:48
  • @BruceLingKing any difference with &pub2.data and &pub2?
    – Jimmy Guo
    Aug 15, 2018 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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