{"version":3,"file":"encryptor.cjs","sourceRoot":"","sources":["../../src/types/encryptor.ts"],"names":[],"mappings":"","sourcesContent":["import type { Json } from '@metamask/utils';\n\nexport type PBKDF2Params = {\n  iterations: number;\n};\n\nexport type KeyDerivationOptions = {\n  algorithm: 'PBKDF2';\n  params: PBKDF2Params;\n};\n\nexport type EncryptionResult = {\n  data: string;\n  iv: string;\n  salt: string;\n  keyMetadata?: KeyDerivationOptions;\n};\n\n/**\n * A generic encryptor interface that supports encrypting and decrypting\n * serializable data with a password.\n */\nexport type GenericEncryptor = {\n  /**\n   * Encrypt the given object with the given password.\n   *\n   * @param password - The password to encrypt with.\n   * @param object - The object to encrypt.\n   * @returns The encrypted string.\n   */\n  encrypt: (password: string, object: Json) => Promise<string>;\n\n  /**\n   * Decrypt the given encrypted string with the given password.\n   *\n   * @param password - The password to decrypt with.\n   * @param encryptedString - The encrypted string to decrypt.\n   * @returns The decrypted object.\n   */\n  decrypt: (password: string, encryptedString: string) => Promise<unknown>;\n\n  /**\n   * Check if the provided vault is up to date with the\n   * desired encryption algorithm.\n   *\n   * @param vault - The encrypted string to check.\n   * @param targetDerivationParams - The desired target derivation params.\n   * @returns The updated encrypted string.\n   */\n  isVaultUpdated: (\n    vault: string,\n    targetDerivationParams?: KeyDerivationOptions,\n  ) => boolean;\n};\n\n/**\n * An encryptor interface that supports encrypting and decrypting\n * serializable data with a password, and exporting and importing keys.\n */\nexport type ExportableKeyEncryptor = GenericEncryptor & {\n  /**\n   * Encrypt the given object with the given encryption key.\n   *\n   * @param key - The encryption key to encrypt with.\n   * @param object - The object to encrypt.\n   * @returns The encryption result.\n   */\n  encryptWithKey: (key: unknown, object: Json) => Promise<EncryptionResult>;\n\n  /**\n   * Decrypt the given encrypted string with the given encryption key.\n   *\n   * @param key - The encryption key to decrypt with.\n   * @param encryptedString - The encrypted string to decrypt.\n   * @returns The decrypted object.\n   */\n  decryptWithKey: (\n    key: unknown,\n    encryptedString: EncryptionResult,\n  ) => Promise<unknown>;\n\n  /**\n   * Generate an encryption key from exported key string.\n   *\n   * @param key - The exported key string.\n   * @returns The encryption key.\n   */\n  importKey: <Type>(key: string) => Promise<Type>;\n\n  /**\n   * Export a generated key as a string.\n   *\n   * @param key - The encryption key.\n   * @returns The exported key string.\n   */\n  exportKey: (key: unknown) => Promise<string>;\n\n  /**\n   * Generate a salt with a given length.\n   *\n   * @param length - The length of the salt, default is 32 bytes.\n   * @returns The base64 encoded salt bytes.\n   */\n  generateSalt: (length?: number) => string;\n\n  /**\n   * Generate an encryption key using a password.\n   *\n   * @param password - The password to use to generate key.\n   * @param salt - The salt string to use in key derivation.\n   * @param exportable - Whether or not the key should be exportable.\n   * @param opts - The options to use for key derivation.\n   * @returns The encryption key.\n   */\n  keyFromPassword: <Type>(\n    password: string,\n    salt: string,\n    exportable?: boolean,\n    opts?: KeyDerivationOptions,\n  ) => Promise<Type>;\n};\n"]}