1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
//! # tempo-x402
//!
//! **HTTP 402 Payment Required** for the Tempo blockchain.
//!
//! Implements pay-per-request API monetization using [EIP-712](https://eips.ethereum.org/EIPS/eip-712)
//! signed authorizations and TIP-20 (ERC-20 compatible) token transfers on the
//! [Tempo](https://tempo.xyz) 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:
//!
//! - **Client** ([`client::X402Client`]) — signs payment authorizations, handles the 402 retry flow
//! - **Server** ([`scheme_server::TempoSchemeServer`]) — gates endpoints, returns 402 with pricing
//! - **Facilitator** ([`scheme_facilitator::TempoSchemeFacilitator`]) — verifies signatures and settles on-chain
//!
//! ## Modules
//!
//! | Module | Purpose |
//! |--------|---------|
//! | [`constants`] | Chain configuration (ID `42431`), token address, well-known addresses |
//! | [`eip712`] | EIP-712 typed-data signing, signature verification, nonce generation |
//! | [`wallet`] | WASM-compatible wallet: key generation, EIP-712 signing, payment payloads |
//! | [`client`] | Client SDK — handles 402 flow automatically |
//! | [`scheme`] | Core trait definitions ([`scheme::SchemeClient`], [`scheme::SchemeFacilitator`], [`scheme::SchemeServer`]) |
//! | [`scheme_server`] | Server implementation: price parsing and payment requirements |
//! | [`scheme_facilitator`] | Facilitator implementation: signature verification and on-chain settlement |
//! | [`tip20`] | On-chain TIP-20 token operations (balance, allowance, transfer, approve) |
//! | [`nonce_store`] | Replay protection backends (in-memory and persistent SQLite) |
//! | [`payment`] | Payment data structures (payloads, requirements, 402 response body) |
//! | [`response`] | Facilitator response types (verify/settle results) |
//! | [`hmac`] | HMAC-SHA256 for facilitator request authentication |
//! | [`security`] | Constant-time comparison utilities |
//! | [`network`] | SSRF protection: private IP detection, DNS validation |
//! | [`facilitator_client`] | HTTP client for calling a remote facilitator |
//! | [`error`] | Error 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
//!
//! | Crate | Purpose |
//! |-------|---------|
//! | **tempo-x402** (this crate) | Core library |
//! | [`tempo-x402-gateway`](https://docs.rs/tempo-x402-gateway) | API gateway + embedded facilitator |
//! | [`tempo-x402-identity`](https://docs.rs/tempo-x402-identity) | Agent identity: wallet, faucet, ERC-8004 |
//! | [`tempo-x402-soul`](https://docs.rs/tempo-x402-soul) | Autonomous cognition: plans, memory, coding agent |
//! | [`tempo-x402-node`](https://docs.rs/tempo-x402-node) | Self-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`
// ---------------------------------------------------------------------------
// Public modules — organized by layer
// ---------------------------------------------------------------------------
/// Chain configuration, token addresses, and well-known constants.
/// Error types for x402 operations.
/// Payment data structures exchanged between client, server, and facilitator.
/// Facilitator response types returned after verify/settle operations.
/// Core trait definitions for the three-party payment model.
/// EIP-712 typed-data signing, signature verification, and nonce generation.
/// WASM-compatible wallet: key generation, EIP-712 signing, payment payloads.
/// TIP-20 (ERC-20 compatible) on-chain token operations.
/// Replay protection via nonce tracking (in-memory and persistent SQLite backends).
/// HMAC-SHA256 utilities for authenticating facilitator requests.
/// Constant-time comparison utilities for timing-attack resistance.
/// Network validation utilities (private IP detection for SSRF protection).
/// [`scheme::SchemeFacilitator`] implementation: signature verification and on-chain settlement.
/// [`scheme::SchemeServer`] implementation: price parsing and payment requirements.
/// HTTP client for calling a remote facilitator's `/verify-and-settle` endpoint.
/// Client SDK for making paid API requests (handles 402 flow automatically).
// ---------------------------------------------------------------------------
// Solidity type bindings (generated by alloy sol! macro)
// ---------------------------------------------------------------------------
use sol;
// EIP-712 struct for payment authorizations.
//
// The `sol!` macro derives `alloy::sol_types::SolStruct` which provides
// `eip712_signing_hash()`. This struct is the on-chain representation of a
// payment authorization that clients sign and facilitators verify.
//
sol!
// TIP-20 (ERC-20 compatible) contract interface for on-chain token operations.
//
// Used by the `tip20` module functions to interact with the pathUSD token contract.
sol!
// ---------------------------------------------------------------------------
// Convenience re-exports — key types available at crate root
// ---------------------------------------------------------------------------
pub use ChainConfig;
pub use X402Error;
pub use TempoSchemeFacilitator;
pub use TempoSchemeServer;
pub use ;