pub struct BybitAuth { /* private fields */ }Expand description
Bybit API authenticator.
Handles request signing using HMAC-SHA256 and header construction for authenticated API requests.
Credentials are automatically zeroed from memory when dropped.
Implementations§
Source§impl BybitAuth
impl BybitAuth
Sourcepub fn new(api_key: String, secret: String) -> Self
pub fn new(api_key: String, secret: String) -> Self
Creates a new BybitAuth instance.
§Arguments
api_key- The API key from Bybit.secret- The secret key from Bybit.
§Security
Credentials are automatically zeroed from memory when the authenticator is dropped.
§Example
use ccxt_exchanges::bybit::BybitAuth;
let auth = BybitAuth::new(
"your-api-key".to_string(),
"your-secret".to_string(),
);Sourcepub fn build_sign_string(
&self,
timestamp: &str,
recv_window: u64,
params: &str,
) -> String
pub fn build_sign_string( &self, timestamp: &str, recv_window: u64, params: &str, ) -> String
Builds the signature string for HMAC signing.
The signature string format is: timestamp + api_key + recv_window + params
§Arguments
timestamp- Unix timestamp in milliseconds as string.recv_window- Receive window in milliseconds.params- Query string (for GET) or request body (for POST).
§Returns
The concatenated string to be signed.
Sourcepub fn sign(&self, timestamp: &str, recv_window: u64, params: &str) -> String
pub fn sign(&self, timestamp: &str, recv_window: u64, params: &str) -> String
Signs a request using HMAC-SHA256.
§Arguments
timestamp- Unix timestamp in milliseconds as string.recv_window- Receive window in milliseconds.params- Query string (for GET) or request body (for POST).
§Returns
Hex-encoded HMAC-SHA256 signature.
§Example
use ccxt_exchanges::bybit::BybitAuth;
let auth = BybitAuth::new(
"api-key".to_string(),
"secret".to_string(),
);
let signature = auth.sign("1234567890000", 5000, "symbol=BTCUSDT");
assert!(!signature.is_empty());Sourcepub fn add_auth_headers(
&self,
headers: &mut HeaderMap,
timestamp: &str,
sign: &str,
recv_window: u64,
)
pub fn add_auth_headers( &self, headers: &mut HeaderMap, timestamp: &str, sign: &str, recv_window: u64, )
Adds authentication headers to a HeaderMap.
Adds the following headers:
- X-BAPI-API-KEY: API key
- X-BAPI-SIGN: HMAC-SHA256 signature (hex encoded)
- X-BAPI-TIMESTAMP: Unix timestamp
- X-BAPI-RECV-WINDOW: Receive window
§Arguments
headers- Mutable reference to HeaderMap to add headers to.timestamp- Unix timestamp in milliseconds as string.sign- Pre-computed signature fromsign()method.recv_window- Receive window in milliseconds.
§Example
use ccxt_exchanges::bybit::BybitAuth;
use reqwest::header::HeaderMap;
let auth = BybitAuth::new(
"api-key".to_string(),
"secret".to_string(),
);
let mut headers = HeaderMap::new();
let timestamp = "1234567890000";
let recv_window = 5000u64;
let signature = auth.sign(timestamp, recv_window, "symbol=BTCUSDT");
auth.add_auth_headers(&mut headers, timestamp, &signature, recv_window);
assert!(headers.contains_key("X-BAPI-API-KEY"));
assert!(headers.contains_key("X-BAPI-SIGN"));
assert!(headers.contains_key("X-BAPI-TIMESTAMP"));
assert!(headers.contains_key("X-BAPI-RECV-WINDOW"));Sourcepub fn create_auth_headers(
&self,
timestamp: &str,
recv_window: u64,
params: &str,
) -> HeaderMap
pub fn create_auth_headers( &self, timestamp: &str, recv_window: u64, params: &str, ) -> HeaderMap
Creates authentication headers for a request.
This is a convenience method that combines sign() and add_auth_headers().
§Arguments
timestamp- Unix timestamp in milliseconds as string.recv_window- Receive window in milliseconds.params- Query string (for GET) or request body (for POST).
§Returns
A HeaderMap containing all authentication headers.