use std::collections::HashMap;
use std::result::Result;
use std::str::FromStr;
use tap_caip::AssetId;
use tap_msg::didcomm::PlainMessage;
use tap_msg::message::{Agent, Party, TapMessageBody, Transfer};
#[tokio::test]
async fn test_tap_didcomm_round_trip() -> Result<(), Box<dyn std::error::Error>> {
let asset = AssetId::from_str("eip155:1/erc20:0xdac17f958d2ee523a2206206994597c13d831ec7")?;
let from_did = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
let to_did = "did:key:z6MkwYyuTCaaDKnMGHpMkteuFpj1KrsFgGXwW3nXdT7k3RQP";
let originator = Party::new(from_did);
let beneficiary = Party::new(to_did);
let body = Transfer {
transaction_id: Some(uuid::Uuid::new_v4().to_string()),
asset: asset.clone(),
originator: Some(originator.clone()),
beneficiary: Some(beneficiary.clone()),
amount: "100.00".to_string(),
agents: vec![
Agent::new(from_did, "originator", from_did),
Agent::new(to_did, "beneficiary", to_did),
],
settlement_id: None,
expiry: None,
transaction_value: None,
connection_id: None,
metadata: HashMap::new(),
memo: None,
};
let to_dids = [to_did];
let didcomm_message = body.to_didcomm_with_route(from_did, to_dids.iter().copied())?;
let packed_message = serde_json::to_string(&didcomm_message)?;
let unpacked_message: PlainMessage = serde_json::from_str(&packed_message)?;
assert_eq!(unpacked_message.from, from_did.to_string());
assert_eq!(unpacked_message.to, vec![to_did.to_string()]);
assert_eq!(unpacked_message.type_, Transfer::message_type());
let unpacked_body = Transfer::from_didcomm(&unpacked_message)?;
assert_eq!(unpacked_body.asset.to_string(), asset.to_string());
assert_eq!(unpacked_body.originator.unwrap().id, from_did);
assert_eq!(unpacked_body.beneficiary.as_ref().unwrap().id, to_did);
assert_eq!(unpacked_body.amount, "100.00");
Ok(())
}