{"version":3,"file":"node-key.cjs","names":[],"sources":["../src/node-key.ts"],"sourcesContent":["/**\n * NodeKey represents a key of a node in the database.\n * Based on the Go iavl node.go NodeKey implementation.\n *\n * Format: 12 bytes total\n * - Bytes 0-7: version (int64, big-endian)\n * - Bytes 8-11: nonce (uint32, big-endian)\n */\n\n/**\n * NodeKey represents a unique identifier for a node in the IAVL tree.\n *\n * The NodeKey consists of:\n * - version: The tree version when this node was created\n * - nonce: A unique nonce to distinguish multiple nodes in the same version\n *\n * The root node of each version has nonce = 1.\n */\nexport class NodeKey {\n  /** Tree version when this node was created */\n  private readonly _version: bigint;\n  /** Unique nonce for this node within the version */\n  private readonly _nonce: number;\n\n  /**\n   * Creates a new NodeKey.\n   *\n   * @param version - Tree version (int64)\n   * @param nonce - Unique nonce (uint32)\n   */\n  constructor(version: bigint | number, nonce: number) {\n    this._version = typeof version === \"bigint\" ? version : BigInt(version);\n    this._nonce = nonce >>> 0; // Ensure uint32\n  }\n\n  /**\n   * Gets the version.\n   */\n  get version(): bigint {\n    return this._version;\n  }\n\n  /**\n   * Gets the nonce.\n   */\n  get nonce(): number {\n    return this._nonce;\n  }\n\n  /**\n   * Serializes the NodeKey to a 12-byte Uint8Array.\n   *\n   * Format:\n   * - Bytes 0-7: version (big-endian uint64)\n   * - Bytes 8-11: nonce (big-endian uint32)\n   *\n   * @returns 12-byte representation of the NodeKey\n   */\n  toBytes(): Uint8Array {\n    const bytes = new Uint8Array(12);\n\n    // Write version (8 bytes, big-endian)\n    const version = this._version;\n    bytes[0] = Number((version >> 56n) & 0xFFn);\n    bytes[1] = Number((version >> 48n) & 0xFFn);\n    bytes[2] = Number((version >> 40n) & 0xFFn);\n    bytes[3] = Number((version >> 32n) & 0xFFn);\n    bytes[4] = Number((version >> 24n) & 0xFFn);\n    bytes[5] = Number((version >> 16n) & 0xFFn);\n    bytes[6] = Number((version >> 8n) & 0xFFn);\n    bytes[7] = Number(version & 0xFFn);\n\n    // Write nonce (4 bytes, big-endian)\n    const nonce = this._nonce;\n    bytes[8] = (nonce >> 24) & 0xFF;\n    bytes[9] = (nonce >> 16) & 0xFF;\n    bytes[10] = (nonce >> 8) & 0xFF;\n    bytes[11] = nonce & 0xFF;\n\n    return bytes;\n  }\n\n  /**\n   * Deserializes a NodeKey from a 12-byte Uint8Array.\n   *\n   * @param bytes - 12-byte array containing the serialized NodeKey\n   * @returns A new NodeKey instance\n   * @throws {Error} If bytes is not exactly 12 bytes\n   */\n  static fromBytes(bytes: Uint8Array): NodeKey {\n    if (bytes.length !== 12) {\n      throw new Error(`NodeKey must be exactly 12 bytes, got ${bytes.length}`);\n    }\n\n    // Read version (8 bytes, big-endian)\n    const version\n      = (BigInt(bytes[0]) << 56n)\n        | (BigInt(bytes[1]) << 48n)\n        | (BigInt(bytes[2]) << 40n)\n        | (BigInt(bytes[3]) << 32n)\n        | (BigInt(bytes[4]) << 24n)\n        | (BigInt(bytes[5]) << 16n)\n        | (BigInt(bytes[6]) << 8n)\n        | BigInt(bytes[7]);\n\n    // Read nonce (4 bytes, big-endian)\n    const nonce\n      = (bytes[8] << 24)\n        | (bytes[9] << 16)\n        | (bytes[10] << 8)\n        | bytes[11];\n\n    return new NodeKey(version, nonce >>> 0); // >>> 0 ensures uint32\n  }\n\n  /**\n   * Creates a root node key for a given version.\n   * Root nodes always have nonce = 1.\n   *\n   * @param version - Tree version\n   * @returns A new NodeKey for the root node\n   */\n  static rootKey(version: bigint | number): NodeKey {\n    return new NodeKey(version, 1);\n  }\n\n  /**\n   * Compares this NodeKey with another for equality.\n   *\n   * @param other - Another NodeKey to compare\n   * @returns true if both version and nonce are equal\n   */\n  equals(other: NodeKey): boolean {\n    return this._version === other._version && this._nonce === other._nonce;\n  }\n\n  /**\n   * Compares this NodeKey with another for ordering.\n   *\n   * @param other - Another NodeKey to compare\n   * @returns negative if this < other, 0 if equal, positive if this > other\n   */\n  compare(other: NodeKey): number {\n    // First compare versions\n    if (this._version < other._version) {\n      return -1;\n    }\n    if (this._version > other._version) {\n      return 1;\n    }\n\n    // If versions are equal, compare nonces\n    if (this._nonce < other._nonce) {\n      return -1;\n    }\n    if (this._nonce > other._nonce) {\n      return 1;\n    }\n\n    return 0;\n  }\n\n  /**\n   * Returns a string representation of the NodeKey.\n   *\n   * @returns String in format \"v{version}n{nonce}\"\n   */\n  toString(): string {\n    return `v${this._version}n${this._nonce}`;\n  }\n}\n\n/**\n * Creates a root NodeKey for the given version.\n * This is a convenience function for NodeKey.rootKey().\n *\n * @param version - Tree version\n * @returns A new NodeKey with nonce = 1\n */\nexport function getRootKey(version: bigint | number): Uint8Array {\n  return NodeKey.rootKey(version).toBytes();\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;AAkBA,IAAa,UAAb,MAAa,QAAQ;;CAEnB,AAAiB;;CAEjB,AAAiB;;;;;;;CAQjB,YAAY,SAA0B,OAAe;AACnD,OAAK,WAAW,OAAO,YAAY,WAAW,UAAU,OAAO,QAAQ;AACvE,OAAK,SAAS,UAAU;;;;;CAM1B,IAAI,UAAkB;AACpB,SAAO,KAAK;;;;;CAMd,IAAI,QAAgB;AAClB,SAAO,KAAK;;;;;;;;;;;CAYd,UAAsB;EACpB,MAAM,QAAQ,IAAI,WAAW,GAAG;EAGhC,MAAM,UAAU,KAAK;AACrB,QAAM,KAAK,OAAQ,WAAW,MAAO,KAAM;AAC3C,QAAM,KAAK,OAAQ,WAAW,MAAO,KAAM;AAC3C,QAAM,KAAK,OAAQ,WAAW,MAAO,KAAM;AAC3C,QAAM,KAAK,OAAQ,WAAW,MAAO,KAAM;AAC3C,QAAM,KAAK,OAAQ,WAAW,MAAO,KAAM;AAC3C,QAAM,KAAK,OAAQ,WAAW,MAAO,KAAM;AAC3C,QAAM,KAAK,OAAQ,WAAW,KAAM,KAAM;AAC1C,QAAM,KAAK,OAAO,UAAU,KAAM;EAGlC,MAAM,QAAQ,KAAK;AACnB,QAAM,KAAM,SAAS,KAAM;AAC3B,QAAM,KAAM,SAAS,KAAM;AAC3B,QAAM,MA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