alto_client/
lib.rs

1//! Client for interacting with `alto`.
2
3use commonware_cryptography::{bls12381, sha256::Digest};
4use commonware_utils::hex;
5use thiserror::Error;
6
7pub mod consensus;
8pub mod utils;
9
10const LATEST: &str = "latest";
11
12pub enum Query {
13    Latest,
14    Index(u64),
15    Digest(Digest),
16}
17
18impl Query {
19    pub fn serialize(&self) -> String {
20        match self {
21            Query::Latest => LATEST.to_string(),
22            Query::Index(index) => hex(&index.to_be_bytes()),
23            Query::Digest(digest) => hex(digest),
24        }
25    }
26}
27
28pub enum IndexQuery {
29    Latest,
30    Index(u64),
31}
32
33impl IndexQuery {
34    pub fn serialize(&self) -> String {
35        match self {
36            IndexQuery::Latest => LATEST.to_string(),
37            IndexQuery::Index(index) => hex(&index.to_be_bytes()),
38        }
39    }
40}
41
42#[derive(Error, Debug)]
43pub enum Error {
44    #[error("reqwest error: {0}")]
45    Reqwest(#[from] reqwest::Error),
46    #[error("tungstenite error: {0}")]
47    Tungstenite(#[from] tokio_tungstenite::tungstenite::Error),
48    #[error("failed: {0}")]
49    Failed(reqwest::StatusCode),
50    #[error("invalid data")]
51    InvalidData,
52}
53
54#[derive(Clone)]
55pub struct Client {
56    uri: String,
57    ws_uri: String,
58    public: bls12381::PublicKey,
59
60    client: reqwest::Client,
61}
62
63impl Client {
64    pub fn new(uri: &str, public: bls12381::PublicKey) -> Self {
65        let uri = uri.to_string();
66        let ws_uri = uri.replace("http", "ws");
67        Self {
68            uri,
69            ws_uri,
70            public,
71
72            client: reqwest::Client::new(),
73        }
74    }
75}