eigen_client_fireblocks/
client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use crate::error::FireBlockError;
use chrono::Utc;
use jsonwebtoken::{encode, Algorithm, EncodingKey, Header};
use mime::APPLICATION_JSON;
use once_cell::sync::Lazy;
use reqwest::header::{HeaderMap, HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::collections::HashMap;
use uuid::Uuid;

const X_API_KEY: &str = "X-API-KEY";

/// AssetID represents the asset identifier as supported by fireblocks
/// TODO : Add more assetid identifiers
#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
pub enum AssetID {
    ETH,
    #[serde(rename = "ETH_TEST5")]
    EthTest5,
    #[serde(rename = "BTC_TEST")]
    BtcTest,
    #[serde(rename = "BASECHAIN_ETH_TEST5")]
    BaseChainEthTest5,
    #[serde(rename = "ETH_TEST6")]
    EthTest6,
}

impl std::fmt::Display for AssetID {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                AssetID::ETH => "ETH",
                AssetID::EthTest5 => "ETH_TEST5",
                AssetID::BtcTest => "BTC_TEST",
                AssetID::BaseChainEthTest5 => "BASECHAIN_ETH_TEST5",
                AssetID::EthTest6 => "ETH_TEST6",
            }
        )
    }
}

// Initialize AssetIDByChain as a HashMap
pub static ASSET_ID_BY_CHAIN: Lazy<HashMap<u64, AssetID>> = Lazy::new(|| {
    let mut m = HashMap::new();
    m.insert(1, AssetID::ETH);
    m.insert(2, AssetID::EthTest5);
    m.insert(17000, AssetID::EthTest6);
    m
});

pub const JWT_EXPIRATION_SECONDS: i64 = 30;

#[derive(Serialize, Deserialize, Debug)]
pub struct ErrorResponse {
    pub message: String,
    pub code: i32,
}

/// Payload for JWT
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
    /// Fireblocks api uri
    uri: String,
    /// Unique identifier. Each request needs to have a unique identifier.
    nonce: String,
    /// The time at which the JWT was issued, in seconds since Epoch.
    iat: i64,
    /// Expiration time of jwt
    exp: i64,
    /// Api key
    sub: String,
    #[serde(rename = "bodyHash")]
    /// Hex-encoded SHA-256 hash of the raw HTTP request body.
    body_hash: String,
}

/// Fireblock Client
#[derive(Debug)]
pub struct Client {
    /// Api key
    api_key: String,
    /// Fireblocks generated secret key based on RS256 (RSASSA-PKCS1-v1_5 using SHA-256 hash) algorithm
    private_key: String,
    ///  Aandbox:  https://sandbox-api.fireblocks.io/v1 , Mainnet: https://api.fireblocks.io/v1
    api_url: String,
}

impl Client {
    pub fn new(api_key: String, private_key: String, api_url: String) -> Self {
        Self {
            api_key,
            private_key,
            api_url,
        }
    }

    ///  Sign the payload
    pub fn sign_jwt(&self, path: &str, body: Option<&str>) -> Result<String, FireBlockError> {
        let now = Utc::now().timestamp();
        let nonce = Uuid::new_v4().to_string();
        let body_hash = match body {
            Some(b) => hex::encode(Sha256::digest(b.as_bytes())),
            None => hex::encode(Sha256::digest("".as_bytes())),
        };

        let claims = Claims {
            uri: path.to_owned(),
            nonce,
            iat: now,
            exp: now + JWT_EXPIRATION_SECONDS, // Adjusted to ensure it's within the required timeframe
            sub: self.api_key.clone(),
            body_hash,
        };

        let encoding_key = EncodingKey::from_rsa_pem(self.private_key.as_bytes())
            .map_err(FireBlockError::JsonWebTokenError)?;

        encode(&Header::new(Algorithm::RS256), &claims, &encoding_key)
            .map_err(FireBlockError::JsonWebTokenError)
    }

    /// GET : Request to the fireblocks endpoint using the given path.
    pub async fn get_request(&self, path: &str) -> Result<String, FireBlockError> {
        let token = self.sign_jwt(path, None)?;

        let client = reqwest::Client::new();
        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", token))?,
        );
        headers.insert(X_API_KEY, HeaderValue::from_str(&self.api_key)?);

        // Make the GET request
        let response = client
            .get(self.api_url.to_owned() + path)
            .headers(headers)
            .send()
            .await?;

        // Check response status and return result
        if response.status().is_success() {
            let response_text = response.text().await?;
            Ok(response_text)
        } else {
            Err(FireBlockError::from(format!(
                "GET Request failed with status: {}",
                response.status()
            )))
        }
    }

    /// POST: Post a request using the fireblocks path api and the appropriate body parameters
    pub async fn post_request(
        &self,
        path: &str,
        body: Option<&str>,
    ) -> Result<String, FireBlockError> {
        let token = self.sign_jwt(path, body)?;

        let client = reqwest::Client::new();
        let mut headers = HeaderMap::new();
        headers.insert(
            AUTHORIZATION,
            HeaderValue::from_str(&format!("Bearer {}", token))?,
        );
        headers.insert(X_API_KEY, HeaderValue::from_str(&self.api_key)?);

        // Make the POST request
        let response = client
            .post(self.api_url.to_owned() + path) // Use api_url here
            .headers(headers)
            .header(CONTENT_TYPE, APPLICATION_JSON.as_ref()) // Set Content-Type header
            .body(body.unwrap_or("").to_string())
            .send()
            .await?;

        // Check response status and return result
        if response.status().is_success() {
            let response_text = response.text().await?;
            Ok(response_text)
        } else {
            Err(FireBlockError::from(format!(
                "POST Request failed with status: {}",
                response.status()
            )))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{AssetID, ASSET_ID_BY_CHAIN};

    #[test]
    fn test_asset_id_by_chain() {
        assert_eq!(AssetID::ETH, *ASSET_ID_BY_CHAIN.get(&1).unwrap());
    }
}