Rust and WASM did-method-plc tools and structures
1//! Example of parsing and working with DID documents 2 3use atproto_plc::{Did, DidBuilder, PlcState, ServiceEndpoint, SigningKey}; 4 5fn main() -> Result<(), Box<dyn std::error::Error>> { 6 println!("DID Document Parsing Example\n"); 7 println!("{}", "=".repeat(60)); 8 9 // Create a DID and document 10 println!("\n1. Creating a new DID..."); 11 let rotation_key = SigningKey::generate_p256(); 12 let signing_key = SigningKey::generate_k256(); 13 14 let (did, operation, _keys) = DidBuilder::new() 15 .add_rotation_key(rotation_key) 16 .add_verification_method("atproto".into(), signing_key) 17 .add_also_known_as("at://alice.bsky.social".into()) 18 .add_service( 19 "atproto_pds".into(), 20 ServiceEndpoint::new( 21 "AtprotoPersonalDataServer".into(), 22 "https://pds.example.com".into(), 23 ), 24 ) 25 .build()?; 26 27 println!(" Created DID: {}", did); 28 29 // Extract state from operation 30 println!("\n2. Extracting PLC state from operation..."); 31 let state = if let Some(rotation_keys) = operation.rotation_keys() { 32 let mut state = PlcState::new(); 33 state.rotation_keys = rotation_keys.to_vec(); 34 // In a real scenario, we'd extract all fields from the operation 35 state 36 } else { 37 PlcState::new() 38 }; 39 40 println!(" Rotation keys: {}", state.rotation_keys.len()); 41 42 // Convert to W3C DID Document 43 println!("\n3. Converting to W3C DID Document..."); 44 let doc = state.to_did_document(&did); 45 46 println!(" Document ID: {}", doc.id); 47 println!(" Verification methods: {}", doc.verification_method.len()); 48 println!(" Services: {}", doc.service.len()); 49 println!(" Also known as: {}", doc.also_known_as.len()); 50 51 // Serialize to JSON 52 println!("\n4. Serializing to JSON..."); 53 let json = serde_json::to_string_pretty(&doc)?; 54 println!("\nDID Document (JSON):\n{}", json); 55 56 // Parse back from JSON 57 println!("\n5. Parsing back from JSON..."); 58 let parsed_doc: atproto_plc::DidDocument = serde_json::from_str(&json)?; 59 println!(" ✅ Successfully parsed!"); 60 println!(" Parsed ID: {}", parsed_doc.id); 61 62 // Validate 63 println!("\n6. Validating document..."); 64 match parsed_doc.validate() { 65 Ok(_) => println!(" ✅ Document is valid!"), 66 Err(e) => println!(" ❌ Validation error: {}", e), 67 } 68 69 println!("\n{}", "=".repeat(60)); 70 println!("\nExample complete!"); 71 72 Ok(()) 73}