alto_client/
lib.rs

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