or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

hdnode-class.mdindex.mdmnemonic-operations.mdutility-functions.md
tile.json

hdnode-class.mddocs/

HDNode Class

Core hierarchical deterministic node implementation providing BIP32 key derivation, wallet management, and extended key operations for Ethereum and other cryptocurrencies.

Capabilities

HDNode Construction

Creates HDNode instances from various sources including mnemonic phrases, seeds, and extended keys.

/**
 * Create HDNode from BIP39 mnemonic phrase
 * @param mnemonic - BIP39 mnemonic phrase
 * @param password - Optional passphrase for seed derivation
 * @param wordlist - Optional wordlist for mnemonic validation
 * @returns HDNode instance at root level (m)
 */
static fromMnemonic(mnemonic: string, password?: string, wordlist?: string | Wordlist): HDNode;

/**
 * Create HDNode from seed bytes
 * @param seed - Seed bytes (16-64 bytes)
 * @returns HDNode instance at root level
 */
static fromSeed(seed: BytesLike): HDNode;

/**
 * Create HDNode from BIP32 extended key
 * @param extendedKey - BIP32 extended key (xpriv/xpub format)
 * @returns HDNode instance at the extended key's level
 */
static fromExtendedKey(extendedKey: string): HDNode;

Usage Examples:

import { HDNode } from "@ethersproject/hdnode";

// From mnemonic
const mnemonic = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about";
const nodeFromMnemonic = HDNode.fromMnemonic(mnemonic);

// From mnemonic with password
const nodeWithPassword = HDNode.fromMnemonic(mnemonic, "mypassword");

// From seed
const seed = "0x3c6d2ac5..."; // 64-byte hex string
const nodeFromSeed = HDNode.fromSeed(seed);

// From extended key
const xpriv = "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi";
const nodeFromXpriv = HDNode.fromExtendedKey(xpriv);

Key Derivation

Derives child keys using BIP32 hierarchical paths with support for hardened and non-hardened derivation.

/**
 * Derive child node from BIP32 path
 * @param path - BIP32 derivation path (e.g., "m/44'/60'/0'/0/0")
 * @returns Derived HDNode instance
 */
derivePath(path: string): HDNode;

Usage Examples:

const rootNode = HDNode.fromMnemonic(mnemonic);

// Standard Ethereum account derivation
const account0 = rootNode.derivePath("m/44'/60'/0'/0/0");
const account1 = rootNode.derivePath("m/44'/60'/0'/0/1");

// Hardened derivation
const hardenedNode = rootNode.derivePath("m/44'/60'/0'");

// Multiple levels
const deepNode = rootNode.derivePath("m/44'/60'/0'/0/0/1/2");

Key Management

Operations for managing keys including creating public-only versions and accessing extended keys.

/**
 * Create neutered (public-only) version of HDNode
 * @returns HDNode with private key removed
 */
neuter(): HDNode;

/**
 * Get BIP32 extended key representation
 * @returns Extended key string (xpriv if private key present, xpub otherwise)
 */
get extendedKey(): string;

Usage Examples:

const privateNode = HDNode.fromMnemonic(mnemonic);

// Create public-only version
const publicNode = privateNode.neuter();
console.log(publicNode.privateKey); // null
console.log(publicNode.publicKey); // Still available

// Get extended keys
const xpriv = privateNode.extendedKey; // Starts with 'xprv'
const xpub = publicNode.extendedKey;   // Starts with 'xpub'

// Public nodes can still derive non-hardened children
const childPublic = publicNode.derivePath("0/1");

Properties

Key Properties

class HDNode {
  /** Private key in hex format (null for neutered nodes) */
  readonly privateKey: string | null;
  
  /** Compressed public key in hex format */
  readonly publicKey: string;
  
  /** Ethereum address derived from public key */
  readonly address: string;
}

Derivation Properties

class HDNode {
  /** BIP32 derivation path (e.g., "m/44'/60'/0'/0/0") */
  readonly path: string;
  
  /** Chain code for key derivation */
  readonly chainCode: string;
  
  /** Child index in derivation (0 for root) */
  readonly index: number;
  
  /** Depth in derivation tree (0 for root) */
  readonly depth: number;
}

Fingerprint Properties

class HDNode {
  /** 4-byte fingerprint of this node's public key */
  readonly fingerprint: string;
  
  /** 4-byte fingerprint of parent node's public key */
  readonly parentFingerprint: string;
}

Mnemonic Properties

class HDNode {
  /** Mnemonic information (only present for nodes created from mnemonic) */
  readonly mnemonic?: Mnemonic;
}

interface Mnemonic {
  /** The mnemonic phrase */
  readonly phrase: string;
  /** The derivation path for this node */
  readonly path: string;
  /** The language locale of the mnemonic */
  readonly locale: string;
}

Error Conditions

The HDNode class throws errors in the following situations:

  • Invalid mnemonic: Mnemonic phrase has wrong length or invalid checksum
  • Invalid path: Derivation path format is incorrect
  • Invalid extended key: Extended key format is malformed
  • Hardened derivation from neutered node: Attempting to derive hardened children from public-only nodes
  • Invalid seed: Seed length is outside 16-64 byte range
  • Invalid index: Child index exceeds maximum value (2^31 - 1)

Error Handling Examples:

try {
  const node = HDNode.fromMnemonic("invalid mnemonic");
} catch (error) {
  console.log("Invalid mnemonic:", error.message);
}

try {
  const publicNode = rootNode.neuter();
  const hardenedChild = publicNode.derivePath("0'"); // Throws error
} catch (error) {
  console.log("Cannot derive hardened child from neutered node");
}