Skip to main content

ipscanner/resources/
provenance.rs

1use serde_json::json;
2
3use crate::types::{
4    AnchoredVerification, Chain, ChainParams, ChainVerification, ExportParams, Jurisdiction,
5    Jurisdictions, ProvenanceCheck, ProvenanceCheckRequest,
6};
7use crate::{Client, Error};
8
9/// Provenance attestations. Get one with [`Client::provenance`].
10#[derive(Debug, Clone, Copy)]
11pub struct Provenance<'a> {
12    client: &'a Client,
13}
14
15impl<'a> Provenance<'a> {
16    pub(crate) fn new(client: &'a Client) -> Self {
17        Provenance { client }
18    }
19
20    /// Checks an IP against a claimed jurisdiction and writes an attestation.
21    pub async fn check(&self, req: &ProvenanceCheckRequest) -> Result<ProvenanceCheck, Error> {
22        self.client.post("/provenance/check", req).await
23    }
24
25    /// Recomputes your attestation chain.
26    pub async fn verify(&self) -> Result<ChainVerification, Error> {
27        self.client.get("/provenance/verify", &[]).await
28    }
29
30    /// Recomputes your chain and returns the latest daily anchor.
31    pub async fn verify_anchored(&self) -> Result<AnchoredVerification, Error> {
32        self.client.post("/provenance/verify", &json!({})).await
33    }
34
35    /// A page of your attestation chain, newest first.
36    pub async fn chain(&self, params: &ChainParams) -> Result<Chain, Error> {
37        let mut q = Vec::new();
38        if let Some(v) = params.limit {
39            q.push(("limit", v.to_string()));
40        }
41        if let Some(v) = params.before {
42            q.push(("before", v.to_string()));
43        }
44        self.client.get("/provenance/chain", &q).await
45    }
46
47    /// The jurisdictions the policy engine knows.
48    pub async fn jurisdictions(&self) -> Result<Vec<Jurisdiction>, Error> {
49        let res: Jurisdictions = self.client.get("/provenance/jurisdictions", &[]).await?;
50        Ok(res.jurisdictions)
51    }
52
53    /// Evidence export for a date range, as raw CSV.
54    pub async fn export(&self, params: &ExportParams) -> Result<String, Error> {
55        let mut q = Vec::new();
56        if let Some(v) = &params.from {
57            q.push(("from", v.clone()));
58        }
59        if let Some(v) = &params.to {
60            q.push(("to", v.clone()));
61        }
62        self.client
63            .get_text("/provenance/export", &q, "text/csv, application/json")
64            .await
65    }
66}