Docs›WASM Guides›VIP Validation
WASM Module Guide

VIP Validation Module

This module handles single-use ticket validation. It reads a token from the guest, checks if it exists in the valid tokens list, and then burns it so it cannot be re-used.

#Validation Logic

Because this runs entirely on the Edge Node without internet, the list of valid tokens must be pre-loaded into the Sled database before the event.

src/lib.rs
#![no_std]

use qos_sdk::{state, input, output, log_msg, LogLevel};

#[no_mangle]
pub extern "C" fn run() -> i32 {
    let payload = input::read().unwrap_or_default();
    let token = core::str::from_utf8(&payload).unwrap_or("");
    
    // Check if token exists in valid list
    // Pre-loaded as "vip:valid:TOKEN_STRING" -> "1"
    // Wait, let's do something simpler: check if it has been used.
    
    // We expect the Admin HUD to pre-load state like:
    // state::set("vip:token:A1B2", b"valid");
    
    // 1. Construct the key (in a real app, use a string formatter crate)
    // Here we assume the token is exactly 4 chars for the example
    if token.len() != 4 {
        output::write(b"{\"status\":\"error\", \"msg\":\"Invalid token length\"}");
        return 1;
    }
    
    let mut key = [0u8; 14];
    key[..10].copy_from_slice(b"vip:token:");
    key[10..].copy_from_slice(token.as_bytes());
    
    let key_str = core::str::from_utf8(&key).unwrap();
    
    if let Some(val) = state::get(key_str) {
        if val == b"valid" {
            // Valid token! Burn it.
            state::set(key_str, b"used").unwrap();
            log_msg(LogLevel::Info, "VIP entry granted");
            output::write(b"{\"status\":\"granted\"}");
            return 0;
        } else if val == b"used" {
            log_msg(LogLevel::Warn, "Replay attack attempted");
            output::write(b"{\"status\":\"denied\", \"msg\":\"Ticket already used\"}");
            return 1;
        }
    }
    
    output::write(b"{\"status\":\"denied\", \"msg\":\"Invalid ticket\"}");
    1
}