ma_core/ipfs/
gateway_resolver.rs1use super::gateway::GatewayPool;
8use super::ttl_cache::{Cached, TtlCache};
9use crate::Document;
10use async_trait::async_trait;
11use web_time::Duration;
12
13#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
18#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
19pub trait DidDocumentResolver: Send + Sync {
20 async fn resolve(&self, did: &str) -> crate::error::Result<Document>;
21
22 fn set_cache_ttls(&self, _positive_ttl: Duration, _negative_ttl: Duration) {}
26
27 fn cache_ttls(&self) -> Option<(Duration, Duration)> {
29 None
30 }
31}
32
33#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
38#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
39pub trait IpnsPathResolver: Send + Sync {
40 async fn resolve_ipns_path(&self, path: &str) -> crate::error::Result<String>;
41}
42
43pub struct IpfsGatewayResolver {
48 pool: GatewayPool,
49 cache: TtlCache<Vec<u8>>,
50}
51
52impl Default for IpfsGatewayResolver {
53 fn default() -> Self {
55 Self::from_pool(GatewayPool::default())
56 }
57}
58
59impl From<GatewayPool> for IpfsGatewayResolver {
60 fn from(pool: GatewayPool) -> Self {
61 Self::from_pool(pool)
62 }
63}
64
65impl IpfsGatewayResolver {
66 #[must_use]
68 pub fn public_default() -> Self {
69 Self::from_pool(GatewayPool::public_default())
70 }
71
72 #[must_use]
76 pub fn new(gateway_url: impl Into<String>) -> Self {
77 Self::from_pool(GatewayPool::new(gateway_url))
78 }
79
80 #[must_use]
83 pub fn local_first(gateway_url: impl Into<String>) -> Self {
84 Self::from_pool(GatewayPool::local_first(gateway_url))
85 }
86
87 fn from_pool(pool: GatewayPool) -> Self {
88 Self {
89 pool,
90 cache: TtlCache::new(Duration::from_mins(1), Duration::from_secs(10)),
91 }
92 }
93
94 #[must_use]
96 pub fn pool(&self) -> &GatewayPool {
97 &self.pool
98 }
99
100 #[must_use]
101 pub fn with_cache_ttls(self, positive_ttl: Duration, negative_ttl: Duration) -> Self {
102 self.cache.set_ttls(positive_ttl, negative_ttl);
103 self
104 }
105
106 #[must_use]
110 pub fn with_base_cooldown(mut self, cooldown: Duration) -> Self {
111 self.pool = self.pool.with_base_cooldown(cooldown);
112 self
113 }
114
115 #[must_use]
118 pub fn with_request_timeout(self, timeout: Duration) -> Self {
119 self.pool.set_request_timeout(Some(timeout));
120 self
121 }
122
123 pub fn set_request_timeout(&self, timeout: Option<Duration>) {
126 self.pool.set_request_timeout(timeout);
127 }
128
129 pub async fn resolve_ipns_path(&self, path: &str) -> crate::error::Result<String> {
131 self.pool.resolve_ipns_path(path).await
132 }
133
134 fn cached_result(cached: Cached<Vec<u8>>, did: String) -> crate::error::Result<Document> {
135 match cached {
136 Cached::Hit(body) => {
137 parse_document_bytes(&body).map_err(|detail| crate::error::Error::Resolution {
138 did,
139 detail: format!("cached document parse failed: {detail}"),
140 })
141 }
142 Cached::Miss(detail) => Err(crate::error::Error::Resolution { did, detail }),
143 }
144 }
145}
146
147#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
148#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
149impl DidDocumentResolver for IpfsGatewayResolver {
150 async fn resolve(&self, did: &str) -> crate::error::Result<Document> {
151 let parsed = crate::Did::try_from(did).map_err(crate::error::Error::Validation)?;
152 let did_key = did.to_string();
153
154 if let Some(cached) = self.cache.read(&did_key) {
155 return Self::cached_result(cached, did_key);
156 }
157
158 let resolve_lock = self.cache.lock_for(&did_key);
159 let _resolve_guard = resolve_lock.lock().await;
160
161 if let Some(cached) = self.cache.read(&did_key) {
164 self.cache.release_lock(&did_key, &resolve_lock);
165 return Self::cached_result(cached, did_key);
166 }
167
168 let path = format!("/ipns/{}", parsed.ipns);
169 let fetched = self
170 .pool
171 .fetch(&path, Some("application/vnd.ipld.dag-cbor"), |body| {
172 parse_document_bytes(body)
173 .map(|document| (document, body.to_vec()))
174 .map_err(|detail| format!("invalid DID document: {detail}"))
175 })
176 .await;
177
178 match fetched {
179 Ok((document, body)) => {
180 self.cache.write_hit(did_key.clone(), body);
181 self.cache.release_lock(&did_key, &resolve_lock);
182 Ok(document)
183 }
184 Err(detail) => {
185 tracing::warn!(did = %did_key, error = %detail, "DID document resolve failed");
186 self.cache.write_miss(did_key.clone(), detail.clone());
187 self.cache.release_lock(&did_key, &resolve_lock);
188 Err(crate::error::Error::Resolution {
189 did: did_key,
190 detail,
191 })
192 }
193 }
194 }
195
196 fn set_cache_ttls(&self, positive_ttl: Duration, negative_ttl: Duration) {
197 self.cache.set_ttls(positive_ttl, negative_ttl);
198 }
199
200 fn cache_ttls(&self) -> Option<(Duration, Duration)> {
201 Some((self.cache.positive_ttl(), self.cache.negative_ttl()))
202 }
203}
204
205#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
206#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
207impl IpnsPathResolver for IpfsGatewayResolver {
208 async fn resolve_ipns_path(&self, path: &str) -> crate::error::Result<String> {
209 self.pool.resolve_ipns_path(path).await
210 }
211}
212
213fn parse_document_bytes(bytes: &[u8]) -> std::result::Result<Document, String> {
214 if let Ok(doc) = Document::decode(bytes) {
217 return Ok(doc);
218 }
219 serde_json::from_slice::<Document>(bytes)
222 .map_err(|json_err| format!("CBOR decode failed and JSON fallback also failed: {json_err}"))
223}
224
225#[cfg(test)]
226mod tests {
227 use super::parse_document_bytes;
228 use crate::generate_identity_from_secret;
229
230 #[test]
231 fn parses_dag_cbor_documents() {
232 let identity = generate_identity_from_secret([7u8; 32]).expect("identity");
233 let cbor = identity.document.encode().expect("cbor");
234 let parsed = parse_document_bytes(&cbor).expect("parsed cbor");
235 assert_eq!(parsed, identity.document);
236 }
237
238 #[test]
239 fn rejects_non_document_payloads() {
240 let err = parse_document_bytes(b"<html>nope</html>").expect_err("invalid payload");
241 assert!(err.contains("CBOR decode failed"));
242 }
243
244 #[test]
245 fn parses_json_fallback_when_cbor_fails() {
246 let identity = generate_identity_from_secret([5u8; 32]).expect("identity");
247 let json = serde_json::to_vec(&identity.document).expect("json serialize");
248 let parsed = parse_document_bytes(&json).expect("JSON fallback should succeed");
249 assert_eq!(parsed, identity.document);
250 }
251
252 #[test]
253 fn resolver_constructors_delegate_to_pool() {
254 use super::IpfsGatewayResolver;
255 let resolver = IpfsGatewayResolver::new("https://example.test/ipfs");
256 assert_eq!(
257 resolver.pool().gateways(),
258 [
259 "https://example.test/ipfs/".to_string(),
260 "https://dweb.link/".to_string(),
261 "https://4everland.io/".to_string(),
262 ]
263 );
264 }
265}