2

I want to make hash same as generating by solidity contract from web3 or ether's js.

In solidity contract hash generation by below : sha256(abi.encodePacked(no of parameters)).

Kindly help for it. how can i generate using web3 or ether's js same hash.

  • Can you show us what have you tried? – Jaime Apr 25 at 8:56
  • ok. Here i share what i perform to get it. I have solidity contract which has function for generate hash as below : hash = sha256(abi.encodePacked(address(this),existingValuation,ValuationBased,ApprovedFunding,AmountForSpendingOutside)); hash from solidity : 0xbf9445cd5490b7d06a4986b0591d84eee666cebc1fb68b72f0bbfbfe528b5763 – chinmay Apr 25 at 9:47
  • I have javascript code which has function for generate hash as below : var Operationhash = abiLib.soliditySHA3(['address', 'uint', 'bool', 'uint','uint'],[new BN(request.body.contract_address.replace('0x', ''), 16),existingValuation,ValuationBased,ApprovedFunding,AmountForSpendingOutside]); hash = '0x' + Buffer.concat([Operationhash]).toString('hex'); hash from code : 0x06c30e714b1ad43ea087953f5ce9d46c1869e316c4ae21b4b80ab7bbcd80bfc9 – chinmay Apr 25 at 9:47
  • here both has become different for same input value. – chinmay Apr 25 at 9:47
  • Your Solidity code is using sha256, but your JavaScript code is using keccak256. Those are two completely different hash algorithms. Try changing your Solidity code to use hash = keccak256(...) instead. – user19510 Apr 25 at 13:11
0

In my opinion, this is a case where it is worthwhile for a contract to present a "helper" function that will ensure that clients can easily match the contract's hash process. I think it's important to ensure they can do so regardless of the language, and will always be able to do so in the future.

The pattern is simple. Whatever the contract is doing, code it as a public pure function. Use it internally.

This ensures external clients will always be able to match the contract's internal logic.

function hashHelper(address a, bytes32 b, uint c) public pure returns(bytes32) {
  return ... // keccak256(..., sha3( ..., whatever
}

function commit(bytes32 hash) public pure {
  // record committment
}

function reveal (address a, bytes32 b, uint c) public {
  require(hashHelper(a,b,c) == hash, "Invalid Hash inputs");
}

When clients call the pure function, it's the same as them running a hash computation locally and nothing confidential is revealed to the network.

Hope it helps.

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.