/** * ICS23 Conversion Utilities * * Converts internal IAVL proof structures (ProofInnerNode, ProofLeafNode, PathToLeaf) * to ICS23 format (LeafOp, InnerOp, ExistenceProof, etc.). * * ICS23 (Inter-Chain Standards 23) is a standard format for Merkle proofs used * across Cosmos SDK chains and other blockchain systems. * * Based on Go IAVL v1.3.5 proof_ics23.go lines 115-175 */ import type { InnerOp, LeafOp, ProofSpec, } from "../generated/cosmos/ics23/v1/proofs.js"; import { HashOp as HashOpEnum, LengthOp as LengthOpEnum, } from "../generated/cosmos/ics23/v1/proofs.js"; import { encodeVarint, } from "../utils/encoding.js"; import type { PathToLeaf, } from "./path-to-leaf.js"; /** * Length prefix byte for SHA256 hashes (32 bytes). * This is prepended to each hash in ICS23 InnerOps. */ const LENGTH_BYTE_32 = 0x20; /** * convertLeafOp creates an ICS23 LeafOp with IAVL-specific encoding. * * The LeafOp defines how to hash a leaf node: * - Prefix contains: varint(height=0) + varint(size=1) + varint(version) * - Hash: SHA256 (applied to the entire leaf structure) * - PrehashValue: SHA256 (values are pre-hashed before encoding) * - PrehashKey: NO_HASH (keys are not pre-hashed) * - Length: VAR_PROTO (protobuf varint encoding for lengths) * * The leaf hash is computed as: * SHA256(prefix + varint(keyLen) + key + varint(valueHashLen) + valueHash) * * Based on Go IAVL v1.3.5 proof_ics23.go lines 115-128 * * @param version - The version when the leaf was created * @returns ICS23 LeafOp for IAVL leaf nodes */ export function convertLeafOp(version: bigint): LeafOp { // Build prefix: varint(0) + varint(1) + varint(version) // These match the encoding in ProofLeafNode.hash() const prefix: Uint8Array[] = []; prefix.push(encodeVarint(0n)); // height = 0 for leaf prefix.push(encodeVarint(1n)); // size = 1 for leaf prefix.push(encodeVarint(version)); // version // Concatenate all prefix parts const totalLength = prefix.reduce((sum, arr) => sum + arr.length, 0); const prefixBytes = new Uint8Array(totalLength); let offset = 0; for (const arr of prefix) { prefixBytes.set(arr, offset); offset += arr.length; } return { hash: HashOpEnum.SHA256, prehashKey: HashOpEnum.NO_HASH, // keys are not pre-hashed prehashValue: HashOpEnum.SHA256, // values ARE pre-hashed (sha256(value)) length: LengthOpEnum.VAR_PROTO, // use protobuf varint for length encoding prefix: prefixBytes, }; } /** * convertInnerOps converts a PathToLeaf to an array of ICS23 InnerOps. * * IMPORTANT: The path is reversed during conversion because: * - IAVL PathToLeaf is ordered ROOT to LEAF (path[0] = root) * - ICS23 expects LEAF to ROOT order (path[0] = leaf's parent) * * For each ProofInnerNode, we create an InnerOp with: * - Prefix: varint(height) + varint(size) + varint(version) + sibling_hash + 0x20 * - Suffix: optional (only if right traversal) * - Hash: SHA256 * * The encoding depends on which direction we traversed: * - If left is set (we went right): * - Prefix = varints + 0x20 + left_hash + 0x20 * - Suffix = empty * - Structure: prefix + child_hash + suffix * - Results in: varints + 0x20 + left_hash + 0x20 + child_hash * - If right is set (we went left): * - Prefix = varints + 0x20 * - Suffix = 0x20 + right_hash * - Structure: prefix + child_hash + suffix * - Results in: varints + 0x20 + child_hash + 0x20 + right_hash * * Based on Go IAVL v1.3.5 proof_ics23.go lines 130-170 * * @param path - PathToLeaf from root to leaf * @returns Array of ICS23 InnerOps in leaf-to-root order */ export function convertInnerOps(path: PathToLeaf): InnerOp[] { const steps: InnerOp[] = []; // Reverse iteration: IAVL path is root→leaf, ICS23 expects leaf→root for (let i = path.length - 1; i >= 0; i--) { const node = path[i]; // Build prefix: varint(height) + varint(size) + varint(version) const prefixParts: Uint8Array[] = []; prefixParts.push(encodeVarint(BigInt(node.height))); prefixParts.push(encodeVarint(node.size)); prefixParts.push(encodeVarint(node.version)); let suffix: Uint8Array = new Uint8Array(0); if (node.left !== null) { // We went right, so left sibling is in the proof // Prefix: varints + 0x20 + left_hash + 0x20 // The final 0x20 is the length prefix for the child hash prefixParts.push(new Uint8Array([LENGTH_BYTE_32])); prefixParts.push(node.left); prefixParts.push(new Uint8Array([LENGTH_BYTE_32])); } else { // We went left, so right sibling is in the proof // Prefix: varints + 0x20 (length prefix for child) // Suffix: 0x20 + right_hash prefixParts.push(new Uint8Array([LENGTH_BYTE_32])); // Build suffix: 0x20 + right_hash suffix = new Uint8Array(1 + (node.right?.length ?? 0)); suffix[0] = LENGTH_BYTE_32; if (node.right) { suffix.set(node.right, 1); } } // Concatenate all prefix parts const totalPrefixLength = prefixParts.reduce( (sum, arr) => sum + arr.length, 0, ); const prefix = new Uint8Array(totalPrefixLength); let offset = 0; for (const arr of prefixParts) { prefix.set(arr, offset); offset += arr.length; } steps.push({ hash: HashOpEnum.SHA256, prefix, suffix, }); } return steps; } /** * IAVL ProofSpec defines the validation rules for IAVL Merkle proofs. * * This matches the ics23.IavlSpec from the Go ics23 library. * * LeafSpec: * - Prefix: [0] (placeholder, actual prefix includes version varints) * - PrehashKey: NO_HASH (keys are not pre-hashed) * - Hash: SHA256 * - PrehashValue: SHA256 (values are pre-hashed) * - Length: VAR_PROTO (protobuf varint encoding) * * InnerSpec: * - ChildOrder: [0, 1] (binary tree: left=0, right=1) * - MinPrefixLength: 4 (minimum: height(1) + size(1) + version(1) + lengthByte(1)) * - MaxPrefixLength: 12 (varints can be up to 10 bytes each for height/size/version) * - ChildSize: 33 (32-byte hash + 1-byte length prefix) * - EmptyChild: null (IAVL doesn't use placeholder values for missing children) * - Hash: SHA256 * * Based on Go ics23 proof.go IavlSpec definition */ export const IavlSpec: ProofSpec = { leafSpec: { hash: HashOpEnum.SHA256, prehashKey: HashOpEnum.NO_HASH, prehashValue: HashOpEnum.SHA256, length: LengthOpEnum.VAR_PROTO, prefix: new Uint8Array([0]), // placeholder, actual varies by version }, innerSpec: { childOrder: [0, 1], // binary tree: left, right minPrefixLength: 4, maxPrefixLength: 12, childSize: 33, // 32-byte hash + 1-byte length prefix emptyChild: new Uint8Array(0), // no empty children in IAVL hash: HashOpEnum.SHA256, }, maxDepth: 0, // no depth limit minDepth: 0, // no minimum depth prehashKeyBeforeComparison: false, };