/** * ProofInnerNode represents an inner node in a proof path. * Used internally during proof construction before conversion to ICS23 format. * * Contract: Left and Right can never both be set. Will result in an empty [] roothash. * * Based on Go IAVL v1.3.5 proof.go ProofInnerNode */ import { sha256, } from "@noble/hashes/sha2.js"; import { encodeBytes, encodeVarint, } from "../utils/encoding.js"; export class ProofInnerNode { /** * Height of this inner node in the tree */ readonly height: number; /** * Size (number of leaf nodes) in the subtree rooted at this node */ readonly size: bigint; /** * Version when this node was created */ readonly version: bigint; /** * Hash of the left child (null if we traversed left) */ readonly left: Uint8Array | null; /** * Hash of the right child (null if we traversed right) */ readonly right: Uint8Array | null; constructor( height: number, size: bigint, version: bigint, left: Uint8Array | null, right: Uint8Array | null, ) { this.height = height; this.size = size; this.version = version; this.left = left; this.right = right; } /** * Compute the hash of this inner node given the child hash. * * The hash is computed as: * SHA256(varint(height) || varint(size) || varint(version) || * varint_bytes(left_or_child) || varint_bytes(right_or_child)) * * Where left_or_child is either the left sibling hash or the child hash, * and right_or_child is either the right sibling hash or the child hash. * * @param childHash - The hash of the child node (the path we traversed) * @returns The computed hash * @throws Error if both left and right are set, or on encoding errors */ hash(childHash: Uint8Array): Uint8Array { // Validate contract: both left and right cannot be set if (this.left !== null && this.right !== null) { throw new Error("both left and right child hashes are set"); } // Build the data to hash const data: Uint8Array[] = []; // Encode height, size, version as varints data.push(encodeVarint(BigInt(this.height))); data.push(encodeVarint(this.size)); data.push(encodeVarint(this.version)); // Encode child hashes based on which path we took if (this.left === null) { // We traversed left, so childHash goes first, then right sibling data.push(encodeBytes(childHash)); if (this.right !== null) { data.push(encodeBytes(this.right)); } else { // If right is also null, encode empty bytes data.push(encodeBytes(new Uint8Array(0))); } } else { // We traversed right, so left sibling goes first, then childHash data.push(encodeBytes(this.left)); data.push(encodeBytes(childHash)); } // Concatenate all data const totalLength = data.reduce((sum, arr) => sum + arr.length, 0); const combined = new Uint8Array(totalLength); let offset = 0; for (const arr of data) { combined.set(arr, offset); offset += arr.length; } // Return SHA256 hash return sha256(combined); } /** * String representation for debugging */ toString(): string { return `ProofInnerNode{ Height: ${this.height} Size: ${this.size} Version: ${this.version} Left: ${this.left ? this.toHex(this.left) : "null"} Right: ${this.right ? this.toHex(this.right) : "null"} }`; } private toHex(bytes: Uint8Array): string { return Array.from(bytes) .map(b => b.toString(16).padStart(2, "0")) .join("") .toUpperCase(); } }