tap-msg 0.7.0

Core message processing library for the Transaction Authorization Protocol
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
//! Composable Lock and Capture message types (TAIP-17).
//!
//! TAIP-17 was renamed from "Escrow" to "Lock" in the May 2026 spec advance to
//! Review. The body shape is unchanged: a Lock requests an agent to hold assets
//! on behalf of one party for the benefit of another, and Capture authorises
//! the release of those assets to the beneficiary. The role string `EscrowAgent`
//! is preserved by the spec and continues to identify the agent holding funds.
//!
//! `Escrow` is kept as a type alias for `Lock` to avoid breaking downstream
//! callers; on-the-wire messages bearing the legacy `#Escrow` type URI are
//! still accepted by the dispatcher in [`crate::message::tap_message_enum`].

use crate::error::{Error, Result};
use crate::message::agent::Agent;
use crate::message::party::Party;
use crate::message::tap_message_trait::TapMessageBody;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// Lock message for holding assets on behalf of parties (TAIP-17).
///
/// A Lock allows one agent to request another agent to hold a specified
/// amount of currency or asset from a party in escrow on behalf of another
/// party.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Lock {
    /// Cryptocurrency asset to be held in escrow (CAIP-19 identifier).
    /// Either `asset` OR `currency` MUST be present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub asset: Option<String>,

    /// ISO 4217 currency code (e.g. "USD", "EUR") for fiat-denominated locks.
    /// Either `asset` OR `currency` MUST be present.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,

    /// Amount to be held in escrow (decimal string).
    pub amount: String,

    /// Party whose assets will be placed in escrow.
    pub originator: Party,

    /// Party who will receive the assets when released.
    pub beneficiary: Party,

    /// Timestamp after which the lock automatically expires and funds are
    /// released back to the originator.
    pub expiry: String,

    /// URL or URI referencing the terms and conditions of the lock.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agreement: Option<String>,

    /// Agents involved in the lock. Exactly one agent MUST have role "EscrowAgent".
    pub agents: Vec<Agent>,

    /// Additional metadata.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, Value>,
}

/// Backward-compatible alias for the renamed `Lock` message.
pub type Escrow = Lock;

impl Lock {
    /// Create a new Lock for cryptocurrency assets.
    pub fn new_with_asset(
        asset: String,
        amount: String,
        originator: Party,
        beneficiary: Party,
        expiry: String,
        agents: Vec<Agent>,
    ) -> Self {
        Self {
            asset: Some(asset),
            currency: None,
            amount,
            originator,
            beneficiary,
            expiry,
            agreement: None,
            agents,
            metadata: HashMap::new(),
        }
    }

    /// Create a new Lock for fiat currency.
    pub fn new_with_currency(
        currency: String,
        amount: String,
        originator: Party,
        beneficiary: Party,
        expiry: String,
        agents: Vec<Agent>,
    ) -> Self {
        Self {
            asset: None,
            currency: Some(currency),
            amount,
            originator,
            beneficiary,
            expiry,
            agreement: None,
            agents,
            metadata: HashMap::new(),
        }
    }

    /// Set the agreement URL.
    pub fn with_agreement(mut self, agreement: String) -> Self {
        self.agreement = Some(agreement);
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: String, value: Value) -> Self {
        self.metadata.insert(key, value);
        self
    }

    /// Find the escrow agent (the agent with `role == "EscrowAgent"`) in the
    /// agents list.
    pub fn escrow_agent(&self) -> Option<&Agent> {
        self.agents
            .iter()
            .find(|a| a.role == Some("EscrowAgent".to_string()))
    }

    /// Find agents that can authorise release (agents acting `for` the beneficiary).
    pub fn authorizing_agents(&self) -> Vec<&Agent> {
        self.agents
            .iter()
            .filter(|a| a.for_parties.0.contains(&self.beneficiary.id))
            .collect()
    }
}

impl TapMessageBody for Lock {
    fn message_type() -> &'static str {
        "https://tap.rsvp/schema/1.0#Lock"
    }

    fn validate(&self) -> Result<()> {
        match (&self.asset, &self.currency) {
            (Some(_), Some(_)) => {
                return Err(Error::Validation(
                    "Lock cannot have both asset and currency specified".to_string(),
                ));
            }
            (None, None) => {
                return Err(Error::Validation(
                    "Lock must have either asset or currency specified".to_string(),
                ));
            }
            _ => {}
        }

        if self.amount.is_empty() {
            return Err(Error::Validation("Lock amount cannot be empty".to_string()));
        }

        if self.expiry.is_empty() {
            return Err(Error::Validation("Lock expiry cannot be empty".to_string()));
        }

        let escrow_agent_count = self
            .agents
            .iter()
            .filter(|a| a.role == Some("EscrowAgent".to_string()))
            .count();

        if escrow_agent_count == 0 {
            return Err(Error::Validation(
                "Lock must have exactly one agent with role 'EscrowAgent'".to_string(),
            ));
        }

        if escrow_agent_count > 1 {
            return Err(Error::Validation(
                "Lock cannot have more than one agent with role 'EscrowAgent'".to_string(),
            ));
        }

        if self.originator.id == self.beneficiary.id {
            return Err(Error::Validation(
                "Lock originator and beneficiary must be different parties".to_string(),
            ));
        }

        Ok(())
    }
}

/// Capture message for releasing locked funds (TAIP-17).
///
/// Capture authorises the release of locked funds to the beneficiary. It can
/// only be sent by agents acting for the beneficiary.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Capture {
    /// Amount to capture (decimal string). If omitted, captures the full
    /// locked amount. MUST be ≤ original amount.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub amount: Option<String>,

    /// Blockchain address for settlement. If omitted, uses the address from
    /// an earlier Authorize.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub settlement_address: Option<String>,

    /// Additional metadata.
    #[serde(default, skip_serializing_if = "HashMap::is_empty")]
    pub metadata: HashMap<String, Value>,
}

