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