Keccak doesn't work properly

Hi, i created an HashTimedLockedContract with three entrypoints.
The first is commit and receive three parameters between those it’s an hash.
In smartpy scenario works:

secret = "hello"
bytes = sp.pack(secret)
hash = sp.keccak(bytes)

but the problem comes when I deploy it.
Either on smartpy explorer and TS script the hash i generate doesn’t work.
For the first one I used a online tool and the hash generated with the same word is different from the one created with the code above.
For the second I used this code lines:

const word = "hello";
const keccak = new Keccak(512);
keccak.digest("hex"); or keccak.digest("binary"); 

and returns me this error:

Uncaught (in promise) 
Object { value: {…}, token: {…}, name: "BytesValidationError", message: "[hash] Invalid bytes: [object Object]" }

I think I don’t know some fundamental rules.
Can someone explain?

sp.pack adds some info to the string, it’s not simply encoding the string into bytes.

It’s called forge into pytezos. There is also a function for packing things in taquito. You can also use"hello")) in a SmartPy scenario.

Then if you paste the packed bytes value into an online keccak encoder it should now give the same result as sp.keccak(sp.pack("hello")).

1 Like


If you want to test how the chain compute the values, you can create a simple contract and do the computation in an entrypoint (not in the constructor, otherwise it will be handled by the SmartPy scenario system).

import smartpy as sp

def main():
    class MyContract(sp.Contract):
        def __init__(self):
   = sp.bytes("0x00")
   = sp.bytes("0x00")

        def ep(self):
   = sp.pack("hello")
   = sp.keccak(

if "templates" not in __name__:
    def test():
        sc = sp.test_scenario(main)
        c1 = main.MyContract()
        sc += c1

If you originate this contract and call the ep entrypoint you can be sure what value the chain computes.

It give me still an error but i will try to figure out.

Object { value: {…}, token: {…}, name: "BytesValidationError", message: "[hash] Invalid bytes: [object Object]" }
message: "[hash] Invalid bytes: [object Object]"
name: "BytesValidationError"

I don’t know what this is. Please give more information.
In which language? In which context ? With which library?

I gave you all the SmartPy information, I can’t do your work for other libraries. Simply understand that you cannot use other libraries inside your smart contract if that’s what you are trying to do.

I’m trying to create a script using TypeScrypt and Taquito to tests my contracts.
You already helped me a lot, don’t worry.

You should ask them directly.

1 Like