CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-ethersproject--hash

Hash utility functions for Ethereum development including ENS domain hashing, message hashing, and EIP-712 typed data encoding.

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

@ethersproject/hash

Hash utility functions for Ethereum development including ENS (Ethereum Name Service) domain hashing with namehash and DNS encoding, message hashing for signature verification, typed data encoding for EIP-712 structured data signing, and string-to-hash conversion utilities. Essential for Ethereum applications requiring cryptographic hash operations, ENS domain resolution, message signing workflows, and structured data hashing.

Package Information

  • Package Name: @ethersproject/hash
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @ethersproject/hash

Core Imports

import { 
  id, 
  namehash, 
  dnsEncode, 
  isValidName, 
  ensNormalize, 
  hashMessage, 
  messagePrefix, 
  _TypedDataEncoder 
} from "@ethersproject/hash";

For CommonJS:

const { 
  id, 
  namehash, 
  dnsEncode, 
  isValidName, 
  ensNormalize, 
  hashMessage, 
  messagePrefix, 
  _TypedDataEncoder 
} = require("@ethersproject/hash");

Basic Usage

import { id, namehash, hashMessage, _TypedDataEncoder } from "@ethersproject/hash";

// Hash a string to get an identifier
const stringHash = id("Hello World");
console.log(stringHash); // 0x592fa743889fc7f92ac2a37bb1f5ba1daf2a5c84741ca0e0061d243a2e6707ba

// Generate ENS namehash for domain resolution
const ensHash = namehash("vitalik.eth");
console.log(ensHash); // 0xee6c4522aab0003e8d14cd40a6af439055fd2577951148c14b6cea9a53475835

// Hash a message for signing
const msgHash = hashMessage("Sign this message");
console.log(msgHash); // Ethereum signed message hash

// Create typed data encoder for EIP-712
const encoder = _TypedDataEncoder.from({
  Person: [
    { name: "name", type: "string" },
    { name: "wallet", type: "address" }
  ]
});

Capabilities

String Hashing

Computes the Keccak256 hash of a UTF-8 string, commonly used for generating Ethereum identifiers and function selectors.

/**
 * Computes the Keccak256 hash of a UTF-8 string
 * @param text - The string to hash
 * @returns The keccak256 hash as a hex string
 */
function id(text: string): string;

ENS Domain Hashing

Computes the ENS namehash of a domain name according to EIP-137 specification for Ethereum Name Service operations.

/**
 * Computes the ENS namehash of a domain name according to EIP-137
 * @param name - The domain name to hash (e.g., "vitalik.eth")
 * @returns The namehash as a hex string
 */
function namehash(name: string): string;

DNS Encoding

Encodes a domain name in DNS wire format for ENS compatibility and domain name resolution. Components longer than 63 bytes will cause an error.

/**
 * Encodes a domain name in DNS wire format for ENS
 * @param name - The domain name to encode
 * @returns The DNS-encoded domain as a hex string
 * @throws Error if any component exceeds 63 bytes in length
 */
function dnsEncode(name: string): string;

ENS Name Validation

Validates whether a string is a valid ENS domain name according to normalization rules.

/**
 * Validates whether a string is a valid ENS domain name
 * @param name - The domain name to validate
 * @returns True if the name is valid, false otherwise
 */
function isValidName(name: string): boolean;

ENS Name Normalization

Normalizes an ENS domain name according to ENSIP-15 specification for consistent domain handling. Throws an error if the name contains invalid components.

/**
 * Normalizes an ENS domain name according to ENSIP-15
 * @param name - The domain name to normalize
 * @returns The normalized domain name
 * @throws Error if the name contains invalid or empty components
 */
function ensNormalize(name: string): string;

Message Hashing

Hashes a message according to Ethereum Signed Message standard (EIP-191) for signature verification workflows.

/**
 * Standard prefix for Ethereum signed messages
 */
const messagePrefix = "\x19Ethereum Signed Message:\n";

/**
 * Hashes a message according to Ethereum Signed Message standard (EIP-191)
 * @param message - The message to hash (string or bytes)
 * @returns The message hash as a hex string
 */
function hashMessage(message: Bytes | string): string;

EIP-712 Typed Data Encoding

Complete encoder implementation for EIP-712 typed structured data signing, enabling secure off-chain signatures with on-chain verification.

/**
 * Encoder for EIP-712 typed structured data
 */
