0

I have a simple smart contract as below. Using go-ethereum-1.8.6 , when i execute contract.sha.call() it returns the correct hash value. However, contract.getSha256.call() it returns 0x, did I do something wrong?

pragma solidity ^0.4.23;

/* Online compiler https://remix.ethereum.org */

contract test{

    address public owner;

    constructor() public {
        owner = msg.sender;
    }

    function getOwner() external view returns (address) {
        return owner;
    }

    function getSha256() external pure returns (bytes32) {

        bytes32 hash = sha256("12345678");

        return (hash);
    }

    function sha() external pure returns (bytes32) {

        bytes32 hash = sha3("12345678");

        return (hash);
    }

}
5
  • Your code works for me, if I copy/paste it into remix. So there shouldn't be a problem with your function.
    – Fischa7
    Commented Apr 30, 2018 at 8:44
  • Yes exactly, it works on remix debugger. But different story if I deploy it on my private net. If I use an older build (bytecode), it works. But with today remix build, it is not. I suspect there may be a bug in recent remix...
    – Consy
    Commented Apr 30, 2018 at 8:50
  • sha3 is deprecated, so maybe it's related. read more here: github.com/ethereum/web3.js/issues/1183. So anyway it's better to use keccak256.
    – leonprou
    Commented Apr 30, 2018 at 9:09
  • problem is on sha256, sha3 is ok
    – Consy
    Commented Apr 30, 2018 at 9:09
  • 1
    It might be that you run into Out-of-Gas for sha256, ripemd160 or ecrecover on a private blockchain. The reason for this is that those are implemented as so-called precompiled contracts and these contracts only really exist after they received the first message (although their contract code is hardcoded). Messages to non-existing contracts are more expensive and thus the execution runs into an Out-of-Gas error. A workaround for this problem is to first send e.g. 1 Wei to each of the contracts before you use them in your actual contracts. This is not an issue on the official or test net.
    – kherwa
    Commented Apr 30, 2018 at 10:07

0