Skip to main content

ma_core/kubo/
resolver.rs

1//! DID/IPNS resolution backed by the Kubo RPC API.
2//!
3//! Native-only (`kubo` feature). This is the single DID/IPNS resolver for
4//! native runtimes, using the Kubo RPC endpoints (`/api/v0/dag/get`,
5//! `/api/v0/name/resolve`). It is the read-side companion to the Kubo
6//! publishing backend. wasm hosts implement the same traits with their own
7//! backend (verified-fetch).
8
9use async_trait::async_trait;
10
11use super::kubo::{fetch_did_document, name_resolve};
12use crate::ipfs::resolver::{DidDocumentResolver, IpnsPathResolver};
13use crate::{Did, Document};
14
15/// Resolves DID documents and IPNS paths via a local Kubo daemon's RPC API.
16///
17/// The ma runtime resolves peer DIDs through its own node
18/// (`http://127.0.0.1:5001` by default) rather than through a public HTTP
19/// gateway. Pass this into [`new_ma_endpoint`](crate::new_ma_endpoint) exactly
20/// like the gateway resolver.
21pub struct KuboDidResolver {
22    kubo_url: String,
23}
24
25impl KuboDidResolver {
26    /// Build a resolver for the given Kubo RPC API URL (e.g.
27    /// `http://127.0.0.1:5001`).
28    #[must_use]
29    pub fn new(kubo_url: impl Into<String>) -> Self {
30        Self {
31            kubo_url: kubo_url.into(),
32        }
33    }
34
35    /// The Kubo RPC API URL this resolver talks to.
36    #[must_use]
37    pub fn kubo_url(&self) -> &str {
38        &self.kubo_url
39    }
40}
41
42#[async_trait]
43impl DidDocumentResolver for KuboDidResolver {
44    async fn resolve(&self, did: &str) -> crate::error::Result<Document> {
45        let parsed = Did::try_from(did).map_err(crate::error::Error::Validation)?;
46        let did_key = parsed.base_id();
47        match fetch_did_document(&self.kubo_url, &parsed).await {
48            Ok(document) => Ok(document),
49            Err(error) => Err(crate::error::Error::Resolution {
50                did: did_key,
51                detail: error.to_string(),
52            }),
53        }
54    }
55}
56
57#[async_trait]
58impl IpnsPathResolver for KuboDidResolver {
59    async fn resolve_ipns_path(&self, path: &str) -> crate::error::Result<String> {
60        if !path.starts_with("/ipns/") || path.len() <= "/ipns/".len() {
61            return Err(crate::error::Error::IpnsResolution {
62                path: path.to_string(),
63                detail: "expected a non-empty /ipns/<name> path".to_string(),
64            });
65        }
66        match name_resolve(&self.kubo_url, path, true).await {
67            Ok(resolved) => Ok(resolved),
68            Err(error) => Err(crate::error::Error::IpnsResolution {
69                path: path.to_string(),
70                detail: error.to_string(),
71            }),
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::KuboDidResolver;
79    use crate::ipfs::resolver::{DidDocumentResolver, IpnsPathResolver};
80
81    fn resolver() -> KuboDidResolver {
82        KuboDidResolver::new("http://127.0.0.1:5001")
83    }
84
85    #[test]
86    fn stores_the_kubo_url() {
87        let resolver = KuboDidResolver::new("http://127.0.0.1:5001");
88        assert_eq!(resolver.kubo_url(), "http://127.0.0.1:5001");
89    }
90
91    #[tokio::test]
92    async fn rejects_invalid_did_before_any_rpc_call() {
93        let error = resolver()
94            .resolve("not-a-did")
95            .await
96            .expect_err("invalid DID must fail before any RPC call");
97        assert!(
98            matches!(error, crate::error::Error::Validation(_)),
99            "{error:?}"
100        );
101    }
102
103    #[tokio::test]
104    async fn rejects_malformed_ipns_path_before_any_rpc_call() {
105        let resolver = resolver();
106        for path in ["", "ipns/k51abc", "/ipfs/bafycid"] {
107            let error = resolver
108                .resolve_ipns_path(path)
109                .await
110                .expect_err("non-/ipns/ path must fail before any RPC call");
111            assert!(
112                matches!(error, crate::error::Error::IpnsResolution { .. }),
113                "{path}: {error:?}"
114            );
115        }
116    }
117}