Skip to main content

Crate x402

Crate x402 

Source
Expand description

§tempo-x402

HTTP 402 Payment Required for the Tempo blockchain.

Implements pay-per-request API monetization using EIP-712 signed authorizations and TIP-20 (ERC-20 compatible) token transfers on the Tempo chain. One header, one on-chain transfer, zero custodial risk.

§How it works

  1. Client requests a protected endpoint
  2. Server responds 402 with pricing (token, amount, recipient)
  3. Client signs an EIP-712 PaymentAuthorization, retries with PAYMENT-SIGNATURE header
  4. Facilitator atomically verifies the signature, checks balance/allowance/nonce, and calls transferFrom on-chain
  5. Server returns the content + transaction hash

The facilitator holds no user funds — it only has token approval to call transferFrom on behalf of clients who explicitly approved it.

§Architecture

Three-party model:

§Modules

ModulePurpose
constantsChain configuration (ID 42431), token address, well-known addresses
eip712EIP-712 typed-data signing, signature verification, nonce generation
walletWASM-compatible wallet: key generation, EIP-712 signing, payment payloads
clientClient SDK — handles 402 flow automatically
schemeCore trait definitions (scheme::SchemeClient, scheme::SchemeFacilitator, scheme::SchemeServer)
scheme_serverServer implementation: price parsing and payment requirements
scheme_facilitatorFacilitator implementation: signature verification and on-chain settlement
tip20On-chain TIP-20 token operations (balance, allowance, transfer, approve)
nonce_storeReplay protection backends (in-memory and persistent SQLite)
paymentPayment data structures (payloads, requirements, 402 response body)
responseFacilitator response types (verify/settle results)
hmacHMAC-SHA256 for facilitator request authentication
securityConstant-time comparison utilities
networkSSRF protection: private IP detection, DNS validation
facilitator_clientHTTP client for calling a remote facilitator
errorError types for all x402 operations

§Quick start

Parse a price and generate payment requirements:

use x402::scheme::SchemeServer;
use x402::scheme_server::TempoSchemeServer;

let server = TempoSchemeServer::default();
let (amount, asset) = server.parse_price("$0.001").unwrap();
assert_eq!(amount, "1000"); // 1000 micro-tokens (6 decimals)

Generate a wallet and sign a payment:

use x402::wallet::{generate_random_key, WalletSigner};

let key = generate_random_key();
let signer = WalletSigner::new(&key).unwrap();
let address = signer.address();

§Workspace crates

CratePurpose
tempo-x402 (this crate)Core library
tempo-x402-gatewayAPI gateway + embedded facilitator
tempo-x402-identityAgent identity: wallet, faucet, ERC-8004
tempo-x402-soulAutonomous cognition: plans, memory, coding agent
tempo-x402-nodeSelf-deploying node with clone orchestration

§Feature flags

  • full (default) — all features: async runtime, SQLite nonce store, HTTP client
  • wasm — WASM-compatible subset: types, EIP-712 signing, wallet (no tokio/rusqlite)
  • demo — includes a demo private key for testing

§Network

  • Chain: Tempo Moderato, Chain ID 42431
  • Token: pathUSD 0x20c0000000000000000000000000000000000000 (6 decimals)
  • RPC: https://rpc.moderato.tempo.xyz

Re-exports§

pub use constants::ChainConfig;
pub use error::X402Error;
pub use scheme_facilitator::TempoSchemeFacilitator;
pub use scheme_server::TempoSchemeServer;
pub use wallet::build_payment_payload;
pub use wallet::generate_random_key;
pub use wallet::recover_message_signer;
pub use wallet::WalletSigner;

Modules§

TIP20
Module containing a contract’s types and functions.
client
Client SDK for making paid API requests (handles 402 flow automatically). x402 Client SDK for making paid API requests.
constants
Chain configuration, token addresses, and well-known constants. Chain configuration and well-known constants for the Tempo blockchain.
eip712
EIP-712 typed-data signing, signature verification, and nonce generation. EIP-712 typed-data signing, signature verification, and nonce generation.
error
Error types for x402 operations. Error types for x402 payment operations.
facilitator_client
HTTP client for calling a remote facilitator’s /verify-and-settle endpoint. HTTP client for calling a remote facilitator’s /verify-and-settle endpoint.
hmac
HMAC-SHA256 utilities for authenticating facilitator requests. HMAC-SHA256 utilities for authenticating facilitator requests.
network
Network validation utilities (private IP detection for SSRF protection). Network validation utilities shared across x402 crates.
nonce_store
Replay protection via nonce tracking (in-memory and persistent SQLite backends). Replay protection via nonce tracking.
payment
Payment data structures exchanged between client, server, and facilitator. Payment data structures exchanged between client, server, and facilitator.
response
Facilitator response types returned after verify/settle operations. Facilitator response types for verify and settle operations.
scheme
Core trait definitions for the three-party payment model. Core trait definitions for the three-party payment model.
scheme_facilitator
scheme::SchemeFacilitator implementation: signature verification and on-chain settlement. SchemeFacilitator implementation for the Tempo blockchain.
scheme_server
scheme::SchemeServer implementation: price parsing and payment requirements. SchemeServer implementation for the Tempo blockchain.
security
Constant-time comparison utilities for timing-attack resistance. Shared security utilities for the x402 payment protocol.
tip20
TIP-20 (ERC-20 compatible) on-chain token operations. TIP-20 (ERC-20 compatible) on-chain token operations.
wallet
WASM-compatible wallet: key generation, EIP-712 signing, payment payloads. WASM-compatible wallet for x402 payments.

Structs§

PaymentAuthorization