0

Code -

pragma solidity ^0.4.18;

contract CA18 {

address public owner;

function CA18 () {
    owner = msg.sender;
}



mapping(address=>uint16) public balances;

function getBalance() view returns (uint16, bytes32) {
    require(msg.sender == owner);
    uint16 mybalance = 1000;
    bytes32 c = keccak256(owner);
    return (mybalance, c);
}



}

> con.getBalance()
[1000, "0x0679d661b585d1b4f0c3fd18f943f19e7fa9777fe23cb96b94f469c20d6f0bd5"]
> web3.sha3(eth.coinbase)
"0x3ba2c5fae04972f5960bd8d1b7f098bf7d4c844da67accd23da00f62e4910fc0"
> con.owner()
"0x46fb9a22689c4a4bfb494baeafbb8b2993725305"
> web3.sha3(con.owner())
"0x3ba2c5fae04972f5960bd8d1b7f098bf7d4c844da67accd23da00f62e4910fc0"

Web3.sha is not the same as keccak256(owner).

The docs state -

sha3(...) returns (bytes32):
alias to keccak256

The outputs show sha3 to be different keccak256. Should they be the same?

(con is the contract instance.)

I just added sha3 for the owner and here is the output -

> con.getBalance()
[1000, "0x0679d661b585d1b4f0c3fd18f943f19e7fa9777fe23cb96b94f469c20d6f0bd5", "0x0679d661b585d1b4f0c3fd18f943f19e7fa9777fe23cb96b94f469c20d6f0bd5"]

function getBalance() view returns (uint16, bytes32, bytes32) {
    require(msg.sender == owner);
    uint16 mybalance = 1000;
    bytes32 c = keccak256(owner);
    bytes32 d = sha3(owner);
    return (mybalance, c, d);
}

Hence the difference is due to web3 and solidity. Has anyone any explanation about this since sha3 is used in the addressing algorithms for storage and they work, and they are also based on keccak256.

2
  • This is related to input types I just saw. Mar 3, 2018 at 10:32
  • Just to clarify about the docs... the docs you pointed to were for Solidity. In Solidity, sha3 and keccak256 should be exactly the same. Comparing to web3.js's sha3, though, will reveal a number of differences.
    – user19510
    Mar 3, 2018 at 11:42

1 Answer 1

1
> web3.sha3(con.owner(),  {encoding:"hex"})
"0x0679d661b585d1b4f0c3fd18f943f19e7fa9777fe23cb96b94f469c20d6f0bd5"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.