Server tools to backfill, tail, mirror, and verify PLC logs
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

mirror: return semantically-correct DIDs

geesawra 4e33100a ee7a33cc

+94 -6
+94 -6
src/mirror.rs
··· 14 14 }; 15 15 use reqwest::{Client, Url}; 16 16 use serde::{Deserialize, Serialize}; 17 - use std::{net::SocketAddr, path::PathBuf, time::Duration}; 17 + use std::{collections::HashMap, net::SocketAddr, path::PathBuf, time::Duration}; 18 + use tokio_postgres::types::Type; 18 19 19 20 #[derive(Clone)] 20 21 struct State { ··· 296 297 297 298 #[derive(Serialize, Deserialize)] 298 299 #[serde(rename_all = "camelCase")] 300 + struct VerificationMethod { 301 + id: String, 302 + #[serde(rename = "type")] 303 + typ: String, 304 + controller: String, 305 + public_key_multibase: String, 306 + } 307 + 308 + impl Default for VerificationMethod { 309 + fn default() -> Self { 310 + Self { 311 + id: "#atproto".to_string(), 312 + typ: "Multikey".to_string(), 313 + controller: Default::default(), 314 + public_key_multibase: Default::default(), 315 + } 316 + } 317 + } 318 + 319 + #[derive(Serialize, Deserialize, Default)] 320 + #[serde(rename_all = "camelCase")] 321 + struct Service { 322 + id: String, 323 + #[serde(rename = "type")] 324 + typ: String, 325 + service_endpoint: String, 326 + } 327 + 328 + #[derive(Serialize, Deserialize)] 329 + #[serde(rename_all = "camelCase")] 299 330 struct DidResponse { 300 331 #[serde(rename = "@context")] 301 332 context: Vec<String>, 302 333 id: String, 303 - #[serde(flatten)] 304 - op: serde_json::Value, 334 + also_known_as: Vec<String>, 335 + verification_method: Vec<VerificationMethod>, 336 + service: Vec<Service>, 305 337 } 306 338 307 339 impl Default for DidResponse { ··· 313 345 "https://w3id.org/security/suites/secp256k1-2019/v1".to_string(), 314 346 ], 315 347 id: Default::default(), 316 - op: serde_json::json!({}), 348 + also_known_as: Default::default(), 349 + verification_method: Default::default(), 350 + service: Default::default(), 317 351 } 318 352 } 319 353 } 320 354 355 + #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 356 + #[serde(rename_all = "camelCase")] 357 + pub struct Operation { 358 + pub services: HashMap<String, PlcService>, 359 + pub also_known_as: Vec<String>, 360 + pub rotation_keys: Vec<String>, 361 + pub verification_methods: HashMap<String, String>, 362 + } 363 + 364 + #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] 365 + #[serde(rename_all = "camelCase")] 366 + pub struct PlcService { 367 + #[serde(rename = "type")] 368 + pub type_field: String, 369 + pub endpoint: String, 370 + } 371 + 321 372 #[handler] 322 373 async fn get_did(Path(did): Path<String>, Data(state): Data<&State>) -> Result<Response> { 323 374 let ds = match state.ds.as_ref() { ··· 350 401 // Add more fields to remove as needed 351 402 } 352 403 353 - let dr = DidResponse { 404 + let mut dr = DidResponse { 354 405 id: did.to_string(), 355 - op: op_value, 356 406 ..Default::default() 357 407 }; 358 408 409 + let op: Operation = match serde_json::from_value(op_value) { 410 + Ok(o) => o, 411 + Err(e) => { 412 + return Ok(e 413 + .to_string() 414 + .with_status(reqwest::StatusCode::INTERNAL_SERVER_ERROR) 415 + .into_response()); 416 + } 417 + }; 418 + 419 + dr.also_known_as = op.also_known_as; 420 + dr.verification_method = op 421 + .verification_methods 422 + .into_iter() 423 + .map(|data| { 424 + let (typ, data) = data; 425 + VerificationMethod { 426 + id: format!("{}#{}", did.clone(), typ), 427 + typ: "Multikey".to_string(), 428 + controller: did.clone(), 429 + public_key_multibase: data.strip_prefix("did:key:").unwrap_or(&data).to_string(), 430 + } 431 + }) 432 + .collect(); 433 + 434 + dr.service = op 435 + .services 436 + .into_iter() 437 + .map(|data| { 438 + let (typ, data) = data; 439 + 440 + Service { 441 + id: format!("#{}", typ), 442 + typ: data.type_field, 443 + service_endpoint: data.endpoint, 444 + } 445 + }) 446 + .collect(); 359 447 Ok(Json(dr).into_response()) 360 448 } 361 449