tap-node 0.6.0

Transaction Authorization Protocol (TAP) node implementation for routing and processing messages
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
#[cfg(feature = "storage")]
mod storage_tests {
    use serde_json::json;
    use tap_msg::didcomm::PlainMessage;
    use tap_msg::message::{payment::Payment, transfer::Transfer, Party};
    use tap_node::storage::{
        MessageDirection, Storage, StorageError, TransactionStatus, TransactionType,
    };

    use tempfile::NamedTempFile;

    /// Create a SQLite database backed by a temporary file for testing
    async fn create_in_memory_storage() -> Storage {
        // Use a temporary file instead of pure in-memory to support connection pooling
        let temp_file = NamedTempFile::new().expect("Failed to create temp file");
        let path = temp_file.path().to_path_buf();

        // Keep the temp file alive by leaking it (it will be cleaned up on process exit)
        std::mem::forget(temp_file);

        Storage::new(Some(path))
            .await
            .expect("Failed to create storage")
    }

    /// Helper to create a test PlainMessage
    fn create_test_message(id: &str, msg_type: &str, body: serde_json::Value) -> PlainMessage {
        PlainMessage {
            id: id.to_string(),
            typ: "application/didcomm-plain+json".to_string(),
            type_: msg_type.to_string(),
            body,
            from: "did:example:alice".to_string(),
            to: vec!["did:example:bob".to_string()],
            thid: Some("thread_123".to_string()),
            pthid: None,
            extra_headers: Default::default(),
            attachments: None,
            created_time: None,
            expires_time: None,
            from_prior: None,
        }
    }

    /// Helper to create a Transfer message
    fn create_transfer_message(id: &str) -> PlainMessage {
        let mut originator = Party::new("did:example:originator");
        originator.add_metadata(
            "name".to_string(),
            serde_json::Value::String("Alice".to_string()),
        );

        let mut beneficiary = Party::new("did:example:beneficiary");
        beneficiary.add_metadata(
            "name".to_string(),
            serde_json::Value::String("Bob".to_string()),
        );

        let transfer_body = Transfer {
            transaction_id: Some(id.to_string()),
            originator: Some(originator),
            beneficiary: Some(beneficiary),
            asset: "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
                .parse()
                .unwrap(),
            amount: "1000000".to_string(),
            agents: vec![],
            memo: Some("Test transfer".to_string()),
            settlement_id: None,
            expiry: None,
            transaction_value: None,
            connection_id: None,
            metadata: Default::default(),
        };

        create_test_message(
            id,
            "https://tap-protocol.io/messages/transfer/1.0",
            serde_json::to_value(&transfer_body).unwrap(),
        )
    }

    /// Helper to create a Payment message
    fn create_payment_message(id: &str) -> PlainMessage {
        let payment_body = Payment {
            transaction_id: Some(id.to_string()),
            asset: Some(
                "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
                    .parse()
                    .unwrap(),
            ),
            amount: "50.00".to_string(),
            currency_code: None,
            supported_assets: None,
            customer: {
                let mut customer = Party::new("did:example:customer");
                customer.add_metadata(
                    "name".to_string(),
                    serde_json::Value::String("Charlie".to_string()),
                );
                Some(customer)
            },
            merchant: {
                let mut merchant = Party::new("did:example:merchant");
                merchant.add_metadata(
                    "name".to_string(),
                    serde_json::Value::String("Dave's Shop".to_string()),
                );
                merchant
            },
            memo: Some("Payment for goods".to_string()),
            invoice: None,
            metadata: Default::default(),
            agents: vec![],
            expiry: None,
            connection_id: None,
            fallback_settlement_addresses: None,
        };

        create_test_message(
            id,
            "https://tap-protocol.io/messages/payment/1.0",
            serde_json::to_value(&payment_body).unwrap(),
        )
    }

    #[tokio::test]
    async fn test_in_memory_storage_creation() {
        let storage = create_in_memory_storage().await;
        // If we get here without panic, storage was created successfully

        // Try to insert a message to verify it's working
        let msg = create_test_message(
            "test_1",
            "https://tap-protocol.io/messages/connect/1.0",
            json!({}),
        );
        assert!(storage
            .log_message(&msg, MessageDirection::Incoming)
            .await
            .is_ok());
    }

    #[tokio::test]
    async fn test_transaction_insertion_and_retrieval() {
        let storage = create_in_memory_storage().await;

        // Create and insert a transfer transaction
        let transfer_msg = create_transfer_message("transfer_001");
        storage.insert_transaction(&transfer_msg).await.unwrap();

        // Retrieve the transaction
        let retrieved = storage.get_transaction_by_id("transfer_001").await.unwrap();
        assert!(retrieved.is_some());

        let tx = retrieved.unwrap();
        assert_eq!(tx.reference_id, "transfer_001");
        assert_eq!(tx.transaction_type, TransactionType::Transfer);
        assert_eq!(tx.status, TransactionStatus::Pending);
        assert_eq!(tx.from_did, Some("did:example:alice".to_string()));
        assert_eq!(tx.to_did, Some("did:example:bob".to_string()));
        assert_eq!(tx.thread_id, Some("thread_123".to_string()));
    }

