/** * Tests for ICS23 conversion utilities * Verifies conversion from internal IAVL proof structures to ICS23 format */ import { sha256, } from "@noble/hashes/sha2.js"; import { describe, expect, it, } from "vitest"; import { HashOp, LengthOp, } from "../generated/cosmos/ics23/v1/proofs.js"; import { encodeVarint, } from "../utils/encoding.js"; import { convertInnerOps, convertLeafOp, IavlSpec, } from "./ics23-convert.js"; import { ProofInnerNode, } from "./proof-inner-node.js"; /** * Helper to create a fake hash from a string (for testing) */ function fakeHash(s: string): Uint8Array { return sha256(new TextEncoder().encode(s)); } /** * Helper to concatenate byte arrays */ function concat(...arrays: Uint8Array[]): Uint8Array { const totalLength = arrays.reduce((sum, arr) => sum + arr.length, 0); const result = new Uint8Array(totalLength); let offset = 0; for (const arr of arrays) { result.set(arr, offset); offset += arr.length; } return result; } describe("convertLeafOp", () => { it("should create LeafOp with correct hash operations", () => { const version = 5n; const leafOp = convertLeafOp(version); expect(leafOp.hash).toBe(HashOp.SHA256); expect(leafOp.prehashKey).toBe(HashOp.NO_HASH); expect(leafOp.prehashValue).toBe(HashOp.SHA256); expect(leafOp.length).toBe(LengthOp.VAR_PROTO); }); it("should create LeafOp with correct prefix encoding", () => { const version = 5n; const leafOp = convertLeafOp(version); // Prefix should be: varint(0) + varint(1) + varint(version) const expectedPrefix = concat( encodeVarint(0n), encodeVarint(1n), encodeVarint(version), ); expect(leafOp.prefix).toEqual(expectedPrefix); }); it("should handle different versions correctly", () => { const versions = [ 1n, 10n, 100n, 1000n, 1000000n, ]; for (const version of versions) { const leafOp = convertLeafOp(version); // Verify prefix contains the version const expectedPrefix = concat( encodeVarint(0n), encodeVarint(1n), encodeVarint(version), ); expect(leafOp.prefix).toEqual(expectedPrefix); } }); it("should handle version 0", () => { const leafOp = convertLeafOp(0n); const expectedPrefix = concat( encodeVarint(0n), encodeVarint(1n), encodeVarint(0n), ); expect(leafOp.prefix).toEqual(expectedPrefix); }); }); describe("convertInnerOps", () => { it("should return empty array for empty path", () => { const result = convertInnerOps([]); expect(result).toEqual([]); }); it("should convert single-level path (left traversal)", () => { const rightHash = fakeHash("right"); const path = [new ProofInnerNode(1, 2n, 1n, null, rightHash)]; const ops = convertInnerOps(path); expect(ops).toHaveLength(1); expect(ops[0].hash).toBe(HashOp.SHA256); // Prefix: varint(1) + varint(2) + varint(1) + 0x20 const expectedPrefix = concat( encodeVarint(1n), encodeVarint(2n), encodeVarint(1n), new Uint8Array([0x20]), ); expect(ops[0].prefix).toEqual(expectedPrefix); // Suffix: 0x20 + rightHash const expectedSuffix = concat(new Uint8Array([0x20]), rightHash); expect(ops[0].suffix).toEqual(expectedSuffix); }); it("should convert single-level path (right traversal)", () => { const leftHash = fakeHash("left"); const path = [new ProofInnerNode(1, 2n, 1n, leftHash, null)]; const ops = convertInnerOps(path); expect(ops).toHaveLength(1); expect(ops[0].hash).toBe(HashOp.SHA256); // Prefix: varint(1) + varint(2) + varint(1) + 0x20 + leftHash + 0x20 const expectedPrefix = concat( encodeVarint(1n), encodeVarint(2n), encodeVarint(1n), new Uint8Array([0x20]), leftHash, new Uint8Array([0x20]), ); expect(ops[0].prefix).toEqual(expectedPrefix); // Suffix should be empty for right traversal expect(ops[0].suffix).toHaveLength(0); }); it("should reverse path order (root-to-leaf becomes leaf-to-root)", () => { // Create a path with 3 nodes: root, middle, leaf's parent // All went left (right hash set) const path = [ new ProofInnerNode(3, 8n, 1n, null, fakeHash("r3")), // root new ProofInnerNode(2, 4n, 1n, null, fakeHash("r2")), // middle new ProofInnerNode(1, 2n, 1n, null, fakeHash("r1")), // leaf's parent ]; const ops = convertInnerOps(path); expect(ops).toHaveLength(3); // First op should be from leaf's parent (path[2]) const expectedPrefix0 = concat( encodeVarint(1n), encodeVarint(2n), encodeVarint(1n), new Uint8Array([0x20]), ); expect(ops[0].prefix).toEqual(expectedPrefix0); // Second op should be from middle (path[1]) const expectedPrefix1 = concat( encodeVarint(2n), encodeVarint(4n), encodeVarint(1n), new Uint8Array([0x20]), ); expect(ops[1].prefix).toEqual(expectedPrefix1); // Third op should be from root (path[0]) const expectedPrefix2 = concat( encodeVarint(3n), encodeVarint(8n), encodeVarint(1n), new Uint8Array([0x20]), ); expect(ops[2].prefix).toEqual(expectedPrefix2); }); it("should handle mixed left/right traversal", () => { const path = [new ProofInnerNode(2, 4n, 1n, null, fakeHash("r2")), new ProofInnerNode(1, 2n, 1n, fakeHash("l1"), null)]; const ops = convertInnerOps(path); expect(ops).toHaveLength(2); // First op (from path[1] - went right) expect(ops[0].suffix).toHaveLength(0); // right traversal has empty suffix // Second op (from path[0] - went left) expect(ops[1].suffix.length).toBeGreaterThan(0); // left traversal has suffix }); it("should handle large heights and sizes", () => { const path = [new ProofInnerNode(100, 1000000n, 500000n, null, fakeHash("r"))]; const ops = convertInnerOps(path); expect(ops).toHaveLength(1); // Prefix should contain the large values encoded as varints const expectedPrefixStart = concat( encodeVarint(100n), encodeVarint(1000000n), encodeVarint(500000n), new Uint8Array([0x20]), ); expect(ops[0].prefix.slice(0, expectedPrefixStart.length)).toEqual( expectedPrefixStart, ); }); it("should handle version 0", () => { const path = [new ProofInnerNode(1, 2n, 0n, null, fakeHash("r"))]; const ops = convertInnerOps(path); expect(ops).toHaveLength(1); // Should include version 0 in prefix const expectedPrefixStart = concat( encodeVarint(1n), encodeVarint(2n), encodeVarint(0n), new Uint8Array([0x20]), ); expect(ops[0].prefix.slice(0, expectedPrefixStart.length)).toEqual( expectedPrefixStart, ); }); }); describe("IavlSpec", () => { it("should have correct leaf spec", () => { expect(IavlSpec.leafSpec).toBeDefined(); expect(IavlSpec.leafSpec?.hash).toBe(HashOp.SHA256); expect(IavlSpec.leafSpec?.prehashKey).toBe(HashOp.NO_HASH); expect(IavlSpec.leafSpec?.prehashValue).toBe(HashOp.SHA256); expect(IavlSpec.leafSpec?.length).toBe(LengthOp.VAR_PROTO); expect(IavlSpec.leafSpec?.prefix).toEqual(new Uint8Array([0])); }); it("should have correct inner spec", () => { expect(IavlSpec.innerSpec).toBeDefined(); expect(IavlSpec.innerSpec?.childOrder).toEqual([0, 1]); expect(IavlSpec.innerSpec?.minPrefixLength).toBe(4); expect(IavlSpec.innerSpec?.maxPrefixLength).toBe(12); expect(IavlSpec.innerSpec?.childSize).toBe(33); // 32 + 1 for length byte expect(IavlSpec.innerSpec?.emptyChild).toEqual(new Uint8Array(0)); expect(IavlSpec.innerSpec?.hash).toBe(HashOp.SHA256); }); it("should have correct depth constraints", () => { expect(IavlSpec.maxDepth).toBe(0); // no max expect(IavlSpec.minDepth).toBe(0); // no min }); it("should not prehash keys before comparison", () => { expect(IavlSpec.prehashKeyBeforeComparison).toBe(false); }); }); describe("Integration: LeafOp + InnerOps", () => { it("should produce valid ICS23 structures that match IAVL encoding", () => { // Simulate a 2-level tree with 3 nodes const version = 10n; const leafOp = convertLeafOp(version); // Path from root to leaf (went left at root, then found leaf) const path = [new ProofInnerNode(1, 2n, version, null, fakeHash("right"))]; const innerOps = convertInnerOps(path); // Verify LeafOp structure expect(leafOp.prefix).toHaveLength( encodeVarint(0n).length + encodeVarint(1n).length + encodeVarint(version).length, ); // Verify InnerOp structure expect(innerOps).toHaveLength(1); expect(innerOps[0].prefix).toHaveLength( encodeVarint(1n).length + encodeVarint(2n).length + encodeVarint(version).length + 1, // 0x20 length byte ); expect(innerOps[0].suffix).toHaveLength(33); // 0x20 + 32-byte hash }); it("should handle complex multi-level paths", () => { const version = 5n; // 4-level path (root to leaf's parent) const path = [ new ProofInnerNode(4, 16n, version, null, fakeHash("r4")), // root: left new ProofInnerNode(3, 8n, version, fakeHash("l3"), null), // level 2: right new ProofInnerNode(2, 4n, version, null, fakeHash("r2")), // level 3: left new ProofInnerNode(1, 2n, version, fakeHash("l1"), null), // leaf parent: right ]; const innerOps = convertInnerOps(path); // Should reverse the order expect(innerOps).toHaveLength(4); // Verify each op has required structure for (const op of innerOps) { expect(op.hash).toBe(HashOp.SHA256); expect(op.prefix.length).toBeGreaterThan(0); // At least one of prefix/suffix should include hash data expect(op.prefix.length + op.suffix.length).toBeGreaterThan(10); } }); });