{"version":3,"file":"proof-inner-node.cjs","names":["data: Uint8Array[]","encodeVarint","encodeBytes"],"sources":["../../src/proof/proof-inner-node.ts"],"sourcesContent":["/**\n * ProofInnerNode represents an inner node in a proof path.\n * Used internally during proof construction before conversion to ICS23 format.\n *\n * Contract: Left and Right can never both be set. Will result in an empty [] roothash.\n *\n * Based on Go IAVL v1.3.5 proof.go ProofInnerNode\n */\n\nimport {\n  sha256,\n} from \"@noble/hashes/sha2.js\";\n\nimport {\n  encodeBytes,\n  encodeVarint,\n} from \"../utils/encoding.js\";\n\nexport class ProofInnerNode {\n  /**\n   * Height of this inner node in the tree\n   */\n  readonly height: number;\n\n  /**\n   * Size (number of leaf nodes) in the subtree rooted at this node\n   */\n  readonly size: bigint;\n\n  /**\n   * Version when this node was created\n   */\n  readonly version: bigint;\n\n  /**\n   * Hash of the left child (null if we traversed left)\n   */\n  readonly left: Uint8Array | null;\n\n  /**\n   * Hash of the right child (null if we traversed right)\n   */\n  readonly right: Uint8Array | null;\n\n  constructor(\n    height: number,\n    size: bigint,\n    version: bigint,\n    left: Uint8Array | null,\n    right: Uint8Array | null,\n  ) {\n    this.height = height;\n    this.size = size;\n    this.version = version;\n    this.left = left;\n    this.right = right;\n  }\n\n  /**\n   * Compute the hash of this inner node given the child hash.\n   *\n   * The hash is computed as:\n   * SHA256(varint(height) || varint(size) || varint(version) ||\n   *        varint_bytes(left_or_child) || varint_bytes(right_or_child))\n   *\n   * Where left_or_child is either the left sibling hash or the child hash,\n   * and right_or_child is either the right sibling hash or the child hash.\n   *\n   * @param childHash - The hash of the child node (the path we traversed)\n   * @returns The computed hash\n   * @throws Error if both left and right are set, or on encoding errors\n   */\n  hash(childHash: Uint8Array): Uint8Array {\n    // Validate contract: both left and right cannot be set\n    if (this.left !== null && this.right !== null) {\n      throw new Error(\"both left and right child hashes are set\");\n    }\n\n    // Build the data to hash\n    const data: Uint8Array[] = [];\n\n    // Encode height, size, version as varints\n    data.push(encodeVarint(BigInt(this.height)));\n    data.push(encodeVarint(this.size));\n    data.push(encodeVarint(this.version));\n\n    // Encode child hashes based on which path we took\n    if (this.left === null) {\n      // We traversed left, so childHash goes first, then right sibling\n      data.push(encodeBytes(childHash));\n      if (this.right !== null) {\n        data.push(encodeBytes(this.right));\n      }\n      else {\n        // If right is also null, encode empty bytes\n        data.push(encodeBytes(new Uint8Array(0)));\n      }\n    }\n    else {\n      // We traversed right, so left sibling goes first, then childHash\n      data.push(encodeBytes(this.left));\n      data.push(encodeBytes(childHash));\n    }\n\n    // Concatenate all data\n    const totalLength = data.reduce((sum, arr) => sum + arr.length, 0);\n    const combined = new Uint8Array(totalLength);\n    let offset = 0;\n    for (const arr of data) {\n      combined.set(arr, offset);\n      offset += arr.length;\n    }\n\n    // Return SHA256 hash\n    return sha256(combined);\n  }\n\n  /**\n   * String representation for debugging\n   */\n  toString(): string {\n    return `ProofInnerNode{\n  Height:  ${this.height}\n  Size:    ${this.size}\n  Version: ${this.version}\n  Left:    ${this.left ? this.toHex(this.left) : \"null\"}\n  Right:   ${this.right ? this.toHex(this.right) : \"null\"}\n}`;\n  }\n\n  private toHex(bytes: Uint8Array): string {\n    return Array.from(bytes)\n      .map(b => b.toString(16).padStart(2, \"0\"))\n      .join(\"\")\n      .toUpperCase();\n  }\n}\n"],"mappings":";;;;;;AAkBA,IAAa,iBAAb,MAA4B;;;;CAI1B,AAAS;;;;CAKT,AAAS;;;;CAKT,AAAS;;;;CAKT,AAAS;;;;CAKT,AAAS;CAET,YACE,QACA,MACA,SACA,MACA,OACA;AACA,OAAK,SAAS;AACd,OAAK,OAAO;AACZ,OAAK,UAAU;AACf,OAAK,OAAO;AACZ,OAAK,QAAQ;;;;;;;;;;;;;;;;CAiBf,KAAK,WAAmC;AAEtC,MAAI,KAAK,SAAS,QAAQ,KAAK,UAAU,KACvC,OAAM,IAAI,MAAM,2CAA2C;EAI7D,MAAMA,OAAqB,EAAE;AAG7B,OAAK,KAAKC,8BAAa,OAAO,KAAK,OAAO,CAAC,CAAC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