    #[tokio::test]
    async fn test_payment_transaction() {
        let storage = create_in_memory_storage().await;

        // Create and insert a payment transaction
        let payment_msg = create_payment_message("payment_001");
        storage.insert_transaction(&payment_msg).await.unwrap();

        // Retrieve and verify
        let retrieved = storage.get_transaction_by_id("payment_001").await.unwrap();
        assert!(retrieved.is_some());

        let tx = retrieved.unwrap();
        assert_eq!(tx.reference_id, "payment_001");
        assert_eq!(tx.transaction_type, TransactionType::Payment);
        assert_eq!(tx.status, TransactionStatus::Pending);
    }

    #[tokio::test]
    async fn test_transaction_list_pagination() {
        let storage = create_in_memory_storage().await;

        // Insert multiple transactions with delays to ensure different timestamps
        for i in 0..5 {
            let msg = create_transfer_message(&format!("transfer_{:03}", i));
            storage.insert_transaction(&msg).await.unwrap();
            // Small delay to ensure different timestamps
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }

        // Test pagination
        let page1 = storage.list_transactions(2, 0).await.unwrap();
        assert_eq!(page1.len(), 2);

        let page2 = storage.list_transactions(2, 2).await.unwrap();
        assert_eq!(page2.len(), 2);

        let page3 = storage.list_transactions(2, 4).await.unwrap();
        assert_eq!(page3.len(), 1);

        // Verify we got the expected IDs in reverse order (newest first)
        assert_eq!(page1[0].reference_id, "transfer_004");
        assert_eq!(page1[1].reference_id, "transfer_003");
        assert_eq!(page2[0].reference_id, "transfer_002");
        assert_eq!(page2[1].reference_id, "transfer_001");
        assert_eq!(page3[0].reference_id, "transfer_000");
    }

    #[tokio::test]
    async fn test_duplicate_transaction_handling() {
        let storage = create_in_memory_storage().await;

        let msg = create_transfer_message("duplicate_001");

        // First insertion should succeed
        assert!(storage.insert_transaction(&msg).await.is_ok());

        // Second insertion should fail with duplicate error
        let result = storage.insert_transaction(&msg).await;
        assert!(matches!(result, Err(StorageError::DuplicateTransaction(_))));
    }

    #[tokio::test]
    async fn test_message_logging_all_directions() {
        let storage = create_in_memory_storage().await;

        // Log incoming messages
        let incoming1 = create_test_message(
            "in_001",
            "https://tap-protocol.io/messages/connect/1.0",
            json!({}),
        );
        let incoming2 = create_test_message(
            "in_002",
            "https://tap-protocol.io/messages/authorize/1.0",
            json!({}),
        );

        storage
            .log_message(&incoming1, MessageDirection::Incoming)
            .await
            .unwrap();
        storage
            .log_message(&incoming2, MessageDirection::Incoming)
            .await
            .unwrap();

        // Log outgoing messages
        let outgoing1 = create_test_message(
            "out_001",
            "https://tap-protocol.io/messages/reject/1.0",
            json!({}),
        );
        let outgoing2 = create_test_message(
            "out_002",
            "https://tap-protocol.io/messages/settle/1.0",
            json!({}),
        );

        storage
            .log_message(&outgoing1, MessageDirection::Outgoing)
            .await
            .unwrap();
        storage
            .log_message(&outgoing2, MessageDirection::Outgoing)
            .await
            .unwrap();

        // Verify all messages are stored
        let all_messages = storage.list_messages(10, 0, None).await.unwrap();
        assert_eq!(all_messages.len(), 4);

        // Verify filtering by direction
        let incoming_only = storage
            .list_messages(10, 0, Some(MessageDirection::Incoming))
            .await
            .unwrap();
        assert_eq!(incoming_only.len(), 2);
        assert!(incoming_only
            .iter()
            .all(|m| m.direction == MessageDirection::Incoming));

        let outgoing_only = storage
            .list_messages(10, 0, Some(MessageDirection::Outgoing))
            .await
            .unwrap();
        assert_eq!(outgoing_only.len(), 2);
        assert!(outgoing_only
            .iter()
            .all(|m| m.direction == MessageDirection::Outgoing));
    }

