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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
use crate::agent_key_manager::{AgentKeyManager, AgentKeyManagerBuilder};
use crate::config::AgentConfig;
#[cfg(all(not(target_arch = "wasm32"), test))]
use crate::did::SyncDIDResolver; // Import SyncDIDResolver trait
use crate::error::{Error, Result};
use crate::key_manager::KeyManager; // Add KeyManager trait
#[cfg(not(target_arch = "wasm32"))]
use crate::message::SecurityMode;
#[cfg(not(target_arch = "wasm32"))]
use crate::message_packing::{PackOptions, Packable, UnpackOptions, Unpackable};
#[cfg(not(target_arch = "wasm32"))]
use async_trait::async_trait;
#[cfg(feature = "native")]
use reqwest::Client;
#[cfg(target_arch = "wasm32")]
use serde::de::DeserializeOwned;
#[cfg(not(target_arch = "wasm32"))]
use serde_json::Value;
use std::path::PathBuf;
use std::sync::Arc;
#[cfg(feature = "native")]
use std::time::Duration;
#[cfg(not(target_arch = "wasm32"))]
use tap_msg::didcomm::{PlainMessage, PlainMessageExt};
use tap_msg::TapMessageBody;
#[cfg(not(target_arch = "wasm32"))]
use tracing::{debug, error, info, warn};
/// Type alias for enhanced agent information: (DID, policies, metadata)
pub type EnhancedAgentInfo = (
String,
Vec<String>,
std::collections::HashMap<String, String>,
);
/// Result of a message delivery attempt
#[derive(Debug, Clone)]
pub struct DeliveryResult {
/// The DID of the recipient
pub did: String,
/// The service endpoint URL that was used for delivery
pub endpoint: String,
/// HTTP status code if the delivery was successful
pub status: Option<u16>,
/// Error message if the delivery failed
pub error: Option<String>,
}
/// The Agent trait defines the interface for all TAP agents
///
/// This trait supports both standalone agent usage and integration with TAP Node.
/// The different receive methods are designed for different usage patterns:
///
/// # Usage Patterns
///
/// ## Node Integration
/// - [`receive_encrypted_message`]: Called by TAP Node for encrypted messages
/// - [`receive_plain_message`]: Called by TAP Node for verified/decrypted messages
///
/// ## Standalone Usage
/// - [`receive_message`]: Handles any message type (plain, signed, encrypted)
///
/// ## Message Sending
/// - [`send_message`]: Sends messages to recipients with optional delivery
///
/// # Examples
///
/// ```rust,no_run
/// use tap_agent::{Agent, TapAgent};
/// use tap_msg::didcomm::PlainMessage;
///
/// async fn process_encrypted_message(agent: &TapAgent, jwe_json: &serde_json::Value) {
/// // This would typically be called by TAP Node
/// if let Err(e) = agent.receive_encrypted_message(jwe_json).await {
/// tracing::error!("Failed to process encrypted message: {}", e);
/// }
/// }
/// ```
#[cfg(not(target_arch = "wasm32"))]
#[async_trait]
pub trait Agent {
/// Gets the agent's DID
fn get_agent_did(&self) -> &str;
/// Gets the service endpoint URL for a recipient
///
/// This method resolves how to reach a given recipient, which could be:
/// - A direct URL if `to` is already a URL
/// - A DID resolution if `to` is a DID
async fn get_service_endpoint(&self, to: &str) -> Result<Option<String>>;
/// Sends a message to one or more recipients
///
/// # Parameters
/// - `message`: The message to send (must implement TapMessageBody)
/// - `to`: List of recipient DIDs or URLs
/// - `deliver`: Whether to actually deliver the message or just pack it
///
/// # Returns
/// - Packed message string
/// - Vector of delivery results (empty if deliver=false)
async fn send_message<
T: TapMessageBody + serde::Serialize + Send + Sync + std::fmt::Debug + 'static,
>(
&self,
message: &T,
to: Vec<&str>,
deliver: bool,
) -> Result<(String, Vec<DeliveryResult>)>;
/// Receives an encrypted message (decrypt and process)
///
/// This method is typically called by TAP Node when routing encrypted
/// messages to agents. The agent should:
/// 1. Parse the JWE from the JSON value
/// 2. Attempt to decrypt using its private keys
/// 3. Process the resulting PlainMessage
///
/// # Parameters
/// - `jwe_value`: JSON representation of the encrypted message (JWE)
async fn receive_encrypted_message(&self, jwe_value: &Value) -> Result<()>;
/// Receives a plain message (already verified/decrypted)
///
/// This method is called by TAP Node after signature verification
/// or by other agents after decryption. The message is ready for
/// business logic processing.
///
/// # Parameters
/// - `message`: The verified/decrypted PlainMessage
async fn receive_plain_message(&self, message: PlainMessage) -> Result<()>;
/// Receives a raw message (for standalone usage - handles any message type)
///
/// This method handles the complete message processing pipeline for
/// standalone agent usage. It can process:
/// - Plain messages (passed through)
/// - Signed messages (signature verified)
/// - Encrypted messages (decrypted)
///
/// # Parameters
/// - `raw_message`: JSON string of any message type
///
/// # Returns
/// - The processed PlainMessage
async fn receive_message(&self, raw_message: &str) -> Result<PlainMessage>;
/// Send a strongly-typed message
///
/// # Parameters
/// - `message`: The typed message to send
/// - `deliver`: Whether to actually deliver the message
///
/// # Returns
/// - Packed message string
/// - Vector of delivery results (empty if deliver=false)
async fn send_typed<T: TapMessageBody + Send + Sync + std::fmt::Debug + 'static>(
&self,
message: PlainMessage<T>,
deliver: bool,
) -> Result<(String, Vec<DeliveryResult>)> {
// Convert to plain message and use existing send infrastructure
let plain_message = message.to_plain_message()?;
let to_vec: Vec<&str> = plain_message.to.iter().map(|s| s.as_str()).collect();
// Extract the body and send using the existing method
let body = serde_json::from_value::<T>(plain_message.body)?;
self.send_message(&body, to_vec, deliver).await
}
/// Send a message with MessageContext support for automatic routing
///
/// This method uses MessageContext to automatically extract participants
/// and routing hints for improved message delivery.
///
/// # Parameters
/// - `message`: The message body that implements both TapMessageBody and MessageContext
/// - `deliver`: Whether to actually deliver the message
///
/// # Returns
/// - Packed message string
/// - Vector of delivery results (empty if deliver=false)
async fn send_with_context<T>(
&self,
message: &T,
deliver: bool,
) -> Result<(String, Vec<DeliveryResult>)>
where
T: TapMessageBody
+ tap_msg::message::MessageContext
+ Send
+ Sync
+ std::fmt::Debug
+ 'static,
{
// Extract participants using MessageContext
let participant_dids = message.participant_dids();
let recipients: Vec<&str> = participant_dids
.iter()
.map(|s| s.as_str())
.filter(|&did| did != self.get_agent_did()) // Don't send to self
.collect();
// Get routing hints for enhanced delivery
let _routing_hints = message.routing_hints();
// TODO: Use routing_hints to optimize delivery
// For now, just use the standard send_message method
self.send_message(message, recipients, deliver).await
}
/// Send a typed message with automatic context routing
///
/// # Parameters
/// - `message`: The typed message with MessageContext support
/// - `deliver`: Whether to actually deliver the message
///
/// # Returns
/// - Packed message string
/// - Vector of delivery results (empty if deliver=false)
async fn send_typed_with_context<T>(
&self,
message: PlainMessage<T>,
deliver: bool,
) -> Result<(String, Vec<DeliveryResult>)>
where
T: TapMessageBody
+ tap_msg::message::MessageContext
+ Send
+ Sync
+ std::fmt::Debug
+ 'static,
{
// Use the enhanced participant extraction
let participants = message.extract_participants_with_context();
let _recipients: Vec<&str> = participants
.iter()
.map(|s| s.as_str())
.filter(|&did| did != self.get_agent_did()) // Don't send to self
.collect();
// Get routing hints
let _routing_hints = message.routing_hints();
// Extract the body and send using the context-aware method
let body = message.body;
self.send_with_context(&body, deliver).await
}
/// Receive and parse a typed message
///
/// # Parameters
/// - `raw_message`: The raw message string
///
/// # Type Parameters
/// - `T`: The expected message body type
///
/// # Returns
/// - The typed message if parsing succeeds
async fn receive_typed<T: TapMessageBody>(&self, raw_message: &str) -> Result<PlainMessage<T>> {
let plain_message = self.receive_message(raw_message).await?;
plain_message
.parse_as()
.map_err(|e| Error::Serialization(e.to_string()))
}
/// Create an Out-of-Band invitation for any TAP message
///
/// # Parameters
/// - `message`: The message to include as a signed attachment
/// - `goal_code`: The goal code (e.g., "tap.payment", "tap.connect")
/// - `goal`: Human-readable goal description
/// - `service_url`: Base URL for the service
///
/// # Returns
/// - The Out-of-Band invitation URL
async fn create_oob_invitation<T: TapMessageBody + serde::Serialize + Send + Sync>(
&self,
message: &T,
goal_code: &str,
goal: &str,
service_url: &str,
) -> Result<String>;
/// Create a payment link from a Payment message
///
/// # Parameters
/// - `payment`: The payment message to create a link for
/// - `config`: Optional configuration (uses defaults if None)
///
/// # Returns
/// - The payment link URL
async fn create_payment_link(
&self,
payment: &tap_msg::message::Payment,
config: Option<crate::payment_link::PaymentLinkConfig>,
) -> Result<String>;
/// Parse an Out-of-Band invitation from a URL
///
/// # Parameters
/// - `url`: The OOB invitation URL
///
/// # Returns
/// - The parsed Out-of-Band invitation
fn parse_oob_invitation(&self, url: &str) -> Result<crate::oob::OutOfBandInvitation>;
/// Process an Out-of-Band invitation and extract the attached message
///
/// # Parameters
/// - `oob_invitation`: The Out-of-Band invitation to process
///
/// # Returns
/// - The extracted and verified PlainMessage if successful
async fn process_oob_invitation(
&self,
oob_invitation: &crate::oob::OutOfBandInvitation,
) -> Result<PlainMessage>;
}
/// A simplified Agent trait for WASM with relaxed bounds
#[cfg(target_arch = "wasm32")]
pub trait WasmAgent {
/// Gets the agent's DID
fn get_agent_did(&self) -> &str;
/// Pack a message for delivery
fn pack_message<T: TapMessageBody + serde::Serialize>(&self, message: &T) -> Result<String>;
/// Unpack a received message
fn unpack_message<T: TapMessageBody + DeserializeOwned>(
&self,
packed_message: &str,
) -> Result<T>;
}
/// TapAgent implementation using the AgentKeyManager for cryptographic operations.
#[derive(Debug, Clone)]
pub struct TapAgent {
/// Configuration for the agent
pub config: AgentConfig,
/// Key Manager for cryptographic operations
key_manager: Arc<AgentKeyManager>,
/// DID Resolver for resolving DIDs to service endpoints
#[cfg(all(not(target_arch = "wasm32"), test))]
resolver: Option<Arc<dyn SyncDIDResolver>>,
/// HTTP client for sending requests
#[cfg(all(feature = "native", not(target_arch = "wasm32")))]
http_client: Option<Client>,
}
impl TapAgent {
/// Returns a reference to the agent's key manager
pub fn key_manager(&self) -> &Arc<AgentKeyManager> {
&self.key_manager
}
/// Creates a new TapAgent with the given configuration and AgentKeyManager
pub fn new(config: AgentConfig, key_manager: Arc<AgentKeyManager>) -> Self {
#[cfg(all(feature = "native", not(target_arch = "wasm32")))]
{
let timeout = Duration::from_secs(config.timeout_seconds.unwrap_or(30));
let client = Client::builder().timeout(timeout).build().ok();
#[cfg(test)]
let agent = TapAgent {
config,
key_manager,
resolver: None,
http_client: client,
};
#[cfg(not(test))]
let agent = TapAgent {
config,
key_manager,
http_client: client,
};
agent
}
#[cfg(not(all(feature = "native", not(target_arch = "wasm32"))))]
{
#[cfg(all(not(target_arch = "wasm32"), test))]
let agent = TapAgent {
config,
key_manager,
resolver: None,
};
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
let agent = TapAgent {
config,
key_manager,
};
#[cfg(target_arch = "wasm32")]
let agent = TapAgent {
config,
key_manager,
};
agent
}
}
/// Creates a new TapAgent with the given configuration, key manager, and DID resolver
#[cfg(all(not(target_arch = "wasm32"), test))]
pub fn new_with_resolver(
config: AgentConfig,
key_manager: Arc<AgentKeyManager>,
resolver: Arc<dyn SyncDIDResolver>,
) -> Self {
#[cfg(feature = "native")]
{
let timeout = Duration::from_secs(config.timeout_seconds.unwrap_or(30));
let client = Client::builder().timeout(timeout).build().ok();
TapAgent {
config,
key_manager,
resolver: Some(resolver),
http_client: client,
}
}
#[cfg(not(feature = "native"))]
{
TapAgent {
config,
key_manager,
resolver: Some(resolver),
}
}
}
/// Creates a new TapAgent with an ephemeral key
///
/// This function generates a new DID key for temporary use.
/// The key is not persisted to storage and will be lost when the agent is dropped.
///
/// # Returns
///
/// A tuple containing the TapAgent and the DID that was generated
pub async fn from_ephemeral_key() -> crate::error::Result<(Self, String)> {
use crate::did::{DIDGenerationOptions, KeyType};
// Create a key manager
let key_manager = AgentKeyManager::new();
// Generate a key
let key = key_manager.generate_key(DIDGenerationOptions {
key_type: KeyType::Ed25519,
})?;
// Create a config with the new DID
let config = AgentConfig::new(key.did.clone()).with_debug(true);
// Create the agent
#[cfg(all(not(target_arch = "wasm32"), test))]
{
// Create a default resolver
let resolver = Arc::new(crate::did::MultiResolver::default());
let agent = Self::new_with_resolver(config, Arc::new(key_manager), resolver);
Ok((agent, key.did))
}
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
{
let agent = Self::new(config, Arc::new(key_manager));
Ok((agent, key.did))
}
#[cfg(target_arch = "wasm32")]
{
let agent = Self::new(config, Arc::new(key_manager));
Ok((agent, key.did))
}
}
/// Creates a new TapAgent from stored keys
///
/// This function uses the AgentKeyManagerBuilder to load keys from storage
///
/// # Arguments
///
/// * `did` - Optional DID to use. If None, the default DID from storage is used.
/// * `debug` - Whether to enable debug mode
///
/// # Returns
///
/// A Result containing either the created agent or an error if no keys are available
pub async fn from_stored_keys(did: Option<String>, debug: bool) -> Result<Self> {
use crate::storage::KeyStorage;
// Load keys from storage
let key_manager_builder = AgentKeyManagerBuilder::new().load_from_default_storage();
let key_manager = key_manager_builder.build()?;
// Get the DIDs available in the key manager
let dids = key_manager.list_keys()?;
if dids.is_empty() {
return Err(Error::Storage(
"No keys found in storage. Generate keys first with 'tap-agent-cli generate --save'".to_string(),
));
}
// Get the DID to use
let agent_did = if let Some(specified_did) = did {
if !dids.contains(&specified_did) {
return Err(Error::Storage(format!(
"Key with DID '{}' not found in storage",
specified_did
)));
}
specified_did
} else {
// Try to get the default DID from storage
let storage = KeyStorage::load_default()?;
storage.default_did.unwrap_or_else(|| dids[0].clone())
};
// Create agent config
let config = AgentConfig::new(agent_did).with_debug(debug);
// Create the agent
#[cfg(all(not(target_arch = "wasm32"), test))]
{
// Create a default resolver
let resolver = Arc::new(crate::did::MultiResolver::default());
Ok(TapAgent::new_with_resolver(
config,
Arc::new(key_manager),
resolver,
))
}
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
{
Ok(TapAgent::new(config, Arc::new(key_manager)))
}
#[cfg(target_arch = "wasm32")]
{
Ok(TapAgent::new(config, Arc::new(key_manager)))
}
}
/// Creates a new TapAgent from a secret helper
///
/// Invokes the secret helper to retrieve the private key for the given DID,
/// then creates a TapAgent using that key.
///
/// # Arguments
///
/// * `config` - The secret helper configuration
/// * `did` - The DID to fetch the key for
/// * `debug` - Whether to enable debug mode
#[cfg(not(target_arch = "wasm32"))]
pub async fn from_secret_helper(
config: &crate::secret_helper::SecretHelperConfig,
did: &str,
debug: bool,
) -> Result<(Self, String)> {
let (private_key, key_type) = config.get_key(did)?;
Self::from_private_key(&private_key, key_type, debug).await
}
/// Creates a new TapAgent from an existing private key
///
/// This function creates a new TapAgent using a provided private key,
/// which can be useful for integrating with external key management systems
/// or when keys are generated outside the TAP agent.
///
/// # Arguments
///
/// * `private_key` - The private key bytes
/// * `key_type` - The type of key (Ed25519, P256, or Secp256k1)
/// * `debug` - Whether to enable debug mode
///
/// # Returns
///
/// A Result containing either the created agent or an error
pub async fn from_private_key(
private_key: &[u8],
key_type: crate::did::KeyType,
debug: bool,
) -> Result<(Self, String)> {
use crate::did::{DIDKeyGenerator, GeneratedKey};
use crate::did::{VerificationMaterial, VerificationMethod, VerificationMethodType};
#[cfg(feature = "crypto-ed25519")]
use curve25519_dalek::edwards::CompressedEdwardsY;
use multibase::{encode, Base};
// Create a key manager to hold our key
let key_manager = AgentKeyManager::new();
// Generate the appropriate key and DID based on the key type
let generated_key = match key_type {
#[cfg(feature = "crypto-ed25519")]
crate::did::KeyType::Ed25519 => {
if private_key.len() != 32 {
return Err(Error::Validation(format!(
"Invalid Ed25519 private key length: {}, expected 32 bytes",
private_key.len()
)));
}
// For Ed25519, we need to derive the public key from the private key
let mut private_key_bytes = [0u8; 32];
private_key_bytes.copy_from_slice(&private_key[0..32]);
let signing_key = ed25519_dalek::SigningKey::from_bytes(&private_key_bytes);
// Get the public key
let verifying_key = ed25519_dalek::VerifyingKey::from(&signing_key);
let public_key = verifying_key.to_bytes().to_vec();
// Create did:key identifier
// Multicodec prefix for Ed25519: 0xed01
let mut prefixed_key = vec![0xed, 0x01];
prefixed_key.extend_from_slice(&public_key);
// Encode the key with multibase (base58btc with 'z' prefix)
let multibase_encoded = encode(Base::Base58Btc, &prefixed_key);
let did = format!("did:key:{}", multibase_encoded);
// Create the verification method ID
let vm_id = format!("{}#{}", did, multibase_encoded);
// Create the verification method
let verification_method = VerificationMethod {
id: vm_id.clone(),
type_: VerificationMethodType::Ed25519VerificationKey2018,
controller: did.clone(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: multibase_encoded.clone(),
},
};
// Create X25519 key for key agreement - Implement the ed25519_to_x25519 conversion directly
let x25519_method_and_agreement = {
// Only Ed25519 public keys must be exactly 32 bytes
if public_key.len() != 32 {
None
} else {
// Try to create a CompressedEdwardsY from the bytes
let edwards_y = match CompressedEdwardsY::from_slice(&public_key) {
Ok(point) => point,
Err(_) => {
return Err(Error::Cryptography(
"Failed to create Edwards point".to_string(),
))
}
};
// Try to decompress to get the Edwards point
let edwards_point = match edwards_y.decompress() {
Some(point) => point,
None => {
return Err(Error::Cryptography(
"Failed to decompress Edwards point".to_string(),
))
}
};
// Convert to Montgomery form
let montgomery_point = edwards_point.to_montgomery();
// Get the raw bytes representation of the X25519 key
let x25519_key = montgomery_point.to_bytes();
// Prefix for X25519: 0xEC01
let mut x25519_prefixed = vec![0xEC, 0x01];
x25519_prefixed.extend_from_slice(&x25519_key);
// Encode the prefixed X25519 key with multibase
let x25519_multibase = encode(Base::Base58Btc, &x25519_prefixed);
// Create the X25519 verification method ID
let x25519_vm_id = format!("{}#{}", did, x25519_multibase);
// Create the X25519 verification method
let x25519_verification_method = VerificationMethod {
id: x25519_vm_id.clone(),
type_: VerificationMethodType::X25519KeyAgreementKey2019,
controller: did.clone(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: x25519_multibase,
},
};
Some((x25519_verification_method, x25519_vm_id))
}
};
// Build verification methods array
let mut verification_methods = vec![verification_method.clone()];
let mut key_agreement = Vec::new();
if let Some((x25519_vm, x25519_id)) = x25519_method_and_agreement {
verification_methods.push(x25519_vm);
key_agreement.push(x25519_id);
}
// Create the DID document
let did_doc = crate::did::DIDDoc {
id: did.clone(),
verification_method: verification_methods,
authentication: vec![vm_id],
key_agreement,
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: Vec::new(),
};
// Create a GeneratedKey with all necessary fields
GeneratedKey {
key_type: crate::did::KeyType::Ed25519,
did: did.clone(),
public_key,
private_key: private_key.to_vec(),
did_doc,
}
}
#[cfg(feature = "crypto-p256")]
crate::did::KeyType::P256 => {
if private_key.len() != 32 {
return Err(Error::Validation(format!(
"Invalid P-256 private key length: {}, expected 32 bytes",
private_key.len()
)));
}
// For P-256, create a signing key from the private key
let signing_key = match p256::ecdsa::SigningKey::from_slice(private_key) {
Ok(key) => key,
Err(e) => {
return Err(Error::Cryptography(format!(
"Failed to create P-256 signing key: {:?}",
e
)))
}
};
// Get the public key in uncompressed form
let public_key = signing_key
.verifying_key()
.to_encoded_point(false)
.to_bytes()
.to_vec();
// Create did:key identifier
// Multicodec prefix for P-256: 0x1200
let mut prefixed_key = vec![0x12, 0x00];
prefixed_key.extend_from_slice(&public_key);
// Encode the key with multibase (base58btc with 'z' prefix)
let multibase_encoded = encode(Base::Base58Btc, &prefixed_key);
let did = format!("did:key:{}", multibase_encoded);
// Create the verification method ID
let vm_id = format!("{}#{}", did, multibase_encoded);
// Create the verification method
let verification_method = VerificationMethod {
id: vm_id.clone(),
type_: VerificationMethodType::EcdsaSecp256k1VerificationKey2019, // Using the available type
controller: did.clone(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: multibase_encoded.clone(),
},
};
// Create the DID document
let did_doc = crate::did::DIDDoc {
id: did.clone(),
verification_method: vec![verification_method],
authentication: vec![vm_id],
key_agreement: Vec::new(),
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: Vec::new(),
};
// Create a GeneratedKey with all necessary fields
GeneratedKey {
key_type: crate::did::KeyType::P256,
did: did.clone(),
public_key,
private_key: private_key.to_vec(),
did_doc,
}
}
#[cfg(feature = "crypto-secp256k1")]
crate::did::KeyType::Secp256k1 => {
if private_key.len() != 32 {
return Err(Error::Validation(format!(
"Invalid Secp256k1 private key length: {}, expected 32 bytes",
private_key.len()
)));
}
// For Secp256k1, create a signing key from the private key
let signing_key = match k256::ecdsa::SigningKey::from_slice(private_key) {
Ok(key) => key,
Err(e) => {
return Err(Error::Cryptography(format!(
"Failed to create Secp256k1 signing key: {:?}",
e
)))
}
};
// Get the public key in uncompressed form
let public_key = signing_key
.verifying_key()
.to_encoded_point(false)
.to_bytes()
.to_vec();
// Create did:key identifier
// Multicodec prefix for Secp256k1: 0xe701
let mut prefixed_key = vec![0xe7, 0x01];
prefixed_key.extend_from_slice(&public_key);
// Encode the key with multibase (base58btc with 'z' prefix)
let multibase_encoded = encode(Base::Base58Btc, &prefixed_key);
let did = format!("did:key:{}", multibase_encoded);
// Create the verification method ID
let vm_id = format!("{}#{}", did, multibase_encoded);
// Create the verification method
let verification_method = VerificationMethod {
id: vm_id.clone(),
type_: VerificationMethodType::EcdsaSecp256k1VerificationKey2019,
controller: did.clone(),
verification_material: VerificationMaterial::Multibase {
public_key_multibase: multibase_encoded.clone(),
},
};
// Create the DID document
let did_doc = crate::did::DIDDoc {
id: did.clone(),
verification_method: vec![verification_method],
authentication: vec![vm_id],
key_agreement: Vec::new(),
assertion_method: Vec::new(),
capability_invocation: Vec::new(),
capability_delegation: Vec::new(),
service: Vec::new(),
};
// Create a GeneratedKey with all necessary fields
GeneratedKey {
key_type: crate::did::KeyType::Secp256k1,
did: did.clone(),
public_key,
private_key: private_key.to_vec(),
did_doc,
}
}
};
// Create secret from the generated key and use it to add to the key manager
let did_generator = DIDKeyGenerator::new();
let _secret = did_generator.create_secret_from_key(&generated_key);
// Add the key to the key manager
key_manager.add_key(&generated_key)?;
// Create a config with the new DID
let config = AgentConfig::new(generated_key.did.clone()).with_debug(debug);
// Create the agent
#[cfg(all(not(target_arch = "wasm32"), test))]
{
// Create a default resolver
let resolver = Arc::new(crate::did::MultiResolver::default());
let agent = Self::new_with_resolver(config, Arc::new(key_manager), resolver);
Ok((agent, generated_key.did))
}
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
{
let agent = Self::new(config, Arc::new(key_manager));
Ok((agent, generated_key.did))
}
#[cfg(target_arch = "wasm32")]
{
let agent = Self::new(config, Arc::new(key_manager));
Ok((agent, generated_key.did))
}
}
/// Determine the appropriate security mode for a message type
///
/// This method implements TAP protocol rules for which security modes
/// should be used with different message types:
/// - Presentation messages use authenticated encryption (AuthCrypt)
/// - All other messages use digital signatures (Signed)
///
/// If security_mode is specified in the agent config, that takes precedence.
///
/// # Parameters
/// * `message_type` - The type of the message
///
/// # Returns
/// The appropriate SecurityMode for the message type
#[cfg(not(target_arch = "wasm32"))]
fn determine_security_mode<T: TapMessageBody>(&self) -> SecurityMode {
// If security mode is explicitly configured, use that
if let Some(ref mode) = self.config.security_mode {
match mode.to_uppercase().as_str() {
"AUTHCRYPT" => return SecurityMode::AuthCrypt,
"ANONCRYPT" => return SecurityMode::AnonCrypt,
_ => return SecurityMode::Signed,
}
}
// Otherwise use type-based rules
let message_type = T::message_type();
if message_type == crate::message::PRESENTATION_MESSAGE_TYPE {
SecurityMode::AuthCrypt
} else {
SecurityMode::Signed
}
}
/// Get the signing key ID for this agent
///
/// Resolves the DID document and returns the first authentication verification method ID
pub async fn get_signing_kid(&self) -> Result<String> {
let did = &self.config.agent_did;
// Try to get the DID document from our key manager first
if let Ok(agent_key) = self.key_manager.get_generated_key(did) {
// Get the first authentication method from the DID document
if let Some(auth_method_id) = agent_key.did_doc.authentication.first() {
return Ok(auth_method_id.clone());
}
// Fallback to first verification method
if let Some(vm) = agent_key.did_doc.verification_method.first() {
return Ok(vm.id.clone());
}
}
// Fallback to guessing based on DID method (for backward compatibility)
if did.starts_with("did:key:") {
let multibase = did.strip_prefix("did:key:").unwrap_or("");
Ok(format!("{}#{}", did, multibase))
} else if did.starts_with("did:web:") {
Ok(format!("{}#keys-1", did))
} else {
Ok(format!("{}#key-1", did))
}
}
/// Get the encryption key ID for a recipient
///
/// Resolves the DID document and returns the appropriate key agreement method ID
pub async fn get_encryption_kid(&self, recipient_did: &str) -> Result<String> {
if recipient_did == self.config.agent_did {
// If asking for our own encryption key, get it from our DID document
if let Ok(agent_key) = self.key_manager.get_generated_key(recipient_did) {
// Look for key agreement methods first
if let Some(agreement_method_id) = agent_key.did_doc.key_agreement.first() {
return Ok(agreement_method_id.clone());
}
// Fallback to authentication method (for keys that do both)
if let Some(auth_method_id) = agent_key.did_doc.authentication.first() {
return Ok(auth_method_id.clone());
}
// Fallback to first verification method
if let Some(vm) = agent_key.did_doc.verification_method.first() {
return Ok(vm.id.clone());
}
}
// Final fallback to signing key
return self.get_signing_kid().await;
}
// For external recipients, try to resolve their DID document
#[cfg(all(not(target_arch = "wasm32"), test))]
if let Some(resolver) = &self.resolver {
if let Ok(Some(did_doc)) = resolver.resolve(recipient_did).await {
// Look for key agreement methods first
if let Some(agreement_method_id) = did_doc.key_agreement.first() {
return Ok(agreement_method_id.clone());
}
// Fallback to authentication method
if let Some(auth_method_id) = did_doc.authentication.first() {
return Ok(auth_method_id.clone());
}
// Fallback to first verification method
if let Some(vm) = did_doc.verification_method.first() {
return Ok(vm.id.clone());
}
}
}
// Fallback to guessing based on DID method (for backward compatibility)
if recipient_did.starts_with("did:key:") {
let multibase = recipient_did.strip_prefix("did:key:").unwrap_or("");
Ok(format!("{}#{}", recipient_did, multibase))
} else if recipient_did.starts_with("did:web:") {
Ok(format!("{}#keys-1", recipient_did))
} else {
Ok(format!("{}#key-1", recipient_did))
}
}
/// Send a message to a specific endpoint
///
/// # Parameters
/// * `packed_message` - The packed message to send
/// * `endpoint` - The endpoint URL to send the message to
///
/// # Returns
/// The HTTP response status code, or error if the request failed
#[cfg(all(feature = "native", not(target_arch = "wasm32")))]
pub async fn send_to_endpoint(&self, packed_message: &str, endpoint: &str) -> Result<u16> {
// Get HTTP client
let client = self
.http_client
.as_ref()
.ok_or_else(|| Error::Networking("HTTP client not available".to_string()))?;
// Send the message to the endpoint via HTTP POST
let response = client
.post(endpoint)
.header("Content-Type", "application/didcomm-encrypted+json")
.body(packed_message.to_string())
.send()
.await
.map_err(|e| Error::Networking(format!("Failed to send message to endpoint: {}", e)))?;
// Get the status code
let status = response.status().as_u16();
// Log the response status
debug!("Message sent to endpoint {}, status: {}", endpoint, status);
Ok(status)
}
#[cfg(any(not(feature = "native"), target_arch = "wasm32"))]
pub async fn send_to_endpoint(&self, _packed_message: &str, _endpoint: &str) -> Result<u16> {
// Feature not enabled or WASM doesn't have http_client
Err(crate::error::Error::NotImplemented(
"HTTP client not available".to_string(),
))
}
/// Create an agent with enhanced configuration (policies and metadata)
pub async fn create_enhanced_agent(
agent_id: String,
policies: Vec<String>,
metadata: std::collections::HashMap<String, String>,
save_to_storage: bool,
) -> Result<(Self, String)> {
Self::create_enhanced_agent_with_path(agent_id, policies, metadata, save_to_storage, None)
.await
}
/// Create an agent with enhanced configuration (policies and metadata) with custom storage path
pub async fn create_enhanced_agent_with_path(
agent_id: String,
policies: Vec<String>,
metadata: std::collections::HashMap<String, String>,
save_to_storage: bool,
storage_path: Option<PathBuf>,
) -> Result<(Self, String)> {
use crate::did::{DIDGenerationOptions, KeyType};
use crate::storage::KeyStorage;
// Create a key manager and generate a key without saving to storage
let key_manager = AgentKeyManager::new();
let generated_key = key_manager.generate_key_without_save(DIDGenerationOptions {
key_type: KeyType::Ed25519,
})?;
// Create a config with the provided agent ID
let config = AgentConfig::new(agent_id.clone()).with_debug(true);
// Add the generated key to the key manager with the custom DID
// Use add_key_without_save to prevent automatic storage write
let mut custom_generated_key = generated_key.clone();
custom_generated_key.did = agent_id.clone();
key_manager.add_key_without_save(&custom_generated_key)?;
// Create the agent
#[cfg(all(not(target_arch = "wasm32"), test))]
let agent = {
let resolver = Arc::new(crate::did::MultiResolver::default());
Self::new_with_resolver(config, Arc::new(key_manager), resolver)
};
#[cfg(all(not(target_arch = "wasm32"), not(test)))]
let agent = Self::new(config, Arc::new(key_manager));
#[cfg(target_arch = "wasm32")]
let agent = Self::new(config, Arc::new(key_manager));
if save_to_storage {
// Save to key storage
let mut key_storage = if let Some(path) = &storage_path {
KeyStorage::load_from_path(path)?
} else {
KeyStorage::load_default()?
};
// Convert the generated key to a stored key
let mut stored_key = KeyStorage::from_generated_key(&custom_generated_key);
stored_key.label = format!(
"agent-{}",
agent_id.split(':').next_back().unwrap_or("agent")
);
key_storage.add_key(stored_key);
if let Some(path) = &storage_path {
key_storage.save_to_path(path)?;
} else {
key_storage.save_default()?;
}
// Create agent directory with policies and metadata
key_storage.create_agent_directory(&agent_id, &policies, &metadata)?;
}
Ok((agent, agent_id))
}
/// Load an enhanced agent from storage with policies and metadata
pub async fn load_enhanced_agent(
did: &str,
) -> Result<(Self, Vec<String>, std::collections::HashMap<String, String>)> {
use crate::storage::KeyStorage;
// Load key storage
let key_storage = KeyStorage::load_default()?;
// Check if the key exists in storage
let agent = if key_storage.keys.contains_key(did) {
// Load agent from stored keys
Self::from_stored_keys(Some(did.to_string()), true).await?
} else {
// If key doesn't exist in storage, create an ephemeral agent
// This is for test scenarios where agents are created but not persisted
let (mut agent, _) = Self::from_ephemeral_key().await?;
agent.config.agent_did = did.to_string();
agent
};
// Load policies and metadata from agent directory
let policies = key_storage.load_agent_policies(did).unwrap_or_default();
let metadata = key_storage.load_agent_metadata(did).unwrap_or_default();
Ok((agent, policies, metadata))
}
/// List all enhanced agents with their policies and metadata
pub fn list_enhanced_agents() -> Result<Vec<EnhancedAgentInfo>> {
Self::list_enhanced_agents_with_path(None)
}
/// List all enhanced agents with their policies and metadata with custom storage path
pub fn list_enhanced_agents_with_path(
storage_path: Option<PathBuf>,
) -> Result<Vec<EnhancedAgentInfo>> {
use crate::storage::KeyStorage;
use std::fs;
let key_storage = if let Some(path) = &storage_path {
KeyStorage::load_from_path(path)?
} else {
KeyStorage::load_default()?
};
let mut agents = Vec::new();
// Get TAP directory
let tap_dir = if let Some(path) = &storage_path {
// For custom paths, the tap directory is the parent of the keys.json file
path.parent()
.ok_or_else(|| Error::Storage("Invalid storage path".to_string()))?
.to_path_buf()
} else {
let home = dirs::home_dir()
.ok_or_else(|| Error::Storage("Could not determine home directory".to_string()))?;
home.join(crate::storage::DEFAULT_TAP_DIR)
};
if !tap_dir.exists() {
return Ok(agents);
}
// Scan for agent directories
for entry in fs::read_dir(&tap_dir)? {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
let dir_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
// Skip known non-agent directories
if dir_name == "keys.json" || dir_name.is_empty() {
continue;
}
// Convert sanitized DID back to original format
let did = dir_name.replace('_', ":");
// Try to load policies and metadata
let policies = key_storage.load_agent_policies(&did).unwrap_or_default();
let metadata = key_storage.load_agent_metadata(&did).unwrap_or_default();
// Only include if there are policies or metadata (indicating an enhanced agent)
if !policies.is_empty() || !metadata.is_empty() {
agents.push((did, policies, metadata));
}
}
}
Ok(agents)
}
}
#[async_trait]
#[cfg(not(target_arch = "wasm32"))]
impl crate::agent::Agent for TapAgent {
fn get_agent_did(&self) -> &str {
&self.config.agent_did
}
async fn get_service_endpoint(&self, to: &str) -> Result<Option<String>> {
// If it's a URL, return it directly
if to.starts_with("http://") || to.starts_with("https://") {
return Ok(Some(to.to_string()));
}
// If it's a DID, try to find a service endpoint using the resolver
if to.starts_with("did:") {
// Use the DID resolver from the AgentKeyManager to get the service endpoints
// For now, we'll use a simple approach that looks for DIDCommMessaging or Web service types
// For testing purposes, attempt to check if TapAgent has a resolver field
#[cfg(test)]
if let Some(resolver) = self.resolver.as_ref() {
if let Ok(Some(did_doc)) = resolver.resolve(to).await {
// Look for services of type DIDCommMessaging first
if let Some(service) = did_doc
.service
.iter()
.find(|s| s.type_ == "DIDCommMessaging")
{
return Ok(Some(service.service_endpoint.clone()));
}
// Then try Web type
if let Some(service) = did_doc.service.iter().find(|s| s.type_ == "Web") {
return Ok(Some(service.service_endpoint.clone()));
}
// No matching service found in DID doc
if !did_doc.service.is_empty() {
// Use the first service as fallback
return Ok(Some(did_doc.service[0].service_endpoint.clone()));
}
}
}
// Fallback to a placeholder URL if no resolver is available or no service found
return Ok(Some(format!(
"https://example.com/did/{}",
to.replace(":", "_")
)));
}
// No service endpoint found
Ok(None)
}
async fn send_message<
T: TapMessageBody + serde::Serialize + Send + Sync + std::fmt::Debug + 'static,
>(
&self,
message: &T,
to: Vec<&str>,
deliver: bool,
) -> Result<(String, Vec<DeliveryResult>)> {
if to.is_empty() {
return Err(Error::Validation("No recipients specified".to_string()));
}
// Log the plaintext message
debug!("\n==== SENDING TAP MESSAGE ====");
debug!("Message Type: {}", T::message_type());
debug!("Recipients: {:?}", to);
// Convert the TapMessageBody to a PlainMessage with explicit routing
let plain_message =
message.to_didcomm_with_route(self.get_agent_did(), to.iter().copied())?;
// Determine the appropriate security mode
let security_mode = self.determine_security_mode::<T>();
debug!("Security Mode: {:?}", security_mode);
// For each recipient, look up service endpoint before sending
for recipient in &to {
if let Ok(Some(endpoint)) = self.get_service_endpoint(recipient).await {
debug!("Found service endpoint for {}: {}", recipient, endpoint);
}
}
// Get the appropriate key IDs
let sender_kid = self.get_signing_kid().await?;
let recipient_kid = if to.len() == 1
&& (security_mode == SecurityMode::AuthCrypt
|| security_mode == SecurityMode::AnonCrypt)
{
Some(self.get_encryption_kid(to[0]).await?)
} else {
None
};
// Create pack options for the plaintext message
let pack_options = PackOptions {
security_mode,
sender_kid: if security_mode == SecurityMode::AnonCrypt {
None
} else {
Some(sender_kid)
},
recipient_kid,
};
// Pack the plain message using the Packable trait
let packed = plain_message.pack(&*self.key_manager, pack_options).await?;
// Log the packed message
debug!("--- PACKED MESSAGE ---");
debug!(
"{}",
serde_json::from_str::<serde_json::Value>(&packed)
.map(|v| serde_json::to_string_pretty(&v).unwrap_or(packed.clone()))
.unwrap_or(packed.clone())
);
debug!("=====================");
// If delivery is not requested, just return the packed message
if !deliver {
return Ok((packed, Vec::new()));
}
// Try to deliver the message to each recipient's service endpoint
let mut delivery_results = Vec::new();
for recipient in &to {
match self.get_service_endpoint(recipient).await {
Ok(Some(endpoint)) => {
debug!("Delivering message to {} at {}", recipient, endpoint);
// Extract message ID for logging
let message_id = match serde_json::from_str::<Value>(&packed) {
Ok(json) => json
.get("id")
.and_then(|id| id.as_str())
.map(String::from)
.unwrap_or_else(|| "unknown".to_string()),
Err(_) => "unknown".to_string(),
};
// Attempt to deliver the message
match self.send_to_endpoint(&packed, &endpoint).await {
Ok(status) => {
info!(
"✅ Delivered message {} to {} at {}",
message_id, recipient, endpoint
);
delivery_results.push(DeliveryResult {
did: recipient.to_string(),
endpoint: endpoint.clone(),
status: Some(status),
error: None,
});
}
Err(e) => {
// Log error but don't fail
let error_msg = format!(
"Failed to deliver message {} to {} at {}: {}",
message_id, recipient, endpoint, e
);
error!("❌ {}", error_msg);
delivery_results.push(DeliveryResult {
did: recipient.to_string(),
endpoint: endpoint.clone(),
status: None,
error: Some(error_msg),
});
}
}
}
Ok(None) => {
warn!(
"⚠️ No service endpoint found for {}, skipping delivery",
recipient
);
}
Err(e) => {
// Log error but don't fail
let error_msg = format!(
"Failed to resolve service endpoint for {}: {}",
recipient, e
);
error!("❌ {}", error_msg);
}
}
}
Ok((packed, delivery_results))
}
async fn receive_encrypted_message(&self, jwe_value: &Value) -> Result<()> {
// Log the received encrypted message
debug!("\n==== RECEIVING ENCRYPTED MESSAGE ====");
debug!("Agent DID: {}", self.get_agent_did());
// Parse as JWE
let jwe: crate::message::Jwe = serde_json::from_value(jwe_value.clone())
.map_err(|e| Error::Serialization(format!("Failed to parse JWE: {}", e)))?;
// Get our encryption key ID
let our_kid = self.get_signing_kid().await.ok();
// Create unpack options (accept both AuthCrypt and AnonCrypt)
let unpack_options = UnpackOptions {
expected_security_mode: SecurityMode::Any,
expected_recipient_kid: our_kid,
require_signature: false,
};
// Decrypt the message
let plain_message: PlainMessage<Value> =
crate::message::Jwe::unpack(&jwe, &*self.key_manager, unpack_options).await?;
debug!(
"Processed encrypted message: {} of type {}",
plain_message.id, plain_message.type_
);
Ok(())
}
async fn receive_plain_message(&self, message: PlainMessage) -> Result<()> {
// Process already verified/decrypted message
debug!("\n==== RECEIVING PLAIN MESSAGE ====");
debug!("Message ID: {}", message.id);
debug!("Message Type: {}", message.type_);
Ok(())
}
async fn receive_message(&self, raw_message: &str) -> Result<PlainMessage> {
// Log the received raw message
debug!("\n==== RECEIVING RAW MESSAGE ====");
debug!("Agent DID: {}", self.get_agent_did());
// First try to parse as JSON to determine message type
let json_value: Value = serde_json::from_str(raw_message)
.map_err(|e| Error::Serialization(format!("Failed to parse message as JSON: {}", e)))?;
// Check if it's an encrypted message (JWE) or signed message (JWS)
let is_encrypted =
json_value.get("protected").is_some() && json_value.get("recipients").is_some();
let is_signed = json_value.get("payload").is_some()
&& (json_value.get("signatures").is_some() || json_value.get("signature").is_some());
debug!(
"Message type detection: encrypted={}, signed={}",
is_encrypted, is_signed
);
if is_signed {
debug!("Detected signed message");
debug!("--- SIGNED MESSAGE ---");
debug!(
"{}",
serde_json::to_string_pretty(&json_value).unwrap_or(raw_message.to_string())
);
debug!("---------------------");
// Parse as JWS
let jws: crate::message::Jws = serde_json::from_value(json_value)
.map_err(|e| Error::Serialization(format!("Failed to parse JWS: {}", e)))?;
// Verify using our resolver
#[cfg(test)]
let plain_message = if let Some(resolver) = &self.resolver {
crate::verification::verify_jws(&jws, &**resolver).await?
} else {
// Fallback to unpacking with key manager for test compatibility
let unpack_options = UnpackOptions {
expected_security_mode: SecurityMode::Signed,
expected_recipient_kid: None,
require_signature: true,
};
crate::message::Jws::unpack(&jws, &*self.key_manager, unpack_options).await?
};
#[cfg(not(test))]
let plain_message = {
// In production, we need a resolver - for now use unpacking
let unpack_options = UnpackOptions {
expected_security_mode: SecurityMode::Signed,
expected_recipient_kid: None,
require_signature: true,
};
crate::message::Jws::unpack(&jws, &*self.key_manager, unpack_options).await?
};
// Log the unpacked message
debug!("--- UNPACKED CONTENT ---");
debug!(
"{}",
serde_json::to_string_pretty(&plain_message)
.unwrap_or_else(|_| format!("{:?}", plain_message))
);
debug!("------------------------");
Ok(plain_message)
} else if is_encrypted {
debug!("Detected encrypted message");
debug!("--- ENCRYPTED MESSAGE ---");
debug!(
"{}",
serde_json::to_string_pretty(&json_value).unwrap_or(raw_message.to_string())
);
debug!("---------------------");
// Get our encryption key ID
let our_kid = self.get_signing_kid().await.ok();
// Create unpack options (accept both AuthCrypt and AnonCrypt for encrypted messages)
let unpack_options = UnpackOptions {
expected_security_mode: SecurityMode::Any,
expected_recipient_kid: our_kid,
require_signature: false,
};
debug!("Unpacking with options: {:?}", unpack_options);
// Unpack the message
let plain_message: PlainMessage =
match String::unpack(&raw_message.to_string(), &*self.key_manager, unpack_options)
.await
{
Ok(msg) => msg,
Err(e) => {
error!("Failed to unpack message: {}", e);
return Err(e);
}
};
// Log the unpacked message
debug!("--- UNPACKED CONTENT ---");
debug!(
"{}",
serde_json::to_string_pretty(&plain_message)
.unwrap_or_else(|_| format!("{:?}", plain_message))
);
debug!("------------------------");
Ok(plain_message)
} else {
// It's already a plain message
debug!("Detected plain message");
debug!("--- PLAIN MESSAGE ---");
debug!(
"{}",
serde_json::to_string_pretty(&json_value).unwrap_or(raw_message.to_string())
);
debug!("---------------------");
// Parse directly as PlainMessage
serde_json::from_str::<PlainMessage>(raw_message)
.map_err(|e| Error::Serialization(format!("Failed to parse PlainMessage: {}", e)))
}
}
async fn create_oob_invitation<T: TapMessageBody + serde::Serialize + Send + Sync>(
&self,
message: &T,
goal_code: &str,
goal: &str,
service_url: &str,
) -> Result<String> {
// Create the DIDComm PlainMessage for the message
let plain_message = message.to_didcomm(self.get_agent_did())?;
// Serialize the plain message for signing (for debugging/logging purposes)
let _message_json = serde_json::to_string(&plain_message)
.map_err(|e| Error::Serialization(format!("Failed to serialize message: {}", e)))?;
// Sign the message using the pack method
let sender_kid = self.get_signing_kid().await?;
let pack_options = crate::message_packing::PackOptions::new().with_sign(&sender_kid);
let signed_message = plain_message.pack(&*self.key_manager, pack_options).await?;
// Create the OOB invitation
let oob_invitation =
crate::oob::OutOfBandInvitation::builder(self.get_agent_did(), goal_code, goal)
.add_signed_attachment("tap-message", &signed_message, Some("Signed TAP message"))
.build();
// Generate the URL
oob_invitation.to_url(service_url)
}
async fn create_payment_link(
&self,
payment: &tap_msg::message::Payment,
config: Option<crate::payment_link::PaymentLinkConfig>,
) -> Result<String> {
let config = config.unwrap_or_default();
// Create a signing function that uses our pack method
let key_manager = self.key_manager.clone();
let agent_did = self.get_agent_did().to_string();
let sign_fn = move |message_json: String| {
let key_manager = key_manager.clone();
let agent_did = agent_did.clone();
async move {
// Parse the message to get a PlainMessage
let plain_message: PlainMessage =
serde_json::from_str(&message_json).map_err(|e| {
Error::Serialization(format!("Failed to parse message for signing: {}", e))
})?;
// Create a temporary agent to get the signing key ID
let temp_config = AgentConfig::new(agent_did.clone());
let temp_agent = TapAgent::new(temp_config, key_manager.clone());
let sender_kid = temp_agent.get_signing_kid().await?;
let pack_options =
crate::message_packing::PackOptions::new().with_sign(&sender_kid);
plain_message.pack(&*key_manager, pack_options).await
}
};
// Use the payment link builder with our signing function
let payment_link =
crate::payment_link::PaymentLink::builder(self.get_agent_did(), payment.clone())
.with_config(config)
.build_with_signer(sign_fn)
.await?;
Ok(payment_link.url)
}
fn parse_oob_invitation(&self, url: &str) -> Result<crate::oob::OutOfBandInvitation> {
crate::oob::OutOfBandInvitation::from_url(url)
}
async fn process_oob_invitation(
&self,
oob_invitation: &crate::oob::OutOfBandInvitation,
) -> Result<PlainMessage> {
// Validate the invitation
oob_invitation.validate()?;
// Get the signed attachment
let attachment = oob_invitation.get_signed_attachment().ok_or_else(|| {
Error::Validation("No signed attachment found in OOB invitation".to_string())
})?;
// Extract the signed message JSON
let signed_message_json = match &attachment.data {
tap_msg::didcomm::AttachmentData::Json { value } => &value.json,
_ => {
return Err(Error::Validation(
"Attachment does not contain JSON data".to_string(),
))
}
};
// Convert to string for processing
let signed_message_str = serde_json::to_string(signed_message_json).map_err(|e| {
Error::Serialization(format!("Failed to serialize signed message: {}", e))
})?;
// Process the signed message using our existing receive_message method
self.receive_message(&signed_message_str).await
}
}