r402-xrpl 0.17.1

XRPL chain support for the x402 payment 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
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
//! Wire format types for XRPL chain interactions.

use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;

use compact_str::CompactString;
use r402_core::chain::{ChainId, DeployedTokenAmount};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use xrpl::core::addresscodec::is_valid_classic_address;

/// The CAIP-2 namespace for XRPL chains.
pub const XRPL_NAMESPACE: &str = "xrpl";

/// Default JSON-RPC HTTP endpoint for XRPL mainnet.
pub const XRPL_MAINNET_RPC_URL: &str = "https://s1.ripple.com:51234";

/// Default JSON-RPC HTTP endpoint for XRPL testnet.
pub const XRPL_TESTNET_RPC_URL: &str = "https://s.altnet.rippletest.net:51234";

/// Default JSON-RPC HTTP endpoint for XRPL devnet.
pub const XRPL_DEVNET_RPC_URL: &str = "https://s.devnet.rippletest.net:51234";

/// An XRPL numeric `NetworkID` used as the CAIP-2 reference (`xrpl:{id}`).
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct XrplChainReference(u32);

impl XrplChainReference {
    /// XRPL mainnet (`xrpl:0`).
    pub const MAINNET: Self = Self(0);

    /// XRPL testnet (`xrpl:1`).
    pub const TESTNET: Self = Self(1);

    /// XRPL devnet (`xrpl:2`).
    pub const DEVNET: Self = Self(2);

    /// Built-in networks with default RPC URLs.
    pub const ALL: &'static [Self] = &[Self::MAINNET, Self::TESTNET, Self::DEVNET];

    /// Constructs a chain reference from a numeric XRPL `NetworkID`.
    #[must_use]
    pub const fn new(network_id: u32) -> Self {
        Self(network_id)
    }

    /// Returns the numeric XRPL `NetworkID`.
    #[must_use]
    pub const fn network_id(self) -> u32 {
        self.0
    }

    /// Returns `true` when XRPL protocol rules require omitting `NetworkID`.
    #[must_use]
    pub const fn is_standard_network(self) -> bool {
        self.0 <= 1024
    }

    /// Returns the CAIP-2 reference string.
    #[must_use]
    pub fn as_str(self) -> CompactString {
        CompactString::from(self.0.to_string())
    }

    /// Returns the default JSON-RPC URL for this network, if known.
    #[must_use]
    pub const fn default_rpc_url(self) -> Option<&'static str> {
        match self.0 {
            0 => Some(XRPL_MAINNET_RPC_URL),
            1 => Some(XRPL_TESTNET_RPC_URL),
            2 => Some(XRPL_DEVNET_RPC_URL),
            _ => None,
        }
    }
}

impl Debug for XrplChainReference {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "XrplChainReference({})", self.0)
    }
}

impl Display for XrplChainReference {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, f)
    }
}

impl FromStr for XrplChainReference {
    type Err = XrplChainReferenceFormatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let id: u32 = s
            .parse()
            .map_err(|_| XrplChainReferenceFormatError::InvalidReference(s.to_owned()))?;
        Ok(Self(id))
    }
}

impl Serialize for XrplChainReference {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.0.to_string())
    }
}

impl<'de> Deserialize<'de> for XrplChainReference {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

impl From<XrplChainReference> for ChainId {
    fn from(value: XrplChainReference) -> Self {
        Self::new(XRPL_NAMESPACE, value.0.to_string())
    }
}

impl TryFrom<ChainId> for XrplChainReference {
    type Error = XrplChainReferenceFormatError;

    fn try_from(value: ChainId) -> Result<Self, Self::Error> {
        let (namespace, reference) = value.into_parts();
        if namespace != XRPL_NAMESPACE {
            return Err(XrplChainReferenceFormatError::InvalidNamespace(namespace));
        }
        Self::from_str(&reference)
            .map_err(|_| XrplChainReferenceFormatError::InvalidReference(reference))
    }
}

/// Error type for parsing XRPL chain references.
#[derive(Debug, thiserror::Error)]
pub enum XrplChainReferenceFormatError {
    /// The namespace was not `"xrpl"`.
    #[error("Invalid namespace {0}, expected xrpl")]
    InvalidNamespace(String),
    /// The reference was not an unsigned 32-bit decimal `NetworkID`.
    #[error("Invalid xrpl chain reference {0}")]
    InvalidReference(String),
}

/// Returns `true` when `network` is an XRPL CAIP-2 identifier (`xrpl:{u32}`).
#[must_use]
pub fn is_xrpl_network(network: &str) -> bool {
    let Some((namespace, reference)) = network.split_once(':') else {
        return false;
    };
    namespace == XRPL_NAMESPACE && XrplChainReference::from_str(reference).is_ok()
}

/// Parses the numeric XRPL `NetworkID` from a CAIP-2 network string.
///
/// # Errors
///
/// Returns [`XrplChainReferenceFormatError`] when the identifier is not `xrpl:{u32}`.
pub fn parse_xrpl_network_id(network: &str) -> Result<u32, XrplChainReferenceFormatError> {
    let chain_id = ChainId::from_str(network)
        .map_err(|_| XrplChainReferenceFormatError::InvalidReference(network.to_owned()))?;
    Ok(XrplChainReference::try_from(chain_id)?.network_id())
}

/// An XRPL classic address (`r…`). X-addresses are rejected.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct XrplClassicAddress(CompactString);

impl XrplClassicAddress {
    /// Returns the address as a string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl Display for XrplClassicAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for XrplClassicAddress {
    type Err = XrplClassicAddressFormatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if is_valid_classic_address(s) {
            Ok(Self(CompactString::from(s)))
        } else {
            Err(XrplClassicAddressFormatError::Invalid(s.to_owned()))
        }
    }
}