    #[tokio::test]
    async fn test_message_retrieval_by_id() {
        let storage = create_in_memory_storage().await;

        let msg = create_test_message(
            "specific_001",
            "https://tap-protocol.io/messages/transfer/1.0",
            json!({ "test": "data" }),
        );

        storage
            .log_message(&msg, MessageDirection::Incoming)
            .await
            .unwrap();

        // Retrieve by ID
        let retrieved = storage.get_message_by_id("specific_001").await.unwrap();
        assert!(retrieved.is_some());

        let stored_msg = retrieved.unwrap();
        assert_eq!(stored_msg.message_id, "specific_001");
        assert_eq!(
            stored_msg.message_type,
            "https://tap-protocol.io/messages/transfer/1.0"
        );
        assert_eq!(stored_msg.from_did, Some("did:example:alice".to_string()));
        assert_eq!(stored_msg.to_did, Some("did:example:bob".to_string()));
        assert_eq!(stored_msg.thread_id, Some("thread_123".to_string()));
        assert_eq!(stored_msg.direction, MessageDirection::Incoming);

        // Verify the JSON content - message_json is already a serde_json::Value
        assert_eq!(stored_msg.message_json["body"]["test"], "data");
    }

    #[tokio::test]
    async fn test_message_duplicate_handling() {
        let storage = create_in_memory_storage().await;

        let msg = create_test_message(
            "dup_msg_001",
            "https://tap-protocol.io/messages/connect/1.0",
            json!({}),
        );

        // First insertion should succeed
        assert!(storage
            .log_message(&msg, MessageDirection::Incoming)
            .await
            .is_ok());

        // Second insertion should silently succeed (idempotent)
        assert!(storage
            .log_message(&msg, MessageDirection::Incoming)
            .await
            .is_ok());

        // Verify only one message is stored
        let messages = storage.list_messages(10, 0, None).await.unwrap();
        assert_eq!(messages.len(), 1);
    }

    #[tokio::test]
    async fn test_message_thread_tracking() {
        let storage = create_in_memory_storage().await;

        // Create messages with thread relationships
        let mut parent_msg = create_test_message(
            "parent_001",
            "https://tap-protocol.io/messages/transfer/1.0",
            json!({}),
        );
        parent_msg.thid = Some("thread_parent".to_string());
        parent_msg.pthid = None;

        let mut child_msg = create_test_message(
            "child_001",
            "https://tap-protocol.io/messages/authorize/1.0",
            json!({}),
        );
        child_msg.thid = Some("thread_child".to_string());
        child_msg.pthid = Some("thread_parent".to_string());

        storage
            .log_message(&parent_msg, MessageDirection::Incoming)
            .await
            .unwrap();
        storage
            .log_message(&child_msg, MessageDirection::Outgoing)
            .await
            .unwrap();

        // Retrieve and verify thread relationships
        let parent = storage
            .get_message_by_id("parent_001")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(parent.thread_id, Some("thread_parent".to_string()));
        assert_eq!(parent.parent_thread_id, None);

        let child = storage
            .get_message_by_id("child_001")
            .await
            .unwrap()
            .unwrap();
        assert_eq!(child.thread_id, Some("thread_child".to_string()));
        assert_eq!(child.parent_thread_id, Some("thread_parent".to_string()));
    }

    #[tokio::test]
    async fn test_message_pagination() {
        let storage = create_in_memory_storage().await;

        // Insert 15 messages
        for i in 0..15 {
            let msg = create_test_message(
                &format!("page_test_{:03}", i),
                "https://tap-protocol.io/messages/connect/1.0",
                json!({}),
            );
            storage
                .log_message(&msg, MessageDirection::Incoming)
                .await
                .unwrap();

            // Small delay to ensure different timestamps
            tokio::time::sleep(tokio::time::Duration::from_millis(10)).await;
        }

        // Test pagination
        let page1 = storage.list_messages(5, 0, None).await.unwrap();
        assert_eq!(page1.len(), 5);

        let page2 = storage.list_messages(5, 5, None).await.unwrap();
        assert_eq!(page2.len(), 5);

        let page3 = storage.list_messages(5, 10, None).await.unwrap();
        assert_eq!(page3.len(), 5);

        // Verify no overlap between pages
        let page1_ids: Vec<_> = page1.iter().map(|m| &m.message_id).collect();
        let page2_ids: Vec<_> = page2.iter().map(|m| &m.message_id).collect();
        assert!(page1_ids.iter().all(|id| !page2_ids.contains(id)));

        // Verify we got messages in the expected order (newest first)
        assert_eq!(page1[0].message_id, "page_test_014");
        assert_eq!(page1[4].message_id, "page_test_010");
        assert_eq!(page2[0].message_id, "page_test_009");
        assert_eq!(page3[4].message_id, "page_test_000");
    }

