Skip to content

Commit

Permalink
fix: feedback from review
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed May 29, 2024
1 parent dcc1a4e commit bcd072c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,6 @@ pub_struct!(Clone, Debug, Deserialize; EvmRecordVerifier {

pub_struct!(Clone, Debug, Deserialize; FreeDomains {
priv_key: FieldElement,
start_time: i64,
end_time: i64,
});

#[derive(Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion src/ecdsa_sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use starknet::core::{
};
use starknet_crypto::{rfc6979_generate_k, sign, SignError};

pub fn ecdsa_sign(
pub fn non_determinist_ecdsa_sign(
private_key: &FieldElement,
message_hash: &FieldElement,
) -> Result<ExtendedSignature, EcdsaSignError> {
Expand Down
40 changes: 33 additions & 7 deletions src/endpoints/campaigns/get_free_domain.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{ecdsa_sign::ecdsa_sign, models::AppState, utils::get_error};
use crate::{ecdsa_sign::non_determinist_ecdsa_sign, models::AppState, utils::get_error};
use axum::{
extract::{Query, State},
response::{IntoResponse, Json},
Expand All @@ -16,6 +16,7 @@ use std::sync::Arc;
pub struct FreeDomainQuery {
addr: FieldElement,
code: String,
domain: String,
}

lazy_static::lazy_static! {
Expand All @@ -32,11 +33,12 @@ pub async fn handler(
State(state): State<Arc<AppState>>,
Query(query): Query<FreeDomainQuery>,
) -> impl IntoResponse {
// verify campaign is active
let now = chrono::Utc::now().timestamp();
if now < state.conf.free_domains.start_time || now > state.conf.free_domains.end_time {
return get_error("Campaign not active".to_string());
// assert domain is a root domain & get domain length
let domain_parts = query.domain.split('.').collect::<Vec<&str>>();
if domain_parts.len() != 2 {
return get_error("Domain must be a root domain".to_string());
}
let domain_len = domain_parts[0].len();

let free_domains = state
.starknetid_db
Expand All @@ -46,7 +48,6 @@ pub async fn handler(
doc! {
"code" : &query.code,
"enabled": true,
"type": "5+letters",
},
None,
)
Expand All @@ -61,9 +62,34 @@ pub async fn handler(
return get_error("Error while verifying coupon code availability".to_string());
}

// Check domain length matches the coupon type
if let Ok(coupon_type) = doc.get_str("type") {
if let Some(pos) = coupon_type.find('+') {
if let Ok(domain_min_size) = coupon_type[..pos].parse::<usize>() {
if domain_len < domain_min_size {
return get_error(format!(
"Domain length is less than {}",
domain_min_size
));
}
} else {
return get_error(
"Failed to parse the numeric part of the coupon type".to_string(),
);
}
} else {
return get_error("Invalid coupon type format".to_string());
}
} else {
return get_error("Error while verifying coupon code type".to_string());
}

// generate the signature
let message_hash = pedersen_hash(&query.addr, &FREE_DOMAIN_STR);
match ecdsa_sign(&state.conf.free_domains.priv_key.clone(), &message_hash) {
match non_determinist_ecdsa_sign(
&state.conf.free_domains.priv_key.clone(),
&message_hash,
) {
Ok(signature) => {
// we blacklist the coupon code
match free_domains
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints/crosschain/ethereum/text_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ pub async fn get_verifier_data(
id: FieldElement,
record_config: &EvmRecordVerifier,
) -> Option<String> {
let mut calls: Vec<FieldElement> = vec![FieldElement::from(record_config.verifier_contracts.len())];
let mut calls: Vec<FieldElement> =
vec![FieldElement::from(record_config.verifier_contracts.len())];
for verifier in &record_config.verifier_contracts {
calls.push(config.contracts.starknetid);
calls.push(selector!("get_verifier_data"));
Expand Down Expand Up @@ -160,7 +161,6 @@ pub async fn get_verifier_data(
None
}
}

}
Err(err) => {
println!("Error while fetching balances: {:?}", err);
Expand Down

0 comments on commit bcd072c

Please sign in to comment.