{"version":3,"sources":["../../../src/crypto/services/WalletKeyEncryptionService.ts"],"sourcesContent":["/**\n * Service for wallet key encryption and decryption operations.\n *\n * @remarks\n * This service separates business logic (wallet key processing) from crypto primitives\n * (ECIES operations). It handles key normalization, data conversion, and format transformation\n * while delegating actual cryptographic operations to the provided ECIES provider.\n *\n * @category Cryptography\n * @internal\n */\n\nimport type { ECIESProvider, ECIESEncrypted } from \"../ecies/interface\";\nimport {\n  processWalletPublicKey,\n  processWalletPrivateKey,\n  parseEncryptedDataBuffer,\n} from \"../../utils/crypto-utils\";\nimport { stringToBytes, bytesToString, toHex, fromHex, concat } from \"viem\";\n\nexport interface WalletKeyEncryptionServiceConfig {\n  /** ECIES provider for encryption/decryption */\n  eciesProvider: ECIESProvider;\n}\n\n/**\n * Service for wallet key encryption and decryption operations\n *\n * @remarks\n * This service encapsulates the business logic for wallet key operations,\n * delegating actual cryptographic operations to the provided ECIES provider.\n * It handles key normalization, data conversion, and format transformation.\n *\n * @internal\n */\nexport class WalletKeyEncryptionService {\n  private readonly eciesProvider: ECIESProvider;\n\n  constructor(config: WalletKeyEncryptionServiceConfig) {\n    this.eciesProvider = config.eciesProvider;\n  }\n\n  /**\n   * Encrypts data using a wallet's public key.\n   *\n   * @param data - The plaintext message to encrypt for the wallet owner.\n   * @param publicKey - The recipient wallet's public key for encryption.\n   * @returns A promise that resolves to the encrypted data as a hex string.\n   * @throws {Error} When encryption fails due to invalid key format.\n   *\n   * @example\n   * ```typescript\n   * const encrypted = await processor.encryptWithWalletPublicKey(\n   *   \"Secret message\",\n   *   \"0x04...\" // 65-byte uncompressed public key\n   * );\n   * console.log(`Encrypted: ${encrypted}`);\n   * ```\n   */\n  async encryptWithWalletPublicKey(\n    data: string,\n    publicKey: string | Uint8Array,\n  ): Promise<string> {\n    // Process the public key to ensure correct format\n    const publicKeyBytes = processWalletPublicKey(publicKey);\n\n    // Normalize to uncompressed format using the provider\n    // This handles compressed keys, raw coordinates, and validates the format\n    const normalizedKey =\n      this.eciesProvider.normalizeToUncompressed(publicKeyBytes);\n\n    // Convert string data to bytes\n    const dataBytes = stringToBytes(data);\n\n    // Perform ECIES encryption\n    const encrypted = await this.eciesProvider.encrypt(\n      normalizedKey,\n      dataBytes,\n    );\n\n    // Concatenate all components for legacy format compatibility\n    const result = concat([\n      encrypted.iv,\n      encrypted.ephemPublicKey,\n      encrypted.ciphertext,\n      encrypted.mac,\n    ]);\n\n    // Return as hex string without 0x prefix for API compatibility\n    return toHex(result).slice(2);\n  }\n\n  /**\n   * Decrypts data using a wallet's private key.\n   *\n   * @param encryptedData - The hex-encoded encrypted data to decrypt.\n   * @param privateKey - The wallet's private key for decryption.\n   * @returns A promise that resolves to the decrypted plaintext message.\n   * @throws {Error} When decryption fails due to invalid data or key format.\n   *\n   * @example\n   * ```typescript\n   * const decrypted = await processor.decryptWithWalletPrivateKey(\n   *   encryptedHexString,\n   *   \"0x...\" // 32-byte private key\n   * );\n   * console.log(`Decrypted: ${decrypted}`);\n   * ```\n   */\n  async decryptWithWalletPrivateKey(\n    encryptedData: string,\n    privateKey: string | Uint8Array,\n  ): Promise<string> {\n    // Process the private key to ensure correct format\n    const privateKeyBytes = processWalletPrivateKey(privateKey);\n\n    // Convert hex string to bytes and parse encrypted components\n    const prefixedHex = encryptedData.startsWith(\"0x\")\n      ? encryptedData\n      : `0x${encryptedData}`;\n    const encryptedBytes = fromHex(prefixedHex as `0x${string}`, \"bytes\");\n    const encrypted = parseEncryptedDataBuffer(encryptedBytes);\n\n    // Perform ECIES decryption\n    const decrypted = await this.eciesProvider.decrypt(\n      privateKeyBytes,\n      encrypted,\n    );\n\n    // Convert bytes back to string\n    return bytesToString(decrypted);\n  }\n\n  /**\n   * Encrypts a Uint8Array with a wallet public key\n   *\n   * @param data - Binary data to encrypt\n   * @param publicKey - Public key as hex string or Uint8Array\n   * @returns Encrypted data structure\n   */\n  async encryptBinary(\n    data: Uint8Array,\n    publicKey: string | Uint8Array,\n  ): Promise<ECIESEncrypted> {\n    const publicKeyBytes = processWalletPublicKey(publicKey);\n    const normalizedKey =\n      this.eciesProvider.normalizeToUncompressed(publicKeyBytes);\n    return this.eciesProvider.encrypt(normalizedKey, data);\n  }\n\n  /**\n   * Decrypts to a Uint8Array with a wallet private key\n   *\n   * @param encrypted - Encrypted data structure\n   * @param privateKey - Private key as hex string or Uint8Array\n   * @returns Decrypted binary data\n   */\n  async decryptBinary(\n    encrypted: ECIESEncrypted,\n    privateKey: string | Uint8Array,\n  ): Promise<Uint8Array> {\n    const privateKeyBytes = processWalletPrivateKey(privateKey);\n    return this.eciesProvider.decrypt(privateKeyBytes, encrypted);\n  }\n\n  /**\n   * Gets the underlying ECIES provider\n   *\n   * @returns The ECIES provider instance\n   */\n  getECIESProvider(): ECIESProvider {\n    return this.eciesProvider;\n  }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAaA,0BAIO;AACP,kBAAqE;AAiB9D,MAAM,2BAA2B;AAAA,EACrB;AAAA,EAEjB,YAAY,QAA0C;AACpD,SAAK,gBAAgB,OAAO;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAmBA,MAAM,2BACJ,MACA,WACiB;AAEjB,UAAM,qBAAiB,4CAAuB,SAAS;AAIvD,UAAM,gBACJ,KAAK,cAAc,wBAAwB,cAAc;AAG3D,UAAM,gBAAY,2BAAc,IAAI;AAGpC,UAAM,YAAY,MAAM,KAAK,cAAc;AAAA,MACzC;AAAA,MACA;AAAA,IACF;AAGA,UAAM,aAAS,oBAAO;AAAA,MACpB,UAAU;AAAA,MACV,UAAU;AAAA,MACV,UAAU;AAAA,MACV,UAAU;AAAA,IACZ,CAAC;AAGD,eAAO,mBAAM,MAAM,EAAE,MAAM,CAAC;AAAA,EAC9B;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA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