const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);
const sha3Example = async () => {
// Hash hex-encoded data via RPC
const hash = await provider.send("web3_sha3", ["0x68656c6c6f"]); // "hello" in hex
console.log(`Hash of 'hello': ${hash}`);
// Compare with ethers.js local computation
const localHash = ethers.keccak256("0x68656c6c6f");
console.log(`Local hash: ${localHash}`);
console.log(`Hashes match: ${hash === localHash}`);
// Compute a function selector
const transferSelector = await provider.send("web3_sha3", [
ethers.toUtf8Bytes("transfer(address,uint256)").reduce(
(hex, byte) => hex + byte.toString(16).padStart(2, '0'),
'0x'
)
]);
console.log(`\nTransfer function selector: ${transferSelector.slice(0, 10)}`);
};
sha3Example();