/**
 * 交易哈希值对象
 */
export class TxHash {
  private readonly _value: string;

  private constructor(value: string) {
    this._value = value;
  }

  static create(value: string): TxHash {
    // 验证格式: 0x 开头，64位十六进制
    const normalized = value.toLowerCase();
    if (!/^0x[a-f0-9]{64}$/.test(normalized)) {
      throw new Error(`Invalid transaction hash: ${value}`);
    }
    return new TxHash(normalized);
  }

  static fromUnchecked(value: string): TxHash {
    return new TxHash(value.toLowerCase());
  }

  get value(): string {
    return this._value;
  }

  equals(other: TxHash): boolean {
    return this._value === other._value;
  }

  toString(): string {
    return this._value;
  }

  /**
   * 获取短格式（用于显示）
   */
  toShort(): string {
    return `${this._value.slice(0, 10)}...${this._value.slice(-8)}`;
  }
}
