Skip to main content

rust_x402/crypto/
mod.rs

1//! Cryptographic utilities for x402 payments
2//!
3//! This module provides cryptographic primitives for the x402 protocol, including
4//! JWT authentication, EIP-712 typed data hashing, and ECDSA signature verification.
5//!
6//! # Architecture
7//!
8//! The crypto module is organized as follows:
9//! - [`jwt`] - JWT token generation for API authentication (primarily Coinbase CDP)
10//! - [`eip712`] - EIP-712 typed data hashing for Ethereum transactions
11//! - [`signature`] - ECDSA signature creation and verification
12//!
13//! # Examples
14//!
15//! ## JWT Authentication
16//!
17//! ```no_run
18//! use rust_x402::crypto::jwt;
19//!
20//! # fn example() -> rust_x402::Result<()> {
21//! // Create an authorization header for Coinbase API
22//! let auth_header = jwt::create_auth_header(
23//!     "api_key_id",
24//!     "api_key_secret",
25//!     "api.cdp.coinbase.com",
26//!     "/platform/v2/x402/verify"
27//! )?;
28//!
29//! // Use the auth_header in HTTP requests
30//! println!("Authorization: {}", auth_header);
31//! # Ok(())
32//! # }
33//! ```
34//!
35//! ## EIP-712 Typed Data Hashing
36//!
37//! ```no_run
38//! use rust_x402::crypto::eip712::{Domain, create_transfer_with_authorization_hash};
39//! use ethereum_types::{Address, H256, U256};
40//! use std::str::FromStr;
41//!
42//! # fn example() -> rust_x402::Result<()> {
43//! let domain = Domain {
44//!     name: "USD Coin".to_string(),
45//!     version: "2".to_string(),
46//!     chain_id: 8453,
47//!     verifying_contract: Address::from_str("0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913")?,
48//! };
49//!
50//! let message_hash = create_transfer_with_authorization_hash(
51//!     &domain,
52//!     Address::from_str("0x...")?,
53//!     Address::from_str("0x...")?,
54//!     U256::from(1000000),
55//!     U256::from(0),
56//!     U256::from(u64::MAX),
57//!     H256::random(),
58//! )?;
59//! # Ok(())
60//! # }
61//! ```
62//!
63//! ## Signature Verification
64//!
65//! ```no_run
66//! use rust_x402::crypto::signature;
67//! use rust_x402::types::ExactEvmPayload;
68//!
69//! # fn example() -> rust_x402::Result<()> {
70//! # let payload: ExactEvmPayload = todo!();
71//! // Verify a payment payload signature
72//! let is_valid = signature::verify_payment_payload(
73//!     &payload,
74//!     "0x857b06519E91e3A54538791bDbb0E22373e36b66",
75//!     "base-sepolia"
76//! )?;
77//!
78//! if is_valid {
79//!     println!("Signature is valid!");
80//! }
81//! # Ok(())
82//! # }
83//! ```
84//!
85//! ## Generating Nonces
86//!
87//! ```
88//! use rust_x402::crypto::signature::generate_nonce;
89//!
90//! // Generate a random nonce for EIP-3009 authorization
91//! let nonce = generate_nonce();
92//! println!("Nonce: {:?}", nonce);
93//! ```
94//!
95//! # Features
96//!
97//! - **JWT Authentication** - Generate JWT tokens for API authentication
98//! - **EIP-712 Hashing** - Hash typed data according to EIP-712 specification
99//! - **Signature Verification** - Verify ECDSA signatures with public key recovery
100//! - **Nonce Generation** - Generate cryptographically secure random nonces
101//! - **Payment Verification** - Complete payment payload signature verification
102
103/// EIP-712 domain separator for EIP-3009 transfers
104pub const EIP712_DOMAIN: &str = r#"{"name":"USD Coin","version":"2","chainId":8453,"verifyingContract":"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"}"#;
105
106pub mod eip712;
107pub mod jwt;
108pub mod signature;
109
110#[cfg(test)]
111mod tests;
112
113// Re-export commonly used items
114pub use eip712::{Domain, TypedData};
115pub use jwt::{create_auth_header, create_auth_header_with_method, generate_jwt, JwtOptions};
116pub use signature::{
117    generate_nonce, sign_message_hash, verify_eip712_signature, verify_payment_payload,
118};