leo-lang 4.4.0

The Leo programming language
Documentation
// Copyright (C) 2019-2026 Provable Inc.
// This file is part of the Leo library.

// The Leo library is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// The Leo library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use leo_errors::{LeoError, Result};

// A valid hash is 61 characters long, with preface "ab1" and all characters lowercase or numbers.
pub fn is_valid_hash(hash: &str) -> Result<(), LeoError> {
    if hash.len() != 61 {
        Err(crate::errors::invalid_input_id_len(hash, "hash").into())
    } else if !(hash.starts_with("ab1") && hash.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())) {
        Err(crate::errors::invalid_input_id(hash, "hash", "ab1").into())
    } else {
        Ok(())
    }
}

// A valid transaction id is 61 characters long, with preface "at1" and all characters lowercase or numbers.
pub fn is_valid_transaction_id(transaction: &str) -> Result<(), LeoError> {
    if transaction.len() != 61 {
        Err(crate::errors::invalid_input_id_len(transaction, "transaction").into())
    } else if !(transaction.starts_with("at1")
        && transaction.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()))
    {
        Err(crate::errors::invalid_input_id(transaction, "transaction", "at1").into())
    } else {
        Ok(())
    }
}

// A valid transition id is 61 characters long, with preface "au1" and all characters lowercase or numbers.
pub fn is_valid_transition_id(transition: &str) -> Result<(), LeoError> {
    if transition.len() != 61 {
        Err(crate::errors::invalid_input_id_len(transition, "transition").into())
    } else if !(transition.starts_with("au1")
        && transition.chars().all(|c| c.is_ascii_lowercase() || c.is_ascii_digit()))
    {
        Err(crate::errors::invalid_input_id(transition, "transition", "au1").into())
    } else {
        Ok(())
    }
}

// A valid numerical input is a u32.
pub fn is_valid_numerical_input(num: &str) -> Result<(), LeoError> {
    if num.parse::<u32>().is_err() { Err(crate::errors::invalid_numerical_input(num).into()) } else { Ok(()) }
}

// A valid height or hash.
pub fn is_valid_height_or_hash(input: &str) -> Result<(), LeoError> {
    match (is_valid_hash(input), is_valid_numerical_input(input)) {
        (Ok(_), _) | (_, Ok(_)) => Ok(()),
        _ => Err(crate::errors::invalid_height_or_hash(input).into()),
    }
}

// Checks if the string is a valid field, allowing for optional `field` suffix.
pub fn is_valid_field(field: &str) -> Result<String, LeoError> {
    let split = field.split("field").collect::<Vec<&str>>();

    if split.len() == 1 && split[0].chars().all(|c| c.is_numeric()) {
        Ok(format!("{field}field"))
    } else if split.len() == 2 && split[0].chars().all(|c| c.is_numeric()) && split[1].is_empty() {
        Ok(field.to_string())
    } else {
        Err(crate::errors::invalid_field(field).into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    // Builds a 61-character id from `prefix` padded with `pad` to the right length.
    fn id(prefix: &str, pad: char) -> String {
        let mut s = prefix.to_string();
        s.extend(std::iter::repeat_n(pad, 61 - prefix.len()));
        s
    }

    #[test]
    fn id_validators_accept_well_formed_ids() {
        assert!(is_valid_hash(&id("ab1", '0')).is_ok());
        assert!(is_valid_transaction_id(&id("at1", '0')).is_ok());
        assert!(is_valid_transition_id(&id("au1", '0')).is_ok());
    }

    #[test]
    fn id_validators_reject_wrong_length() {
        assert!(is_valid_hash("ab1").is_err());
        assert!(is_valid_transaction_id(&format!("{}x", id("at1", '0'))).is_err());
    }

    #[test]
    fn id_validators_reject_wrong_prefix() {
        assert!(is_valid_hash(&id("xy1", '0')).is_err());
        assert!(is_valid_transaction_id(&id("ab1", '0')).is_err());
        assert!(is_valid_transition_id(&id("at1", '0')).is_err());
    }

    #[test]
    fn id_validators_reject_invalid_chars() {
        // Correct prefix and length but a non-lowercase/digit char: previously slipped through.
        assert!(is_valid_hash(&id("ab1", 'A')).is_err());
        assert!(is_valid_transaction_id(&id("at1", '!')).is_err());
        assert!(is_valid_transition_id(&id("au1", 'Z')).is_err());
    }
}