A library for ATProtocol identities.

fix: updating dagcbor validation function

Changed files
+15 -23
crates
atproto-attestation
src
+15 -23
crates/atproto-attestation/src/cid.rs
··· 144 144 create_dagbor_cid(&record_obj) 145 145 } 146 146 147 - /// Validates that a CID string conforms to AT Protocol attestation requirements. 147 + /// Validates that a CID string is a valid DAG-CBOR CID for AT Protocol attestations. 148 148 /// 149 149 /// This function performs strict validation to ensure the CID meets the exact 150 150 /// specifications required for AT Protocol attestations: ··· 164 164 /// 165 165 /// # Returns 166 166 /// 167 - /// * `true` if the CID meets all AT Protocol requirements 167 + /// * `true` if the CID is a valid DAG-CBOR CID with SHA-256 hash 168 168 /// * `false` if the CID is invalid or doesn't meet any requirement 169 169 /// 170 170 /// # Examples 171 171 /// 172 172 /// ```rust 173 - /// use atproto_attestation::cid::validate_cid_format; 173 + /// use atproto_attestation::cid::validate_dagcbor_cid; 174 174 /// 175 175 /// // Valid AT Protocol CID (CIDv1, DAG-CBOR, SHA-256) 176 176 /// let valid_cid = "bafyreigw5bqvbz6m3c3zjpqhxwl4njlnbbnw5xvptbx6dzfxjqcde6lt3y"; 177 - /// assert!(validate_cid_format(valid_cid)); 177 + /// assert!(validate_dagcbor_cid(valid_cid)); 178 178 /// 179 179 /// // Invalid: Empty string 180 - /// assert!(!validate_cid_format("")); 180 + /// assert!(!validate_dagcbor_cid("")); 181 181 /// 182 182 /// // Invalid: Not a CID 183 - /// assert!(!validate_cid_format("not-a-cid")); 183 + /// assert!(!validate_dagcbor_cid("not-a-cid")); 184 184 /// 185 185 /// // Invalid: CIDv0 (starts with Qm) 186 186 /// let cid_v0 = "QmYwAPJzv5CZsnA625ub3XtLxT3Tz5Lno5Wqv9eKewWKjE"; 187 - /// assert!(!validate_cid_format(cid_v0)); 187 + /// assert!(!validate_dagcbor_cid(cid_v0)); 188 188 /// ``` 189 - /// 190 - /// # Use Cases 191 - /// 192 - /// This function is typically used to: 193 - /// - Validate CIDs in attestation signatures before verification 194 - /// - Ensure CIDs in remote attestations match expected format 195 - /// - Validate user-provided CIDs in API requests 196 - /// - Verify CIDs generated by external systems conform to AT Protocol standards 197 - pub fn validate_cid_format(cid: &str) -> bool { 189 + pub fn validate_dagcbor_cid(cid: &str) -> bool { 198 190 if cid.is_empty() { 199 191 return false 200 192 } ··· 452 444 } 453 445 454 446 #[test] 455 - fn test_validate_cid_format() { 447 + fn test_validate_dagcbor_cid() { 456 448 // Test valid CID (generated from our own create_dagbor_cid function) 457 449 let valid_data = serde_json::json!({"test": "data"}); 458 450 let valid_cid = create_dagbor_cid(&valid_data).unwrap(); 459 451 let valid_cid_str = valid_cid.to_string(); 460 - assert!(validate_cid_format(&valid_cid_str), "Valid CID should pass validation"); 452 + assert!(validate_dagcbor_cid(&valid_cid_str), "Valid CID should pass validation"); 461 453 462 454 // Test empty string 463 - assert!(!validate_cid_format(""), "Empty string should fail validation"); 455 + assert!(!validate_dagcbor_cid(""), "Empty string should fail validation"); 464 456 465 457 // Test invalid CID string 466 - assert!(!validate_cid_format("not-a-cid"), "Invalid string should fail validation"); 467 - assert!(!validate_cid_format("abc123"), "Invalid string should fail validation"); 458 + assert!(!validate_dagcbor_cid("not-a-cid"), "Invalid string should fail validation"); 459 + assert!(!validate_dagcbor_cid("abc123"), "Invalid string should fail validation"); 468 460 469 461 // Test CIDv0 (starts with Qm, uses different format) 470 462 let cid_v0 = "QmYwAPJzv5CZsnA625ub3XtLxT3Tz5Lno5Wqv9eKewWKjE"; 471 - assert!(!validate_cid_format(cid_v0), "CIDv0 should fail validation"); 463 + assert!(!validate_dagcbor_cid(cid_v0), "CIDv0 should fail validation"); 472 464 473 465 // Test valid CID base32 format but wrong codec (not DAG-CBOR) 474 466 // This is a valid CID but uses raw codec (0x55) instead of DAG-CBOR (0x71) 475 467 let wrong_codec = "bafkreigw5bqvbz6m3c3zjpqhxwl4njlnbbnw5xvptbx6dzfxjqcde6lt3y"; 476 - assert!(!validate_cid_format(wrong_codec), "CID with wrong codec should fail"); 468 + assert!(!validate_dagcbor_cid(wrong_codec), "CID with wrong codec should fail"); 477 469 478 470 // Test that our constants match what we're checking 479 471 assert_eq!(DAG_CBOR_CODEC, 0x71, "DAG-CBOR codec constant should be 0x71");