I want to sign some data in client using eosjs-ecc.sign() and result is a string like "SIG_K1_KcTmv..." I want to use the recover_key(const checksum256* digest, const char* sig, size_t siglen, char* pub, size_t publen ); to recover the public key. However the parameter sig need to type "signature"(which is 66 byes). How do I convert result of eosjs-ecc.sign to be used in contract recover_key?

  • directly pass the SIG_K1_xxx with type signature, and contract will convert it directly for you
    – Jimmy Guo
    Jul 26, 2018 at 5:26
  • If I directly pass the SIG_K1_xx string, the recover_key function in contract throw exception. It is expect some format of binary data
    – Wayne
    Jul 26, 2018 at 17:41
  • I say with type signature
    – Jimmy Guo
    Jul 27, 2018 at 4:55

1 Answer 1


You need to use signature structure from eoslib small working example of keycheck function is attached.

#include <eosiolib/eosio.hpp>
#include <eosiolib/crypto.h>
#include <string>

template<typename CharT>
static std::string to_hex( const CharT* d, uint32_t s ) {
    std::string r;
    const char* to_hex="0123456789abcdef";
    uint8_t* c = (uint8_t*)d;
    for( uint32_t i = 0; i < s; ++i ) {
        (r += to_hex[(c[i] >> 4)]) += to_hex[(c[i] & 0x0f)];
return r;

namespace example {

   using namespace eosio;

   class recover : public contract
     using contract::contract;

     recover(account_name self) : contract(self) {};

    ///@abi action
    void keycheck(const checksum256 &digest, const signature &sig, const public_key &crosscheck_pk)
        public_key pk;
        auto r = recover_key(&digest, (char *)&sig, sizeof(sig), (char *)&pk, sizeof(pk));
        print_f("public_key: %\n", to_hex(&pk, sizeof(pk)).c_str());
        print_f("crosscheck public_key: %\n", to_hex(&crosscheck_pk, sizeof(crosscheck_pk)).c_str());
        auto cmp = memcmp(&pk, &crosscheck_pk, sizeof(public_key));
        eosio_assert(memcmp(&pk, &crosscheck_pk, sizeof(public_key)) == 0, "keycheck: pubkey cross check failed");


EOSIO_ABI(example::recover, (keycheck))
  • how to send parameters? first parameters is data's hash? thank you. cleos push action signature keycheck '["01c77713adf37144f4d4ab1fff11c66d9a54d5fef490253674bf009ef586a066", "SIG_K1_K2jD49Nz8h77fXbZvHgojsygojb9SnUBKs4PMXSFLABnDcSvPB1QJG3rsYVz6tpXYEBSfaxWKqpSqyzFv86wf4KjFEETat", "EOS4wpa8kRBbZof9JEpPAAgbnN65NhbBobW4x5gyxQoFfamhQCAMX"]' -p signature
    – yilin wang
    Nov 8, 2018 at 7:19

Your Answer

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

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