Skip to main content

fly402_core/
lib.rs

1//! # 402fly Core
2//!
3//! Core library for the 402fly payment protocol with Solana blockchain integration.
4//!
5//! This library provides the fundamental types, error handling, and payment processing
6//! capabilities for implementing the 402fly protocol - an open standard enabling AI agents
7//! to autonomously pay for API access using Solana blockchain micropayments.
8//!
9//! ## Features
10//!
11//! - **Payment Models**: `PaymentRequest` and `PaymentAuthorization` for structured payment flow
12//! - **Error Handling**: Comprehensive error types for all 402fly operations
13//! - **Solana Integration**: `SolanaPaymentProcessor` for blockchain transactions
14//! - **Serialization**: Base64-encoded JSON for HTTP headers
15//!
16//! ## Example
17//!
18//! ```rust,no_run
19//! use fly402_core::{PaymentRequest, SolanaPaymentProcessor};
20//! use solana_sdk::signature::Keypair;
21//!
22//! #[tokio::main]
23//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
24//!     // Parse payment request from API response
25//!     let payment_request = PaymentRequest::from_json(r#"{
26//!         "max_amount_required": "0.10",
27//!         "asset_type": "SPL",
28//!         "asset_address": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
29//!         "payment_address": "7xKXtg2CW87d97TXJSDpbD5jBkheTqA83TZRuJosgAsU",
30//!         "network": "solana-devnet",
31//!         "expires_at": "2025-10-31T12:00:00Z",
32//!         "nonce": "abc123",
33//!         "payment_id": "pay_123",
34//!         "resource": "/api/premium-data"
35//!     }"#)?;
36//!
37//!     // Create payment processor
38//!     let processor = SolanaPaymentProcessor::new(
39//!         "https://api.devnet.solana.com",
40//!         None
41//!     );
42//!
43//!     // Create and send payment
44//!     let keypair = Keypair::new(); // Use your actual keypair
45//!     let authorization = processor.create_payment(&payment_request, &keypair).await?;
46//!
47//!     // Use authorization in retry request
48//!     let header_value = authorization.to_header_value()?;
49//!     println!("X-Payment-Authorization: {}", header_value);
50//!
51//!     Ok(())
52//! }
53//! ```
54
55pub mod errors;
56pub mod models;
57pub mod payment_processor;
58
59// Re-export commonly used types
60pub use errors::{X402Error, X402Result};
61pub use models::{PaymentAuthorization, PaymentRequest};
62pub use payment_processor::SolanaPaymentProcessor;
63
64/// Library version
65pub const VERSION: &str = env!("CARGO_PKG_VERSION");
66
67#[cfg(test)]
68mod tests {
69    use super::*;
70
71    #[test]
72    fn test_version() {
73        assert!(!VERSION.is_empty());
74    }
75}