Skip to content

Hash

A 32-byte hash as hex: the hash of a block or a transaction, a storage root, a log topic. The module also has Keccak-256.

import { Hash } from "@ethect/core";
// the topic of the ERC-20 Transfer event
const topic = Hash.keccak256Utf8("Transfer(address,address,uint256)");
// "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"
Export Kind Description
Hash type A 32-byte hash as hex.
schema schema Exactly 32 bytes of hex.
keccak256 function Keccak-256 of bytes.
keccak256Utf8 function Keccak-256 of a string, encoded as UTF-8.

The type. It is Hex.Hex under another name, so a string literal that starts with 0x is one.

import { Eth } from "@ethect/core/rpc";
// a made-up transaction hash
const hash = `0x${"ab".repeat(32)}` as const;
const receipt = Eth.getTransactionReceipt(hash);

The type does not check the length. A method that takes a hash checks it with schema before anything is sent.

The schema of a 32-byte hash: Hex.sized(32).

import { Schema } from "effect";
import { Hash } from "@ethect/core";
const decode = Schema.decodeUnknownEffect(Hash.schema);
const ok = decode(`0x${"ab".repeat(32)}`);
// fails: Expected 32 bytes of 0x-prefixed hex
const short = decode("0x1234");
  • Type: Hash.Hash. Encoded: a string.
  • It accepts both letter cases and decodes to lowercase, so two hashes read from a node compare with ===.
  • Use it in your own methods and schemas, for any 32-byte value.

Returns the Keccak-256 hash of bytes.

import { Effect } from "effect";
import { Hash, Hex } from "@ethect/core";
const hash = Hex.toBytes("0xdeadbeef").pipe(
Effect.map(Hash.keccak256),
// "0xd4fd4e189132273036449fc9e11198c739161b4c0116a9a2dccdfa1c492006f1"
);
  • Parameters: bytes (Uint8Array).
  • Returns: Hash.Hash, in lowercase.
  • It takes bytes, not hex. To hash a hex string, decode it first with Hex.toBytes, as above.

Returns the Keccak-256 hash of a string, encoded as UTF-8: the hash of a function or an event signature.

import { Hash } from "@ethect/core";
// the first 4 bytes are the function selector
const selector = Hash.keccak256Utf8("transfer(address,uint256)").slice(0, 10);
// "0xa9059cbb"
  • Parameters: value (string).
  • Returns: Hash.Hash, in lowercase.
  • Keccak-256, not SHA3-256. It is the variant Ethereum uses, from before the standard was final. The hash of the empty string is 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470.
  • Not branded. Unlike an Address, a Hash is a plain hex string, so a literal works where one is expected.
  • Lowercase from a node. A hash in a result is lowercase, as is one from keccak256. Only a hash you wrote by hand can be in another case: lowercase it before comparing.
  • No ABI coding. The two functions hash. They do not encode arguments: Abi.encode does.