    #[tokio::test]
    async fn test_non_transaction_messages_not_in_transactions_table() {
        let storage = create_in_memory_storage().await;

        // Create non-transaction messages
        let connect_msg = create_test_message(
            "connect_001",
            "https://tap-protocol.io/messages/connect/1.0",
            json!({}),
        );
        let auth_msg = create_test_message(
            "auth_001",
            "https://tap-protocol.io/messages/authorize/1.0",
            json!({}),
        );

        // Log them as messages
        storage
            .log_message(&connect_msg, MessageDirection::Incoming)
            .await
            .unwrap();
        storage
            .log_message(&auth_msg, MessageDirection::Incoming)
            .await
            .unwrap();

        // Verify they're in messages table
        let messages = storage.list_messages(10, 0, None).await.unwrap();
        assert_eq!(messages.len(), 2);

        // Verify they're NOT in transactions table
        let transactions = storage.list_transactions(10, 0).await.unwrap();
        assert_eq!(transactions.len(), 0);

        // Now add a transfer message
        let transfer_msg = create_transfer_message("transfer_001");
        storage
            .log_message(&transfer_msg, MessageDirection::Incoming)
            .await
            .unwrap();
        storage.insert_transaction(&transfer_msg).await.unwrap();

        // Verify it's in both tables
        let messages = storage.list_messages(10, 0, None).await.unwrap();
        assert_eq!(messages.len(), 3);

        let transactions = storage.list_transactions(10, 0).await.unwrap();
        assert_eq!(transactions.len(), 1);
        assert_eq!(transactions[0].reference_id, "transfer_001");
    }

    #[tokio::test]
    async fn test_concurrent_operations() {
        let storage = create_in_memory_storage().await;

        // Spawn multiple tasks to insert messages concurrently
        let mut handles = vec![];

        for i in 0..10 {
            let storage_clone = storage.clone();
            let handle = tokio::spawn(async move {
                // Use thread ID and index to ensure unique message IDs
                let thread_id = std::thread::current().id();
                let msg = create_test_message(
                    &format!("concurrent_{:03}_{:?}", i, thread_id),
                    "https://tap-protocol.io/messages/connect/1.0",
                    json!({}),
                );
                storage_clone
                    .log_message(&msg, MessageDirection::Incoming)
                    .await
            });
            handles.push(handle);
        }

        // Wait for all tasks to complete and collect results
        let mut successes = 0;
        for handle in handles {
            match handle.await {
                Ok(Ok(_)) => successes += 1,
                Ok(Err(e)) => eprintln!("Task failed with error: {:?}", e),
                Err(e) => eprintln!("Task panicked: {:?}", e),
            }
        }

        // At least most should succeed (allowing for some connection pool contention)
        assert!(
            successes >= 8,
            "Only {} out of 10 concurrent operations succeeded",
            successes
        );

        // Verify messages were inserted
        let messages = storage.list_messages(20, 0, None).await.unwrap();
        assert!(
            messages.len() >= 8,
            "Only {} messages were inserted",
            messages.len()
        );
    }

    #[tokio::test]
    async fn test_storage_error_handling() {
        let storage = create_in_memory_storage().await;

        // Test invalid transaction type (not Transfer or Payment)
        let invalid_msg = create_test_message(
            "invalid_001",
            "https://tap-protocol.io/messages/connect/1.0",
            json!({}),
        );

        let result = storage.insert_transaction(&invalid_msg).await;
        assert!(matches!(
            result,
            Err(StorageError::InvalidTransactionType(_))
        ));

        // Test retrieval of non-existent records
        let tx = storage.get_transaction_by_id("non_existent").await.unwrap();
        assert!(tx.is_none());

        let msg = storage.get_message_by_id("non_existent").await.unwrap();
        assert!(msg.is_none());
    }

    #[tokio::test]
    async fn test_message_json_integrity() {
        let storage = create_in_memory_storage().await;

        // Create a message with complex body
        let complex_body = json!({
            "nested": {
                "field": "value",
                "array": [1, 2, 3],
                "bool": true
            },
            "unicode": "Hello δΈ–η•Œ 🌍",
            "special_chars": "Line1\nLine2\tTab"
        });

        let msg = create_test_message(
            "json_test_001",
            "https://tap-protocol.io/messages/test/1.0",
            complex_body,
        );

        storage
            .log_message(&msg, MessageDirection::Incoming)
            .await
            .unwrap();

        // Retrieve and verify JSON integrity
        let retrieved = storage
            .get_message_by_id("json_test_001")
            .await
            .unwrap()
            .unwrap();
        let parsed: PlainMessage = serde_json::from_value(retrieved.message_json).unwrap();

        assert_eq!(parsed.body["nested"]["field"], "value");
        assert_eq!(parsed.body["nested"]["array"][1], 2);
        assert_eq!(parsed.body["unicode"], "Hello δΈ–η•Œ 🌍");
        assert_eq!(parsed.body["special_chars"], "Line1\nLine2\tTab");
    }
}