anya_core/web/
web5_adapter.rs

1// Web5Adapter: Isolates all web5-rust logic for DID, DWN, and VC operations
2// This is a scaffold. Implementations will be filled in as we refactor usages.
3
4use serde::{Deserialize, Serialize};
5use reqwest::Client;
6use std::error::Error;
7
8#[derive(Clone)]
9pub struct Web5Adapter {
10    pub service_url: String,
11    pub client: Client,
12}
13
14#[derive(Serialize, Deserialize)]
15pub struct CreateDidRequest {
16    pub method: String,
17}
18
19#[derive(Serialize, Deserialize)]
20pub struct DidDocumentResponse {
21    pub did: String,
22    pub document: serde_json::Value,
23}
24
25impl Web5Adapter {
26    pub fn new(service_url: &str) -> Self {
27        Self {
28            service_url: service_url.to_string(),
29            client: Client::new(),
30        }
31    }
32
33    pub async fn create_did(&self, method: &str) -> Result<DidDocumentResponse, Box<dyn Error>> {
34        let req = CreateDidRequest {
35            method: method.to_string(),
36        };
37        let url = format!("{}/did/create", self.service_url);
38        let resp = self.client.post(&url)
39            .json(&req)
40            .send()
41            .await?;
42        let did_doc = resp.json::<DidDocumentResponse>().await?;
43        Ok(did_doc)
44    }
45    pub async fn resolve_did(&self, did: &str) -> Result<DidDocumentResponse, Box<dyn Error>> {
46        let url = format!("{}/did/resolve", self.service_url);
47        let resp = self.client.get(&url)
48            .query(&[("did", did)])
49            .send()
50            .await?;
51        let did_doc = resp.json::<DidDocumentResponse>().await?;
52        Ok(did_doc)
53    }
54
55    pub async fn dwn_send_message(&self, message: serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>> {
56        let url = format!("{}/dwn/send", self.service_url);
57        let resp = self.client.post(&url)
58            .json(&message)
59            .send()
60            .await?;
61        let result = resp.json::<serde_json::Value>().await?;
62        Ok(result)
63    }
64
65    pub async fn dwn_get_messages(&self, filter: Option<serde_json::Value>) -> Result<serde_json::Value, Box<dyn Error>> {
66        let url = format!("{}/dwn/messages", self.service_url);
67        let req = filter.unwrap_or_default();
68        let resp = self.client.post(&url)
69            .json(&req)
70            .send()
71            .await?;
72        let result = resp.json::<serde_json::Value>().await?;
73        Ok(result)
74    }
75
76    pub async fn issue_credential(&self, payload: serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>> {
77        let url = format!("{}/vc/issue", self.service_url);
78        let resp = self.client.post(&url)
79            .json(&payload)
80            .send()
81            .await?;
82        let result = resp.json::<serde_json::Value>().await?;
83        Ok(result)
84    }
85
86    pub async fn verify_credential(&self, payload: serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>> {
87        let url = format!("{}/vc/verify", self.service_url);
88        let resp = self.client.post(&url)
89            .json(&payload)
90            .send()
91            .await?;
92        let result = resp.json::<serde_json::Value>().await?;
93        Ok(result)
94    }
95
96    pub async fn store_credential(&self, payload: serde_json::Value) -> Result<serde_json::Value, Box<dyn Error>> {
97        let url = format!("{}/vc/store", self.service_url);
98        let resp = self.client.post(&url)
99            .json(&payload)
100            .send()
101            .await?;
102        let result = resp.json::<serde_json::Value>().await?;
103        Ok(result)
104    }
105
106    pub async fn get_credential(&self, id: &str) -> Result<serde_json::Value, Box<dyn Error>> {
107        let url = format!("{}/vc/get", self.service_url);
108        let resp = self.client.get(&url)
109            .query(&[("id", id)])
110            .send()
111            .await?;
112        let result = resp.json::<serde_json::Value>().await?;
113        Ok(result)
114    }
115
116    pub async fn list_credentials(&self) -> Result<serde_json::Value, Box<dyn Error>> {
117        let url = format!("{}/vc/list", self.service_url);
118        let resp = self.client.get(&url)
119            .send()
120            .await?;
121        let result = resp.json::<serde_json::Value>().await?;
122        Ok(result)
123    }
124}