rust-x402 0.3.0

HTTP-native micropayments with x402 protocol
Documentation
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! Tests for facilitator client

use super::FacilitatorClient;
use crate::types::{
    ExactEvmPayload, ExactEvmPayloadAuthorization, FacilitatorConfig, PaymentPayload,
    PaymentRequirements,
};
use crate::X402Error;
use mockito::{Matcher, Server};
use serde_json::json;
use std::collections::HashMap;
use std::time::Duration;

#[tokio::test]
async fn test_facilitator_client_creation() {
    let config = FacilitatorConfig::new("https://example.com/facilitator");
    let client = FacilitatorClient::new(config).unwrap();
    assert_eq!(client.url(), "https://example.com/facilitator");
}

#[tokio::test]
async fn test_facilitator_verify_success() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("POST", "/verify")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "isValid": true,
                "payer": "0x857b06519E91e3A54538791bDbb0E22373e36b66"
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let response = client
        .verify(&payment_payload, &payment_requirements)
        .await
        .unwrap();
    assert!(response.is_valid);
    assert_eq!(
        response.payer,
        Some("0x857b06519E91e3A54538791bDbb0E22373e36b66".to_string())
    );
}

#[tokio::test]
async fn test_facilitator_verify_failure() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("POST", "/verify")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "isValid": false,
                "invalidReason": "insufficient_funds",
                "payer": "0x857b06519E91e3A54538791bDbb0E22373e36b66"
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let response = client
        .verify(&payment_payload, &payment_requirements)
        .await
        .unwrap();
    assert!(!response.is_valid);
    assert_eq!(
        response.invalid_reason,
        Some("insufficient_funds".to_string())
    );
}

#[tokio::test]
async fn test_facilitator_settle_success() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("POST", "/settle")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "success": true,
                "transaction": "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
                "network": "base-sepolia",
                "payer": "0x857b06519E91e3A54538791bDbb0E22373e36b66"
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let response = client
        .settle(&payment_payload, &payment_requirements)
        .await
        .unwrap();
    assert!(response.success);
    assert_eq!(
        response.transaction,
        "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef"
    );
    assert_eq!(response.network, "base-sepolia");
}

#[tokio::test]
async fn test_facilitator_settle_failure() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("POST", "/settle")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "success": false,
                "errorReason": "transaction_failed",
                "transaction": "",
                "network": "base-sepolia",
                "payer": "0x857b06519E91e3A54538791bDbb0E22373e36b66"
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let response = client
        .settle(&payment_payload, &payment_requirements)
        .await
        .unwrap();
    assert!(!response.success);
    assert_eq!(
        response.error_reason,
        Some("transaction_failed".to_string())
    );
    assert_eq!(response.transaction, "");
}

#[tokio::test]
async fn test_facilitator_server_error() {
    let mut server = Server::new_async().await;
    let _mock = server.mock("POST", "/verify").with_status(500).create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let result = client.verify(&payment_payload, &payment_requirements).await;
    assert!(result.is_err());
    assert!(result
        .unwrap_err()
        .to_string()
        .contains("Verification failed with status: 500"));
}

#[tokio::test]
async fn test_facilitator_supported() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/supported")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "kinds": [
                    {
                        "x402Version": 1,
                        "scheme": "exact",
                        "network": "base-sepolia"
                    },
                    {
                        "x402Version": 1,
                        "scheme": "exact",
                        "network": "base"
                    }
                ]
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let supported = client.supported().await.unwrap();
    assert_eq!(supported.kinds.len(), 2);
    assert_eq!(supported.kinds[0].scheme, "exact");
    assert_eq!(supported.kinds[0].network, "base-sepolia");
    assert_eq!(supported.kinds[1].network, "base");
}

