{"version":3,"file":"ics23-convert.cjs","names":["prefix: Uint8Array[]","encodeVarint","HashOpEnum","LengthOpEnum","steps: InnerOp[]","prefixParts: Uint8Array[]","suffix: Uint8Array","IavlSpec: ProofSpec"],"sources":["../../src/proof/ics23-convert.ts"],"sourcesContent":["/**\n * ICS23 Conversion Utilities\n *\n * Converts internal IAVL proof structures (ProofInnerNode, ProofLeafNode, PathToLeaf)\n * to ICS23 format (LeafOp, InnerOp, ExistenceProof, etc.).\n *\n * ICS23 (Inter-Chain Standards 23) is a standard format for Merkle proofs used\n * across Cosmos SDK chains and other blockchain systems.\n *\n * Based on Go IAVL v1.3.5 proof_ics23.go lines 115-175\n */\n\nimport type {\n  InnerOp,\n  LeafOp,\n  ProofSpec,\n} from \"../generated/cosmos/ics23/v1/proofs.js\";\nimport {\n  HashOp as HashOpEnum,\n  LengthOp as LengthOpEnum,\n} from \"../generated/cosmos/ics23/v1/proofs.js\";\nimport {\n  encodeVarint,\n} from \"../utils/encoding.js\";\nimport type {\n  PathToLeaf,\n} from \"./path-to-leaf.js\";\n\n/**\n * Length prefix byte for SHA256 hashes (32 bytes).\n * This is prepended to each hash in ICS23 InnerOps.\n */\nconst LENGTH_BYTE_32 = 0x20;\n\n/**\n * convertLeafOp creates an ICS23 LeafOp with IAVL-specific encoding.\n *\n * The LeafOp defines how to hash a leaf node:\n * - Prefix contains: varint(height=0) + varint(size=1) + varint(version)\n * - Hash: SHA256 (applied to the entire leaf structure)\n * - PrehashValue: SHA256 (values are pre-hashed before encoding)\n * - PrehashKey: NO_HASH (keys are not pre-hashed)\n * - Length: VAR_PROTO (protobuf varint encoding for lengths)\n *\n * The leaf hash is computed as:\n * SHA256(prefix + varint(keyLen) + key + varint(valueHashLen) + valueHash)\n *\n * Based on Go IAVL v1.3.5 proof_ics23.go lines 115-128\n *\n * @param version - The version when the leaf was created\n * @returns ICS23 LeafOp for IAVL leaf nodes\n */\nexport function convertLeafOp(version: bigint): LeafOp {\n  // Build prefix: varint(0) + varint(1) + varint(version)\n  // These match the encoding in ProofLeafNode.hash()\n  const prefix: Uint8Array[] = [];\n  prefix.push(encodeVarint(0n)); // height = 0 for leaf\n  prefix.push(encodeVarint(1n)); // size = 1 for leaf\n  prefix.push(encodeVarint(version)); // version\n\n  // Concatenate all prefix parts\n  const totalLength = prefix.reduce((sum, arr) => sum + arr.length, 0);\n  const prefixBytes = new Uint8Array(totalLength);\n  let offset = 0;\n  for (const arr of prefix) {\n    prefixBytes.set(arr, offset);\n    offset += arr.length;\n  }\n\n  return {\n    hash: HashOpEnum.SHA256,\n    prehashKey: HashOpEnum.NO_HASH, // keys are not pre-hashed\n    prehashValue: HashOpEnum.SHA256, // values ARE pre-hashed (sha256(value))\n    length: LengthOpEnum.VAR_PROTO, // use protobuf varint for length encoding\n    prefix: prefixBytes,\n  };\n}\n\n/**\n * convertInnerOps converts a PathToLeaf to an array of ICS23 InnerOps.\n *\n * IMPORTANT: The path is reversed during conversion because:\n * - IAVL PathToLeaf is ordered ROOT to LEAF (path[0] = root)\n * - ICS23 expects LEAF to ROOT order (path[0] = leaf's parent)\n *\n * For each ProofInnerNode, we create an InnerOp with:\n * - Prefix: varint(height) + varint(size) + varint(version) + sibling_hash + 0x20\n * - Suffix: optional (only if right traversal)\n * - Hash: SHA256\n *\n * The encoding depends on which direction we traversed:\n * - If left is set (we went right):\n *   - Prefix = varints + 0x20 + left_hash + 0x20\n *   - Suffix = empty\n *   - Structure: prefix + child_hash + suffix\n *   - Results in: varints + 0x20 + left_hash + 0x20 + child_hash\n * - If right is set (we went left):\n *   - Prefix = varints + 0x20\n *   - Suffix = 0x20 + right_hash\n *   - Structure: prefix + child_hash + suffix\n *   - Results in: varints + 0x20 + child_hash + 0x20 + right_hash\n *\n * Based on Go IAVL v1.3.5 proof_ics23.go lines 130-170\n *\n * @param path - PathToLeaf from root to leaf\n * @returns Array of ICS23 InnerOps in leaf-to-root order\n */\nexport function convertInnerOps(path: PathToLeaf): InnerOp[] {\n  const steps: InnerOp[] = [];\n\n  // Reverse iteration: IAVL path is root→leaf, ICS23 expects leaf→root\n  for (let i = path.length - 1; i >= 0; i--) {\n    const node = path[i];\n\n    // Build prefix: varint(height) + varint(size) + varint(version)\n    const prefixParts: Uint8Array[] = [];\n    prefixParts.push(encodeVarint(BigInt(node.height)));\n    prefixParts.push(encodeVarint(node.size));\n    prefixParts.push(encodeVarint(node.version));\n\n    let suffix: Uint8Array = new Uint8Array(0);\n\n    if (node.left !== null) {\n      // We went right, so left sibling is in the proof\n      // Prefix: varints + 0x20 + left_hash + 0x20\n      // The final 0x20 is the length prefix for the child hash\n      prefixParts.push(new Uint8Array([LENGTH_BYTE_32]));\n      prefixParts.push(node.left);\n      prefixParts.push(new Uint8Array([LENGTH_BYTE_32]));\n    }\n    else {\n      // We went left, so right sibling is in the proof\n      // Prefix: varints + 0x20 (length prefix for child)\n      // Suffix: 0x20 + right_hash\n      prefixParts.push(new Uint8Array([LENGTH_BYTE_32]));\n\n      // Build suffix: 0x20 + right_hash\n      suffix = new Uint8Array(1 + (node.right?.length ?? 0));\n      suffix[0] = LENGTH_BYTE_32;\n      if (node.right) {\n        suffix.set(node.right, 1);\n      }\n    }\n\n    // Concatenate all prefix parts\n    const totalPrefixLength = prefixParts.reduce(\n      (sum, arr) => sum + arr.length,\n      0,\n    );\n    const prefix = new Uint8Array(totalPrefixLength);\n    let offset = 0;\n    for (const arr of prefixParts) {\n      prefix.set(arr, offset);\n      offset += arr.length;\n    }\n\n    steps.push({\n      hash: HashOpEnum.SHA256,\n      prefix,\n      suffix,\n    });\n  }\n\n  return steps;\n}\n\n/**\n * IAVL ProofSpec defines the validation rules for IAVL Merkle proofs.\n *\n * This matches the ics23.IavlSpec from the Go ics23 library.\n *\n * LeafSpec:\n * - Prefix: [0] (placeholder, actual prefix includes version varints)\n * - PrehashKey: NO_HASH (keys are not pre-hashed)\n * - Hash: SHA256\n * - PrehashValue: SHA256 (values are pre-hashed)\n * - Length: VAR_PROTO (protobuf varint encoding)\n *\n * InnerSpec:\n * - ChildOrder: [0, 1] (binary tree: left=0, right=1)\n * - MinPrefixLength: 4 (minimum: height(1) + size(1) + version(1) + lengthByte(1))\n * - MaxPrefixLength: 12 (varints can be up to 10 bytes each for height/size/version)\n * - ChildSize: 33 (32-byte hash + 1-byte length prefix)\n * - EmptyChild: null (IAVL doesn't use placeholder values for missing children)\n * - Hash: SHA256\n *\n * Based on Go ics23 proof.go IavlSpec definition\n */\nexport const IavlSpec: ProofSpec = {\n  leafSpec: {\n    hash: HashOpEnum.SHA256,\n    prehashKey: HashOpEnum.NO_HASH,\n    prehashValue: HashOpEnum.SHA256,\n    length: LengthOpEnum.VAR_PROTO,\n    prefix: new Uint8Array([0]), // placeholder, actual varies by version\n  },\n  innerSpec: {\n    childOrder: [0, 1], // binary tree: left, right\n    minPrefixLength: 4,\n    maxPrefixLength: 12,\n    childSize: 33, // 32-byte hash + 1-byte length prefix\n    emptyChild: new Uint8Array(0), // no empty children in IAVL\n    hash: HashOpEnum.SHA256,\n  },\n  maxDepth: 0, // no depth limit\n  minDepth: 0, // no minimum depth\n  prehashKeyBeforeComparison: false,\n};\n"],"mappings":";;;;;;;;AAgCA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;AAoBvB,SAAgB,cAAc,SAAyB;CAGrD,MAAMA,SAAuB,EAAE;AAC/B,QAAO,KAAKC,8BAAa,GAAG,CAAC;AAC7B,QAAO,KAAKA,8BAAa,GAAG,CAAC;AAC7B,QAAO,KAAKA,8BAAa,QAAQ,CAAC;CAGlC,MAAM,cAAc,OAAO,QAAQ,KAAK,QAAQ,MAAM,IAAI,QAAQ,EAAE;CACpE,MAAM,cAAc,IAAI,WAAW,YAAY;CAC/C,IAAI,SAAS;AACb,MAAK,MAAM,OAAO,QAAQ;AACxB,cAAY,IAAI,KAAK,OAAO;AAC5B,YAAU,IAAI;;AAGhB,QAAO;EACL,MAAMC,sBAAW;EACjB,YAAYA,sBAAW;EACvB,cAAcA,sBAAW;EACzB,QAAQC,wBAAa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