Metadata-Version: 2.4
Name: libdave-rs
Version: 0.3.1
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Communications :: Chat
Classifier: Topic :: Security :: Cryptography
Classifier: Typing :: Typed
License-File: LICENSE
License-File: THIRD_PARTY_LICENSES
Summary: Python bindings for Discord's DAVE end-to-end encryption (libdave), via libdave-rs
Keywords: discord,dave,e2ee,mls,voice
Author: mineygg
License-Expression: MIT
Requires-Python: >=3.9
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/mineygg/libdave-rs

# libdave-rs for Python

Python bindings for [libdave](https://github.com/discord/libdave), Discord's DAVE
end-to-end encryption (MLS) for voice and video, built on
[libdave-rs](https://github.com/mineygg/libdave-rs).

libdave, mlspp and OpenSSL are compiled in and linked statically, so the wheels have
**no runtime dependencies**. One `abi3` wheel per platform covers every CPython 3.9+.

```sh
pip install libdave-rs
```

```python
import libdave_rs as dave
from libdave_rs import MediaType

print(dave.max_supported_protocol_version())

with dave.SessionManager(self_user_id=42, group_id=1234567890) as manager:
    manager.add_user(43)
    manager.on_select_protocol_ack(dave.max_supported_protocol_version())
    key_package = manager.get_marshalled_key_package()   # -> DAVE_MLS_KEY_PACKAGE

    # ...feed the voice-gateway opcodes into the on_* methods...

    frame = manager.encrypt(MediaType.AUDIO, ssrc, opus_frame)   # bytes, or None
    plain = manager.decrypt(MediaType.AUDIO, 43, wire_frame)     # bytes, or None
```

Everything the Node package exports exists here under the same name in snake_case, with the
same parameters in the same order (`DaveSessionManager` is the Node name for `SessionManager`;
both work). `scripts/check-parity.py` enforces this in CI. Python adds context managers
(`with ...:`), `bytearray` inputs and `int` ids on top.

`SessionManager` implements the gateway bookkeeping (transitions, epochs, per-user
ratchets). `Session`, `Encryptor`, `Decryptor` and `KeyRatchet` are the lower-level
building blocks it is made of.

## Notes

- **Ids** (users and groups) are Discord snowflakes: pass an `int` or its decimal `str`.
  User ids in results (`roster`, `roster_signatures`) are `str`.
- **Binary inputs** may be `bytes` or `bytearray`; outputs are `bytes`.
- **Threads and the GIL.** Every native call releases the GIL, so encrypting audio on one
  thread never stalls the rest of your program. Objects are thread-safe: calls on the same
  object are serialised, calls on different objects run in parallel.
- **asyncio.** Every call is fast and non-blocking except the fingerprint, which libdave
  derives on a worker thread. Like the Promise in the Node bindings,
  `await session.get_pairwise_fingerprint(version, user_id)` is awaitable and needs a running
  event loop; `get_pairwise_fingerprint_sync` blocks (with the GIL released) for use elsewhere.
- **Errors.** Bad arguments raise `ValueError`/`TypeError`. libdave failures raise
  `DaveError`. Using an object after `destroy()` (or after leaving its `with` block) raises
  `DestroyedError`, a `DaveError` subclass. `SessionManager.encrypt/decrypt` return `None`
  instead of raising when a frame cannot be processed, so a lost key never kills a voice loop.
- **Callbacks** (`Encryptor.set_protocol_version_changed_callback`) run on the calling
  thread right after the call that triggered them, so they may safely use the encryptor.
- **Logging.** libdave prints to stdout by default. Call `libdave_rs.enable_logging()` to
  route it to the standard `logging` module (logger `libdave_rs`), or
  `libdave_rs.set_log_sink(callback)` for full control. Either replaces the stdout output.
- **Types.** The package ships type stubs (`py.typed`).

## Building from source

Needs a Rust toolchain (1.88+), a C++20 compiler, CMake, and the git submodules
(`git submodule update --init`). Nothing else: OpenSSL is vendored.

```sh
pip install maturin
maturin develop --release -m crates/libdave-py/Cargo.toml   # into the active venv
pytest crates/libdave-py/tests
```

## License

MIT. Third-party notices (libdave, mlspp, OpenSSL, ...) are in `THIRD_PARTY_LICENSES`.

