tiny-crypto-suite
Version:
Tiny tools, big crypto β seamless encryption and certificate handling for modern web and Node apps.
293 lines (186 loc) β’ 7.41 kB
Markdown
### π₯ `importSession(key, pickled, password?)`
Imports and restores a **1:1 Olm session** from a pickled string and stores it in the internal session map.
#### π Returns
- **Promise<void>**
#### π§Ό Behavior
- Creates a new `Olm.Session` and unpickles it using the provided `password`.
- Saves the session to `this.sessions` using the provided `key`.
- Persists the session in IndexedDB under `sessions`.
- Emits `TinyOlmEvents.ImportSession`.
#### π Note
This is typically used when syncing session state between devices or restoring it after reinitialization.
#### π§βπ» Example
```javascript
await tinyOlmInstance.importSession(userId, pickledSession);
```
---
### π `getAllSessions()`
Retrieves all active sessions.
#### π Returns
- **Map<string, Olm.Session>**: A map of all active sessions, where the key is the userId.
#### π§βπ» Example
```javascript
const allSessions = tinyOlmInstance.getAllSessions();
console.log(allSessions);
```
---
### π§βπ» `getSession(userId)`
Retrieves the session for a specific **userId**.
#### π₯ Parameters
| Name | Type | Description |
|----------|----------|---------------------------------------------------|
| `userId` | `string` | 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**: If no session exists for the specified **userId**.
#### π§βπ» Example
```javascript
const session = tinyOlmInstance.getSession('@user:domain.com');
console.log(session);
```
---
### β `removeSession(userId)`
Removes the session for a specific **userId**.
#### π₯ Parameters
| Name | Type | Description |
|----------|----------|---------------------------------------------------|
| `userId` | `string` | The userId whose session is to be removed. |
#### π Returns
- **boolean**: Returns `true` if the session was removed, otherwise `false`.
#### π§ Throws
- **Error**: If no session exists for the specified **userId**.
#### π§βπ» Example
```javascript
const wasRemoved = await tinyOlmInstance.removeSession('@user:domain.com');
console.log(wasRemoved); // true if session was removed
```
---
### π§Ή `clearSessions()`
Clears all active sessions.
#### π Returns
- **void**
#### π§βπ» Example
```javascript
await tinyOlmInstance.clearSessions();
```
---
### π§ͺ `hasSession(userId)`
Checks whether there is an active Olm session with a specific user.
#### π₯ Parameters
- `userId` *(string)*: The user ID to check for an existing session.
#### π Returns
- **boolean**: `true` if a session exists, `false` otherwise.
#### π§βπ» Example
```javascript
if (tinyOlmInstance.hasSession('@alice')) {
console.log('Session with Alice exists!');
}
```
---
### π οΈ `exportSession(userId, password = this.password)`
Exports a specific **Olm.Session** with a given user.
#### π₯ Parameters
| Name | Type | Description |
|-------------|----------|--------------------------------------------------|
| `userId` | `string` | The user ID of the remote device. |
| `password` | `string` | The password used to encrypt the pickle. Default is the current instance password. |
#### π Returns
- `string`: The pickled Olm session.
#### β οΈ Throws
- `Error`: If the session is not found.
#### π§ What it does
- Exports the session with the specified user as a pickled string, encrypted with the specified password.
#### π§βπ» Example
```javascript
const pickledSession = tinyOlmInstance.exportSession('@user:domain.com', 'mySecurePassword');
```
---
### π€ `createOutboundSession(theirIdentityKey, theirOneTimeKey, theirUsername)`
Creates an outbound Olm session with another user using their identity and one-time keys.
#### π₯ Parameters
- `theirIdentityKey` *(string)*: Identity key of the target user.
- `theirOneTimeKey` *(string)*: One-time key of the target user.
- `theirUsername` *(string)*: User ID of the target user.
#### π Returns
- **void**
#### π Throws
- **Error**: If the account is not initialized or no one-time key is available.
#### π§βπ» Example
```javascript
await tinyOlmInstance.createOutboundSession(theirIdentityKey, theirOneTimeKey, '@alice');
```
---
### π₯ `createInboundSession(senderIdentityKey, ciphertext, senderUsername)`
Creates an inbound Olm session based on a received ciphertext and sender's identity key.
#### π₯ Parameters
- `senderIdentityKey` *(string)*: Identity key of the sender.
- `ciphertext` *(string)*: Encrypted message received.
- `senderUsername` *(string)*: User ID of the sender.
#### π Returns
- **void**
#### π Throws
- **Error**: If the account is not initialized.
#### π§βπ» Example
```javascript
await tinyOlmInstance.createInboundSession(senderIdentityKey, ciphertext, '@bob');
```
---
### π `encryptMessage(toUsername, plaintext)`
Encrypts a raw plaintext message using an existing session with the recipient.
#### π₯ Parameters
- `toUsername: string` β The user ID of the recipient.
- `plaintext: string` β The message content in plaintext.
#### π€ Returns
- `EncryptedMessage`: The encrypted message object (`{ type, body }`).
#### β οΈ Throws
- If no session exists with the given user.
#### π§βπ» Example
```js
const encrypted = tinyOlmInstance.encryptMessage('alice', 'Hello!');
```
---
### π `decryptMessage(fromUsername, messageType, ciphertext)`
Decrypts a ciphertext message from a known session.
#### π₯ Parameters
- `fromUsername: string` β The sender's user ID.
- `messageType: 0 | 1` β The message type (0 = pre-key, 1 = message).
- `ciphertext: string` β The received encrypted content.
#### π€ Returns
- `string`: The decrypted plaintext.
#### β οΈ Throws
- If no session exists with the sender.
#### π§βπ» Example
```js
const plaintext = tinyOlmInstance.decryptMessage('alice', 1, encrypted.body);
```
---
### π§ͺ `encrypt(toUsername, data)`
Serializes and encrypts any data structure using the appropriate serializer (`TinyCryptoParser`).
#### π₯ Parameters
- `toUsername: string` β The recipientβs user ID.
- `data: *` β The data to serialize and encrypt.
#### π€ Returns
- `EncryptedMessage`: The encrypted result of the serialized content.
#### π§ Behavior
- Uses `serializeDeep()` if `isDeep === true`, otherwise `serialize()`.
#### π§βπ» Example
```js
const encrypted = tinyOlmInstance.encrypt('bob', { hello: 'world' });
```
---
### π `decrypt(fromUsername, messageType, plaintext, expectedType = null)`
Decrypts and deserializes a message received from another user.
#### π₯ Parameters
- `fromUsername: string` β The senderβs user ID.
- `messageType: 0 | 1` β The type of Olm message.
- `plaintext: string` β The encrypted message.
- `expectedType?: string | null` β Optional: expected data type to validate deserialization.
#### π€ Returns
- `*`: The deserialized content (any JS object or value).
#### π§ Behavior
- Uses `deserializeDeep()` if `isDeep === true`, otherwise `deserialize()`.
#### π§βπ» Example
```js
const object = tinyOlmInstance.decrypt('bob', 1, ciphertext, 'object');
```