Skip to content

Commit

Permalink
comments for utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Ad96el committed Dec 6, 2023
1 parent e5b24e5 commit 9b52927
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ use crate::{
User,
};

/// Get the current block number from the blockchain.
///
/// # Arguments
/// - `config`: A reference to the `Configuration` struct containing blockchain configuration.
///
/// # Returns
/// A `Result` containing the current block number or an error.
pub async fn get_current_block(config: &Configuration) -> Result<u64, subxt::Error> {
let api = config.get_client().await?;
let block_number = api
Expand All @@ -25,6 +32,13 @@ pub async fn get_current_block(config: &Configuration) -> Result<u64, subxt::Err
Ok(block_number)
}

/// Get the next transaction counter for a user from the blockchain.
///
/// # Arguments
/// - `config`: A reference to the `Configuration` struct containing blockchain configuration.
///
/// # Returns
/// A `Result` containing the next transaction counter or an error.
pub async fn get_next_tx_counter(config: &Configuration) -> Result<u64, AppError> {
let api = config.get_client().await?;
let did_doc_addr = kilt::storage().did().did(&config.get_did()?);
Expand All @@ -39,6 +53,14 @@ pub async fn get_next_tx_counter(config: &Configuration) -> Result<u64, AppError
Ok(tx_counter)
}

/// Calculate a digital signature for a given data payload.
///
/// # Arguments
/// - `call`: The data payload to be signed.
/// - `config`: The `Configuration` containing signing information.
///
/// # Returns
/// A `Result` containing the digital signature or an error.
pub fn calculate_signature(
call: &[u8],
config: Configuration,
Expand All @@ -50,6 +72,14 @@ pub fn calculate_signature(
))
}

/// Check if a user is allowed to see certain attestation data.
///
/// # Arguments
/// - `user`: The user data.
/// - `attestations`: A vector of attestation responses.
///
/// # Returns
/// An `Result` indicating whether the user is allowed to see the data or an error.
pub fn is_user_allowed_to_see_data(
user: ReqData<User>,
attestatations: &Vec<AttestationResponse>,
Expand All @@ -68,6 +98,15 @@ pub fn is_user_allowed_to_see_data(
}
}

/// Check if a user is allowed to update certain attestation data.
///
/// # Arguments
/// - `user`: The user data.
/// - `attestation_id`: The ID of the attestation request.
/// - `db_executor`: A reference to the database executor.
///
/// # Returns
/// An `Result` indicating whether the user is allowed to update the data or an error.
pub async fn is_user_allowed_to_update_data(
user: ReqData<User>,
attestation_id: &Uuid,
Expand All @@ -84,6 +123,13 @@ pub async fn is_user_allowed_to_update_data(
}
}

/// Check if a user is an administrator.
///
/// # Arguments
/// - `user`: The user data.
///
/// # Returns
/// An `Result` indicating whether the user is an administrator or an error.
pub fn is_user_admin(user: ReqData<User>) -> Result<(), actix_web::Error> {
if !user.is_admin {
Err(actix_web::error::ErrorUnauthorized(
Expand Down

0 comments on commit 9b52927

Please sign in to comment.