2

I have a function which accepts one bytes16 argument.

  function start(
    bytes16 reference
  )

I have some events which I use to track behaviour, using reference as the index. Everything works well enough except when a user submits a reference with 17-32 chars. Between web3 and the SC, reference is being truncated, keeping only the 16 bytes to the left and the rest as zero.

I added validations for this on the client-side but I still wanted to setup the SC to handle this more gracefully. I was expecting it to revert transaction with a type error but that only happens if the argument has over 32 bytes.

I would imagine I should be adding a modifier to require the right length but I'm having a hard time finding similar cases to understand how it should be done. What's the best practice in a situation like this?

  • 1
    This question is a little confusing. A bytes16 is always exactly 16 bytes long... I'm not sure what it is that you want to check. (On the client, you can certainly validate the data before you put it into a bytes16, but by the time you're in the smart contract, you have a bytes16 already.) – smarx Jan 25 at 17:38
  • Apologies - in short I was expecting the Smart Contract function to revert the transaction due to the incorrect data type instead of truncating the argument. – Rafael Jegundo Jan 25 at 18:53
  • I guess my question comes down to: should I be validating this on the Smart Contract or on the client-side to trigger a proper response to the user? – Rafael Jegundo Jan 25 at 18:54
  • It's not the smart contract truncating; it's the client-side code. So yes, you should do the validation there. – smarx Jan 25 at 22:05
0

should I be validating this on the Smart Contract or on the client-side to trigger a proper response to the user

You could parse the msg.data within the contract to check if additional bytes have been submitted, but it will cost extra gas and the check is better done at the client side in the UI.

1

If your function is external (or public) you have the whole message sent to the contract in msg.data.

You can parse it following the solidity abi to determine if incorrect data was submitted.

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.