Mint your DAT in RUST

Dependencies

cargo init --bin lazai-dat-rust
cd lazai-dat-rust

# Alith (from GitHub) with needed features
cargo add alith --git https://github.com/0xLazAI/alith --features "lazai,wallet,crypto,ipfs"

# Runtime & utils
cargo add tokio --features full
cargo add reqwest --features json
cargo add anyhow
cargo add hex
cargo add rand_08

Env vars

export PRIVATE_KEY=<your wallet private key>   # must start with 0x
export IPFS_JWT=<your pinata ipfs jwt>

src/main.rs

use anyhow::Result;
use reqwest;
use rand_08::thread_rng;

use alith::data::crypto::{encrypt, DecodeRsaPublicKey, Pkcs1v15Encrypt, RsaPublicKey};
use alith::data::storage::{DataStorage, PinataIPFS, UploadOptions};
use alith::lazai::{Client, ProofRequest, U256};

#[tokio::main]
async fn main() -> Result<()> {
    // PRIVATE_KEY is read by the wallet inside Client::new_default()
    let client = Client::new_default()?;
    let ipfs = PinataIPFS::default();

    // 1) Prepare privacy data and encrypt it
    let data_file_name = "your_encrypted_data.txt";
    let privacy_data = "Your Privacy Data";
    let encryption_seed = "Sign to retrieve your encryption key";

    // Sign the seed to derive a password
    let password = client
        .wallet
        .sign_message_hex(encryption_seed.as_bytes())
        .await?;
    let encrypted_data = encrypt(privacy_data, password.clone())?;

    // 2) Upload encrypted data to IPFS and get a share link
    let token = std::env::var("IPFS_JWT")?;
    let file_meta = ipfs
        .upload(
            UploadOptions::builder()
                .name(data_file_name.to_string())
                .data(encrypted_data)
                .token(token.clone())
                .build(),
        )
        .await?;
    let url = ipfs.get_share_link(token, file_meta.id).await?;

    // 3) Register the URL on LazAI
    let mut file_id = client.get_file_id_by_url(url.as_str()).await?;
    if file_id.is_zero() {
        file_id = client.add_file(url.as_str()).await?;
    }

    // 4) Request a proof from a verified computing node
    client.request_proof(file_id, U256::from(100)).await?;
    let job_id = client.file_job_ids(file_id).await?.last().cloned().unwrap();
    let job = client.get_job(job_id).await?;
    let node_info = client.get_node(job.nodeAddress).await?.unwrap();

    let node_url = node_info.url;
    let pub_key_pem = node_info.publicKey;

    // Encrypt the password with the node's RSA public key
    let pub_key = RsaPublicKey::from_pkcs1_pem(&pub_key_pem)?;
    let mut rng = thread_rng();
    let enc_key_bytes = pub_key.encrypt(&mut rng, Pkcs1v15Encrypt, password.as_bytes())?;
    let encryption_key = hex::encode(enc_key_bytes);

    // Send proof request to the node
    let resp = reqwest::Client::new()
        .post(format!("{node_url}/proof"))
        .json(
            &ProofRequest::builder()
                .job_id(job_id.to())
                .file_id(file_id.to())
                .file_url(url)
                .encryption_key(encryption_key)
                .encryption_seed(encryption_seed.to_string())
                .build(),
        )
        .send()
        .await?;

    if resp.status().is_success() {
        println!("✅ Proof request sent successfully");
    } else {
        println!("❌ Failed to send proof request: {:?}", resp);
    }

    // 5) Claim DAT reward
    client.request_reward(file_id, None).await?;
    println!("✅ Reward requested for file id {}", file_id);

    Ok(())
}

Run

cargo run

Expected output

✅ Proof request sent successfully
✅ Reward requested for file id <file_id>

Last updated