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