impl Capture {
    /// Create a new Capture for the full locked amount.
    pub fn new() -> Self {
        Self {
            amount: None,
            settlement_address: None,
            metadata: HashMap::new(),
        }
    }

    /// Create a new Capture for a partial amount.
    pub fn with_amount(amount: String) -> Self {
        Self {
            amount: Some(amount),
            settlement_address: None,
            metadata: HashMap::new(),
        }
    }

    /// Set the settlement address.
    pub fn with_settlement_address(mut self, address: String) -> Self {
        self.settlement_address = Some(address);
        self
    }

    /// Add metadata.
    pub fn with_metadata(mut self, key: String, value: Value) -> Self {
        self.metadata.insert(key, value);
        self
    }
}

impl Default for Capture {
    fn default() -> Self {
        Self::new()
    }
}

impl TapMessageBody for Capture {
    fn message_type() -> &'static str {
        "https://tap.rsvp/schema/1.0#Capture"
    }

    fn validate(&self) -> Result<()> {
        if let Some(ref amount) = self.amount {
            if amount.is_empty() {
                return Err(Error::Validation(
                    "Capture amount cannot be empty".to_string(),
                ));
            }
        }

        if let Some(ref address) = self.settlement_address {
            if address.is_empty() {
                return Err(Error::Validation(
                    "Capture settlement_address cannot be empty".to_string(),
                ));
            }
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_lock_with_asset() {
        let originator = Party::new("did:example:alice");
        let beneficiary = Party::new("did:example:bob");
        let agent1 = Agent::new(
            "did:example:alice-wallet",
            "OriginatorAgent",
            "did:example:alice",
        );
        let agent2 = Agent::new(
            "did:example:bob-wallet",
            "BeneficiaryAgent",
            "did:example:bob",
        );
        let escrow_agent = Agent::new(
            "did:example:escrow-service",
            "EscrowAgent",
            "did:example:escrow-service",
        );

        let lock = Lock::new_with_asset(
            "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string(),
            "100.00".to_string(),
            originator,
            beneficiary,
            "2025-06-25T00:00:00Z".to_string(),
            vec![agent1, agent2, escrow_agent],
        );

        assert!(lock.validate().is_ok());
        assert!(lock.escrow_agent().is_some());
        assert_eq!(
            lock.escrow_agent().unwrap().role,
            Some("EscrowAgent".to_string())
        );
    }

    #[test]
    fn test_lock_with_currency() {
        let originator = Party::new("did:example:buyer");
        let beneficiary = Party::new("did:example:seller");
        let escrow_agent = Agent::new(
            "did:example:escrow-bank",
            "EscrowAgent",
            "did:example:escrow-bank",
        );

        let lock = Lock::new_with_currency(
            "USD".to_string(),
            "500.00".to_string(),
            originator,
            beneficiary,
            "2025-07-01T00:00:00Z".to_string(),
            vec![escrow_agent],
        )
        .with_agreement("https://marketplace.example/purchase/98765".to_string());

        assert!(lock.validate().is_ok());
        assert_eq!(lock.currency, Some("USD".to_string()));
        assert_eq!(
            lock.agreement,
            Some("https://marketplace.example/purchase/98765".to_string())
        );
    }

    #[test]
    fn test_lock_validation_errors() {
        let originator = Party::new("did:example:alice");
        let beneficiary = Party::new("did:example:bob");

        let lock_no_agent = Lock::new_with_asset(
            "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string(),
            "100.00".to_string(),
            originator.clone(),
            beneficiary.clone(),
            "2025-06-25T00:00:00Z".to_string(),
            vec![],
        );
        assert!(lock_no_agent.validate().is_err());

        let mut lock_both = Lock::new_with_asset(
            "eip155:1/erc20:0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string(),
            "100.00".to_string(),
            originator.clone(),
            beneficiary.clone(),
            "2025-06-25T00:00:00Z".to_string(),
            vec![Agent::new(
                "did:example:escrow",
                "EscrowAgent",
                "did:example:escrow",
            )],
        );
        lock_both.currency = Some("USD".to_string());
        assert!(lock_both.validate().is_err());

        let lock_same_party = Lock::new_with_currency(
            "USD".to_string(),
            "100.00".to_string(),
            originator.clone(),
            originator.clone(),
            "2025-06-25T00:00:00Z".to_string(),
            vec![Agent::new(
                "did:example:escrow",
                "EscrowAgent",
                "did:example:escrow",
            )],
        );
        assert!(lock_same_party.validate().is_err());
    }

    #[test]
    fn test_capture() {
        let capture = Capture::new();
        assert!(capture.validate().is_ok());
        assert!(capture.amount.is_none());
        assert!(capture.settlement_address.is_none());

        let capture_with_amount = Capture::with_amount("95.00".to_string())
            .with_settlement_address(
                "eip155:1:0x742d35Cc6634C0532925a3b844Bc9e7595f1234".to_string(),
            );
        assert!(capture_with_amount.validate().is_ok());
        assert_eq!(capture_with_amount.amount, Some("95.00".to_string()));
        assert_eq!(
            capture_with_amount.settlement_address,
            Some("eip155:1:0x742d35Cc6634C0532925a3b844Bc9e7595f1234".to_string())
        );
    }

    #[test]
    fn test_capture_validation_errors() {
        let mut capture = Capture::new();
        capture.amount = Some("".to_string());
        assert!(capture.validate().is_err());

        let mut capture2 = Capture::new();
        capture2.settlement_address = Some("".to_string());
        assert!(capture2.validate().is_err());
    }
}