#[tokio::test]
async fn test_facilitator_with_auth_headers() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("POST", "/verify")
        .with_status(200)
        .with_header("content-type", "application/json")
        .match_header("Authorization", "Bearer test-token")
        .match_header("Correlation-Context", Matcher::Regex(r".*".to_string()))
        .with_body(
            json!({
                "x402Version": 1,
                "isValid": true,
                "payer": "0x857b06519E91e3A54538791bDbb0E22373e36b66"
            })
            .to_string(),
        )
        .create();

    let create_auth_headers = || {
        let mut headers = HashMap::new();
        let mut verify_headers = HashMap::new();
        verify_headers.insert("Authorization".to_string(), "Bearer test-token".to_string());
        verify_headers.insert(
            "Correlation-Context".to_string(),
            "test=correlation".to_string(),
        );
        headers.insert("verify".to_string(), verify_headers);
        Ok(headers)
    };

    let config =
        FacilitatorConfig::new(server.url()).with_auth_headers(Box::new(create_auth_headers));
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let response = client
        .verify(&payment_payload, &payment_requirements)
        .await
        .unwrap();
    assert!(response.is_valid);
}

#[tokio::test]
async fn test_facilitator_timeout() {
    // Test with a very short timeout and a URL that will timeout
    let config = FacilitatorConfig::new("http://10.255.255.1:9999") // Non-routable IP
        .with_timeout(Duration::from_millis(1));
    let client = FacilitatorClient::new(config).unwrap();

    let payment_payload = create_test_payment_payload();
    let payment_requirements = create_test_payment_requirements();

    let result = client.verify(&payment_payload, &payment_requirements).await;
    assert!(result.is_err());
    // Check for timeout-related error - be more flexible with error messages
    let error_msg = result.unwrap_err().to_string();
    assert!(
        error_msg.contains("timeout")
            || error_msg.contains("connection")
            || error_msg.contains("network")
            || error_msg.contains("unreachable")
            || error_msg.contains("refused")
            || error_msg.contains("No route to host")
            || error_msg.contains("failed to connect")
            || error_msg.contains("Connection refused")
            || error_msg.contains("Network is unreachable")
            || error_msg.contains("Name or service not known")
            || error_msg.contains("Temporary failure in name resolution")
            || error_msg.contains("error sending request")
            || error_msg.contains("HTTP error")
            || error_msg.contains("Facilitator error"),
        "Expected timeout/connection error, got: {}",
        error_msg
    );
}

#[tokio::test]
async fn test_network_mismatch_returns_error() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("POST", "/verify")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "isValid": true,
                "payer": "0x857b06519E91e3A54538791bDbb0E22373e36b66"
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    // Create payment payload with different network than requirements
    let authorization = ExactEvmPayloadAuthorization::new(
        "0x857b06519E91e3A54538791bDbb0E22373e36b66",
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
        "1000000",
        "1745323800",
        "1745323985",
        "0xf3746613c2d920b5fdabc0856f2aeb2d4f88ee6037b8cc5d04a71a4462f13480",
    );

    let payload = ExactEvmPayload {
        signature: "0x2d6a7588d6acca505cbf0d9a4a227e0c52c6c34008c8e8986a1283259764173608a2ce6496642e377d6da8dbbf5836e9bd15092f9ecab05ded3d6293af148b571c".to_string(),
        authorization,
    };

    // Payment with "base" network
    let payment_payload = PaymentPayload::new("exact", "base", payload);

    // Requirements with "base-sepolia" network - should cause panic
    let payment_requirements = PaymentRequirements::new(
        "exact",
        "base-sepolia", // Different network - should panic
        "1000000",
        "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
        "https://example.com/test",
        "Test payment",
    );

    // This should return an error due to network mismatch
    let result = client
        .verify_with_network_validation(&payment_payload, &payment_requirements)
        .await;

    // Verify that we get an error for network mismatch
    assert!(result.is_err(), "Network mismatch should result in error");

    // Verify the error is specifically a payment verification error
    let error = result.unwrap_err();
    match error {
        X402Error::PaymentVerificationFailed { reason: _ } => {
            // This is the expected error type
        }
        _ => panic!("Expected PaymentVerificationFailed error, got: {:?}", error),
    }

    // Verify the error message content
    let error_msg = error.to_string();
    assert!(
        error_msg.contains("Network mismatch detected"),
        "Error should contain 'Network mismatch detected' - actual: {}",
        error_msg
    );
    assert!(
        error_msg.contains("base") && error_msg.contains("base-sepolia"),
        "Error should contain both network names - actual: {}",
        error_msg
    );
}

