web3_sha3 - Optimism

This method computes and returns the Keccak-256 hash (NOT standard SHA3-256) of the input data. Keccak-256 is the hash function used throughout Ethereum and Optimism to compute addresses, transaction hashes, and other cryptographic values. The input must be a hexadecimal string.

Parameters

ParameterTypeDescriptionRequired
dataDATAThe data to hash, as a hexadecimal string.Yes

Request Sample

cURL

bashcurl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "web3_sha3",
    "params": ["0x68656c6c6f"],
    "id": "getblock.io"
}'

JavaScript (Axios)

javascriptconst axios = require('axios');

const url = 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/';

// "hello world" in hex
const payload = {
    jsonrpc: '2.0',
    method: 'web3_sha3',
    params: ['0x68656c6c6f'],
    id: 'getblock.io'
};

axios.post(url, payload, {
    headers: { 'Content-Type': 'application/json' }
})
.then(response => console.log('Hash:', response.data.result))
.catch(error => console.error(error));

Python

pythonimport requests

url = "https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"

payload = {
    "jsonrpc": "2.0",
    "method": "web3_sha3",
    "params": ["0x68656c6c6f"],
    "id": "getblock.io"
}

response = requests.post(url, headers={"Content-Type": "application/json"}, json=payload)
print(f"Hash: {response.json()['result']}")

Rust

rustuse reqwest::Client;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    let payload = json!({
        "jsonrpc": "2.0",
        "method": "web3_sha3",
        "params": ["0x68656c6c6f"],
        "id": "getblock.io"
    });

    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&payload)
        .send()
        .await?;

    let result: serde_json::Value = response.json().await?;
    println!("{:#?}", result);
    
    Ok(())
}

Response

A successful response returns the following:

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": "0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8"
}

Response Parameters

  • id: A unique request identifier, matching the id sent in the request body.
  • jsonrpc: Specifies the use of JSON-RPC version 2.0.
  • result: The Keccak-256 hash of the input data as a 32-byte hexadecimal string.

Use Case

The web3_sha3 method is commonly used for:

  • Data hashing
  • Signature verification
  • Address computation
  • Cryptographic operations

Error Handling

Error CodeDescription
-32602Invalid params - malformed hex
-32603Internal error

Web3 Integration

Ethers.js

javascriptconst { ethers } = require('ethers');

// Compute keccak256 locally
const hash = ethers.keccak256(ethers.toUtf8Bytes('hello world'));
console.log('Hash:', hash);

Viem

javascriptimport { keccak256, toHex } from 'viem';

const hash = keccak256(toHex('hello world'));
console.log('Hash:', hash);