regit-web3 1.0.1

Modular Web3 primitives for exact values, chain and provider reads, swap quotes, and transaction preparation
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
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2026 Regit

use std::{collections::BTreeSet, fmt};

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha512};

use crate::error::{Error, ValidationError};

use super::{Address, Hash, Ledger};

fn invalid_record() -> Error {
    ValidationError::InvalidXrplRecord.into()
}

/// Bounded opaque protocol bytes encoded as canonical uppercase hexadecimal.
///
/// Encoding validation does not establish canonical XRPL binary structure or
/// validate transaction signatures. Metadata is retained without lossy parsing.
#[derive(Clone, Eq, PartialEq)]
pub struct HexData(Vec<u8>);
impl HexData {
    /// Maximum bytes retained for an individual transaction or metadata blob.
    pub const MAX_BYTES: usize = 1_048_576;
    /// Parses nonempty even-width hexadecimal without a prefix.
    ///
    /// # Errors
    /// Rejects invalid encoding, empty data and values above the byte bound.
    pub fn parse(text: &str) -> Result<Self, Error> {
        if text.is_empty()
            || text.len() > Self::MAX_BYTES * 2
            || !text.len().is_multiple_of(2)
            || !text.bytes().all(|byte| byte.is_ascii_hexdigit())
        {
            return Err(invalid_record());
        }
        const_hex::decode(text)
            .map(Self)
            .map_err(|_| invalid_record())
    }
    /// Returns exact opaque bytes, without binary or signature validation.
    #[must_use]
    pub fn bytes(&self) -> &[u8] {
        &self.0
    }
    /// Returns canonical uppercase hexadecimal.
    #[must_use]
    pub fn to_hex(&self) -> String {
        const_hex::encode_upper(&self.0)
    }
    /// Computes XRPL's transaction ID as SHA-512 first half of `TXN\0 || bytes`.
    ///
    /// This is not SHA-512/256, a signing digest or proof of signature validity.
    #[must_use]
    pub fn transaction_hash(&self) -> Hash {
        let mut digest = Sha512::new();
        digest.update(b"TXN\0");
        digest.update(&self.0);
        let full = digest.finalize();
        let mut bytes = [0; 32];
        bytes.copy_from_slice(&full[..32]);
        Hash::from_bytes(bytes)
    }
}
impl fmt::Debug for HexData {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("HexData")
            .field("byte_length", &self.0.len())
            .finish_non_exhaustive()
    }
}
impl Serialize for HexData {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.to_hex())
    }
}
impl<'de> Deserialize<'de> for HexData {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
    }
}

/// A bounded source-reported ledger execution result code.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(transparent)]
pub struct ResultCode(String);
impl ResultCode {
    /// Records `tesSUCCESS` or a claimed-cost `tec` result from applied metadata.
    ///
    /// # Errors
    /// Rejects malformed codes and non-ledger engine result classes.
    pub fn parse(code: &str) -> Result<Self, Error> {
        let valid = code == "tesSUCCESS"
            || code.strip_prefix("tec").is_some_and(|suffix| {
                !suffix.is_empty()
                    && suffix
                        .bytes()
                        .all(|b| b.is_ascii_uppercase() || b.is_ascii_digit() || b == b'_')
            });
        if !valid || code.len() > 64 {
            return Err(invalid_record());
        }
        Ok(Self(code.into()))
    }
    /// Returns the exact protocol result code.
    #[must_use]
    pub fn as_str(&self) -> &str {
        &self.0
    }
    /// Reports source-reported success independently of ledger validation.
    #[must_use]
    pub fn is_success(&self) -> bool {
        self.0 == "tesSUCCESS"
    }
}
impl<'de> Deserialize<'de> for ResultCode {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        Self::parse(&String::deserialize(d)?).map_err(serde::de::Error::custom)
    }
}

