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
/// Binance API Credentials.
///
/// Communication with Binance API USER_DATA endpoints requires
/// valid API credentials.
///
/// Note: Production and TESTNET API Credentials are not
/// interchangeable.
///
/// [API Documentation](https://binance-docs.github.io/apidocs/spot/en/#api-key-restrictions)
///
#[derive(PartialEq, Eq, Clone)]
pub struct Credentials {
    pub api_key: String,
    pub signature: Signature,
}

#[derive(PartialEq, Eq, Clone)]
pub enum Signature {
    Hmac(HmacSignature),
    Rsa(RsaSignature),
}

#[derive(PartialEq, Eq, Clone)]
pub struct HmacSignature {
    pub api_secret: String,
}

#[derive(PartialEq, Eq, Clone)]
pub struct RsaSignature {
    pub key: String,
    pub password: Option<String>,
}

impl Credentials {
    pub fn from_rsa(api_key: impl Into<String>, key: impl Into<String>) -> Self {
        Credentials {
            api_key: api_key.into(),
            signature: Signature::Rsa(RsaSignature {
                key: key.into(),
                password: None,
            }),
        }
    }
    pub fn from_rsa_protected(
        api_key: impl Into<String>,
        key: impl Into<String>,
        password: impl Into<String>,
    ) -> Self {
        Credentials {
            api_key: api_key.into(),
            signature: Signature::Rsa(RsaSignature {
                key: key.into(),
                password: Some(password.into()),
            }),
        }
    }
    pub fn from_hmac(api_key: impl Into<String>, api_secret: impl Into<String>) -> Self {
        Credentials {
            api_key: api_key.into(),
            signature: Signature::Hmac(HmacSignature {
                api_secret: api_secret.into(),
            }),
        }
    }
}

impl std::fmt::Debug for Credentials {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Credentials")
            .field("api_key", &"[redacted]")
            .finish()
    }
}