tap_msg/examples/
didcomm_message_example.rs

1//! Example usage of the new DIDComm Message approach for TAP messages.
2
3use crate::error::Result;
4use crate::message::{Authorize, Participant, Reject, Settle, TapMessageBody, Transfer};
5use didcomm::Message;
6use std::collections::HashMap;
7use std::str::FromStr;
8use tap_caip::AssetId;
9
10/// Example function to create a Transfer message using the new approach.
11pub fn create_transfer_message_example() -> Result<Message> {
12    // Create originator and beneficiary participants
13    let originator = Participant {
14        id: "did:example:alice".to_string(),
15        role: Some("originator".to_string()),
16        policies: None,
17        leiCode: None,
18    };
19
20    let beneficiary = Participant {
21        id: "did:example:bob".to_string(),
22        role: Some("beneficiary".to_string()),
23        policies: None,
24        leiCode: None,
25    };
26
27    // Create a transfer body
28    let transfer_body = Transfer {
29        asset: AssetId::from_str("eip155:1/erc20:0x123456789abcdef").unwrap(),
30        originator,
31        beneficiary: Some(beneficiary),
32        amount: "10.00".to_string(),
33        memo: None,
34        agents: vec![],
35        settlement_id: None,
36        transaction_id: uuid::Uuid::new_v4().to_string(),
37        metadata: HashMap::new(),
38    };
39
40    // Convert the Transfer body to a DIDComm message
41    let message = transfer_body.to_didcomm(Some(
42        "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
43    ))?;
44
45    // The message is ready to be encrypted and sent
46    Ok(message)
47}
48
49/// Example function to process a received Transfer message.
50pub fn process_transfer_message_example(message: &Message) -> Result<()> {
51    // First, check if this is a TAP message
52    if message.type_.contains("transfer") {
53        println!(
54            "Received message is a TAP message of type: {}",
55            message.type_
56        );
57
58        // Extract the transfer body
59        let transfer = Transfer::from_didcomm(message)?;
60
61        // Now we can work with the transfer data
62        println!("Transfer amount: {}", transfer.amount);
63        println!("Asset: {:?}", transfer.asset);
64
65        if let Some(ref beneficiary) = transfer.beneficiary {
66            println!("Beneficiary: {}", beneficiary.id);
67        }
68
69        println!("Originator: {}", transfer.originator.id);
70    } else {
71        println!("Not a transfer message");
72    }
73
74    Ok(())
75}
76
77/// Example function to create a Reject message.
78pub fn create_reject_message_example(transaction_id: &str) -> Result<Message> {
79    let reject_body = Reject {
80        transaction_id: transaction_id.to_string(),
81        reason: "COMPLIANCE_FAILURE: Unable to comply with transfer requirements. Further documentation needed.".to_string(),
82    };
83
84    // Convert the Reject body to a DIDComm message
85    let message = reject_body.to_didcomm(Some(
86        "did:key:z6MkmRsjkKHNrBiVz5mhiqhJVYf9E9mxg3MVGqgqMkRwCJd6",
87    ))?;
88
89    // The message is ready to be encrypted and sent
90    Ok(message)
91}
92
93/// Example function to create a Settle message.
94pub fn create_settle_message_example(transaction_id: &str) -> Result<Message> {
95    let settle_body = Settle {
96        transaction_id: transaction_id.to_string(),
97        settlement_id: "0x123456789abcdef".to_string(),
98        amount: Some("100.0".to_string()),
99    };
100
101    // Convert the Settle body to a DIDComm message
102    let message = settle_body.to_didcomm(Some(
103        "did:key:z6MkmRsjkKHNrBiVz5mhiqhJVYf9E9mxg3MVGqgqMkRwCJd6",
104    ))?;
105
106    // The message is ready to be encrypted and sent
107    Ok(message)
108}
109
110/// This example shows how to use the common interface to work with various TAP message types.
111pub fn process_any_tap_message_example(message: &Message) -> Result<()> {
112    // Get the message type
113    let type_str = &message.type_;
114
115    match () {
116        _ if type_str.contains("transfer") => {
117            // Handle Transfer message
118            let transfer = Transfer::from_didcomm(message)?;
119            println!("Processing Transfer: {}", transfer.amount);
120        }
121        _ if type_str.contains("authorize") => {
122            // Handle Authorize message
123            let authorize = Authorize::from_didcomm(message)?;
124            println!(
125                "Processing Authorization for transfer: {}",
126                authorize.transaction_id
127            );
128        }
129        _ if type_str.contains("reject") => {
130            // Handle Reject message
131            let reject = Reject::from_didcomm(message)?;
132            println!(
133                "Processing Rejection for transfer: {}",
134                reject.transaction_id
135            );
136            println!("Reason: {}", reject.reason);
137        }
138        _ if type_str.contains("settle") => {
139            // Handle Settle message
140            let settle = Settle::from_didcomm(message)?;
141            println!(
142                "Processing Settlement for transfer: {}",
143                settle.transaction_id
144            );
145            println!("Settlement ID: {}", settle.settlement_id);
146            println!("Amount: {}", settle.amount.unwrap_or_default());
147        }
148        _ => {
149            println!("Unknown message type");
150        }
151    }
152
153    Ok(())
154}