/// Source-reported transaction inclusion and execution facts, never inferred from absence.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "StatusFields")]
pub struct TransactionStatus {
    hash: Hash,
    ledger: Option<Ledger>,
    execution: Option<ResultCode>,
}
impl TransactionStatus {
    /// Records actual inclusion and optional applied execution metadata.
    ///
    /// # Errors
    /// Rejects validated inclusion without execution metadata and execution
    /// metadata without any reported inclusion. Validation does not imply success.
    pub fn new(
        hash: Hash,
        ledger: Option<Ledger>,
        execution: Option<ResultCode>,
    ) -> Result<Self, Error> {
        if ledger.is_some_and(Ledger::validated) && execution.is_none()
            || execution.is_some() && ledger.is_none()
        {
            return Err(invalid_record());
        }
        Ok(Self {
            hash,
            ledger,
            execution,
        })
    }
    /// Returns the exact queried transaction ID.
    #[must_use]
    pub const fn hash(&self) -> Hash {
        self.hash
    }
    /// Returns actual reported inclusion, if known.
    #[must_use]
    pub const fn ledger(&self) -> Option<Ledger> {
        self.ledger
    }
    /// Returns applied execution code, separately from validation.
    #[must_use]
    pub const fn execution(&self) -> Option<&ResultCode> {
        self.execution.as_ref()
    }
    /// Reports only the source's validation assessment.
    #[must_use]
    pub fn validated(&self) -> bool {
        self.ledger.is_some_and(Ledger::validated)
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct StatusFields {
    hash: Hash,
    #[serde(deserialize_with = "crate::domain::deserialize_optional")]
    ledger: Option<Ledger>,
    #[serde(deserialize_with = "crate::domain::deserialize_optional")]
    execution: Option<ResultCode>,
}
impl TryFrom<StatusFields> for TransactionStatus {
    type Error = Error;
    fn try_from(v: StatusFields) -> Result<Self, Error> {
        Self::new(v.hash, v.ledger, v.execution)
    }
}

/// An arbitrary transaction's exact opaque binary payload and reported inclusion.
///
/// The payload hash is recomputed, without claiming binary/signature validation.
/// Execution semantics remain in separately retrieved typed status or metadata.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "TransactionFields")]
pub struct Transaction {
    hash: Hash,
    payload: HexData,
    metadata: Option<HexData>,
    ledger: Option<Ledger>,
}
impl Transaction {
    /// Records exact payload bytes after recomputing their transaction ID.
    ///
    /// # Errors
    /// Rejects hash mismatch, missing metadata for validated inclusion and
    /// metadata without any reported ledger. No signatures are accepted as proven.
    pub fn new(
        hash: Hash,
        payload: HexData,
        metadata: Option<HexData>,
        ledger: Option<Ledger>,
    ) -> Result<Self, Error> {
        if payload.transaction_hash() != hash
            || ledger.is_some_and(Ledger::validated) && metadata.is_none()
            || metadata.is_some() && ledger.is_none()
        {
            return Err(invalid_record());
        }
        Ok(Self {
            hash,
            payload,
            metadata,
            ledger,
        })
    }
    /// Returns the recomputed transaction ID.
    #[must_use]
    pub const fn hash(&self) -> Hash {
        self.hash
    }
    /// Returns exact opaque transaction bytes.
    #[must_use]
    pub const fn payload(&self) -> &HexData {
        &self.payload
    }
    /// Returns actual opaque applied metadata, when reported.
    #[must_use]
    pub const fn metadata(&self) -> Option<&HexData> {
        self.metadata.as_ref()
    }
    /// Returns reported inclusion, independently of payload identity.
    #[must_use]
    pub const fn ledger(&self) -> Option<Ledger> {
        self.ledger
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct TransactionFields {
    hash: Hash,
    payload: HexData,
    #[serde(deserialize_with = "crate::domain::deserialize_optional")]
    metadata: Option<HexData>,
    #[serde(deserialize_with = "crate::domain::deserialize_optional")]
    ledger: Option<Ledger>,
}
impl TryFrom<TransactionFields> for Transaction {
    type Error = Error;
    fn try_from(v: TransactionFields) -> Result<Self, Error> {
        Self::new(v.hash, v.payload, v.metadata, v.ledger)
    }
}

/// An explicit positive inclusive ledger range used by account-history queries.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "RangeFields")]
pub struct LedgerRange {
    minimum: u32,
    maximum: u32,
}
impl LedgerRange {
    /// Records a bounded inclusive range without selecting a moving latest ledger.
    ///
    /// # Errors
    /// Rejects zero or descending endpoints.
    pub const fn new(minimum: u32, maximum: u32) -> Result<Self, Error> {
        if minimum == 0 || maximum < minimum {
            return Err(Error::Validation(ValidationError::InvalidPageRequest));
        }
        Ok(Self { minimum, maximum })
    }
    /// Returns the earliest requested or actually searched ledger.
    #[must_use]
    pub const fn minimum(self) -> u32 {
        self.minimum
    }
    /// Returns the latest requested or actually searched ledger.
    #[must_use]
    pub const fn maximum(self) -> u32 {
        self.maximum
    }
    fn contains(self, index: u32) -> bool {
        (self.minimum..=self.maximum).contains(&index)
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RangeFields {
    minimum: u32,
    maximum: u32,
}
impl TryFrom<RangeFields> for LedgerRange {
    type Error = Error;
    fn try_from(v: RangeFields) -> Result<Self, Error> {
        Self::new(v.minimum, v.maximum)
    }
}

/// The ledger/sequence continuation shape used by supported `account_tx` backends.
///
/// Markers are server-defined. This type does not imply universal marker support
/// or transaction ordering.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "CursorFields")]
pub struct HistoryMarker {
    ledger: u32,
    seq: u32,
}
impl HistoryMarker {
    /// Records a positive ledger and transaction sequence/index marker.
    ///
    /// # Errors
    /// Rejects a zero ledger. Sequence zero is valid.
    pub const fn new(ledger: u32, seq: u32) -> Result<Self, Error> {
        if ledger == 0 {
            return Err(Error::Validation(ValidationError::InvalidPageRequest));
        }
        Ok(Self { ledger, seq })
    }
    /// Returns the marker ledger.
    #[must_use]
    pub const fn ledger(self) -> u32 {
        self.ledger
    }
    /// Returns the marker's sequence/index without interpreting provider ordering.
    #[must_use]
    pub const fn sequence(self) -> u32 {
        self.seq
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct CursorFields {
    ledger: u32,
    seq: u32,
}
impl TryFrom<CursorFields> for HistoryMarker {
    type Error = Error;
    fn try_from(v: CursorFields) -> Result<Self, Error> {
        Self::new(v.ledger, v.seq)
    }
}

/// A bounded history query retaining exact range, direction and continuation.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "HistoryRequestFields")]
pub struct HistoryRequest {
    range: LedgerRange,
    limit: u16,
    forward: bool,
    marker: Option<HistoryMarker>,
}
impl HistoryRequest {
    /// Records explicit pagination; servers may search a smaller available range.
    ///
    /// # Errors
    /// Rejects limits outside 1..=400 or a marker outside the requested range.
    pub fn new(
        range: LedgerRange,
        limit: u16,
        forward: bool,
        marker: Option<HistoryMarker>,
    ) -> Result<Self, Error> {
        if !(1..=400).contains(&limit)
            || marker.is_some_and(|cursor| !range.contains(cursor.ledger()))
        {
            return Err(ValidationError::InvalidPageRequest.into());
        }
        Ok(Self {
            range,
            limit,
            forward,
            marker,
        })
    }
    /// Returns the exact requested search range.
    #[must_use]
    pub const fn range(&self) -> LedgerRange {
        self.range
    }
    /// Returns the hard local entry bound.
    #[must_use]
    pub const fn limit(&self) -> u16 {
        self.limit
    }
    /// Reports whether pages proceed oldest first; intra-page order is unspecified.
    #[must_use]
    pub const fn forward(&self) -> bool {
        self.forward
    }
    /// Returns the optional requested continuation.
    #[must_use]
    pub const fn marker(&self) -> Option<HistoryMarker> {
        self.marker
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HistoryRequestFields {
    range: LedgerRange,
    limit: u16,
    forward: bool,
    #[serde(deserialize_with = "crate::domain::deserialize_optional")]
    marker: Option<HistoryMarker>,
}
impl TryFrom<HistoryRequestFields> for HistoryRequest {
    type Error = Error;
    fn try_from(v: HistoryRequestFields) -> Result<Self, Error> {
        Self::new(v.range, v.limit, v.forward, v.marker)
    }
}

/// A bounded account-history page of hash-checked opaque transactions.
///
/// Searched range remains explicit and may be smaller than requested. No marker
/// means the source completed that searched range, never an exhaustive lifetime
/// history guarantee. Account involvement remains the indexer's reported fact.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "HistoryFields")]
pub struct HistoryPage {
    account: Address,
    request: HistoryRequest,
    searched: LedgerRange,
    transactions: Vec<Transaction>,
    next: Option<HistoryMarker>,
}
impl HistoryPage {
    /// Validates page identity uniqueness, actual inclusion and search-range bounds.
    ///
    /// # Errors
    /// Rejects excess entries, duplicate hashes, nonvalidated or out-of-range
    /// inclusion, a searched range extending beyond requested, and a nonadvancing
    /// or out-of-range continuation. Intra-page ordering is not invented.
    pub fn new(
        account: Address,
        request: HistoryRequest,
        searched: LedgerRange,
        transactions: Vec<Transaction>,
        next: Option<HistoryMarker>,
    ) -> Result<Self, Error> {
        let requested = request.range();
        let mut hashes = BTreeSet::new();
        if transactions.len() > usize::from(request.limit())
            || searched.minimum() < requested.minimum()
            || searched.maximum() > requested.maximum()
            || transactions.iter().any(|tx| {
                !hashes.insert(tx.hash())
                    || !tx.ledger().is_some_and(|ledger| {
                        ledger.validated() && searched.contains(ledger.index())
                    })
            })
            || next.is_some_and(|marker| {
                Some(marker) == request.marker() || !searched.contains(marker.ledger())
            })
        {
            return Err(invalid_record());
        }
        Ok(Self {
            account,
            request,
            searched,
            transactions,
            next,
        })
    }
    /// Returns the perspective account.
    #[must_use]
    pub const fn account(&self) -> Address {
        self.account
    }
    /// Returns the full requested range and pagination.
    #[must_use]
    pub const fn request(&self) -> &HistoryRequest {
        &self.request
    }
    /// Returns the provider's actually searched ledger range.
    #[must_use]
    pub const fn searched(&self) -> LedgerRange {
        self.searched
    }
    /// Returns actual hash-checked transactions with source-reported inclusion.
    #[must_use]
    pub fn transactions(&self) -> &[Transaction] {
        &self.transactions
    }
    /// Returns continuation within the searched range.
    #[must_use]
    pub const fn next_marker(&self) -> Option<HistoryMarker> {
        self.next
    }
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct HistoryFields {
    account: Address,
    request: HistoryRequest,
    searched: LedgerRange,
    transactions: Vec<Transaction>,
    #[serde(deserialize_with = "crate::domain::deserialize_optional")]
    next: Option<HistoryMarker>,
}
impl TryFrom<HistoryFields> for HistoryPage {
    type Error = Error;
    fn try_from(v: HistoryFields) -> Result<Self, Error> {
        Self::new(v.account, v.request, v.searched, v.transactions, v.next)
    }
}