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
Markdown
### ๐ฅ `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);
```