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.update(word);
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?
Thanks

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 sp.show(sp.pack("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

Bonus:

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

@sp.module
def main():
    class MyContract(sp.Contract):
        def __init__(self):
            self.data.packed = sp.bytes("0x00")
            self.data.keccak = sp.bytes("0x00")

        @sp.entrypoint
        def ep(self):
            self.data.packed = sp.pack("hello")
            self.data.keccak = sp.keccak(self.data.packed)

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

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

Thanks!
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.
Thanks!

You should ask them directly.

1 Like