5

I've been trying to store a hardcoded call in a contract, but I'm getting confused on the encoding.

Let's say I have the following bytes:

0x9461d6f7000000000000000000000000000000000000000000000000000000000000000a796f000000000000000000000000000000000000000000000000000000000000

This translates to calling function testData(uint256 testVar, bytes32 otherVar) with 10 & "yo".

If I store this as a bytes like so:

bytes public hardcodedCallData = "0x9461d6f7000000000000000000000000000000000000000000000000000000000000000a796f000000000000000000000000000000000000000000000000000000000000";

And then do a address.call(hardcodedCallData), it does not work. The fallback function is triggered instead, assuming that it is malformed. When storing it as such, it seems it decodes to bytes that look different.

enter image description here

I seem to have a misunderstanding on encoding here somewhere. Given calldata, how do I store it as bytes as hardcoded, so I can do the above?

I tried variations (playing with strings for example), but not able to figure this out.

6

The issue is that when you use a string literal like

bytes public hardcodedCallData = "0x9461d6f7000000000000000000000000000000000000000000000000000000000000000a796f000000000000000000000000000000000000000000000000000000000000";

Solidity interprets the string as ASCII. In fact, if you do

 "307839343631643666373030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030306137393666303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030303030".decode('hex').encode("ascii")

in Python, you'll see that this is the ASCII encoding of your intended hex value.

What you should be using is

bytes public hardcodedCallData = hex"9461d6f7000000000000000000000000000000000000000000000000000000000000000a796f000000000000000000000000000000000000000000000000000000000000";

See http://solidity.readthedocs.io/en/develop/types.html?highlight=hex#hexadecimal-literals

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.