-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from starknet-id/feat/pro-score-signers-task
feat: pro score signers task
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod verify_signers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |