SessionCrypto class

TON Connect session cryptography (SessionCrypto in @tonconnect/protocol).

End-to-end encryption for the TON Connect HTTP bridge on top of NaCl crypto_box (X25519 + XSalsa20-Poly1305). The bridge stays untrusted: every message carries a fresh 24-byte random nonce and is framed as NONCE || TAG || CIPHERTEXT (NaCl order: the 16-byte Poly1305 authenticator comes first, then the ciphertext).

Protocol roles are symmetric: each side encrypts what it sends with the peer's public key and decrypts what it receives with the sender's public key. sessionId is this side's public key as 64 lowercase hex chars and is used as the bridge client_id.

Transport calls (encrypt/decrypt) are routed through QuickCrypto, hence through the active backend (pure-Dart or native Rust) with byte-identical results; like any backend call, a backend must be selected first. Key handling (X25519) stays pure-Dart and needs no backend. Web-safe. Interop is locked by the tweetnacl / pytonconnect golden vectors in test/crypto/xsalsa20_poly1305/.

Constructors

SessionCrypto.fromKeypair({required List<int> publicKey, required List<int> secretKey})
Restores a persisted session, verifying the public key matches the secret key (throws CryptoException on mismatch, mirroring SessionCrypto::from_persisted in ton-connect-core).
factory
SessionCrypto.generate({List<int>? secretKey})
Creates a fresh session, generating a random 32-byte secret key with QuickCrypto (web-safe Fortuna PRNG), or derives the session from an explicit secretKey (e.g. fixed vectors/tests).
factory

Properties

hashCode → int
The hash code for this object.
no setterinherited
publicKey → BuBytes
This session's public key (32 bytes).
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
sessionId → String
Bridge-level client_id: the public key as 64 lowercase hex chars.
no setter

Methods

clean() → void
Best-effort wipe of the session secret key.
decrypt(List<int> senderPublicKey, List<int> payload) → BuBytes?
Decrypts a NONCE || TAG || CIPHERTEXT bridge payload from senderPublicKey. Returns the plaintext, or null when truncated or unauthenticated (never throws on attacker input).
decryptFromBase64(List<int> senderPublicKey, String payload) → String?
Decrypts a base64 bridge payload. Returns null on bad base64, truncation, or authentication failure.
decryptString(List<int> senderPublicKey, List<int> payload) → String?
Decrypts payload and UTF-8 decodes it (TON Connect decrypt returns a string). Returns null on authentication failure or when the plaintext is not valid UTF-8 (never throws on attacker input).
encrypt(List<int> peerPublicKey, List<int> plaintext, {List<int>? nonce}) → BuBytes
Encrypts plaintext for peerPublicKey, returning NONCE || TAG || CIPHERTEXT. A fresh random nonce is generated per call; pass nonce only for deterministic vectors/tests.
encryptString(List<int> peerPublicKey, String message, {List<int>? nonce}) → BuBytes
Encrypts a UTF-8 message for peerPublicKey (TON Connect encrypt takes a string). Returns raw NONCE || TAG || CIPHERTEXT bytes; base64-encode them for the bridge POST /message body.
encryptToBase64(List<int> peerPublicKey, String message, {List<int>? nonce}) → String
Same as encryptString but base64-encoded for the bridge body.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
stringifyKeypair() → TonConnectKeyPair
Exports the keypair as hex strings for persistent storage.
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited

Constants

keyLength → const int
Session key length in bytes.
minMessageLength → const int
Minimum length of a bridge payload (NONCE || TAG || CIPHERTEXT).
nonceLength → const int
Per-message nonce length in bytes.
tagLength → const int
Poly1305 tag length in bytes (inside the ciphertext).