use crate::core::{PolymarketError, Result};
use crate::types::ApiCredentials;
use alloy_primitives::{hex::encode_prefixed, Address, U256};
use alloy_signer::SignerSync;
use alloy_signer_local::PrivateKeySigner;
use alloy_sol_types::{eip712_domain, sol};
use base64::engine::general_purpose::{STANDARD, URL_SAFE, URL_SAFE_NO_PAD};
use base64::engine::Engine;
use hmac::{Hmac, Mac};
use serde::Serialize;
use sha2::Sha256;
use std::collections::HashMap;
use std::time::{SystemTime, UNIX_EPOCH};
const POLY_ADDR_HEADER: &str = "poly_address";
const POLY_SIG_HEADER: &str = "poly_signature";
const POLY_TS_HEADER: &str = "poly_timestamp";
const POLY_NONCE_HEADER: &str = "poly_nonce";
const POLY_API_KEY_HEADER: &str = "poly_api_key";
const POLY_PASS_HEADER: &str = "poly_passphrase";
pub type Headers = HashMap<&'static str, String>;
sol! {
struct ClobAuth {
address address;
string timestamp;
uint256 nonce;
string message;
}
}
sol! {
struct Order {
uint256 salt;
address maker;
address signer;
address taker;
uint256 tokenId;
uint256 makerAmount;
uint256 takerAmount;
uint256 expiration;
uint256 nonce;
uint256 feeRateBps;
uint8 side;
uint8 signatureType;
}
}
fn decode_api_secret(secret: &str) -> Vec<u8> {
URL_SAFE
.decode(secret)
.or_else(|_| URL_SAFE_NO_PAD.decode(secret))
.or_else(|_| STANDARD.decode(secret))
.unwrap_or_else(|_| secret.as_bytes().to_vec())
}
fn format_body_for_signature<T>(body: &T) -> Result<String>
where
T: ?Sized + Serialize,
{
serde_json::to_string(body).map_err(|e| PolymarketError::Parse {
message: format!("Failed to serialize body: {}", e),
source: Some(Box::new(e)),
})
}
#[must_use]
pub fn get_current_unix_time_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("Time went backwards")
.as_secs()
}
pub fn build_clob_auth_typed_data(
address: Address,
timestamp: &str,
nonce: U256,
) -> serde_json::Value {
use serde_json::json;
let message_content = "This message attests that I control the given wallet";
let polygon_chain_id = 137;
json!({
"domain": {
"name": "ClobAuthDomain",
"version": "1",
"chainId": polygon_chain_id
},
"types": {
"EIP712Domain": [
{ "name": "name", "type": "string" },
{ "name": "version", "type": "string" },
{ "name": "chainId", "type": "uint256" }
],
"ClobAuth": [
{ "name": "address", "type": "address" },
{ "name": "timestamp", "type": "string" },
{ "name": "nonce", "type": "uint256" },
{ "name": "message", "type": "string" }
]
},
"primaryType": "ClobAuth",
"message": {
"address": format!("{:?}", address),
"timestamp": timestamp,
"nonce": nonce.to_string(),
"message": message_content
}
})
}
pub fn sign_clob_auth_message(
signer: &PrivateKeySigner,
timestamp: String,
nonce: U256,
) -> Result<String> {
let message = "This message attests that I control the given wallet".to_string();
let polygon_chain_id = 137;
let auth_struct = ClobAuth {
address: signer.address(),
timestamp,
nonce,
message,
};
let domain = eip712_domain!(
name: "ClobAuthDomain",
version: "1",
chain_id: polygon_chain_id,
);
let signature = signer
.sign_typed_data_sync(&auth_struct, &domain)
.map_err(|e| PolymarketError::crypto(format!("EIP-712 signature failed: {}", e)))?;
Ok(encode_prefixed(signature.as_bytes()))
}
pub fn sign_order_message(
signer: &PrivateKeySigner,
order: Order,
chain_id: u64,
verifying_contract: Address,
) -> Result<String> {
let domain = eip712_domain!(
name: "Polymarket CTF Exchange",
version: "1",
chain_id: chain_id,
verifying_contract: verifying_contract,
);
let signature = signer
.sign_typed_data_sync(&order, &domain)
.map_err(|e| PolymarketError::crypto(format!("Order signature failed: {}", e)))?;
Ok(encode_prefixed(signature.as_bytes()))
}
pub fn build_hmac_signature<T>(
secret: &str,
timestamp: u64,
method: &str,
request_path: &str,
body: Option<&T>,
) -> Result<String>
where
T: ?Sized + Serialize,
{
let mut mac = Hmac::<Sha256>::new_from_slice(&decode_api_secret(secret))
.map_err(|e| PolymarketError::crypto(format!("Invalid HMAC key: {}", e)))?;
let body_string = match body {
Some(b) => format_body_for_signature(b)?,
None => String::new(),
};
let message = format!(
"{}{}{}{}",
timestamp,
method.to_uppercase(),
request_path,
body_string
);
mac.update(message.as_bytes());
let result = mac.finalize();
Ok(URL_SAFE.encode(result.into_bytes()))
}
pub fn create_l1_headers(signer: &PrivateKeySigner, nonce: Option<U256>) -> Result<Headers> {
let timestamp = get_current_unix_time_secs().to_string();
let nonce = nonce.unwrap_or(U256::ZERO);
let signature = sign_clob_auth_message(signer, timestamp.clone(), nonce)?;
let address = encode_prefixed(signer.address().as_slice());
Ok(HashMap::from([
(POLY_ADDR_HEADER, address),
(POLY_SIG_HEADER, signature),
(POLY_TS_HEADER, timestamp),
(POLY_NONCE_HEADER, nonce.to_string()),
]))
}
pub fn create_l2_headers<T>(
signer: &PrivateKeySigner,
api_creds: &ApiCredentials,
method: &str,
req_path: &str,
body: Option<&T>,
) -> Result<Headers>
where
T: ?Sized + Serialize,
{
let address = encode_prefixed(signer.address().as_slice());
create_l2_headers_with_address(&address, api_creds, method, req_path, body)
}
pub fn create_l2_headers_with_address<T>(
address: &str,
api_creds: &ApiCredentials,
method: &str,
req_path: &str,
body: Option<&T>,
) -> Result<Headers>
where
T: ?Sized + Serialize,
{
let address = if address.starts_with("0x") {
address.to_string()
} else {
format!("0x{}", address)
};
let timestamp = get_current_unix_time_secs();
let hmac_signature =
build_hmac_signature(&api_creds.secret, timestamp, method, req_path, body)?;
Ok(HashMap::from([
(POLY_ADDR_HEADER, address),
(POLY_SIG_HEADER, hmac_signature),
(POLY_TS_HEADER, timestamp.to_string()),
(POLY_API_KEY_HEADER, api_creds.api_key.clone()),
(POLY_PASS_HEADER, api_creds.passphrase.clone()),
]))
}
pub fn create_l2_headers_with_body_string(
address: &str,
api_creds: &ApiCredentials,
method: &str,
req_path: &str,
body_str: &str,
timestamp: u64,
) -> Result<Headers> {
let address = if address.starts_with("0x") {
address.to_string()
} else {
format!("0x{}", address)
};
let hmac_signature =
build_hmac_signature_from_string(&api_creds.secret, timestamp, method, req_path, body_str)?;
Ok(HashMap::from([
(POLY_ADDR_HEADER, address),
(POLY_SIG_HEADER, hmac_signature),
(POLY_TS_HEADER, timestamp.to_string()),
(POLY_API_KEY_HEADER, api_creds.api_key.clone()),
(POLY_PASS_HEADER, api_creds.passphrase.clone()),
]))
}
pub fn build_hmac_signature_from_string(
secret: &str,
timestamp: u64,
method: &str,
request_path: &str,
body_str: &str,
) -> Result<String> {
let mut mac = Hmac::<Sha256>::new_from_slice(&decode_api_secret(secret))
.map_err(|e| PolymarketError::crypto(format!("Invalid HMAC key: {}", e)))?;
let message = format!(
"{}{}{}{}",
timestamp,
method.to_uppercase(),
request_path,
body_str
);
mac.update(message.as_bytes());
let result = mac.finalize();
Ok(URL_SAFE.encode(result.into_bytes()))
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn test_unix_timestamp() {
let timestamp = get_current_unix_time_secs();
assert!(timestamp > 1_600_000_000); }
#[test]
fn test_hmac_signature() {
let result =
build_hmac_signature::<String>("test_secret", 1234567890, "GET", "/test", None);
assert!(result.is_ok());
}
#[test]
fn test_hmac_signature_with_body() {
let body = r#"{"test": "data"}"#;
let result = build_hmac_signature("test_secret", 1234567890, "POST", "/orders", Some(body));
assert!(result.is_ok());
let signature = result.unwrap();
assert!(!signature.is_empty());
}
#[test]
fn test_hmac_signature_consistency() {
let secret = "test_secret";
let timestamp = 1234567890;
let method = "GET";
let path = "/test";
let sig1 = build_hmac_signature::<String>(secret, timestamp, method, path, None).unwrap();
let sig2 = build_hmac_signature::<String>(secret, timestamp, method, path, None).unwrap();
assert_eq!(sig1, sig2);
}
#[test]
fn test_hmac_signature_different_inputs() {
let secret = "test_secret";
let timestamp = 1234567890;
let sig1 = build_hmac_signature::<String>(secret, timestamp, "GET", "/test", None).unwrap();
let sig2 =
build_hmac_signature::<String>(secret, timestamp, "POST", "/test", None).unwrap();
let sig3 =
build_hmac_signature::<String>(secret, timestamp, "GET", "/other", None).unwrap();
assert_ne!(sig1, sig2);
assert_ne!(sig1, sig3);
assert_ne!(sig2, sig3);
}
#[test]
fn test_decode_api_secret_with_urlsafe_padding() {
assert_eq!(decode_api_secret("cQ=="), b"q".to_vec());
}
#[test]
fn test_format_body_for_signature_json() {
let body = json!({ "order": { "foo": 1 } });
let formatted = format_body_for_signature(&body).expect("Formatting should succeed");
assert_eq!(formatted, r#"{"order":{"foo":1}}"#);
}
#[test]
fn test_create_l1_headers() {
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
let result = create_l1_headers(&signer, Some(U256::from(12345)));
assert!(result.is_ok());
let headers = result.unwrap();
assert!(headers.contains_key("poly_address"));
assert!(headers.contains_key("poly_signature"));
assert!(headers.contains_key("poly_timestamp"));
assert!(headers.contains_key("poly_nonce"));
}
#[test]
fn test_create_l1_headers_different_nonces() {
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
let headers_1 = create_l1_headers(&signer, Some(U256::from(12345))).unwrap();
let headers_2 = create_l1_headers(&signer, Some(U256::from(54321))).unwrap();
assert_ne!(
headers_1.get("poly_signature"),
headers_2.get("poly_signature")
);
assert_eq!(headers_1.get("poly_address"), headers_2.get("poly_address"));
}
#[test]
fn test_create_l2_headers() {
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
let api_creds = ApiCredentials {
api_key: "test_key".to_string(),
secret: "test_secret".to_string(),
passphrase: "test_passphrase".to_string(),
};
let result = create_l2_headers::<String>(&signer, &api_creds, "GET", "/test", None);
assert!(result.is_ok());
let headers = result.unwrap();
assert!(headers.contains_key("poly_api_key"));
assert!(headers.contains_key("poly_signature"));
assert!(headers.contains_key("poly_timestamp"));
assert!(headers.contains_key("poly_passphrase"));
assert_eq!(headers.get("poly_api_key").unwrap(), "test_key");
assert_eq!(headers.get("poly_passphrase").unwrap(), "test_passphrase");
}
#[test]
fn test_eip712_signature_format() {
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
let result = create_l1_headers(&signer, Some(U256::from(12345)));
assert!(result.is_ok());
let headers = result.unwrap();
let signature = headers.get("poly_signature").unwrap();
assert!(signature.starts_with("0x"));
assert_eq!(signature.len(), 132);
}
}