extern crate tap_msg;
use std::collections::HashMap;
use tap_caip::AssetId;
use tap_msg::message::tap_message_trait::{create_tap_message, TapMessage, TapMessageBody};
use tap_msg::message::{Agent, Party, Transfer};
#[test]
fn test_to_didcomm_extracts_all_agents_when_no_sender() {
let asset = "eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7"
.parse::<AssetId>()
.unwrap();
let originator = Party::new("did:web:agent1.example");
let beneficiary = Party::new("did:web:agent2.example");
let agent3 = Agent::new(
"did:pkh:eip155:1:0x1234a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
"settlementAddress",
"did:pkh:eip155:1:0x1234a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
);
let body = Transfer {
transaction_id: Some(uuid::Uuid::new_v4().to_string()),
asset,
originator: Some(originator.clone()),
beneficiary: Some(beneficiary.clone()),
amount: "100.00".to_string(),
agents: vec![agent3.clone()],
settlement_id: None,
expiry: None,
transaction_value: None,
connection_id: None,
metadata: HashMap::new(),
memo: None,
};
let sender_did = "did:web:sender.example";
let message = body.to_didcomm(sender_did).unwrap();
assert_eq!(message.from, sender_did);
assert!(!message.to.is_empty());
assert_eq!(message.to.len(), 3);
assert!(message.to.contains(&originator.id));
assert!(message.to.contains(&beneficiary.id));
assert!(message.to.contains(&agent3.id));
}
#[test]
fn test_to_didcomm_filters_sender_when_specified() {
let asset = "eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7"
.parse::<AssetId>()
.unwrap();
let sender_did = "did:web:agent1.example";
let originator = Party::new(sender_did);
let beneficiary = Party::new("did:web:agent2.example");
let agent3 = Agent::new(
"did:pkh:eip155:1:0x1234a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
"settlementAddress",
"did:pkh:eip155:1:0x1234a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
);
let body = Transfer {
transaction_id: Some(uuid::Uuid::new_v4().to_string()),
asset,
originator: Some(originator.clone()),
beneficiary: Some(beneficiary.clone()),
amount: "100.00".to_string(),
agents: vec![agent3.clone()],
settlement_id: None,
expiry: None,
transaction_value: None,
connection_id: None,
metadata: HashMap::new(),
memo: None,
};
let message = body.to_didcomm(sender_did).unwrap();
assert_eq!(message.from, sender_did);
assert!(!message.to.contains(&sender_did.to_string()));
assert!(message.to.contains(&beneficiary.id));
assert!(message.to.contains(&agent3.id));
}
#[test]
fn test_to_didcomm_with_route() {
let asset = "eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7"
.parse::<AssetId>()
.unwrap();
let sender_did = "did:web:agent1.example";
let originator = Party::new(sender_did);
let beneficiary = Party::new("did:web:agent2.example");
let agent3 = Agent::new(
"did:pkh:eip155:1:0x1234a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
"settlementAddress",
"did:pkh:eip155:1:0x1234a96D359eC26a11e2C2b3d8f8B8942d5Bfcdb",
);
let body = Transfer {
transaction_id: Some(uuid::Uuid::new_v4().to_string()),
asset,
originator: Some(originator.clone()),
beneficiary: Some(beneficiary.clone()),
amount: "100.00".to_string(),
agents: vec![agent3.clone()],
settlement_id: None,
expiry: None,
transaction_value: None,
connection_id: None,
metadata: HashMap::new(),
memo: None,
};
let recipients = [beneficiary.id.as_str()];
let mut message = body.to_didcomm(sender_did).unwrap();
message.to = recipients.iter().map(|s| s.to_string()).collect();
assert_eq!(message.from, sender_did);
assert_eq!(message.to.len(), 1);
assert!(message.to.contains(&beneficiary.id));
assert!(!message.to.contains(&agent3.id));
}
#[test]
fn test_create_tap_message() {
let asset = "eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7"
.parse::<AssetId>()
.unwrap();
let sender_did = "did:web:agent1.example";
let originator = Party::new(sender_did);
let beneficiary = Party::new("did:web:agent2.example");
let body = Transfer {
transaction_id: Some(uuid::Uuid::new_v4().to_string()),
asset,
originator: Some(originator.clone()),
beneficiary: Some(beneficiary.clone()),
amount: "100.00".to_string(),
agents: vec![],
settlement_id: None,
expiry: None,
transaction_value: None,
connection_id: None,
metadata: HashMap::new(),
memo: None,
};
let message = create_tap_message(
&body,
Some("test-id-123".to_string()),
sender_did,
&[beneficiary.id.as_str()],
)
.unwrap();
assert_eq!(message.id, "test-id-123");
assert_eq!(message.from, sender_did);
assert_eq!(message.to.len(), 1);
assert!(message.to.contains(&beneficiary.id));
}
#[test]
fn test_get_all_participants() {
let asset = "eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7"
.parse::<AssetId>()
.unwrap();
let sender_did = "did:web:agent1.example";
let originator = Party::new(sender_did);
let beneficiary = Party::new("did:web:agent2.example");
let body = Transfer {
transaction_id: Some(uuid::Uuid::new_v4().to_string()),
asset,
originator: Some(originator.clone()),
beneficiary: Some(beneficiary.clone()),
amount: "100.00".to_string(),
agents: vec![],
settlement_id: None,
expiry: None,
transaction_value: None,
connection_id: None,
metadata: HashMap::new(),
memo: None,
};
let message = body.to_didcomm(sender_did).unwrap();
let participants = message.get_all_participants();
assert_eq!(participants.len(), 2); assert!(participants.contains(&sender_did.to_string()));
assert!(participants.contains(&beneficiary.id));
}