impl Serialize for XrplClassicAddress {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for XrplClassicAddress {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

impl AsRef<str> for XrplClassicAddress {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

/// Errors that can occur when parsing an XRPL classic address.
#[derive(Debug, thiserror::Error)]
pub enum XrplClassicAddressFormatError {
    /// The string is not a valid XRPL classic address.
    #[error("invalid xrpl classic address: {0}")]
    Invalid(String),
}

/// Wire string used for both `payTo` (classic `r-address`) and `asset`
/// (`XRP`, 3-character currency, or 40-hex currency).
///
/// [`r402_core::wire::PaymentRequirements::as_concrete`] uses one address
/// type for both fields.
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct XrplAddress(CompactString);

impl XrplAddress {
    /// Returns the value as a string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Returns `true` when this value is native XRP.
    #[must_use]
    pub fn is_xrp(&self) -> bool {
        self.0 == "XRP"
    }
}

impl Display for XrplAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for XrplAddress {
    type Err = XrplAddressFormatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if is_valid_classic_address(s) || is_xrpl_asset_id(s) {
            Ok(Self(CompactString::from(s)))
        } else {
            Err(XrplAddressFormatError::Invalid(s.to_owned()))
        }
    }
}

impl Serialize for XrplAddress {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for XrplAddress {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

impl AsRef<str> for XrplAddress {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl From<XrplClassicAddress> for XrplAddress {
    fn from(value: XrplClassicAddress) -> Self {
        Self(value.0)
    }
}

/// Errors that can occur when parsing an XRPL pay-to or asset identifier.
#[derive(Debug, thiserror::Error)]
pub enum XrplAddressFormatError {
    /// The string is not a classic address or asset identifier.
    #[error("invalid xrpl address or asset: {0}")]
    Invalid(String),
}

fn is_xrpl_asset_id(s: &str) -> bool {
    if s == "XRP" {
        return true;
    }
    if s.len() == 3 && s.bytes().all(|b| b.is_ascii() && !b.is_ascii_control()) {
        return true;
    }
    s.len() == 40 && s.bytes().all(|b| b.is_ascii_hexdigit())
}

/// Payment amount: integer drops for XRP, or an issued-currency decimal string.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XrplTokenAmount(CompactString);

impl XrplTokenAmount {
    /// Returns the amount string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Returns `true` when the amount is an unsigned integer string.
    #[must_use]
    pub fn is_integer(&self) -> bool {
        is_integer_string(self.as_str())
    }
}

impl Display for XrplTokenAmount {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl FromStr for XrplTokenAmount {
    type Err = XrplTokenAmountFormatError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if is_decimal_string(s) {
            Ok(Self(CompactString::from(s)))
        } else {
            Err(XrplTokenAmountFormatError::Invalid(s.to_owned()))
        }
    }
}

impl Serialize for XrplTokenAmount {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

impl<'de> Deserialize<'de> for XrplTokenAmount {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

impl From<u64> for XrplTokenAmount {
    fn from(value: u64) -> Self {
        Self(CompactString::from(value.to_string()))
    }
}

impl From<u128> for XrplTokenAmount {
    fn from(value: u128) -> Self {
        Self(CompactString::from(value.to_string()))
    }
}

impl From<&str> for XrplTokenAmount {
    fn from(value: &str) -> Self {
        Self(CompactString::from(value))
    }
}

/// Error parsing an XRPL payment amount.
#[derive(Debug, thiserror::Error)]
pub enum XrplTokenAmountFormatError {
    /// The string is not an unsigned integer or decimal.
    #[error("invalid xrpl token amount: {0}")]
    Invalid(String),
}

/// Returns `true` when `value` is an unsigned base-10 integer string.
#[must_use]
pub fn is_integer_string(value: &str) -> bool {
    !value.is_empty() && value.bytes().all(|b| b.is_ascii_digit())
}

/// Returns `true` when `value` is a non-negative decimal string.
#[must_use]
pub fn is_decimal_string(value: &str) -> bool {
    let mut parts = value.split('.');
    let Some(whole) = parts.next() else {
        return false;
    };
    if whole.is_empty() || !whole.bytes().all(|b| b.is_ascii_digit()) {
        return false;
    }
    parts.next().is_none_or(|fraction| {
        !fraction.is_empty()
            && fraction.bytes().all(|b| b.is_ascii_digit())
            && parts.next().is_none()
    })
}

/// An issued currency identified by `(currency, issuer)`.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XrplIssuedCurrency {
    /// 3-character code or 40-hex currency.
    pub currency: CompactString,
    /// Classic address of the issuer.
    pub issuer: XrplClassicAddress,
}

/// Native XRP or an issued currency.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum XrplAsset {
    /// Native XRP; amount is integer drops.
    Xrp,
    /// Issued currency; amount is an XRPL decimal value string.
    Iou(XrplIssuedCurrency),
}

impl XrplAsset {
    /// Returns the `PaymentRequirements.asset` string.
    #[must_use]
    pub fn as_str(&self) -> &str {
        match self {
            Self::Xrp => "XRP",
            Self::Iou(iou) => iou.currency.as_str(),
        }
    }
}

/// Token deployment metadata for an XRPL asset on one network.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct XrplTokenDeployment {
    /// The XRPL network where this asset is used.
    pub chain_reference: XrplChainReference,
    /// Native XRP or an issued currency.
    pub asset: XrplAsset,
    /// Decimal places used when parsing human amounts for this asset.
    pub decimals: u8,
}

impl XrplTokenDeployment {
    /// Creates a new token deployment.
    #[must_use]
    pub const fn new(chain_reference: XrplChainReference, asset: XrplAsset, decimals: u8) -> Self {
        Self {
            chain_reference,
            asset,
            decimals,
        }
    }

