3

I'm trying to call Smart Contract with next method signature

calculateHash(string)

and Code like:

function calculateHash( string testStr ) constant returns ( bytes32 ){
    return sha3( testStr );
  }

I have next Sha3: 0xa8cbcecd37e0c4ac16c3135987b900352c43486006715fbca927444ab937b3af so first 4 bytes looks like 0xa8cbcecd Parameter for example is "Hello, Words!" According to ABI documentation I should encocde it like

bytes, of length k (which is assumed to be of type uint256):

enc(X) = enc(k) pad_right(X), i.e. the number of bytes is encoded as a uint256 followed by the actual value of X as a byte sequence, followed by the minimum number of zero-bytes such that len(enc(X)) is a multiple of 32.

string:

enc(X) = enc(enc_utf8(X)), i.e. X is utf-8 encoded and this value is interpreted as of bytes type and encoded further. Note that the length used in this subsequent encoding is the number of bytes of the utf-8 encoded string, not its number of characters.

so in my case it is

enc(enc_utf8("Hello, World!)) = end(Ox48656c6c6f2c20576f726c6421) = 
000000000000000000000000000000000000000000000000000000000000000d
48656c6c6f2c20576f726c642100000000000000000000000000000000000000

so full POST looks like

{"method":"eth_call","params":[{"from":"0x0027440CB2A41a3CF38Da9f89B0380311D146330","to":"0x3aDbCF5E934B0D78F0012C173032E2e2f3586F63","data":"0xa8cbcecd000000000000000000000000000000000000000000000000000000000000000d48656c6c6f2c20576f726c642100000000000000000000000000000000000000"}],"id":2,"jsonrpc":"2.0"}

But I have next return {
    "jsonrpc": "2.0",
    "result": "0x",
    "id": 2
}

Where is my mistake? Thank you Alex

2

the problem was fixed, I forgot about offset for dynamic parameters So encoding was correct 000000000000000000000000000000000000000000000000000000000000000d 48656c6c6f2c20576f726c642100000000000000000000000000000000000000 but I have to add next to start after method signature 0000000000000000000000000000000000000000000000000000000000000020 so full part looks like

0000000000000000000000000000000000000000000000000000000000000020 000000000000000000000000000000000000000000000000000000000000000d 48656c6c6f2c20576f726c642100000000000000000000000000000000000000 a little strange, but working

  • Remember to come back in a couple of days and accept your own answer :-) (There's a 48-hour delay before you can.) – Richard Horrocks Jul 2 '17 at 13:12

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.