You can use web3.utils.soliditySha3()
Alternatively you can try this way:
web3.sha3(web3.utils.toHex(string1) + address1, {encoding:"hex"});
Note that address1
should not be prefixed with 0x.
Example:
> web3.sha3(web3.utils.toHex("test1") + "0AbdAce70D3790235af448C88547603b945604ea", {encoding:"hex"});
"0xd7ced52b610c8794965f5bb365f280999a9790b147bd0dfc70dea6edb2616c15"
Produces the same result as this code in Solidity:
keccak256(abi.encodePacked("test1", 0x0AbdAce70D3790235af448C88547603b945604ea));
Does anyone know how to make web3.sha3(...args), with any arguments (uint, address, string, etc) exactly equal to keccak256 in Solidity?
You need to make sure that the 1st argument that you pass to web3.sha3() function is equal to the tightly packed arguments for keccak256 function in Solidity. The easiest way is to use web3.toHex() for each argument (except addresses as they are already in hex), then concatenate the results and set encoding to hex in the options for web3.sha3()
as I've shown in the example.