pub struct OkxAuth { /* private fields */ }Expand description
OKX 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 OkxAuth
impl OkxAuth
Sourcepub fn new(api_key: String, secret: String, passphrase: String) -> Self
pub fn new(api_key: String, secret: String, passphrase: String) -> Self
Creates a new OkxAuth instance.
§Arguments
api_key- The API key from OKX.secret- The secret key from OKX.passphrase- The passphrase set when creating the API key.
§Security
Credentials are automatically zeroed from memory when the authenticator is dropped.
§Example
use ccxt_exchanges::okx::OkxAuth;
let auth = OkxAuth::new(
"your-api-key".to_string(),
"your-secret".to_string(),
"your-passphrase".to_string(),
);Sourcepub fn passphrase(&self) -> &str
pub fn passphrase(&self) -> &str
Returns the passphrase.
Sourcepub fn build_sign_string(
&self,
timestamp: &str,
method: &str,
path: &str,
body: &str,
) -> String
pub fn build_sign_string( &self, timestamp: &str, method: &str, path: &str, body: &str, ) -> String
Builds the signature string for HMAC signing.
The signature string format is: timestamp + method + path + body
§Arguments
timestamp- ISO 8601 timestamp (e.g., “2021-01-01T00:00:00.000Z”).method- HTTP method (GET, POST, DELETE, etc.).path- Request path including query string (e.g., “/api/v5/account/balance”).body- Request body (empty string for GET requests).
§Returns
The concatenated string to be signed.
Sourcepub fn sign(
&self,
timestamp: &str,
method: &str,
path: &str,
body: &str,
) -> String
pub fn sign( &self, timestamp: &str, method: &str, path: &str, body: &str, ) -> String
Signs a request using HMAC-SHA256.
§Arguments
timestamp- ISO 8601 timestamp (e.g., “2021-01-01T00:00:00.000Z”).method- HTTP method (GET, POST, DELETE, etc.).path- Request path including query string.body- Request body (empty string for GET requests).
§Returns
Base64-encoded HMAC-SHA256 signature.
§Example
use ccxt_exchanges::okx::OkxAuth;
let auth = OkxAuth::new(
"api-key".to_string(),
"secret".to_string(),
"passphrase".to_string(),
);
let signature = auth.sign("2021-01-01T00:00:00.000Z", "GET", "/api/v5/account/balance", "");
assert!(!signature.is_empty());Sourcepub fn add_auth_headers(
&self,
headers: &mut HeaderMap,
timestamp: &str,
sign: &str,
)
pub fn add_auth_headers( &self, headers: &mut HeaderMap, timestamp: &str, sign: &str, )
Adds authentication headers to a HeaderMap.
Adds the following headers:
- OK-ACCESS-KEY: API key
- OK-ACCESS-SIGN: HMAC-SHA256 signature
- OK-ACCESS-TIMESTAMP: ISO 8601 timestamp
- OK-ACCESS-PASSPHRASE: API passphrase
§Arguments
headers- Mutable reference to HeaderMap to add headers to.timestamp- ISO 8601 timestamp.sign- Pre-computed signature fromsign()method.
§Example
use ccxt_exchanges::okx::OkxAuth;
use reqwest::header::HeaderMap;
let auth = OkxAuth::new(
"api-key".to_string(),
"secret".to_string(),
"passphrase".to_string(),
);
let mut headers = HeaderMap::new();
let timestamp = "2021-01-01T00:00:00.000Z";
let signature = auth.sign(timestamp, "GET", "/api/v5/account/balance", "");
auth.add_auth_headers(&mut headers, timestamp, &signature);
assert!(headers.contains_key("OK-ACCESS-KEY"));
assert!(headers.contains_key("OK-ACCESS-SIGN"));
assert!(headers.contains_key("OK-ACCESS-TIMESTAMP"));
assert!(headers.contains_key("OK-ACCESS-PASSPHRASE"));Sourcepub fn create_auth_headers(
&self,
timestamp: &str,
method: &str,
path: &str,
body: &str,
) -> HeaderMap
pub fn create_auth_headers( &self, timestamp: &str, method: &str, path: &str, body: &str, ) -> HeaderMap
Creates authentication headers for a request.
This is a convenience method that combines sign() and add_auth_headers().
§Arguments
timestamp- ISO 8601 timestamp.method- HTTP method (GET, POST, DELETE, etc.).path- Request path including query string.body- Request body (empty string for GET requests).
§Returns
A HeaderMap containing all authentication headers.