Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for multiple verifiers, use the most recent #28

Merged
merged 1 commit into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub_struct!(Clone, Deserialize; Database {
pub_struct!(Clone, Deserialize; Contracts {
starknetid: FieldElement,
naming: FieldElement,
verifier: FieldElement,
verifiers: Vec<FieldElement>,
old_verifier: FieldElement,
pop_verifier: FieldElement,
});
Expand Down
36 changes: 29 additions & 7 deletions src/endpoints/domain_to_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ pub async fn handler(
let mut headers = HeaderMap::new();
headers.insert("Cache-Control", HeaderValue::from_static("max-age=30"));

let domains = state.starknetid_db.collection::<mongodb::bson::Document>("domains");
let domains = state
.starknetid_db
.collection::<mongodb::bson::Document>("domains");
match get_custom_resolver(&domains, &query.domain).await {
None => {}
Some(res) => {
Expand All @@ -35,7 +37,9 @@ pub async fn handler(
}
}

let starknet_ids = state.starknetid_db.collection::<mongodb::bson::Document>("id_owners");
let starknet_ids = state
.starknetid_db
.collection::<mongodb::bson::Document>("id_owners");

let domain_document = domains
.find_one(
Expand Down Expand Up @@ -71,6 +75,17 @@ pub async fn handler(
Ok(Some(doc)) => doc.get_str("owner").ok().map(String::from).unwrap(),
_ => return get_error("Error while fetching starknet-id from database".to_string()),
};
let current_social_verifiers = state
.conf
.contracts
.verifiers
.clone()
.into_iter()
.map(|x| to_hex(&x))
.collect::<Vec<_>>();
let mut all_social_verifiers = current_social_verifiers.clone();
all_social_verifiers.extend(vec![to_hex(&state.conf.contracts.old_verifier)]);

let pipeline = vec![
doc! {
"$match": {
Expand All @@ -79,7 +94,7 @@ pub async fn handler(
"field": {
"$in": ["0x0000000000000000000000000000000000000000000000000000676974687562", "0x0000000000000000000000000000000000000000000000000074776974746572", "0x00000000000000000000000000000000000000000000000000646973636f7264"]
},
"verifier": { "$in": [ to_hex(&state.conf.contracts.verifier), to_hex(&state.conf.contracts.old_verifier)] } // modified this to accommodate both verifiers
"verifier": { "$in": all_social_verifiers } // modified this to accommodate all social verifiers
},
{
"field": "0x0000000000000000000000000070726f6f665f6f665f706572736f6e686f6f64",
Expand All @@ -90,6 +105,11 @@ pub async fn handler(
"_cursor.to": null,
}
},
doc! {
"$sort": doc! {
"_cursor.from": 1
}
},
doc! {
"$group": {
"_id": { "field": "$field", "verifier": "$verifier" }, // group by both field and verifier
Expand All @@ -98,7 +118,9 @@ pub async fn handler(
},
];

let starknet_ids_data = state.starknetid_db.collection::<Document>("id_verifier_data");
let starknet_ids_data = state
.starknetid_db
.collection::<Document>("id_verifier_data");
let results = starknet_ids_data.aggregate(pipeline, None).await;

let mut github = None;
Expand All @@ -124,7 +146,7 @@ pub async fn handler(
(
"0x0000000000000000000000000000000000000000000000000000676974687562",
verifier,
) if verifier == to_hex(&state.conf.contracts.verifier) => {
) if current_social_verifiers.contains(&verifier.to_string()) => {
github = doc.get_str("data").ok().and_then(|data| {
FieldElement::from_hex_be(data)
.map(|fe| fe.to_string())
Expand All @@ -145,7 +167,7 @@ pub async fn handler(
(
"0x0000000000000000000000000000000000000000000000000074776974746572",
verifier,
) if verifier == to_hex(&state.conf.contracts.verifier) => {
) if current_social_verifiers.contains(&verifier.to_string()) => {
twitter = doc.get_str("data").ok().and_then(|data| {
FieldElement::from_hex_be(data)
.map(|fe| fe.to_string())
Expand All @@ -166,7 +188,7 @@ pub async fn handler(
(
"0x00000000000000000000000000000000000000000000000000646973636f7264",
verifier,
) if verifier == to_hex(&state.conf.contracts.verifier) => {
) if current_social_verifiers.contains(&verifier.to_string()) => {
discord = doc.get_str("data").ok().and_then(|data| {
FieldElement::from_hex_be(data)
.map(|fe| fe.to_string())
Expand Down
35 changes: 28 additions & 7 deletions src/endpoints/id_to_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ pub async fn handler(
let mut headers = HeaderMap::new();
headers.insert("Cache-Control", HeaderValue::from_static("max-age=30"));

let domains = state.starknetid_db.collection::<mongodb::bson::Document>("domains");
let starknet_ids = state.starknetid_db.collection::<mongodb::bson::Document>("id_owners");
let domains = state
.starknetid_db
.collection::<mongodb::bson::Document>("domains");
let starknet_ids = state
.starknetid_db
.collection::<mongodb::bson::Document>("id_owners");

let hex_id = to_hex(&query.id);

Expand Down Expand Up @@ -79,6 +83,16 @@ pub async fn handler(
return get_error("starknet id not found".to_string());
}

let current_social_verifiers = state
.conf
.contracts
.verifiers
.clone()
.into_iter()
.map(|x| to_hex(&x))
.collect::<Vec<_>>();
let mut all_social_verifiers = current_social_verifiers.clone();
all_social_verifiers.extend(vec![to_hex(&state.conf.contracts.old_verifier)]);
let owner = owner.unwrap();
let pipeline = vec![
doc! {
Expand All @@ -88,7 +102,7 @@ pub async fn handler(
"field": {
"$in": ["0x0000000000000000000000000000000000000000000000000000676974687562", "0x0000000000000000000000000000000000000000000000000074776974746572", "0x00000000000000000000000000000000000000000000000000646973636f7264"]
},
"verifier": { "$in": [ to_hex(&state.conf.contracts.verifier), to_hex(&state.conf.contracts.old_verifier)] } // modified this to accommodate both verifiers
"verifier": { "$in": all_social_verifiers } // modified this to accommodate both verifiers
},
{
"field": "0x0000000000000000000000000070726f6f665f6f665f706572736f6e686f6f64",
Expand All @@ -99,6 +113,11 @@ pub async fn handler(
"_cursor.to": null,
}
},
doc! {
"$sort": doc! {
"_cursor.from": 1
}
},
doc! {
"$group": {
"_id": { "field": "$field", "verifier": "$verifier" }, // group by both field and verifier
Expand All @@ -107,7 +126,9 @@ pub async fn handler(
},
];

let starknet_ids_data = state.starknetid_db.collection::<Document>("id_verifier_data");
let starknet_ids_data = state
.starknetid_db
.collection::<Document>("id_verifier_data");
let results = starknet_ids_data.aggregate(pipeline, None).await;

let mut github = None;
Expand Down Expand Up @@ -137,7 +158,7 @@ pub async fn handler(
(
"0x0000000000000000000000000000000000000000000000000000676974687562",
verifier,
) if verifier == to_hex(&state.conf.contracts.verifier) => {
) if current_social_verifiers.contains(&verifier.to_string()) => {
github = doc.get_str("data").ok().and_then(|data| {
FieldElement::from_hex_be(data)
.map(|fe| fe.to_string())
Expand All @@ -158,7 +179,7 @@ pub async fn handler(
(
"0x0000000000000000000000000000000000000000000000000074776974746572",
verifier,
) if verifier == to_hex(&state.conf.contracts.verifier) => {
) if current_social_verifiers.contains(&verifier.to_string()) => {
twitter = doc.get_str("data").ok().and_then(|data| {
FieldElement::from_hex_be(data)
.map(|fe| fe.to_string())
Expand All @@ -179,7 +200,7 @@ pub async fn handler(
(
"0x00000000000000000000000000000000000000000000000000646973636f7264",
verifier,
) if verifier == to_hex(&state.conf.contracts.verifier) => {
) if current_social_verifiers.contains(&verifier.to_string()) => {
discord = doc.get_str("data").ok().and_then(|data| {
FieldElement::from_hex_be(data)
.map(|fe| fe.to_string())
Expand Down