Skip to content

Commit

Permalink
Merge pull request #258 from starknet-id/feat/pro-score-signers-task
Browse files Browse the repository at this point in the history
feat: pro score signers task
  • Loading branch information
Th0rgal authored Sep 11, 2024
2 parents fabbf71 + ccad2f7 commit c5ce8e4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/endpoints/quests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/endpoints/quests/proscore/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod verify_signers;
87 changes: 87 additions & 0 deletions src/endpoints/quests/proscore/verify_signers.rs
Original file line number Diff line number Diff line change
@@ -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<Arc<AppState>>,
Query(query): Query<VerifyQuery>,
) -> impl IntoResponse {
let task_id = 192;
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 signers: {}", e)),
}
}

fn parse_braavos_signers(data: &[FieldElement]) -> Result<bool, String> {
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::<usize>()
.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::<usize>()
.unwrap();
let webauthn_count = data[webauthn_start_idx_usize];
if webauthn_count > FieldElement::ZERO {
return Ok(true);
}

Ok(false)
}

0 comments on commit c5ce8e4

Please sign in to comment.