UNPKG

tiny-crypto-suite

Version:

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

207 lines (138 loc) โ€ข 6.42 kB
### ๐Ÿ“ฅ `importInboundGroupSession(key, pickled, password?)` Imports and restores an **inbound group Olm session** from a pickled string for decrypting messages from other users. #### ๐Ÿ” Returns - **Promise<void>** #### ๐Ÿงผ Behavior - Creates a new `Olm.InboundGroupSession` and unpickles it using the `password`. - Stores the session in `this.groupInboundSessions` under the given `key`. - Persists the session in IndexedDB under `groupInboundSessions`. - Emits `TinyOlmEvents.ImportInboundGroupSession`. #### ๐Ÿ“Œ Note Commonly used when receiving encrypted messages from other users in group chats. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript await tinyOlmInstance.importInboundGroupSession(senderKey, pickledInboundSession); ``` --- ### ๐Ÿ’ฌ `exportInboundGroupSession(roomId, userId, password = this.password)` Exports an **inbound group session** for a specific room and sender. #### ๐Ÿ“ฅ Parameters | Name | Type | Description | |-------------|----------|--------------------------------------------------| | `roomId` | `string` | The ID of the room. | | `userId` | `string` | The sender's userId or session owner. | | `password` | `string` | The password used to encrypt the pickle. Default is the current instance password. | #### ๐Ÿ” Returns - `string`: The pickled inbound group session. #### โš ๏ธ Throws - `Error`: If the inbound group session is not found. #### ๐Ÿง  What it does - Exports the inbound group session for the specified room and sender as a pickled string, encrypted with the specified password. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const pickledInboundGroupSession = tinyOlmInstance.exportInboundGroupSession('roomId123', '@user:domain.com', 'mySecurePassword'); ``` --- ### ๐Ÿ› ๏ธ `importGroupSessionId(roomId, userId, sessionKey)` Imports an inbound group session using a provided session key. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the session will be imported. - **userId** *(string)*: The ID of the user to whom the session belongs. - **sessionKey** *(string)*: The session key to import the group session. #### ๐Ÿ” Returns - **void** #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript await tinyOlmInstance.importGroupSessionId('room123', 'user456', 'sessionKey123'); ``` --- ### ๐Ÿ› ๏ธ `getAllInboundGroupSessions()` Returns all inbound group sessions. #### ๐Ÿ” Returns - **Map<string, Olm.InboundGroupSession>**: A map of all inbound group sessions, indexed by session ID. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const allSessions = tinyOlmInstance.getAllInboundGroupSessions(); console.log(allSessions); ``` --- ### ๐Ÿ› ๏ธ `getInboundGroupSession(roomId, userId)` Returns a specific inbound group session by room ID and userId. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the inbound group session exists. - **userId** *(string)*: The ID of the user whose inbound group session is to be retrieved. #### ๐Ÿ” Returns - **Olm.InboundGroupSession**: The inbound group session for the specified room and user. #### ๐Ÿ›‘ Throws - **Error**: If no inbound session exists for the given room and userId. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const session = tinyOlmInstance.getInboundGroupSession('room123', 'user456'); console.log(session); // Olm.InboundGroupSession ``` --- ### ๐Ÿ› ๏ธ `removeInboundGroupSession(roomId, userId)` Removes a specific inbound group session by room ID and userId. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room whose inbound group session will be removed. - **userId** *(string)*: The ID of the user whose inbound group session will be removed. #### ๐Ÿ” Returns - **boolean**: Returns `true` if the session was removed, otherwise `false`. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const success = await tinyOlmInstance.removeInboundGroupSession('room123', 'user456'); console.log(success); // true if removed ``` --- ### ๐Ÿ› ๏ธ `clearInboundGroupSessions()` Clears all inbound group sessions. #### ๐Ÿ” Returns - **void** #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript await tinyOlmInstance.clearInboundGroupSessions(); console.log('All inbound group sessions cleared.'); ``` --- ### ๐Ÿ› ๏ธ `decryptGroupMessage(roomId, userId, encryptedMessage)` Decrypts an encrypted group message using the inbound group session. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the message was sent. - **userId** *(string)*: The ID of the user who is decrypting the message. - **encryptedMessage** *(Object)*: An object containing: - `body` *(string)*: The encrypted message body. - `session_id` *(string)*: The session ID for the encrypted message. - `message_index` *(number)*: The message index. #### ๐Ÿ” Returns - **Object**: An object containing: - `message_index` *(number)*: The message index of the decrypted message. - `plaintext` *(string)*: The decrypted plaintext message. #### ๐Ÿ›‘ Throws - **Error**: If no inbound session exists for the given room and userId. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const decryptedMessage = tinyOlmInstance.decryptGroupMessage('room123', 'user456', encryptedMessage); console.log(decryptedMessage); ``` --- ### ๐Ÿ› ๏ธ `decryptGroupContent(roomId, userId, encryptedMessage, expectedType = null)` Decrypts encrypted content using the inbound group session. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the content was sent. - **userId** *(string)*: The ID of the user who is decrypting the content. - **encryptedMessage** *(Object)*: An object containing: - `body` *(string)*: The encrypted content body. - `session_id` *(string)*: The session ID for the encrypted content. - `message_index` *(number)*: The message index. - **expectedType** *(string|null)*: Optionally specify the expected type of the decrypted data. If provided, the method will validate the type of the deserialized value. #### ๐Ÿ” Returns - **Object**: An object containing: - `message_index` *(number)*: The message index of the decrypted content. - `content` *(any)*: The decrypted content. #### ๐Ÿ›‘ Throws - **Error**: If no inbound session exists for the given room and userId. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const decryptedContent = tinyOlmInstance.decryptGroupContent('room123', 'user456', encryptedContent); console.log(decryptedContent); ```