0

I have an function as shown below and I an inserted json into the contract. The string actually seems to be after the first 100 bytes and I am little confused by this.

Here is the function -

function addInventory ( string inventory, bytes32 hashInventory) public {
    inventories[hashInventory] = inventory;
    inventoryHashes.push(hashInventory);
    eventNewInventory(hashInventory);
 }

Whole input to the Transaction -

0x513303f40000000000000000000000000000000000000000000000000000000000000040651cf427005fbe7159b59790d64aa3b1babb70c8d40a8c44e707582e0e2100a300000000000000000000000000000000000000000000000000000000000001127b22637573746f6d65725f7265665f32223a22222c2277617265686f7573655f6e616d65223a2246545a222c226c6f636174696f6e223a22416d7374657264616d222c22696e76656e746f72795f6964223a2246545a2d4d543337222c22626f6c5f6e756d626572223a22333234393230222c22636f6d6d6f646974795f74797065223a22416c756d696e69756d222c22636f6d6d6f646974795f6465736372697074696f6e223a22416c756d696e69756d20496e676f7473222c227765696768745f696e5f6d74223a2231392e393938222c226f776e6572223a22436f6e636f7264205265736f7572636573204c7464222c22637573746f6d65725f7265665f31223a22545820536f757468203332227d0000000000000000000000000000

The string is -

{"customer_ref_2":"","warehouse_name":"FTZ","location":"Amsterdam","inventory_id":"FTZ-MT37","bol_number":"324920","commodity_type":"Aluminium","commodity_description":"Aluminium Ingots","weight_in_mt":"19.998","owner":"Concord Resources Ltd","customer_ref_1":"TX South 32"}

This corresponds to -

127b22637573746f6d65725f7265665f32223a22222c2277617265686f7573655f6e616d65223a2246545a222c226c6f636174696f6e223a22416d7374657264616d222c22696e76656e746f72795f6964223a2246545a2d4d543337222c22626f6c5f6e756d626572223a22333234393230222c22636f6d6d6f646974795f74797065223a22416c756d696e69756d222c22636f6d6d6f646974795f6465736372697074696f6e223a22416c756d696e69756d20496e676f7473222c227765696768745f696e5f6d74223a2231392e393938222c226f776e6572223a22436f6e636f7264205265736f7572636573204c7464222c22637573746f6d65725f7265665f31223a22545820536f757468203332227d0000000000000000000000000000

Solidity put the hash before the string, and the format seems to be hash, number 1, string.

The hash of the string is -

0x651cf427005fbe7159b59790d64aa3b1babb70c8d40a8c44e707582e0e2100a3

Is the 00..1 some kind of parameter count to the function? Also it seems to put the string after the hash but the function had the string first.

Can anyone explain that?

Edit -

This is the field that confuses me -

000000000000000000000000000000000000000000000000000000000000011

It is 63 characters. I expected 32 byte blocks. But I checked the strings and the hash ends and the string starts 63 chars later so 1 char is lost somehow.

0

https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html gives the details of how arguments are encoded. In particular, the "Use of Dynamic Types" example is probably the most relevant.

In particular in your data:

  • 513303f4 is the function selector (first four bytes of the keccak256 hash of "addInventory(string,bytes32)")
  • 0000000000000000000000000000000000000000000000000000000000000040 is the offset to the start of the string parameter data.
  • 651cf427005fbe7159b59790d64aa3b1babb70c8d40a8c44e707582e0e2100a3 is the bytes32 parameter.
  • 0000000000000000000000000000000000000000000000000000000000000112 is the length of the string parameter data.
  • The rest (7b2263...) is the ASCII-encoded string data (including some right padding).
  • that is brilliant. The extra char got me but after checking it did not change the ascii conversion at all. – Trevor Oakley Jun 6 '18 at 15:58
  • As for the field that confuses you (sorry, didn't see that edit), it is 32 bytes (64 hex characters), and it ends in 0112. – smarx Jun 6 '18 at 16:38
  • when I did the conversion to ascii the extra byte did not change the string output. But the input is still in reverse order in the txn to the actual func. I assume the VM puts the fixed length fields first. – Trevor Oakley Jun 7 '18 at 11:17
  • 0x12 in ASCII is a "device control" character, so it's not visible. As to the ordering of fields, it's sort of true that fixed-length fields come first. I linked to the full details of how arguments are ABI encoded. – smarx Jun 7 '18 at 14:38

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.