diff --git a/server/src/endpoints/deposit.rs b/server/src/endpoints/deposit.rs index 965cc109..b9c850ba 100644 --- a/server/src/endpoints/deposit.rs +++ b/server/src/endpoints/deposit.rs @@ -1,11 +1,11 @@ use std::str::FromStr; -use bitcoin::hashes::sha256; +use bitcoin::hashes::{sha256, Hash}; use rocket::{serde::json::Json, response::status, State, http::Status}; use secp256k1_zkp::{XOnlyPublicKey, schnorr::Signature, Message, Secp256k1, PublicKey}; use serde::{Serialize, Deserialize}; use serde_json::{Value, json}; -use crate::server::StateChainEntity; +use crate::{server::StateChainEntity, server_config::Enclave}; #[get("/deposit/get_token")] pub async fn get_token(statechain_entity: &State) -> status::Custom> { @@ -75,6 +75,37 @@ pub async fn token_init(statechain_entity: &State) -> status:: return status::Custom(Status::Ok, Json(response_body)); } +fn get_random_enclave_index(statechain_id: &str, enclaves: &Vec) -> Result { + let index_from_statechain_id = get_enclave_index_from_statechain_id(statechain_id, enclaves.len() as u32); + + println!("index_from_statechain_id: {}", index_from_statechain_id); + + let selected_enclave = enclaves.get(index_from_statechain_id).unwrap(); + if selected_enclave.allow_deposit { + return Ok(index_from_statechain_id); + } else { + for (i, enclave) in enclaves.iter().enumerate() { + if enclave.allow_deposit { + return Ok(i); + } + } + } + + Err("No valid enclave found with allow_deposit set to true".to_string()) +} + +fn get_enclave_index_from_statechain_id(statechain_id: &str, enclave_array_len: u32) -> usize { + let hash = sha256::Hash::hash(statechain_id.as_bytes()); + let hash_bytes = hash.as_byte_array(); + let mut bytes = [0u8; 16]; + bytes.copy_from_slice(&hash_bytes[..16]); + let random_number = u128::from_be_bytes(bytes); + + println!("statechain_id: {}", statechain_id); + println!("random_number: {}", random_number); + return (random_number % enclave_array_len as u128) as usize; +} + #[post("/deposit/init/pod", format = "json", data = "")] pub async fn post_deposit(statechain_entity: &State, deposit_msg1: Json) -> status::Custom> { @@ -136,7 +167,7 @@ pub async fn post_deposit(statechain_entity: &State, deposit_m let config = crate::server_config::ServerConfig::load(); - let enclave_index = crate::endpoints::utils::get_random_enclave_index(&statechain_id, &config.enclaves).unwrap(); + let enclave_index = get_random_enclave_index(&statechain_id, &config.enclaves).unwrap(); let lockbox_endpoint = config.enclaves.get(enclave_index).unwrap().url.clone(); let path = "get_public_key"; diff --git a/server/src/endpoints/utils.rs b/server/src/endpoints/utils.rs index 94d9ab87..87bdbc2f 100644 --- a/server/src/endpoints/utils.rs +++ b/server/src/endpoints/utils.rs @@ -54,37 +54,6 @@ pub async fn validate_signature(pool: &sqlx::PgPool, signed_message_hex: &str, s secp.verify_schnorr(&signed_message, &msg, &auth_key).is_ok() } -pub fn get_random_enclave_index(statechain_id: &str, enclaves: &Vec) -> Result { - let index_from_statechain_id = get_enclave_index_from_statechain_id(statechain_id, enclaves.len() as u32); - - println!("index_from_statechain_id: {}", index_from_statechain_id); - - let selected_enclave = enclaves.get(index_from_statechain_id).unwrap(); - if selected_enclave.allow_deposit { - return Ok(index_from_statechain_id); - } else { - for (i, enclave) in enclaves.iter().enumerate() { - if enclave.allow_deposit { - return Ok(i); - } - } - } - - Err("No valid enclave found with allow_deposit set to true".to_string()) -} - -fn get_enclave_index_from_statechain_id(statechain_id: &str, enclave_array_len: u32) -> usize { - let hash = sha256::Hash::hash(statechain_id.as_bytes()); - let hash_bytes = hash.as_byte_array(); - let mut bytes = [0u8; 16]; - bytes.copy_from_slice(&hash_bytes[..16]); - let random_number = u128::from_be_bytes(bytes); - - println!("statechain_id: {}", statechain_id); - println!("random_number: {}", random_number); - return (random_number % enclave_array_len as u128) as usize; -} - #[get("/info/config")] pub async fn info_config() -> status::Custom> {