this repo has no description
1use bytes::Bytes; 2use cid::Cid; 3use std::collections::HashMap; 4mod common; 5 6#[tokio::test] 7#[ignore = "depends on external live server state; run manually with --ignored"] 8async fn test_verify_live_commit() { 9 let client = reqwest::Client::new(); 10 let did = "did:plc:zp3oggo2mikqntmhrc4scby4"; 11 let resp = client 12 .get(format!( 13 "https://testpds.wizardry.systems/xrpc/com.atproto.sync.getRepo?did={}", 14 did 15 )) 16 .send() 17 .await 18 .expect("Failed to fetch repo"); 19 assert!( 20 resp.status().is_success(), 21 "getRepo failed: {}", 22 resp.status() 23 ); 24 let car_bytes = resp.bytes().await.expect("Failed to read body"); 25 println!("CAR bytes: {} bytes", car_bytes.len()); 26 let mut cursor = std::io::Cursor::new(&car_bytes[..]); 27 let (roots, blocks) = parse_car(&mut cursor).expect("Failed to parse CAR"); 28 println!("CAR roots: {:?}", roots); 29 println!("CAR blocks: {}", blocks.len()); 30 assert!(!roots.is_empty(), "No roots in CAR"); 31 let root_cid = roots[0]; 32 let root_block = blocks.get(&root_cid).expect("Root block not found"); 33 let commit = 34 jacquard_repo::commit::Commit::from_cbor(root_block).expect("Failed to parse commit"); 35 println!("Commit DID: {}", commit.did().as_str()); 36 println!("Commit rev: {}", commit.rev()); 37 println!("Commit prev: {:?}", commit.prev()); 38 println!("Commit sig length: {} bytes", commit.sig().len()); 39 let resp = client 40 .get(format!("https://plc.directory/{}", did)) 41 .send() 42 .await 43 .expect("Failed to fetch DID doc"); 44 let did_doc_text = resp.text().await.expect("Failed to read body"); 45 println!("DID doc: {}", did_doc_text); 46 let did_doc: jacquard::common::types::did_doc::DidDocument<'_> = 47 serde_json::from_str(&did_doc_text).expect("Failed to parse DID doc"); 48 let pubkey = did_doc 49 .atproto_public_key() 50 .expect("Failed to get public key") 51 .expect("No public key"); 52 println!("Public key codec: {:?}", pubkey.codec); 53 println!("Public key bytes: {} bytes", pubkey.bytes.len()); 54 match commit.verify(&pubkey) { 55 Ok(()) => println!("SIGNATURE VALID!"), 56 Err(e) => { 57 println!("SIGNATURE VERIFICATION FAILED: {:?}", e); 58 let unsigned = commit_unsigned_bytes(&commit); 59 println!("Unsigned bytes length: {} bytes", unsigned.len()); 60 panic!("Signature verification failed"); 61 } 62 } 63} 64 65fn commit_unsigned_bytes(commit: &jacquard_repo::commit::Commit<'_>) -> Vec<u8> { 66 #[derive(serde::Serialize)] 67 struct UnsignedCommit<'a> { 68 did: &'a str, 69 version: i64, 70 data: &'a cid::Cid, 71 rev: &'a jacquard::types::string::Tid, 72 prev: Option<&'a cid::Cid>, 73 #[serde(with = "serde_bytes")] 74 sig: &'a [u8], 75 } 76 let unsigned = UnsignedCommit { 77 did: commit.did().as_str(), 78 version: 3, 79 data: commit.data(), 80 rev: commit.rev(), 81 prev: commit.prev(), 82 sig: &[], 83 }; 84 serde_ipld_dagcbor::to_vec(&unsigned).unwrap() 85} 86 87#[allow(clippy::type_complexity)] 88fn parse_car( 89 cursor: &mut std::io::Cursor<&[u8]>, 90) -> Result<(Vec<Cid>, HashMap<Cid, Bytes>), Box<dyn std::error::Error>> { 91 use std::io::Read; 92 fn read_varint<R: Read>(r: &mut R) -> std::io::Result<u64> { 93 let mut result = 0u64; 94 let mut shift = 0; 95 loop { 96 let mut byte = [0u8; 1]; 97 r.read_exact(&mut byte)?; 98 result |= ((byte[0] & 0x7f) as u64) << shift; 99 if byte[0] & 0x80 == 0 { 100 break; 101 } 102 shift += 7; 103 } 104 Ok(result) 105 } 106 let header_len = read_varint(cursor)? as usize; 107 let mut header_bytes = vec![0u8; header_len]; 108 cursor.read_exact(&mut header_bytes)?; 109 #[derive(serde::Deserialize)] 110 struct CarHeader { 111 #[allow(dead_code)] 112 version: u64, 113 roots: Vec<cid::Cid>, 114 } 115 let header: CarHeader = serde_ipld_dagcbor::from_slice(&header_bytes)?; 116 let mut blocks = HashMap::new(); 117 while let Ok(len) = read_varint(cursor) { 118 let block_len = len as usize; 119 if block_len == 0 { 120 break; 121 } 122 let mut block_data = vec![0u8; block_len]; 123 if cursor.read_exact(&mut block_data).is_err() { 124 break; 125 } 126 let cid_bytes = &block_data[..]; 127 let (cid, cid_len) = parse_cid(cid_bytes)?; 128 let content = Bytes::copy_from_slice(&block_data[cid_len..]); 129 blocks.insert(cid, content); 130 } 131 Ok((header.roots, blocks)) 132} 133fn parse_cid(bytes: &[u8]) -> Result<(Cid, usize), Box<dyn std::error::Error>> { 134 if bytes[0] == 0x01 { 135 let codec = bytes[1]; 136 let _hash_type = bytes[2]; 137 let hash_len = bytes[3] as usize; 138 let cid_len = 4 + hash_len; 139 let cid = Cid::new_v1( 140 codec as u64, 141 cid::multihash::Multihash::from_bytes(&bytes[2..cid_len])?, 142 ); 143 Ok((cid, cid_len)) 144 } else { 145 Err("Unsupported CID version".into()) 146 } 147}