Strange line in Solidty docs

I was looking at the example contract in the solidity docs here: https://solidity.readthedocs.io/en/v0.5.3/contracts.html (the first one with the ‘OwnedToken’ contract in it.)

If you scroll down to the bottom of that example there is this “isTokenTransferOK” function and the the body of that function is:

// Check an arbitrary condition to see if transfer should proceed
return keccak256(abi.encodePacked(currentOwner, newOwner))[0] == 0x7f;

I’m rather confused by this line. I get what the keccak function does and I basically get what abi.encodePacked does, but what is the “[0]” at the end and why would all of that equal the hex code 0x7f?

The notes say this is an arbitrary condition, so not sure if that means this is nonsense anyway or what.

Any help here would be great.

Anyone have info on this?

Would be nice to get a response from someone.

In the example the author just wants to check if the first part of the hash equals to 0x7f;
To do this you can return the hash of [0] as they show in the example.

You can open remix and run this to see it yourself.

pragma solidity 0.5.12;

contract test {
    
    function a () public view returns (bytes32) {
        return keccak256(abi.encodePacked(msg.sender)) [0];
    }
}

cheers,
Dani

1 Like

I’m still not getting it. What is this zero in brackets? Brackets typically indicate an array.

Why is it the hashof zero? The keccak function has an abi.encodepacked within it and (in the example I gave you) two addresses within that. It would be helpful (if you would be so kind) if you could break down what each part of this statement does in order. Then I would get it.

Hey @CryptoEatsTheWorld

What you said is correct.

bytes in Solidity represents a dynamic array of bytes. It’s a shorthand for byte[] .

Solidity presents two type of bytes types :

  • fixed-sized byte arrays
  • Dynamically-sized byte arrays.

You can define a variables by using the keyword bytesX where X represents the sequence of bytes. X can be from 1 up to 32

byte is an alias for bytes1 and therefore stores a single byte.




Why is it the hashof zero? The keccak function has an abi.encodepacked within it and (in the example I gave you) two addresses within that.

Wha they are doing in the example is just hashing two addresses together, then returning a boolean.

If addressHashed [0] == ‘0x…’ return true

Cheers,
Dani

1 Like

OK, I didn’t realize that was the format of the output of the keccack function - bytes and that this is an array. Thanks for that explanation.

My still lingering questions now are: WHY would anyone use this as a check in the example contract I linked to above to determine whether to proceed with a transfer? I don’t get the value of doing this.

WHY would anyone use this as a check in the example contract I linked to above to determine whether to proceed with a transfer?

There is not a reason, it is just the way they decided to build the contract.
Different contracts require different evaluation methods, I never used this one to be honest. Everything depends on how you decide to build your smart contract.

OK, but what I mean is what does one actually learn by using this method?