// Helper functions for creating test data
fn create_test_payment_payload() -> PaymentPayload {
    let authorization = ExactEvmPayloadAuthorization::new(
        "0x857b06519E91e3A54538791bDbb0E22373e36b66",
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
        "1000000",
        "1745323800",
        "1745323985",
        "0xf3746613c2d920b5fdabc0856f2aeb2d4f88ee6037b8cc5d04a71a4462f13480",
    );

    let payload = ExactEvmPayload {
        signature: "0x2d6a7588d6acca505cbf0d9a4a227e0c52c6c34008c8e8986a1283259764173608a2ce6496642e377d6da8dbbf5836e9bd15092f9ecab05ded3d6293af148b571c".to_string(),
        authorization,
    };

    PaymentPayload::new("exact", "base-sepolia", payload)
}

fn create_test_payment_requirements() -> PaymentRequirements {
    PaymentRequirements::new(
        "exact",
        "base-sepolia",
        "1000000",
        "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
        "0x209693Bc6afc0C5328bA36FaF03C514EF312287C",
        "https://example.com/test",
        "Test payment",
    )
}

#[tokio::test]
async fn test_facilitator_discovery_list() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/discovery/resources")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "items": [
                    {
                        "resource": "https://example.com/resource1",
                        "type": "http",
                        "x402Version": 1,
                        "accepts": [],
                        "lastUpdated": 1640995200
                    }
                ],
                "pagination": {
                    "total": 1,
                    "limit": 10,
                    "offset": 0
                }
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let response = client.list_all().await;
    assert!(response.is_ok(), "Discovery list should succeed");

    let discovery_response = response.unwrap();
    assert_eq!(discovery_response.items.len(), 1);
    assert_eq!(
        discovery_response.items[0].resource,
        "https://example.com/resource1"
    );
    assert_eq!(discovery_response.items[0].r#type, "http");
}

#[tokio::test]
async fn test_facilitator_discovery_with_filters() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/discovery/resources")
        .match_query(Matcher::AllOf(vec![
            Matcher::UrlEncoded("type".to_string(), "http".to_string()),
            Matcher::UrlEncoded("limit".to_string(), "5".to_string()),
        ]))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "items": [],
                "pagination": {
                    "total": 0,
                    "limit": 5,
                    "offset": 0
                }
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let filters = crate::client::DiscoveryFilters::new()
        .with_resource_type("http")
        .with_limit(5);

    let response = client.list(Some(filters)).await;
    assert!(response.is_ok(), "Discovery with filters should succeed");

    let discovery_response = response.unwrap();
    assert_eq!(discovery_response.items.len(), 0);
    assert_eq!(discovery_response.pagination.limit, 5);
}

#[tokio::test]
async fn test_facilitator_discovery_by_type() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/discovery/resources")
        .match_query(Matcher::UrlEncoded("type".to_string(), "api".to_string()))
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "items": [
                    {
                        "resource": "https://api.example.com",
                        "type": "api",
                        "x402Version": 1,
                        "accepts": [],
                        "lastUpdated": 1640995200
                    }
                ],
                "pagination": {
                    "total": 1,
                    "limit": 10,
                    "offset": 0
                }
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let response = client.list_by_type("api").await;
    assert!(response.is_ok(), "Discovery by type should succeed");

    let discovery_response = response.unwrap();
    assert_eq!(discovery_response.items.len(), 1);
    assert_eq!(discovery_response.items[0].r#type, "api");
}

