UNPKG

tiny-crypto-suite

Version:

Tiny tools, big crypto — seamless encryption and certificate handling for modern web and Node apps.

932 lines 38.8 kB
export default TinyOlmInstance; /** * TinyOlm instance is a lightweight wrapper for handling encryption sessions using the Olm cryptographic library. * * This class is **not available for production mode**. * * @beta * @class */ declare class TinyOlmInstance { /** * Creates a new TinyOlm instance for a specific userId. * * @param {string} userId - The userId to associate with the account and sessions. * @param {string} deviceId - The device id to associate with the account and sessions. * @param {string} [password] - The optional password to associate with the account and sessions. */ constructor(userId: string, deviceId: string, password?: string); /** @type {string} */ password: string; /** @type {string} */ userId: string; /** @type {string} */ deviceId: string; /** @type {Olm.Account|null} */ account: Olm.Account | null; /** @type {Map<string, Olm.Session>} */ sessions: Map<string, Olm.Session>; /** @type {Map<string, Olm.OutboundGroupSession>} */ groupSessions: Map<string, Olm.OutboundGroupSession>; /** @type {Map<string, Olm.InboundGroupSession>} */ groupInboundSessions: Map<string, Olm.InboundGroupSession>; /** * Provides access to a secure internal EventEmitter for subclass use only. * * This method exposes a dedicated EventEmitter instance intended specifically for subclasses * that extend the main class. It prevents subclasses from accidentally or intentionally using * the primary class's public event system (`emit`), which could lead to unpredictable behavior * or interference in the base class's event flow. * * For security and consistency, this method is designed to be accessed only once. * Multiple accesses are blocked to avoid leaks or misuse of the internal event bus. * * @returns {EventEmitter} A special internal EventEmitter instance for subclass use. * @throws {Error} If the method is called more than once. */ getSysEvents(): EventEmitter; /** * @typedef {(...args: any[]) => void} ListenerCallback * A generic callback function used for event listeners. */ /** * Sets the maximum number of listeners for the internal event emitter. * * @param {number} max - The maximum number of listeners allowed. */ setMaxListeners(max: number): void; /** * Emits an event with optional arguments. * @param {string | symbol} event - The name of the event to emit. * @param {...any} args - Arguments passed to event listeners. * @returns {boolean} `true` if the event had listeners, `false` otherwise. */ emit(event: string | symbol, ...args: any[]): boolean; /** * Registers a listener for the specified event. * @param {string | symbol} event - The name of the event to listen for. * @param {ListenerCallback} listener - The callback function to invoke. * @returns {this} The current class instance (for chaining). */ on(event: string | symbol, listener: (...args: any[]) => void): this; /** * Registers a one-time listener for the specified event. * @param {string | symbol} event - The name of the event to listen for once. * @param {ListenerCallback} listener - The callback function to invoke. * @returns {this} The current class instance (for chaining). */ once(event: string | symbol, listener: (...args: any[]) => void): this; /** * Removes a listener from the specified event. * @param {string | symbol} event - The name of the event. * @param {ListenerCallback} listener - The listener to remove. * @returns {this} The current class instance (for chaining). */ off(event: string | symbol, listener: (...args: any[]) => void): this; /** * Alias for `on`. * @param {string | symbol} event - The name of the event. * @param {ListenerCallback} listener - The callback to register. * @returns {this} The current class instance (for chaining). */ addListener(event: string | symbol, listener: (...args: any[]) => void): this; /** * Alias for `off`. * @param {string | symbol} event - The name of the event. * @param {ListenerCallback} listener - The listener to remove. * @returns {this} The current class instance (for chaining). */ removeListener(event: string | symbol, listener: (...args: any[]) => void): this; /** * Removes all listeners for a specific event, or all events if no event is specified. * @param {string | symbol} [event] - The name of the event. If omitted, all listeners from all events will be removed. * @returns {this} The current class instance (for chaining). */ removeAllListeners(event?: string | symbol): this; /** * Returns the number of times the given `listener` is registered for the specified `event`. * If no `listener` is passed, returns how many listeners are registered for the `event`. * @param {string | symbol} eventName - The name of the event. * @param {Function} [listener] - Optional listener function to count. * @returns {number} Number of matching listeners. */ listenerCount(eventName: string | symbol, listener?: Function): number; /** * Adds a listener function to the **beginning** of the listeners array for the specified event. * The listener is called every time the event is emitted. * @param {string | symbol} eventName - The event name. * @param {ListenerCallback} listener - The callback function. * @returns {this} The current class instance (for chaining). */ prependListener(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Adds a **one-time** listener function to the **beginning** of the listeners array. * The next time the event is triggered, this listener is removed and then invoked. * @param {string | symbol} eventName - The event name. * @param {ListenerCallback} listener - The callback function. * @returns {this} The current class instance (for chaining). */ prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): this; /** * Returns an array of event names for which listeners are currently registered. * @returns {(string | symbol)[]} Array of event names. */ eventNames(): (string | symbol)[]; /** * Gets the current maximum number of listeners allowed for any single event. * @returns {number} The max listener count. */ getMaxListeners(): number; /** * Returns a copy of the listeners array for the specified event. * @param {string | symbol} eventName - The event name. * @returns {Function[]} An array of listener functions. */ listeners(eventName: string | symbol): Function[]; /** * Returns a copy of the internal listeners array for the specified event, * including wrapper functions like those used by `.once()`. * @param {string | symbol} eventName - The event name. * @returns {Function[]} An array of raw listener functions. */ rawListeners(eventName: string | symbol): Function[]; /** * Returns the internal TinyPromiseQueue instance (tiny-essentials module) used to manage queued operations. * * @returns {TinyPromiseQueue} The internal request queue instance. */ getQueue(): TinyPromiseQueue; /** * Add a new value type and its converter function. * @param {string} typeName * @param {(data: any) => any} getFunction * @param {(data: any) => { __type: string, value?: any }} convertFunction */ addValueType(typeName: string, getFunction: (data: any) => any, convertFunction: (data: any) => { __type: string; value?: any; }): void; /** * Indicates whether the serialization or deserialization should be performed deeply. * @type {boolean} */ isDeep: boolean; /** * Sets the deep serialization and deserialization mode. * If the argument is a boolean, updates the deep mode accordingly. * Throws an error if the value is not a boolean. * * @param {boolean} value - A boolean indicating whether deep mode should be enabled. * @throws {Error} Throws if the provided value is not a boolean. */ setDeepMode(value: boolean): void; /** * Checks if the current environment is a browser with IndexedDB support. * @throws {Error} If not running in a browser or if IndexedDB is unavailable. */ validateIsBrowser(): void; /** * Enables or disables the functions of use in-memory storage. * * When set to `true`, operations will use local in-memory storage, * useful for tests or temporary sessions that don't require persistence. * * @param {boolean} value - `true` to use in-memory storage, `false` to disable it. */ setUseLocal(value: boolean): void; /** * Returns whether in-memory storage is currently being used. * * @returns {boolean} `true` if the system is set to use in-memory storage, otherwise `false`. */ isUseLocal(): boolean; /** * Returns the name of the current IndexedDB database. * * @returns {string} The database name. * @throws {Error} If the database name is not set. */ getDbName(): string; /** * Returns the active IndexedDB database instance. * * @returns {IDBDatabase} The open database instance. * @throws {Error} If the database has not been initialized. */ getDb(): IDBDatabase; /** * Checks whether the internal IndexedDB instance has been initialized. * * @returns {boolean} `true` if the database instance exists and is ready for use, otherwise `false`. */ existsDb(): boolean; /** * Initializes the IndexedDB database and restores previously saved state. * * @param {string} [dbName='TinyOlmInstance'] - The name of the IndexedDB database. * @returns {Promise<IDBDatabase>} Resolves when the database is ready. * @throws {Error} If not in a browser or if the database is already initialized. */ initIndexedDb(dbName?: string): Promise<IDBDatabase>; _testIndexedDb(): Promise<void>; /** * Validates that a given userId follows the user ID format. * * A valid Matrix user ID must start with '@', contain at least one character, * then a ':', followed by at least one character (e.g., "@user:domain.com"). * * @param {string} userId - The Matrix user ID to validate. * @throws {Error} Throws an error if the userId does not match the expected format. * @returns {void} */ checkUserId(userId: string): void; /** * Sets the new password of instance. * * @param {string} newPassword - The new password. * @throws {Error} Throws if the provided value is not a string. */ setPassword(newPassword: string): Promise<void>; /** * Returns the current password used for (un)pickling. * * @returns {string} The current password. * @throws {Error} Throws if the password is not set. */ getPassword(): string; /** * Sets the userId of this instance. * * @param {string} newUserId - The new userId. * @throws {Error} Throws if the provided value is not a string. */ setUserId(newUserId: string): Promise<void>; /** * Returns the current userId. * * @returns {string} The current userId. * @throws {Error} Throws if the userId is not set. */ getUserId(): string; /** * Sets the device ID of this instance. * * @param {string} newDeviceId - The new device ID. * @throws {Error} Throws if the provided value is not a string. */ setDeviceId(newDeviceId: string): Promise<void>; /** * Returns the current device ID. * * @returns {string} The current device ID. * @throws {Error} Throws if the device ID is not set. */ getDeviceId(): string; /** * @typedef {Object} ExportedOlmInstance * @property {string|null} account - Pickled Olm.Account object. * @property {Record<string, string>|null} sessions - Pickled Olm.Session objects, indexed by session ID. * @property {Record<string, string>|null} groupSessions - Pickled Olm.OutboundGroupSession objects, indexed by room/session ID. * @property {Record<string, string>|null} groupInboundSessions - Pickled Olm.InboundGroupSession objects, indexed by sender key or session ID. */ /** * Export the current Olm account as a pickled string. * * @param {string} [password=this.password] - The password used to encrypt the pickle. * @returns {string} The pickled Olm account. * @throws {Error} If the account is not initialized. */ exportAccount(password?: string): string; /** * Export a specific Olm session with a given user. * * @param {string} userId - The userId of the remote device. * @param {string} [password=this.password] - The password used to encrypt the pickle. * @returns {string} The pickled Olm session. * @throws {Error} If the session is not found. */ exportSession(userId: string, password?: string): string; /** * Export an outbound group session for a specific room. * * @param {string} roomId - The ID of the room. * @param {string} [password=this.password] - The password used to encrypt the pickle. * @returns {string} The pickled outbound group session. * @throws {Error} If the group session is not found. */ exportGroupSession(roomId: string, password?: string): string; /** * Export an inbound group session for a specific room and sender. * * @param {string} roomId - The ID of the room. * @param {string} userId - The sender's userId or session owner. * @param {string} [password=this.password] - The password used to encrypt the pickle. * @returns {string} The pickled inbound group session. * @throws {Error} If the inbound group session is not found. */ exportInboundGroupSession(roomId: string, userId: string, password?: string): string; /** * @param {string} [password] The password used to pickle. If you do not enter any, the predefined password will be used. * @returns {ExportedOlmInstance} Serial structure */ exportInstance(password?: string): { /** * - Pickled Olm.Account object. */ account: string | null; /** * - Pickled Olm.Session objects, indexed by session ID. */ sessions: Record<string, string> | null; /** * - Pickled Olm.OutboundGroupSession objects, indexed by room/session ID. */ groupSessions: Record<string, string> | null; /** * - Pickled Olm.InboundGroupSession objects, indexed by sender key or session ID. */ groupInboundSessions: Record<string, string> | null; }; /** * Export a specific Olm session with a given user from indexedDb. * * @param {string} userId - The userId of the remote device. * @returns {Promise<string>} The pickled Olm session. * @throws {Error} If the session is not found. */ exportDbSession(userId: string): Promise<string>; /** * Export an outbound group session for a specific room from indexedDb. * * @param {string} roomId - The ID of the room. * @returns {Promise<string>} The pickled outbound group session. * @throws {Error} If the group session is not found. */ exportDbGroupSession(roomId: string): Promise<string>; /** * Export an inbound group session for a specific room and sender from indexedDb. * * @param {string} roomId - The ID of the room. * @param {string} userId - The sender's userId or session owner. * @returns {Promise<string>} The pickled inbound group session. * @throws {Error} If the inbound group session is not found. */ exportDbInboundGroupSession(roomId: string, userId: string): Promise<string>; /** * @returns {Promise<ExportedOlmInstance>} Serial structure */ exportDbInstance(): Promise<{ /** * - Pickled Olm.Account object. */ account: string | null; /** * - Pickled Olm.Session objects, indexed by session ID. */ sessions: Record<string, string> | null; /** * - Pickled Olm.OutboundGroupSession objects, indexed by room/session ID. */ groupSessions: Record<string, string> | null; /** * - Pickled Olm.InboundGroupSession objects, indexed by sender key or session ID. */ groupInboundSessions: Record<string, string> | null; }>; /** * Import and restore an Olm account from a pickled string. * * @param {string} pickled - The pickled Olm account string. * @param {string} [password=this.password] - The password used to decrypt the pickle. * @returns {Promise<void>} */ importAccount(pickled: string, password?: string): Promise<void>; /** * Import and restore an Olm session from a pickled string. * * @param {string} key - The session key used to index this session (usually userId or `userId|deviceId`). * @param {string} pickled - The pickled Olm session string. * @param {string} [password=this.password] - The password used to decrypt the pickle. * @returns {Promise<void>} */ importSession(key: string, pickled: string, password?: string): Promise<void>; /** * Import and restore an outbound group session from a pickled string. * * @param {string} key - The key used to index the group session (usually the roomId). * @param {string} pickled - The pickled Olm.OutboundGroupSession string. * @param {string} [password=this.password] - The password used to decrypt the pickle. * @returns {Promise<void>} */ importGroupSession(key: string, pickled: string, password?: string): Promise<void>; /** * Import and restore an inbound group session from a pickled string. * * @param {string} key - The key used to index the inbound group session (usually sender key or `roomId|sender`). * @param {string} pickled - The pickled Olm.InboundGroupSession string. * @param {string} [password=this.password] - The password used to decrypt the pickle. * @returns {Promise<void>} */ importInboundGroupSession(key: string, pickled: string, password?: string): Promise<void>; /** * @param {ExportedOlmInstance} data Returned object of exportInstance * @param {string} [password] The password used to pickle * @returns {Promise<void[]>} */ importInstance(data: { /** * - Pickled Olm.Account object. */ account: string | null; /** * - Pickled Olm.Session objects, indexed by session ID. */ sessions: Record<string, string> | null; /** * - Pickled Olm.OutboundGroupSession objects, indexed by room/session ID. */ groupSessions: Record<string, string> | null; /** * - Pickled Olm.InboundGroupSession objects, indexed by sender key or session ID. */ groupInboundSessions: Record<string, string> | null; }, password?: string): Promise<void[]>; /** * Retrieves all active sessions. * * @returns {Map<string, Olm.Session>} A map of all active sessions where the key is the userId. */ getAllSessions(): Map<string, Olm.Session>; /** * Retrieves the session for a specific userId. * * @param {string} userId - The userId whose session is to be retrieved. * @returns {Olm.Session} The session for the specified userId, or null if no session exists. * @throws {Error} Throws an error if no session exists for the specified userId. */ getSession(userId: string): Olm.Session; /** * Removes the session for a specific userId. * * @param {string} userId - The userId whose session is to be removed. * @returns {Promise<boolean>} Returns true if the session was removed, otherwise false. * @throws {Error} Throws an error if no session exists for the specified userId. */ removeSession(userId: string): Promise<boolean>; /** * Clears all active sessions. * * @returns {Promise<void>} */ clearSessions(): Promise<void>; /** * Initializes the Olm library and creates a new account. * * @returns {Promise<void>} */ init(): Promise<void>; /** * Creates a new outbound group session for a specific room. * @param {string} roomId * @returns {Promise<Olm.OutboundGroupSession>} */ createGroupSession(roomId: string): Promise<Olm.OutboundGroupSession>; /** * Exports the current outbound group session key for a room. * @param {string} roomId * @returns {string} * @throws {Error} If no outbound session exists for the given room. */ exportGroupSessionId(roomId: string): string; /** * Exports the current outbound group session key for a room from the indexdedDb * @param {string} roomId * @returns {Promise<string>} * @throws {Error} If no outbound session exists for the given room. */ exportDbGroupSessionId(roomId: string): Promise<string>; /** * Imports an inbound group session using a provided session key. * @param {string} roomId * @param {string} userId * @param {string} sessionKey * @returns {Promise<void>} */ importGroupSessionId(roomId: string, userId: string, sessionKey: string): Promise<void>; /** * Returns all outbound group sessions. * @returns {Map<string, Olm.OutboundGroupSession>} */ getAllGroupSessions(): Map<string, Olm.OutboundGroupSession>; /** * Returns a specific outbound group session by room ID. * @param {string} roomId * @returns {Olm.OutboundGroupSession} * @throws {Error} If no outbound session exists for the given room. */ getGroupSession(roomId: string): Olm.OutboundGroupSession; /** * Removes a specific outbound group session by room ID. * @param {string} roomId * @returns {Promise<boolean>} True if a session was removed, false otherwise. */ removeGroupSession(roomId: string): Promise<boolean>; /** * Clears all group sessions. * * @returns {Promise<void>} */ clearGroupSessions(): Promise<void>; /** * Returns all inbound group sessions. * @returns {Map<string, Olm.InboundGroupSession>} */ getAllInboundGroupSessions(): Map<string, Olm.InboundGroupSession>; /** * Returns a specific inbound group session by room ID and userId. * @param {string} roomId * @param {string} [userId] * @returns {Olm.InboundGroupSession} * @throws {Error} If no inbound session exists for the given room and userId. */ getInboundGroupSession(roomId: string, userId?: string): Olm.InboundGroupSession; /** * Removes a specific inbound group session by room ID and userId. * @param {string} roomId * @param {string} userId * @returns {Promise<boolean>} True if a session was removed, false otherwise. */ removeInboundGroupSession(roomId: string, userId: string): Promise<boolean>; /** * Clears all inbound group sessions. * * @returns {Promise<void>} */ clearInboundGroupSessions(): Promise<void>; /** * Retrieves the identity keys (curve25519 and ed25519) for the account. * * @returns {{curve25519: Record<string, string>, ed25519: string}} * @throws {Error} Throws an error if account is not initialized. */ getIdentityKeys(): { curve25519: Record<string, string>; ed25519: string; }; /** * Generates a specified number of one-time keys for the account and signs them. * * @param {number} [number=10] - The number of one-time keys to generate. * @returns {Promise<Record<string, { * key: string, * signatures: Record<string, Record<string, string>> * }>>} * @throws {Error} Throws an error if account is not initialized. */ generateOneTimeKeys(number?: number): Promise<Record<string, { key: string; signatures: Record<string, Record<string, string>>; }>>; /** @type {Record<string, { key: string, signatures: Record<string, Record<string, string>> }>} */ signedOneTimeKeys: Record<string, { key: string; signatures: Record<string, Record<string, string>>; }> | undefined; /** * Retrieves the one-time keys currently available for the account. * * @returns {{curve25519: Record<string, string>}} * @throws {Error} Throws an error if account is not initialized. */ getOneTimeKeys(): { curve25519: Record<string, string>; }; /** * Marks the current set of keys as published, preventing them from being reused. * * @returns {Promise<void>} * @throws {Error} Throws an error if account is not initialized. */ markKeysAsPublished(): Promise<void>; /** * Creates an outbound session with another user using their identity and one-time keys. * * @param {string} theirIdentityKey - The identity key of the target user. * @param {string} theirOneTimeKey - The one-time key of the target user. * @param {string} theirUsername - The userId of the target user. * @returns {Promise<void>} * @throws {Error} Throws an error if account is not initialized. */ createOutboundSession(theirIdentityKey: string, theirOneTimeKey: string, theirUsername: string): Promise<void>; /** * Creates an inbound session from a received encrypted message. * * @param {string} senderIdentityKey - The sender's identity key. * @param {string} ciphertext - The ciphertext received. * @param {string} senderUsername - The userId of the sender. * @returns {Promise<void>} * @throws {Error} Throws an error if account is not initialized. */ createInboundSession(senderIdentityKey: string, ciphertext: string, senderUsername: string): Promise<void>; /** * Checks if there is an active session with a specific userId. * * @param {string} userId - The userId to check. * @returns {boolean} True if a session exists, false otherwise. */ hasSession(userId: string): boolean; /** * Exports the device identity keys and available one-time keys format. * * @returns {object} * @throws {Error} Throws an error if account is not initialized. */ exportIdentityAndOneTimeKeys(): object; /** * Disposes the instance by clearing all sessions and the account. * * @returns {Promise<void>} */ dispose(): Promise<void>; /** * Regenerates the identity keys by creating a new account. * * This process will: * - Free the current Olm.Account and create a new one. * - Generate new curve25519 and ed25519 identity keys. * * Important: After regenerating the identity keys, you must: * - Generate new one-time keys by calling `generateOneTimeKeys()`. * - Mark the keys as published by calling `markKeysAsPublished()`. * - Update your device information on the server to broadcast the new keys. * * @returns {Promise<void>} */ regenerateIdentityKeys(): Promise<void>; /** * @typedef {Object} EncryptedMessage * @property {0 | 1} type - The type of the message (0 for pre-key, 1 for message). * @property {string} body - The encrypted message body. */ /** * Creates an encrypted event structure for sending a message to a device. * * @param {EncryptedMessage} encrypted - The encrypted message details. * @param {string} toDeviceCurve25519Key - The recipient's Curve25519 key for encryption. * @returns {object} The encrypted message event ready to be sent. */ getEncryptEvent(encrypted: { /** * - The type of the message (0 for pre-key, 1 for message). */ type: 0 | 1; /** * - The encrypted message body. */ body: string; }, toDeviceCurve25519Key: string): object; /** * Encrypts a plaintext message to a specified user. * * @param {string} toUsername - The userId of the recipient. * @param {string} plaintext - The plaintext message to encrypt. * @returns {EncryptedMessage} The encrypted message. * @throws {Error} Throws an error if no session exists with the given userId. */ encryptMessage(toUsername: string, plaintext: string): { /** * - The type of the message (0 for pre-key, 1 for message). */ type: 0 | 1; /** * - The encrypted message body. */ body: string; }; /** * Decrypts a received ciphertext message from a specified user. * * @param {string} fromUsername - The userId of the sender. * @param {number} messageType - The type of the message (0: pre-key, 1: message). * @param {string} ciphertext - The ciphertext to decrypt. * @returns {string} The decrypted plaintext message. * @throws {Error} Throws an error if no session exists with the given userId. */ decryptMessage(fromUsername: string, messageType: number, ciphertext: string): string; /** * Encrypts a data to a specified user. * * @param {string} toUsername - The userId of the recipient. * @param {*} data - The content to encrypt. * @returns {EncryptedMessage} The encrypted message. * @throws {Error} Throws an error if no session exists with the given userId. */ encrypt(toUsername: string, data: any): { /** * - The type of the message (0 for pre-key, 1 for message). */ type: 0 | 1; /** * - The encrypted message body. */ body: string; }; /** * Decrypts a received data from a specified user. * * @param {string} fromUsername - The userId of the sender. * @param {number} messageType - The type of the message (0: pre-key, 1: message). * @param {string} plaintext - The decrypted content to decrypt. * @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value. * @returns {*} The decrypted plaintext message. * @throws {Error} Throws an error if no session exists with the given userId. */ decrypt(fromUsername: string, messageType: number, plaintext: string, expectedType?: string | null): any; /** * Encrypts a plaintext message for a specific room using the outbound group session. * @param {string} roomId * @param {string} plaintext * @returns {EncryptedData} * @throws {Error} If no outbound session exists for the given room. */ encryptGroupMessage(roomId: string, plaintext: string): { body: string; session_id: string; message_index: number; }; /** * Decrypts an encrypted group message using the inbound group session. * @param {string} roomId * @param {string} userId * @param {EncryptedData} encryptedMessage * @returns {DecryptedGroupMessage} * @throws {Error} If no inbound session exists for the given room and userId. */ decryptGroupMessage(roomId: string, userId: string, encryptedMessage: { body: string; session_id: string; message_index: number; }): { message_index: number; plaintext: string; }; /** * Encrypts a content for a specific room using the outbound group session. * @param {string} roomId * @param {*} data * @returns {EncryptedData} * @throws {Error} If no outbound session exists for the given room. */ encryptGroupContent(roomId: string, data: any): { body: string; session_id: string; message_index: number; }; /** * Decrypts an encrypted content using the inbound group session. * @param {string} roomId * @param {string} userId * @param {EncryptedData} encryptedMessage * @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value. * @returns {DecryptedGroupContent} * @throws {Error} If no inbound session exists for the given room and userId. */ decryptGroupContent(roomId: string, userId: string, encryptedMessage: { body: string; session_id: string; message_index: number; }, expectedType?: string | null): { message_index: number; content: string; }; /** * Encrypts a plaintext message to a specified user from indexedDb. * * @param {string} toUsername - The userId of the recipient. * @param {string} plaintext - The plaintext message to encrypt. * @returns {Promise<EncryptedMessage>} The encrypted message. * @throws {Error} Throws an error if no session exists with the given userId. */ encryptMessageV2(toUsername: string, plaintext: string): Promise<{ /** * - The type of the message (0 for pre-key, 1 for message). */ type: 0 | 1; /** * - The encrypted message body. */ body: string; }>; /** * Decrypts a received ciphertext message from a specified user from indexedDb. * * @param {string} fromUsername - The userId of the sender. * @param {number} messageType - The type of the message (0: pre-key, 1: message). * @param {string} ciphertext - The ciphertext to decrypt. * @returns {Promise<string>} The decrypted plaintext message. * @throws {Error} Throws an error if no session exists with the given userId. */ decryptMessageV2(fromUsername: string, messageType: number, ciphertext: string): Promise<string>; /** * Encrypts a data to a specified user from indexedDb. * * @param {string} toUsername - The userId of the recipient. * @param {*} data - The content to encrypt. * @returns {Promise<EncryptedMessage>} The encrypted message. * @throws {Error} Throws an error if no session exists with the given userId. */ encryptV2(toUsername: string, data: any): Promise<{ /** * - The type of the message (0 for pre-key, 1 for message). */ type: 0 | 1; /** * - The encrypted message body. */ body: string; }>; /** * Decrypts a received data from a specified user from indexedDb. * * @param {string} fromUsername - The userId of the sender. * @param {number} messageType - The type of the message (0: pre-key, 1: message). * @param {string} plaintext - The decrypted content to decrypt. * @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value. * @returns {Promise<*>} The decrypted plaintext message. * @throws {Error} Throws an error if no session exists with the given userId. */ decryptV2(fromUsername: string, messageType: number, plaintext: string, expectedType?: string | null): Promise<any>; /** * Encrypts a plaintext message for a specific room using the outbound group session from indexedDb. * @param {string} roomId * @param {string} plaintext * @returns {Promise<EncryptedData>} * @throws {Error} If no outbound session exists for the given room. */ encryptGroupMessageV2(roomId: string, plaintext: string): Promise<{ body: string; session_id: string; message_index: number; }>; /** * Decrypts an encrypted group message using the inbound group session from indexedDb. * @param {string} roomId * @param {string} userId * @param {EncryptedData} encryptedMessage * @returns {Promise<DecryptedGroupMessage>} * @throws {Error} If no inbound session exists for the given room and userId. */ decryptGroupMessageV2(roomId: string, userId: string, encryptedMessage: { body: string; session_id: string; message_index: number; }): Promise<{ message_index: number; plaintext: string; }>; /** * Encrypts a content for a specific room using the outbound group session from indexedDb. * @param {string} roomId * @param {*} data * @returns {Promise<EncryptedData>} * @throws {Error} If no outbound session exists for the given room. */ encryptGroupContentV2(roomId: string, data: any): Promise<{ body: string; session_id: string; message_index: number; }>; /** * Decrypts an encrypted content using the inbound group session from indexedDb. * @param {string} roomId * @param {string} userId * @param {EncryptedData} encryptedMessage * @param {string|null} [expectedType=null] - Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value. * @returns {Promise<DecryptedGroupContent>} * @throws {Error} If no inbound session exists for the given room and userId. */ decryptGroupContentV2(roomId: string, userId: string, encryptedMessage: { body: string; session_id: string; message_index: number; }, expectedType?: string | null): Promise<{ message_index: number; content: string; }>; /** * Destroys the instance by disposing internal resources and removing all event listeners. * * This method ensures a clean shutdown of the instance by first calling `dispose()`—which is expected * to release external or internal resources (such as timers or memory references)—and then * removes all listeners from both `#events` and `#sysEvents` to prevent memory leaks or unintended behavior. * * It should be called when the instance is no longer needed. * * @returns {Promise<void>} Resolves when cleanup is complete. */ destroy(): Promise<void>; #private; } import { EventEmitter } from 'events'; import { TinyPromiseQueue } from 'tiny-essentials'; //# sourceMappingURL=TinyOlmInstance.d.mts.map