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
54pub struct Client {
55    uri: String,
56    ws_uri: String,
57    public: bls12381::PublicKey,
58}
59
60impl Client {
61    pub fn new(uri: &str, public: bls12381::PublicKey) -> Self {
62        let uri = uri.to_string();
63        let ws_uri = uri.replace("http", "ws");
64        Self {
65            uri,
66            ws_uri,
67            public,
68        }
69    }
70}