#[tokio::test]
async fn test_facilitator_discovery_error() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/discovery/resources")
        .with_status(500)
        .with_header("content-type", "application/json")
        .with_body(r#"{"error": "Internal server error"}"#)
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let response = client.list_all().await;
    assert!(response.is_err(), "Discovery should fail with 500 error");

    let error = response.unwrap_err();
    assert!(error
        .to_string()
        .contains("Discovery failed with status: 500"));
}

#[tokio::test]
async fn test_facilitator_supported_with_auth_headers() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/supported")
        .match_header("Authorization", "Bearer test-token")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "kinds": [
                    {
                        "x402Version": 1,
                        "scheme": "exact",
                        "network": "base-sepolia",
                        "metadata": {
                            "description": "Test metadata",
                            "version": "1.0.0"
                        }
                    }
                ]
            })
            .to_string(),
        )
        .create();

    let auth_config = || -> crate::Result<HashMap<String, HashMap<String, String>>> {
        let mut headers = HashMap::new();
        let mut supported_headers = HashMap::new();
        supported_headers.insert("Authorization".to_string(), "Bearer test-token".to_string());
        headers.insert("supported".to_string(), supported_headers);
        Ok(headers)
    };

    let config = FacilitatorConfig {
        url: server.url(),
        timeout: None,
        create_auth_headers: Some(std::sync::Arc::new(auth_config)),
    };
    let client = FacilitatorClient::new(config).unwrap();

    let response = client.supported().await;
    assert!(
        response.is_ok(),
        "Supported should succeed with auth headers"
    );

    let supported = response.unwrap();
    assert_eq!(supported.kinds.len(), 1);
    assert_eq!(supported.kinds[0].scheme, "exact");
    assert_eq!(supported.kinds[0].network, "base-sepolia");
    assert!(supported.kinds[0].metadata.is_some());

    let metadata = supported.kinds[0].metadata.as_ref().unwrap();
    assert_eq!(metadata["description"], "Test metadata");
    assert_eq!(metadata["version"], "1.0.0");
}

#[tokio::test]
async fn test_facilitator_supported_without_auth_headers() {
    let mut server = Server::new_async().await;
    let _mock = server
        .mock("GET", "/supported")
        .with_status(200)
        .with_header("content-type", "application/json")
        .with_body(
            json!({
                "x402Version": 1,
                "kinds": [
                    {
                        "x402Version": 1,
                        "scheme": "exact",
                        "network": "base-sepolia"
                    }
                ]
            })
            .to_string(),
        )
        .create();

    let config = FacilitatorConfig::new(server.url());
    let client = FacilitatorClient::new(config).unwrap();

    let response = client.supported().await;
    assert!(
        response.is_ok(),
        "Supported should succeed without auth headers"
    );

    let supported = response.unwrap();
    assert_eq!(supported.kinds.len(), 1);
    assert_eq!(supported.kinds[0].scheme, "exact");
    assert_eq!(supported.kinds[0].network, "base-sepolia");
    assert!(supported.kinds[0].metadata.is_none());
}

#[test]
fn test_facilitator_client_creation_with_invalid_config() {
    let config = FacilitatorConfig {
        url: "invalid-url".to_string(),
        timeout: None,
        create_auth_headers: None,
    };

    let result = FacilitatorClient::new(config);
    assert!(result.is_err(), "Should fail with invalid URL");

    let error = result.unwrap_err();
    assert!(error
        .to_string()
        .contains("Facilitator URL must start with http:// or https://"));
}

#[test]
fn test_facilitator_client_creation_with_valid_config() {
    let config = FacilitatorConfig {
        url: "https://example.com/facilitator".to_string(),
        timeout: Some(std::time::Duration::from_secs(30)),
        create_auth_headers: None,
    };

    let result = FacilitatorClient::new(config);
    assert!(result.is_ok(), "Should succeed with valid config");
}