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