class _TypedDataEncoder {
  readonly primaryType: string;
  readonly types: Record<string, Array<TypedDataField>>;

  /**
   * Creates a new TypedDataEncoder instance
   * @param types - The type definitions for structured data
   */
  constructor(types: Record<string, Array<TypedDataField>>);

  /**
   * Get encoder function for a specific type
   * @param type - The type name to get encoder for
   * @returns Encoder function for the specified type
   */
  getEncoder(type: string): (value: any) => string;

  /**
   * Encode a type definition string
   * @param name - The type name to encode
   * @returns The encoded type string
   */
  encodeType(name: string): string;

  /**
   * Encode data for a specific type
   * @param type - The type name
   * @param value - The value to encode
   * @returns The encoded data as hex string
   */
  encodeData(type: string, value: any): string;

  /**
   * Hash a struct according to EIP-712
   * @param name - The struct type name
   * @param value - The struct value
   * @returns The struct hash as hex string
   */
  hashStruct(name: string, value: Record<string, any>): string;

  /**
   * Encode the primary type value
   * @param value - The value to encode
   * @returns The encoded value as hex string
   */
  encode(value: Record<string, any>): string;

  /**
   * Hash the primary type value
   * @param value - The value to hash
   * @returns The value hash as hex string
   */
  hash(value: Record<string, any>): string;

  /**
   * Visit all values in the structure with a callback
   * @param value - The structured data to visit
   * @param callback - Function called for each value
   * @returns The transformed data structure
   */
  visit(value: Record<string, any>, callback: (type: string, data: any) => any): any;

  /**
   * Create encoder instance from type definitions
   * @param types - The type definitions
   * @returns New TypedDataEncoder instance
   */
  static from(types: Record<string, Array<TypedDataField>>): _TypedDataEncoder;

  /**
   * Get the primary type from type definitions
   * @param types - The type definitions
   * @returns The primary type name
   */
  static getPrimaryType(types: Record<string, Array<TypedDataField>>): string;

  /**
   * Hash a struct with given types and value
   * @param name - The struct type name
   * @param types - The type definitions
   * @param value - The struct value
   * @returns The struct hash as hex string
   */
  static hashStruct(name: string, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): string;

  /**
   * Hash EIP-712 domain separator
   * @param domain - The domain object
   * @returns The domain hash as hex string
   */
  static hashDomain(domain: TypedDataDomain): string;

  /**
   * Encode typed data according to EIP-712
   * @param domain - The domain object
   * @param types - The type definitions
   * @param value - The structured data value
   * @returns The encoded typed data as hex string
   */
  static encode(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): string;

  /**
   * Hash typed data according to EIP-712
   * @param domain - The domain object
   * @param types - The type definitions
   * @param value - The structured data value
   * @returns The typed data hash as hex string
   */
  static hash(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): string;

  /**
   * Resolve ENS names to addresses in typed data
   * @param domain - The domain object
   * @param types - The type definitions
   * @param value - The structured data value
   * @param resolveName - Function to resolve ENS names
   * @returns Promise resolving to domain and value with resolved addresses
   */
  static async resolveNames(
    domain: TypedDataDomain, 
    types: Record<string, Array<TypedDataField>>, 
    value: Record<string, any>, 
    resolveName: (name: string) => Promise<string>
  ): Promise<{ domain: TypedDataDomain; value: any }>;

  /**
   * Get formatted payload for signing
   * @param domain - The domain object
   * @param types - The type definitions
   * @param value - The structured data value
   * @returns The formatted payload object
   */
  static getPayload(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, value: Record<string, any>): any;
}

Types

/**
 * Type definition for structured data fields
 */
interface TypedDataField {
  name: string;
  type: string;
}

/**
 * EIP-712 domain separator fields
 */
interface TypedDataDomain {
  name?: string;
  version?: string;
  chainId?: BigNumberish;
  verifyingContract?: string;
  salt?: BytesLike;
}

/**
 * Bytes type for binary data
 */
type Bytes = ArrayLike<number>;

/**
 * Types that can be converted to bytes
 */
type BytesLike = Bytes | string;

/**
 * Types that can be converted to BigNumber
 */
type BigNumberish = Bytes | string | number;
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/@ethersproject/hash@5.8.x
Publish Source
CLI
Badge
tessl/npm-ethersproject--hash badge