    /// Creates a deployed token amount from drops (XRP) or a decimal string (IOU).
    #[must_use]
    pub fn amount(
        &self,
        v: impl Into<XrplTokenAmount>,
    ) -> DeployedTokenAmount<XrplTokenAmount, Self> {
        DeployedTokenAmount {
            amount: v.into(),
            token: self.clone(),
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, reason = "test assertions")]
mod tests {
    use super::*;

    #[test]
    fn classic_address_rejects_x_address_and_empty() {
        let valid = "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh";
        assert!(valid.parse::<XrplClassicAddress>().is_ok());
        assert!("".parse::<XrplClassicAddress>().is_err());
        assert!("not-an-address".parse::<XrplClassicAddress>().is_err());
        assert!(
            "X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ"
                .parse::<XrplClassicAddress>()
                .is_err()
        );
    }

    #[test]
    fn asset_ids_parse_as_address() {
        assert!("XRP".parse::<XrplAddress>().is_ok());
        assert!("USD".parse::<XrplAddress>().is_ok());
        assert!(
            "524C555344000000000000000000000000000000"
                .parse::<XrplAddress>()
                .is_ok()
        );
        assert!("not-an-asset".parse::<XrplAddress>().is_err());
    }

    #[test]
    fn token_amount_decimal_and_integer() {
        let drops: XrplTokenAmount = "1000000".parse().unwrap();
        assert!(drops.is_integer());
        let iou: XrplTokenAmount = "10.5".parse().unwrap();
        assert!(!iou.is_integer());
        assert!("".parse::<XrplTokenAmount>().is_err());
        assert!("10.5.5".parse::<XrplTokenAmount>().is_err());
        assert!("-1".parse::<XrplTokenAmount>().is_err());
    }

    #[test]
    fn chain_reference_roundtrip() {
        let chain_id: ChainId = XrplChainReference::TESTNET.into();
        assert_eq!(chain_id.to_string(), "xrpl:1");
        let back = XrplChainReference::try_from(chain_id).unwrap();
        assert_eq!(back, XrplChainReference::TESTNET);
        assert!(is_xrpl_network("xrpl:0"));
        assert!(is_xrpl_network("xrpl:2"));
        assert!(!is_xrpl_network("eip155:1"));
        assert!(!is_xrpl_network("xrpl"));
        assert!(XrplChainReference::MAINNET.is_standard_network());
        assert!(XrplChainReference::new(1024).is_standard_network());
        assert!(!XrplChainReference::new(1025).is_standard_network());
    }
}