From 462a8a8f331aa0b307897e86171596665a46b884 Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Tue, 10 Sep 2024 17:30:49 +0200 Subject: [PATCH 1/2] feat: pro score signers task --- src/endpoints/quests/mod.rs | 1 + src/endpoints/quests/proscore/mod.rs | 1 + .../quests/proscore/verify_signers.rs | 87 +++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 src/endpoints/quests/proscore/mod.rs create mode 100644 src/endpoints/quests/proscore/verify_signers.rs diff --git a/src/endpoints/quests/mod.rs b/src/endpoints/quests/mod.rs index 86fab169..8620cc01 100644 --- a/src/endpoints/quests/mod.rs +++ b/src/endpoints/quests/mod.rs @@ -4,6 +4,7 @@ pub mod discord_fw_callback; pub mod ekubo; pub mod focustree; pub mod nostra; +pub mod proscore; pub mod starknet; pub mod starknetid; pub mod uri; diff --git a/src/endpoints/quests/proscore/mod.rs b/src/endpoints/quests/proscore/mod.rs new file mode 100644 index 00000000..18f9b51c --- /dev/null +++ b/src/endpoints/quests/proscore/mod.rs @@ -0,0 +1 @@ +pub mod verify_signers; diff --git a/src/endpoints/quests/proscore/verify_signers.rs b/src/endpoints/quests/proscore/verify_signers.rs new file mode 100644 index 00000000..51ae79bd --- /dev/null +++ b/src/endpoints/quests/proscore/verify_signers.rs @@ -0,0 +1,87 @@ +use std::sync::Arc; + +use crate::{ + models::{AppState, VerifyQuery}, + utils::{get_error, CompletedTasksTrait}, +}; +use axum::{ + extract::{Query, State}, + http::StatusCode, + response::IntoResponse, + Json, +}; +use axum_auto_routes::route; +use serde_json::json; +use starknet::{ + core::types::{BlockId, BlockTag, FieldElement, FunctionCall}, + macros::selector, + providers::Provider, +}; + +#[route(get, "/quests/proscore/verify_signers")] +pub async fn handler( + State(state): State>, + Query(query): Query, +) -> impl IntoResponse { + let task_id = 134; + match state + .provider + .call( + FunctionCall { + contract_address: query.addr, + entry_point_selector: selector!("get_signers"), + calldata: vec![], + }, + BlockId::Tag(BlockTag::Latest), + ) + .await + { + Ok(result) => match parse_braavos_signers(&result) { + Ok(true) => match state.upsert_completed_task(query.addr, task_id).await { + Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(), + Err(e) => get_error(format!("{}", e)), + }, + Ok(false) => get_error("You have not enabled 2FA in your wallet".to_string()), + Err(e) => get_error(format!("Error while parsing Braavos signers: {}", e)), + }, + Err(e) => get_error(format!("Error while fetching wallet guardians: {}", e)), + } +} + +fn parse_braavos_signers(data: &[FieldElement]) -> Result { + if data.len() < 4 { + return Err("Input data is too short to parse".to_string()); + } + let result_len = FieldElement::from(data.len()); + + // Determine how many values are associated with stark_signers + let stark_count = data[0]; + + // Calculate the start index for secp256r1 based on stark_signers count + let secp_start_idx = FieldElement::ONE + stark_count; + if secp_start_idx >= result_len { + return Err("Data array does not contain secp256r1 info".to_string()); + } + let secp_start_idx_dec = FieldElement::to_string(&secp_start_idx) + .parse::() + .unwrap(); + let secp_count = data[secp_start_idx_dec]; + if secp_count > FieldElement::ZERO { + return Ok(true); + } + if secp_start_idx + secp_count >= result_len { + return Err("Data array does not contain enough values for secp256r1".to_string()); + } + + // Calculate the start index for webauthn based on previous values + let webauthn_start_idx = secp_start_idx + FieldElement::ONE + secp_count; + let webauthn_start_idx_usize = FieldElement::to_string(&webauthn_start_idx) + .parse::() + .unwrap(); + let webauthn_count = data[webauthn_start_idx_usize]; + if webauthn_count > FieldElement::ZERO { + return Ok(true); + } + + Ok(false) +} From ccad2f74ca72db85458565371e4edba88890a6d2 Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Tue, 10 Sep 2024 17:41:21 +0200 Subject: [PATCH 2/2] updating task id --- src/endpoints/quests/proscore/verify_signers.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/endpoints/quests/proscore/verify_signers.rs b/src/endpoints/quests/proscore/verify_signers.rs index 51ae79bd..f7107b88 100644 --- a/src/endpoints/quests/proscore/verify_signers.rs +++ b/src/endpoints/quests/proscore/verify_signers.rs @@ -23,7 +23,7 @@ pub async fn handler( State(state): State>, Query(query): Query, ) -> impl IntoResponse { - let task_id = 134; + let task_id = 192; match state .provider .call( @@ -44,7 +44,7 @@ pub async fn handler( Ok(false) => get_error("You have not enabled 2FA in your wallet".to_string()), Err(e) => get_error(format!("Error while parsing Braavos signers: {}", e)), }, - Err(e) => get_error(format!("Error while fetching wallet guardians: {}", e)), + Err(e) => get_error(format!("Error while fetching wallet signers: {}", e)), } }