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
//! Cryptographic utilities for x402 payments
//!
//! This module provides cryptographic primitives for the x402 protocol, including
//! JWT authentication, EIP-712 typed data hashing, and ECDSA signature verification.
//!
//! # Architecture
//!
//! The crypto module is organized as follows:
//! - [`jwt`] - JWT token generation for API authentication (primarily Coinbase CDP)
//! - [`eip712`] - EIP-712 typed data hashing for Ethereum transactions
//! - [`signature`] - ECDSA signature creation and verification
//!
//! # Examples
//!
//! ## JWT Authentication
//!
//! ```no_run
//! use rust_x402::crypto::jwt;
//!
//! # fn example() -> rust_x402::Result<()> {
//! // Create an authorization header for Coinbase API
//! let auth_header = jwt::create_auth_header(
//! "api_key_id",
//! "api_key_secret",
//! "api.cdp.coinbase.com",
//! "/platform/v2/x402/verify"
//! )?;
//!
//! // Use the auth_header in HTTP requests
//! println!("Authorization: {}", auth_header);
//! # Ok(())
//! # }
//! ```
//!
//! ## EIP-712 Typed Data Hashing
//!
//! ```no_run
//! use rust_x402::crypto::eip712::{Domain, create_transfer_with_authorization_hash};
//! use ethereum_types::{Address, H256, U256};
//! use std::str::FromStr;
//!
//! # fn example() -> rust_x402::Result<()> {
//! let domain = Domain {
//! name: "USD Coin".to_string(),
//! version: "2".to_string(),
//! chain_id: 8453,
//! verifying_contract: Address::from_str("0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913")?,
//! };
//!
//! let message_hash = create_transfer_with_authorization_hash(
//! &domain,
//! Address::from_str("0x...")?,
//! Address::from_str("0x...")?,
//! U256::from(1000000),
//! U256::from(0),
//! U256::from(u64::MAX),
//! H256::random(),
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! ## Signature Verification
//!
//! ```no_run
//! use rust_x402::crypto::signature;
//! use rust_x402::types::ExactEvmPayload;
//!
//! # fn example() -> rust_x402::Result<()> {
//! # let payload: ExactEvmPayload = todo!();
//! // Verify a payment payload signature
//! let is_valid = signature::verify_payment_payload(
//! &payload,
//! "0x857b06519E91e3A54538791bDbb0E22373e36b66",
//! "base-sepolia"
//! )?;
//!
//! if is_valid {
//! println!("Signature is valid!");
//! }
//! # Ok(())
//! # }
//! ```
//!
//! ## Generating Nonces
//!
//! ```
//! use rust_x402::crypto::signature::generate_nonce;
//!
//! // Generate a random nonce for EIP-3009 authorization
//! let nonce = generate_nonce();
//! println!("Nonce: {:?}", nonce);
//! ```
//!
//! # Features
//!
//! - **JWT Authentication** - Generate JWT tokens for API authentication
//! - **EIP-712 Hashing** - Hash typed data according to EIP-712 specification
//! - **Signature Verification** - Verify ECDSA signatures with public key recovery
//! - **Nonce Generation** - Generate cryptographically secure random nonces
//! - **Payment Verification** - Complete payment payload signature verification
/// EIP-712 domain separator for EIP-3009 transfers
pub const EIP712_DOMAIN: &str = r#"{"name":"USD Coin","version":"2","chainId":8453,"verifyingContract":"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913"}"#;
// Re-export commonly used items
pub use ;
pub use ;
pub use ;