rust_x402/types/mod.rs
1//! Core types for the x402 protocol
2//!
3//! This module defines all the core data structures and types used throughout the x402
4//! protocol implementation. It provides type-safe representations of payment requirements,
5//! payment payloads, network configurations, and facilitator responses.
6//!
7//! # Architecture
8//!
9//! The types module is organized as follows:
10//! - [`network`] - Network configuration and chain-specific details
11//! - [`payment`] - Payment requirements and payload structures
12//! - [`facilitator`] - Facilitator configuration and response types
13//! - [`discovery`] - Discovery API types for resource discovery
14//! - [`constants`] - Protocol constants (networks, schemes, addresses)
15//!
16//! # Examples
17//!
18//! ## Creating Payment Requirements
19//!
20//! ```
21//! use rust_x402::types::{PaymentRequirements, Network};
22//!
23//! # fn example() -> rust_x402::Result<()> {
24//! let mut requirements = PaymentRequirements::new(
25//! "exact", // scheme
26//! "base-sepolia", // network
27//! "1000000", // amount (1 USDC)
28//! "0x036CbD53842c5426634e7929541eC2318f3dCF7e", // USDC contract
29//! "0x209693Bc6afc0C5328bA36FaF03C514EF312287C", // recipient
30//! "https://api.example.com/resource", // resource URL
31//! "API access payment", // description
32//! );
33//!
34//! // Set USDC metadata
35//! requirements.set_usdc_info(Network::Testnet)?;
36//! # Ok(())
37//! # }
38//! ```
39//!
40//! ## Creating Payment Payload
41//!
42//! ```
43//! use rust_x402::types::{PaymentPayload, ExactEvmPayload, ExactEvmPayloadAuthorization};
44//!
45//! # fn example() -> rust_x402::Result<()> {
46//! let authorization = ExactEvmPayloadAuthorization::new(
47//! "0x857b06519E91e3A54538791bDbb0E22373e36b66", // from
48//! "0x209693Bc6afc0C5328bA36FaF03C514EF312287C", // to
49//! "1000000", // value
50//! "1745323800", // validAfter
51//! "1745323985", // validBefore
52//! "0xf3746613c2d920b5fdabc0856f2aeb2d4f88ee6037b8cc5d04a71a4462f13480", // nonce
53//! );
54//!
55//! let payload = ExactEvmPayload {
56//! signature: "0x2d6a...".to_string(),
57//! authorization,
58//! };
59//!
60//! let payment = PaymentPayload::new("exact", "base-sepolia", payload);
61//!
62//! // Encode to base64 for HTTP header
63//! let encoded = payment.to_base64()?;
64//! # Ok(())
65//! # }
66//! ```
67//!
68//! ## Network Configuration
69//!
70//! ```
71//! use rust_x402::types::{Network, NetworkConfig, networks};
72//!
73//! // Using the Network enum
74//! let network = Network::Testnet;
75//! println!("Network: {}", network.as_str());
76//! println!("USDC: {}", network.usdc_address());
77//!
78//! // Using NetworkConfig for detailed info
79//! let config = NetworkConfig::base_sepolia();
80//! println!("Chain ID: {}", config.chain_id);
81//! println!("USDC Contract: {}", config.usdc_contract);
82//!
83//! // Using constants
84//! let usdc = networks::get_usdc_address("base-sepolia");
85//! println!("USDC address: {:?}", usdc);
86//! ```
87//!
88//! ## Facilitator Configuration
89//!
90//! ```
91//! use rust_x402::types::FacilitatorConfig;
92//! use std::time::Duration;
93//!
94//! # fn example() -> rust_x402::Result<()> {
95//! let config = FacilitatorConfig::new("https://x402.org/facilitator")
96//! .with_timeout(Duration::from_secs(30));
97//!
98//! // Validate the configuration
99//! config.validate()?;
100//! # Ok(())
101//! # }
102//! ```
103//!
104//! # Type Categories
105//!
106//! ## Payment Types
107//! - [`PaymentRequirements`] - Describes what payment is required
108//! - [`PaymentPayload`] - Contains the actual payment authorization
109//! - [`ExactEvmPayload`] - EVM-specific payment data (EIP-3009)
110//! - [`ExactEvmPayloadAuthorization`] - Authorization parameters
111//!
112//! ## Response Types
113//! - [`VerifyResponse`] - Payment verification result
114//! - [`SettleResponse`] - Payment settlement result
115//! - [`SupportedKinds`] - Supported payment schemes and networks
116//! - [`DiscoveryResponse`] - Resource discovery results
117//!
118//! ## Configuration Types
119//! - [`FacilitatorConfig`] - Facilitator client configuration
120//! - [`NetworkConfig`] - Chain-specific network configuration
121//! - [`Network`] - Simple network enum (Mainnet/Testnet)
122
123pub mod constants;
124pub mod discovery;
125pub mod facilitator;
126pub mod network;
127pub mod payment;
128
129// Re-export commonly used types
130pub use constants::{networks, schemes};
131pub use discovery::{DiscoveryResource, DiscoveryResponse, PaginationInfo};
132pub use facilitator::{
133 AuthHeadersFn, AuthHeadersFnArc, AuthHeadersFnBox, FacilitatorConfig, SettleResponse,
134 SupportedKind, SupportedKinds, VerifyResponse,
135};
136pub use network::{Network, NetworkConfig};
137pub use payment::{
138 ExactEvmPayload, ExactEvmPayloadAuthorization, PaymentPayload, PaymentRequirements,
139 PaymentRequirementsResponse, X402_VERSION,
140};