The output of hash function SHA256 is a hex string of size 64. For example: 3ed54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab
I want to call the function of a smart contract with the following structure:
function register(bytes32 hash)
and pass the hex as an argument.
I use the web3 JavaScript library.
First I tried to pass the argument without any conversion. The result was a truncation of the last 32 bytes
Then I tried web3's fromAscii
function with the same results.
I also tried hexToBytes
and bytesToHex
functions from the 1.x version. hexToBytes
returns a byte array of size 32. When I pass the array to the smart contract function the results are totally different from the expected ones.
So how do I convert a hex of 32 bytes (64 character hex) in a format suitable for a bytes32
function argument ?
I know I can use bytes
or string
instead of bytes32
but I would rather avoid it as the hex of SHA256 is indeed 32 bytes.
EDIT
I create a test contract and test it in remix. I call set
function with input 0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab
. The value is not truncated and I get the expected results.
pragma solidity ^0.4.24;
contract Test {
bytes32 public hash;
function set(bytes32 h) public {
hash = h;
}
}
So, the problem should be in the web3 library or JS itself right ?
UPDATE
I use the truffle-contract
library to call my smart contract's functions