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
CryptoExceptionon mismatch, mirroringSessionCrypto::from_persistedinton-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 || CIPHERTEXTbridgepayloadfromsenderPublicKey. Returns the plaintext, ornullwhen truncated or unauthenticated (never throws on attacker input). -
decryptFromBase64(
List< int> senderPublicKey, String payload) → String? -
Decrypts a base64 bridge payload. Returns
nullon bad base64, truncation, or authentication failure. -
decryptString(
List< int> senderPublicKey, List<int> payload) → String? -
Decrypts
payloadand UTF-8 decodes it (TON Connectdecryptreturns a string). Returnsnullon 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
plaintextforpeerPublicKey, returningNONCE || TAG || CIPHERTEXT. A fresh random nonce is generated per call; passnonceonly for deterministic vectors/tests. -
encryptString(
List< int> peerPublicKey, String message, {List<int> ? nonce}) → BuBytes -
Encrypts a UTF-8
messageforpeerPublicKey(TON Connectencrypttakes a string). Returns rawNONCE || TAG || CIPHERTEXTbytes; base64-encode them for the bridgePOST /messagebody. -
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).