rust-x402 0.3.0

HTTP-native micropayments with x402 protocol
Documentation
//! Tests for cryptographic utilities

use super::{eip712, jwt, signature};
use ethereum_types::Address;
use std::str::FromStr;

#[test]
fn test_jwt_creation() {
    let token = jwt::create_auth_header(
        "test_key",
        "test_secret",
        "api.cdp.coinbase.com",
        "/platform/v2/x402/verify",
    );
    assert!(token.is_ok());
    assert!(token.unwrap().starts_with("Bearer "));
}

#[test]
fn test_domain_creation() {
    let domain = eip712::Domain {
        name: "USD Coin".to_string(),
        version: "2".to_string(),
        chain_id: 8453,
        verifying_contract: Address::from_str("0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913")
            .unwrap(),
    };

    assert_eq!(domain.name, "USD Coin");
    assert_eq!(domain.version, "2");
    assert_eq!(domain.chain_id, 8453);
}

#[test]
fn test_nonce_generation() {
    let nonce1 = signature::generate_nonce();
    let nonce2 = signature::generate_nonce();

    // Nonces should be different
    assert_ne!(nonce1, nonce2);

    // Nonces should be valid H256 values
    assert_eq!(nonce1.as_bytes().len(), 32);
    assert_eq!(nonce2.as_bytes().len(), 32);
}

#[test]
fn test_payment_payload_verification() {
    // Create a test payment payload with valid decimal values
    let auth = crate::types::ExactEvmPayloadAuthorization::new(
        "0x857b06519E91e3A54538791bDbb0E22373e36b66",
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
        "1000000000000000000", // 1 USDC in wei (18 decimals)
        "1745323800",          // Valid timestamp
        "1745323985",          // Valid timestamp
        "0xf3746613c2d920b5fdabc0856f2aeb2d4f88ee6037b8cc5d04a71a4462f13480", // Nonce with 0x prefix
    );

    let payload = crate::types::ExactEvmPayload {
        signature: "0x2d6a7588d6acca505cbf0d9a4a227e0c52c6c34008c8e8986a1283259764173608a2ce6496642e377d6da8dbbf5836e9bd15092f9ecab05ded3d6293af148b571c".to_string(),
        authorization: auth,
    };

    // This should not panic, even if verification fails
    let result = signature::verify_payment_payload(
        &payload,
        "0x857b06519E91e3A54538791bDbb0E22373e36b66",
        "base-sepolia",
    );
    match result {
        Ok(_) => println!("Verification succeeded"),
        Err(e) => println!("Verification failed with error: {}", e),
    }
    // The verification result might be true or false, but the function should not panic
    // For now, we'll just check that it doesn't panic, regardless of the result
    let _ = result;
}

#[test]
fn test_invalid_payment_payload_validation() {
    // Test that invalid payment payloads are properly handled
    let auth = crate::types::ExactEvmPayloadAuthorization::new(
        "0x857b06519E91e3A54538791bDbb0E22373e36b66",
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
        "1000000",
        "1745323800",
        "1745323985",
        "0xf3746613c2d920b5fdabc0856f2aeb2d4f88ee6037b8cc5d04a71a4462f13480",
    );

    let payload = crate::types::ExactEvmPayload {
        signature: "0x2d6a7588d6acca505cbf0d9a4a227e0c52c6c34008c8e8986a1283259764173608a2ce6496642e377d6da8dbbf5836e9bd15092f9ecab05ded3d6293af148b571c".to_string(),
        authorization: auth,
    };

    // Test with valid payload - should not panic
    let valid_payment_payload = crate::types::PaymentPayload {
        x402_version: 1,
        scheme: "exact".to_string(),
        network: "base-sepolia".to_string(),
        payload: payload.clone(),
    };

    // This should not panic and should return a result (either Ok or Err)
    let result = signature::verify_payment_payload(
        &valid_payment_payload.payload,
        "0x857b06519E91e3A54538791bDbb0E22373e36b66",
        "base-sepolia",
    );

    // The result should be handled gracefully without panicking
    match result {
        Ok(_) => println!("Verification succeeded"),
        Err(e) => println!("Verification failed with error: {}", e),
    }

    // Test that the function doesn't panic even with invalid data
    // This test verifies that invalid data is handled gracefully
}