Documentation
    Preparing search index...

    Class SessionCrypto

    Implements the TON Connect session-encryption protocol on top of NaCl's crypto_box.

    The protocol is symmetric: each side encrypts the messages it sends and decrypts the messages it receives. On the dApp side that means encrypting outgoing AppMessage and decrypting incoming WalletMessage; the wallet does the reverse.

    import { SessionCrypto, Base64, hexToByteArray } from '@tonconnect/protocol';

    // Generate a fresh session
    const session = new SessionCrypto();
    const myClientId = session.sessionId; // hex public key (sent to the peer)

    // Encrypt an outgoing message for the peer
    const ciphertext = session.encrypt(
    JSON.stringify(message),
    hexToByteArray(peerClientId)
    );

    // Decrypt an incoming message from the peer
    const plaintext = session.decrypt(
    Base64.decode(bridgeMessage.message).toUint8Array(),
    hexToByteArray(bridgeMessage.from)
    );
    Index

    Constructors

    Properties

    Methods

    Constructors

    • Reuse an existing KeyPair (resuming a session) or generate a fresh one (crypto_box.keyPair()) when omitted.

      Parameters

      Returns SessionCrypto

    Properties

    sessionId: string

    Bridge-level client_id — the public key as a 64-character lowercase hex string. Share with the peer during connect; treat as semi-private (do not publish broadly).

    Methods

    • Decrypt the nonce || ciphertext blob received from the bridge. Throws if nacl.box.open rejects the message — wrong key, truncated input or tampered ciphertext.

      Parameters

      • message: Uint8Array
      • senderPublicKey: Uint8Array

      Returns string

    • Encrypt message for receiverPublicKey using a fresh 24-byte random nonce. Returns nonce || ciphertext as raw bytes; base64-encode this value before placing it in the bridge POST /message body.

      Parameters

      • message: string
      • receiverPublicKey: Uint8Array

      Returns Uint8Array

    • Export the underlying keypair as a KeyPair of hex strings. Persist this in dApp / wallet storage to resume the session later.

      Returns KeyPair