1

I'm new to the EOS platform.

For my use case I'm trying to pass a sha256 hash to a function in my contract and store it in a table which is indexed by that hash.

I've realized you can't have the primary key of a table be anything other than a uint64_t, but apparently you can create a secondary index with a 256 bit type.

Looks like I'm supposed to use a key256 type as my secondary index, can get my contract wasm to compile and deploy a contract using that. But when I go to use it I get "The type defined i n the ABI is invalid".

Looks like in the ABI it created this section:

"types": [
    {
        "new_type_name": "key256",
        "type": "fixed_key<32>"
    }
],

Something wrong with the abigen or am I using the wrong type for a hash?

1 Answer 1

3

Figured it out, trick was to use a combination of checksum256 and key256.

Used the dice contract as an example:

  struct offer {
     uint64_t          id;
     account_name      owner;
     asset             bet;
     checksum256       commitment;
     uint64_t          gameid = 0;

     uint64_t primary_key()const { return id; }

     uint64_t by_bet()const { return (uint64_t)bet.amount; }

     key256 by_commitment()const { return get_commitment(commitment); }

     static key256 get_commitment(const checksum256& commitment) {
        const uint64_t *p64 = reinterpret_cast<const uint64_t *>(&commitment);
        return key256::make_from_word_sequence<uint64_t>(p64[0], p64[1], p64[2], p64[3]);
     }

     EOSLIB_SERIALIZE( offer, (id)(owner)(bet)(commitment)(gameid) )
  };

  typedef eosio::multi_index< N(offer), offer,
     indexed_by< N(bet), const_mem_fun<offer, uint64_t, &offer::by_bet > >,
     indexed_by< N(commitment), const_mem_fun<offer, key256,  &offer::by_commitment> >
   > offer_index;

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.