Implementing Minimal ADNL Client-Server in Rust for TON Blockchain

ADNL Implementation Status

The current implementation status of the ADNL features is summarized in the table below:

Feature Status
ADNL Client :white_check_mark: Implemented
ADNL Server :x: Not implemented
async :white_check_mark: Implemented
ed25519 libs curve25519_dalek + x25519_dalek

Quickstart Guide

To get started with the ADNL client implementation in Rust, you can run the following example:

cargo run --example time

Below is a brief explanation of the Rust code for the ADNL client:

use adnl::AdnlClient;
use anyhow::{anyhow, Context, Result};
use std::net::SocketAddrV4;

#[tokio::main]
async fn main() -> Result<()> {
    // Decode the liteserver public key
    let remote_public: [u8; 32] = base64::decode("JhXt7H1dZTgxQTIyGiYV4f9VUARuDxFl/1kVBjLSMB8=")
        .context("Error decode base64")?
        .try_into().map_err(|_| anyhow!("Bad public key length"))?;

    let ls_ip = "65.21.74.140";
    let ls_port = 46427;

    // Create an AdnlClient instance
    let mut client =
        AdnlClient::connect(remote_public, SocketAddrV4::new(ls_ip.parse()?, ls_port)).await?;

    // Prepare a serialized TL query for getting the time
    let mut query = hex::decode("7af98bb435263e6c95d6fecb497dfd0aa5f031e7d412986b5ce720496db512052e8f2d100cdf068c7904345aad16000000000000")?;

    // Send the query over ADNL using a random nonce
    client.send(&mut query).await?;

    // Receive the result into a vector with an 8192 bytes buffer
    let mut result = Vec::<u8>::new();
    client.receive(&mut result).await?;

    // Extract the time from the serialized TL answer
    println!(
        "received: {}",
        u32::from_le_bytes(result[result.len() - 7..result.len() - 3].try_into()?)
    );
    Ok(())
}

This example demonstrates how to connect to a TON liteserver using the ADNL client, send a query to get the current time, and receive the response. The use of async ensures that the operations are performed asynchronously, providing efficient network communication.

For further information and updates, you can refer to the crates.io package and the documentation.


Feel free to share your thoughts or questions regarding this implementation. Any contributions or suggestions for improving the ADNL client-server model in Rust are highly welcome!