rust-x402 0.2.2

HTTP-native micropayments with x402 protocol
Documentation
# x402 Rust Implementation

![x402 Logo](logo.png)

A **high-performance, type-safe** Rust implementation of the x402 HTTP-native micropayment protocol.

## ๐Ÿ“ฆ Installation

Add this to your `Cargo.toml`:

```toml
[dependencies]
rust-x402 = "0.2.2"
```

## โœจ Features

- ๐Ÿš€ **HTTP-native micropayments**: Leverage the HTTP 402 status code for payment requirements
- โ›“๏ธ **Blockchain integration**: Support for EIP-3009 token transfers with real wallet integration
- ๐ŸŒ **Web framework support**: Middleware for Axum, Actix Web, and Warp
- ๐Ÿ’ฐ **Facilitator integration**: Built-in support for payment verification and settlement
- ๐Ÿ“ฆ **Standalone facilitator**: Production-ready facilitator server as standalone binary
- ๐Ÿ—„๏ธ **Redis storage**: Optional Redis backend for distributed nonce storage
- ๐Ÿ”’ **Type safety**: Strongly typed Rust implementation with comprehensive error handling
- ๐Ÿงช **Comprehensive testing**: 114 tests with 100% pass rate covering all real implementations
- ๐Ÿ—๏ธ **Real implementations**: Production-ready wallet, blockchain, and facilitator clients
- ๐ŸŒŠ **Multipart & Streaming**: Full support for large file uploads and streaming responses
- ๐Ÿ“ก **HTTP/3 Support**: Optional HTTP/3 (QUIC) support for modern high-performance networking

## ๐Ÿš€ Quick Start

### Creating a Payment Server with Axum

```rust,no_run
use axum::{response::Json, routing::get};
use rust_x402::{
    axum::{create_payment_app, examples, AxumPaymentConfig},
    types::FacilitatorConfig,
};
use rust_decimal::Decimal;
use std::str::FromStr;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create facilitator config
    let facilitator_config = FacilitatorConfig::default();
    
    // Create payment configuration
    let payment_config = AxumPaymentConfig::new(
        Decimal::from_str("0.0001")?,
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
    )
    .with_description("Premium API access")
    .with_facilitator_config(facilitator_config)
    .with_testnet(true);

    // Create the application with payment middleware
    let app = create_payment_app(payment_config, |router| {
        router.route("/joke", get(examples::joke_handler))
    });

    // Start server
    let listener = tokio::net::TcpListener::bind("0.0.0.0:4021").await?;
    axum::serve(listener, app).await?;

    Ok(())
}
```

### ๐Ÿ’ณ Making Payments with a Client

```rust
use rust_x402::client::X402Client;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = X402Client::new()?;
    
    // Make a request to a protected resource
    let response = client.get("http://localhost:4021/joke").send().await?;
    
    if response.status() == 402 {
        println!("Payment required! Status: {}", response.status());
        // Handle payment required - parse PaymentRequirements and create signed payload
        // See examples/client.rs for complete implementation
    } else {
        let text = response.text().await?;
        println!("Response: {}", text);
    }
    
    Ok(())
}
```

### ๐Ÿญ Running the Standalone Facilitator Server

The facilitator can run as a standalone binary with optional Redis storage:

```bash
# In-memory storage (default)
cargo run --bin facilitator --features axum

# Redis storage backend
STORAGE_BACKEND=redis cargo run --bin facilitator --features axum,redis

# Custom configuration
BIND_ADDRESS=0.0.0.0:4020 \
REDIS_URL=redis://localhost:6379 \
REDIS_KEY_PREFIX=x402:nonce: \
cargo run --bin facilitator --features axum,redis
```

## ๐Ÿ—๏ธ Architecture

The Rust implementation is organized into several modules:

- ๐Ÿ“ฆ **`types`**: Core data structures and type definitions
- ๐ŸŒ **`client`**: HTTP client with x402 payment support
- ๐Ÿ’ฐ **`facilitator`**: Payment verification and settlement
- ๐Ÿ—„๏ธ **`facilitator_storage`**: Nonce storage backends (in-memory and Redis)
- ๐Ÿ”ง **`middleware`**: Web framework middleware implementations
- ๐Ÿ” **`crypto`**: Cryptographic utilities for payment signing
- โŒ **`error`**: Comprehensive error handling
- ๐Ÿฆ **`wallet`**: Real wallet integration with EIP-712 signing
- โ›“๏ธ **`blockchain`**: Blockchain client for network interactions
- ๐Ÿญ **`blockchain_facilitator`**: Blockchain-based facilitator implementation
- ๐Ÿ“ก **`http3`**: HTTP/3 (QUIC) support (feature-gated)
- ๐Ÿ”„ **`proxy`**: Reverse proxy with streaming support

## ๐ŸŒ Supported Web Frameworks

- ๐Ÿš€ **Axum**: Modern, ergonomic web framework
- โšก **Actix Web**: High-performance actor-based framework
- ๐Ÿชถ **Warp**: Lightweight, composable web server

## ๐ŸŒ HTTP Protocol Support

- โœ… **HTTP/1.1**: Full support with chunked transfer encoding
- โœ… **HTTP/2**: Full support with multiplexing
- โœ… **Multipart**: Support for `multipart/form-data` uploads (via `multipart` feature)
- โœ… **Streaming**: Chunked and streaming responses (via `streaming` feature)
- ๐Ÿ”œ **HTTP/3** (optional): QUIC-based HTTP/3 via `http3` feature flag

## ๐ŸŽ›๏ธ Optional Features

x402 supports optional features for a modular build:

```toml
[dependencies]
rust-x402 = { version = "0.2.2", features = ["http3", "streaming", "multipart"] }
```

- **`http3`**: Enable HTTP/3 (QUIC) support
- **`streaming`**: Enable chunked and streaming responses
- **`multipart`**: Enable `multipart/form-data` upload support (requires `streaming`)
- **`redis`**: Enable Redis backend for facilitator storage
- **`axum`**: Enable Axum web framework integration (default)
- **`actix-web`**: Enable Actix Web framework integration
- **`warp`**: Enable Warp web framework integration

## โ›“๏ธ Blockchain Support

Currently supports:
- ๐Ÿ›๏ธ **Base**: Base mainnet and testnet
- โ„๏ธ **Avalanche**: Avalanche mainnet and Fuji testnet
- ๐Ÿ“œ **EIP-3009**: Transfer with Authorization standard

## ๐Ÿ“š Examples

See the `examples/` directory for complete working examples:
- ๐Ÿš€ `axum_server.rs`: Payment server using Axum
- ๐Ÿ’ณ `client.rs`: Client making payments
- ๐Ÿ’ฐ `facilitator.rs`: Custom facilitator implementation
- ๐Ÿฆ `real_implementation_demo.rs`: Real wallet and blockchain integration
- ๐Ÿ” `real_wallet_integration.rs`: Production-ready wallet integration

## ๐Ÿ“Š Testing

- โœ… **114 tests** with 100% pass rate
- ๐Ÿงช **Comprehensive coverage** of all real implementations
- ๐Ÿ” **Integration tests** for end-to-end workflows
- ๐Ÿ›ก๏ธ **Error handling tests** for robust error scenarios
- ๐ŸŒŠ **Multipart & streaming tests** for file upload/download scenarios
- ๐Ÿ“ก **HTTP/3 tests** (with `http3` feature)
- ๐Ÿ—„๏ธ **Redis storage tests** with auto-skip when unavailable
- โš™๏ธ **Feature-gated tests** for modular builds

## ๐Ÿ“„ License

Licensed under the Apache License, Version 2.0. See LICENSE for details.