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