UNPKG

tiny-crypto-suite

Version:

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

220 lines (142 loc) โ€ข 5.89 kB
### ๐Ÿ“ฅ `importGroupSession(key, pickled, password?)` Imports and restores an **outbound group Olm session** from a pickled string and registers it with the instance. #### ๐Ÿ” Returns - **Promise<void>** #### ๐Ÿงผ Behavior - Creates a new `Olm.OutboundGroupSession` and unpickles it using the `password`. - Stores the session in `this.groupSessions` under the specified `key`. - Persists the session in IndexedDB under `groupSessions`. - Emits `TinyOlmEvents.ImportGroupSession`. #### ๐Ÿ“Œ Note Used for resuming group encryption sessions, typically keyed by `roomId`. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript await tinyOlmInstance.importGroupSession(roomId, pickledGroupSession); ``` --- ### ๐ŸŒ `exportGroupSession(roomId, password = this.password)` Exports an **outbound group session** for a specific room. #### ๐Ÿ“ฅ Parameters | Name | Type | Description | |-------------|----------|--------------------------------------------------| | `roomId` | `string` | The ID of the room. | | `password` | `string` | The password used to encrypt the pickle. Default is the current instance password. | #### ๐Ÿ” Returns - `string`: The pickled outbound group session. #### โš ๏ธ Throws - `Error`: If the group session is not found. #### ๐Ÿง  What it does - Exports the outbound group session for the specified room as a pickled string, encrypted with the specified password. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const pickledGroupSession = tinyOlmInstance.exportGroupSession('roomId123', 'mySecurePassword'); ``` --- ### ๐Ÿ› ๏ธ `createGroupSession(roomId)` Creates a new outbound group session for a specific room. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the session will be created. #### ๐Ÿ” Returns - **Olm.OutboundGroupSession**: The newly created outbound group session. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const roomId = 'room123'; const session = await tinyOlmInstance.createGroupSession(roomId); console.log(session); // Olm.OutboundGroupSession ``` --- ### ๐Ÿ› ๏ธ `exportGroupSessionId(roomId)` Exports the current outbound group session key for a room. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room whose group session key is being exported. #### ๐Ÿ” Returns - **string**: The session key for the outbound group session. #### ๐Ÿ›‘ Throws - **Error**: If no outbound session exists for the given room. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const sessionKey = tinyOlmInstance.exportGroupSessionId('room123'); console.log(sessionKey); // The exported session key ``` --- ### ๐Ÿ› ๏ธ `getAllGroupSessions()` Returns all outbound group sessions. #### ๐Ÿ” Returns - **Map<string, Olm.OutboundGroupSession>**: A map of all outbound group sessions, indexed by room ID. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const allSessions = tinyOlmInstance.getAllGroupSessions(); console.log(allSessions); ``` --- ### ๐Ÿ› ๏ธ `getGroupSession(roomId)` Returns a specific outbound group session by room ID. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room whose group session you want to retrieve. #### ๐Ÿ” Returns - **Olm.OutboundGroupSession**: The outbound group session for the specified room. #### ๐Ÿ›‘ Throws - **Error**: If no outbound session exists for the given room. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const session = tinyOlmInstance.getGroupSession('room123'); console.log(session); // Olm.OutboundGroupSession ``` --- ### ๐Ÿ› ๏ธ `removeGroupSession(roomId)` Removes a specific outbound group session by room ID. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room whose group session should be removed. #### ๐Ÿ” Returns - **boolean**: Returns `true` if the session was removed, otherwise `false`. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const success = await tinyOlmInstance.removeGroupSession('room123'); console.log(success); // true if removed ``` --- ### ๐Ÿ› ๏ธ `clearGroupSessions()` Clears all group sessions. #### ๐Ÿ” Returns - **void** #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript await tinyOlmInstance.clearGroupSessions(); console.log('All group sessions cleared.'); ``` --- ### ๐Ÿ› ๏ธ `encryptGroupMessage(roomId, plaintext)` Encrypts a plaintext message for a specific room using the outbound group session. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the message will be encrypted. - **plaintext** *(string)*: The plaintext message to be encrypted. #### ๐Ÿ” Returns - **Object**: An object containing the following fields: - `body` *(string)*: The encrypted ciphertext of the message. - `session_id` *(string)*: The session ID of the outbound group session. - `message_index` *(number)*: The message index of the encrypted message. #### ๐Ÿ›‘ Throws - **Error**: If no outbound session exists for the given room. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const encryptedMessage = tinyOlmInstance.encryptGroupMessage('room123', 'Hello, World!'); console.log(encryptedMessage); ``` --- ### ๐Ÿ› ๏ธ `encryptGroupContent(roomId, data)` Encrypts content for a specific room using the outbound group session. #### ๐Ÿ’ก Parameters - **roomId** *(string)*: The ID of the room where the content will be encrypted. - **data** *(any)*: The content to be encrypted. #### ๐Ÿ” Returns - **Object**: An object containing the following fields: - `body` *(string)*: The encrypted ciphertext of the content. - `session_id` *(string)*: The session ID of the outbound group session. - `message_index` *(number)*: The message index of the encrypted content. #### ๐Ÿ›‘ Throws - **Error**: If no outbound session exists for the given room. #### ๐Ÿง‘โ€๐Ÿ’ป Example ```javascript const encryptedContent = tinyOlmInstance.encryptGroupContent('room123', { someKey: 'someValue' }); console.log(encryptedContent); ```