I am trying to access big_map data for a specific key, but I found that I need to provide the hash of the key to retrieve the data. Is there a way to get the hash for a key? Knowing the algorithm used to generate the key should be good enough too.


Quick way (using Pytezos library):

>>> from pytezos.michelson.pack import get_key_hash
>>> get_key_hash({"string": "KT1PWx2mnDueood7fEmfbBDKx1D9BAnnXitn"}, {"prim": "address"})

Where first argument is the key (Micheline JSON), and the second one is the expression type (Micheline JSON).

Here is the algorithm and intermediate results (for value/type used above):

  1. Pack key (e.g. using pack_data RPC endpoint)
  2. Take Blake2b hash (digest size = 32)
  3. Prepend "0d2c401b"
  4. Base58 encode with checksum

I ended up using ConseilJS for this. I used the TezosMessageUtils.encodeBigMapKey function in ConseilJS to create the hash key. Here's an example of how to convert the key to the hash :

const packedKey = TezosMessageUtils.encodeBigMapKey(Buffer.from(TezosMessageUtils.writePackedData(secretHash, "bytes"), "hex"));

You need to put the appropriate type of your key in the writePackedData before encoding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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