โก layer
A modular, production-grade async Rust implementation of the Telegram MTProto protocol.
Written from the ground up to understand Telegram's internals at the lowest level.
๐งฉ What is layer?
layer is a hand-written, bottom-up async Rust implementation of the
Telegram MTProto protocol. Every component โ
from the .tl schema parser, to the AES-IGE cipher, to the Diffie-Hellman key
exchange, to the async update stream โ is written and owned by this project.
No black boxes. No magic. Just Rust, all the way down.
๐๏ธ Crate Overview
| Crate | Description |
|---|---|
layer-tl-parser |
Parses .tl schema text into an AST |
layer-tl-gen |
Generates Rust code from the AST at build time |
layer-tl-types |
All Layer 222 constructors, functions and enums |
layer-crypto |
AES-IGE, RSA, SHA, DH key derivation |
layer-mtproto |
MTProto session, DH exchange, message framing |
layer-client |
High-level async client โ auth, bots, updates, 2FA |
layer-app |
Interactive demo binary (not published) |
layer-connect |
Raw DH connection demo (not published) |
layer/
โโโ layer-tl-parser/ โโ Parses .tl schema text โ AST
โโโ layer-tl-gen/ โโ AST โ Rust source (build-time)
โโโ layer-tl-types/ โโ Auto-generated types, functions & enums (Layer 222)
โโโ layer-crypto/ โโ AES-IGE, RSA, SHA, auth key derivation
โโโ layer-mtproto/ โโ MTProto session, DH, framing, transport
โโโ layer-client/ โโ High-level async Client API
โโโ layer-connect/ โโ Demo: raw DH + getConfig
โโโ layer-app/ โโ Demo: interactive login + update stream
๐ Quick Start
Add to your Cargo.toml:
[]
= "0.1.2"
= { = "1", = ["full"] }
๐ค User Account
use ;
async
๐ค Bot
use ;
async
โ Features
๐ Cryptography
- AES-IGE encryption / decryption (MTProto 2.0)
- RSA encryption with Telegram's public keys
- SHA-1 and SHA-256 hashing
- Auth key derivation from DH nonce material
- PQ factorization (Pollard's rho)
- Diffie-Hellman shared secret computation
๐ก MTProto
- Full 3-step DH key exchange handshake
- MTProto 2.0 encrypted sessions
- Proper message framing (salt, session_id, msg_id, seq_no)
- Abridged TCP transport
msg_container(multi-message) unpacking- gzip-packed response decompression
- Server salt tracking, pong, bad_server_salt handling
๐ฆ TL Type System
- Full
.tlschema parser - Build-time Rust code generation
- All Layer 222 constructors โ 2,295 definitions
Serializable/Deserializabletraits for all typesRemoteCalltrait for all RPC functions- Optional:
Debug,serde,name_for_id(u32)
๐ค Client
Client::connect()โ async TCP + DH + initConnection- Session persistence across restarts
- Phone code login + 2FA SRP
- Bot token login
- DC migration (PHONE_MIGRATE, USER_MIGRATE)
- FLOOD_WAIT auto-retry with configurable policy
- Async update stream with typed events
- Send / delete / fetch messages
- Dialogs list
- Username / peer resolution
- Raw
RemoteCallescape hatch for any API method
๐ง Feature Flags (layer-tl-types)
| Feature | Default | Description |
|---|---|---|
tl-api |
โ | High-level Telegram API schema |
tl-mtproto |
โ | Low-level MTProto schema |
impl-debug |
โ | #[derive(Debug)] on generated types |
impl-from-type |
โ | From<types::T> for enums::E |
impl-from-enum |
โ | TryFrom<enums::E> for types::T |
name-for-id |
โ | name_for_id(u32) -> Option<&'static str> |
impl-serde |
โ | serde::Serialize / Deserialize |
๐ Updating to a New TL Layer
# 1. Replace the schema
# 2. Build โ types regenerate automatically
๐งช Tests
๐ License
Licensed under either of, at your option:
- MIT License โ see LICENSE-MIT
- Apache License, Version 2.0 โ see LICENSE-APACHE
๐ค Author
๐ฆ Project: github.com/ankit-chaubey/layer
๐ Acknowledgements
-
Lonami โ for grammers. The architecture, design decisions, SRP math, and session handling in this project are all directly inspired by grammers. It's a fantastic library and an even better learning resource. Thank you for making it open source! ๐
-
Telegram โ for the detailed MTProto specification.
-
The Rust async ecosystem โ
tokio,getrandom,flate2, and friends.
layer โ because sometimes you have to build it yourself to truly understand it.