Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
145 changes: 145 additions & 0 deletions crates/core/src/xdr/codec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,103 @@ fn hex_decode(input: &str) -> Result<Vec<u8>, String> {
.collect()
}

/// Trait for convenient XDR encoding/decoding of Stellar XDR types.
///
/// This trait provides a unified interface for serializing and deserializing
/// Stellar XDR types to/from raw bytes and base64-encoded strings. It wraps
/// the underlying `stellar-xdr` crate's `ReadXdr` and `WriteXdr` traits with
/// error handling and base64 support.
///
/// # Examples
///
/// ```rust
/// use prism_core::xdr::codec::XdrCodec;
/// use stellar_xdr::LedgerEntry;
///
/// // Assuming you have a LedgerEntry instance
/// let entry: LedgerEntry = /* ... */;
///
/// // Serialize to bytes
/// let bytes = entry.to_xdr_bytes()?;
///
/// // Deserialize from bytes
/// let decoded = LedgerEntry::from_xdr_bytes(&bytes)?;
///
/// // Serialize to base64
/// let base64 = entry.to_xdr_base64()?;
///
/// // Deserialize from base64
/// let decoded = LedgerEntry::from_xdr_base64(&base64)?;
/// ```
pub trait XdrCodec: Sized {
/// Deserialize from XDR-encoded bytes.
///
/// # Arguments
/// * `bytes` - Raw XDR-encoded byte slice
///
/// # Returns
/// The deserialized instance
///
/// # Errors
/// Returns `PrismError::XdrError` if deserialization fails
fn from_xdr_bytes(bytes: &[u8]) -> PrismResult<Self>;

/// Serialize to XDR-encoded bytes.
///
/// # Returns
/// The XDR-encoded bytes
///
/// # Errors
/// Returns `PrismError::XdrError` if serialization fails
fn to_xdr_bytes(&self) -> PrismResult<Vec<u8>>;

/// Deserialize from base64-encoded XDR string.
///
/// # Arguments
/// * `xdr_base64` - Base64-encoded XDR string
///
/// # Returns
/// The deserialized instance
///
/// # Errors
/// Returns `PrismError::XdrError` if base64 decoding or XDR deserialization fails
fn from_xdr_base64(xdr_base64: &str) -> PrismResult<Self> {
let bytes = decode_xdr_base64(xdr_base64)?;
Self::from_xdr_bytes(&bytes)
}

/// Serialize to base64-encoded XDR string.
///
/// # Returns
/// The base64-encoded XDR string
///
/// # Errors
/// Returns `PrismError::XdrError` if XDR serialization or base64 encoding fails
fn to_xdr_base64(&self) -> PrismResult<String> {
Ok(encode_xdr_base64(&self.to_xdr_bytes()?))
}
}

/// Implementation of XdrCodec for stellar_xdr::LedgerEntry.
///
/// Provides reliable XDR serialization and deserialization for Stellar LedgerEntry
/// objects, which represent the state of accounts, trustlines, and contracts.
/// This is essential for State Archive handling and RPC responses.
///
/// The implementation uses the underlying stellar-xdr crate's ReadXdr and WriteXdr
/// traits with proper error mapping to PrismError.
impl XdrCodec for stellar_xdr::LedgerEntry {
fn from_xdr_bytes(bytes: &[u8]) -> PrismResult<Self> {
stellar_xdr::LedgerEntry::from_xdr(bytes, stellar_xdr::Limits::none())
.map_err(|e| PrismError::XdrError(format!("LedgerEntry decode failed: {e}")))
}

fn to_xdr_bytes(&self) -> PrismResult<Vec<u8>> {
stellar_xdr::WriteXdr::to_xdr(self, stellar_xdr::Limits::none())
.map_err(|e| PrismError::XdrError(format!("LedgerEntry encode failed: {e}")))
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -91,6 +188,54 @@ mod tests {
assert!(result.is_err());
}

#[test]
fn test_ledger_entry_xdr_codec_round_trip() {
let data_entry = stellar_xdr::DataEntry {
account_id: stellar_xdr::AccountId(stellar_xdr::PublicKey::PublicKeyTypeEd25519(
stellar_xdr::Uint256([1u8; 32]),
)),
data_name: stellar_xdr::String64::try_from(b"test-key".to_vec()).unwrap(),
data_value: stellar_xdr::DataValue::try_from(b"test-value".to_vec()).unwrap(),
ext: stellar_xdr::DataEntryExt::V0,
};

let entry = stellar_xdr::LedgerEntry {
last_modified_ledger_seq: 42,
data: stellar_xdr::LedgerEntryData::Data(data_entry),
ext: stellar_xdr::LedgerEntryExt::V0,
};

let encoded = entry.to_xdr_bytes().expect("Failed to encode LedgerEntry");
let decoded = stellar_xdr::LedgerEntry::from_xdr_bytes(&encoded)
.expect("Failed to decode LedgerEntry");

assert_eq!(entry, decoded);
}

#[test]
fn test_ledger_entry_xdr_codec_base64_round_trip() {
let data_entry = stellar_xdr::DataEntry {
account_id: stellar_xdr::AccountId(stellar_xdr::PublicKey::PublicKeyTypeEd25519(
stellar_xdr::Uint256([2u8; 32]),
)),
data_name: stellar_xdr::String64::try_from(b"test-key".to_vec()).unwrap(),
data_value: stellar_xdr::DataValue::try_from(b"test-value".to_vec()).unwrap(),
ext: stellar_xdr::DataEntryExt::V0,
};

let entry = stellar_xdr::LedgerEntry {
last_modified_ledger_seq: 43,
data: stellar_xdr::LedgerEntryData::Data(data_entry),
ext: stellar_xdr::LedgerEntryExt::V0,
};

let xdr_base64 = entry.to_xdr_base64().expect("Failed to encode ledger entry to base64");
let decoded = stellar_xdr::LedgerEntry::from_xdr_base64(&xdr_base64)
.expect("Failed to decode ledger entry from base64");

assert_eq!(entry, decoded);
}

#[test]
fn test_transaction_result_round_trip() {
// Create a simple TransactionResult with success code
Expand Down
1 change: 1 addition & 0 deletions vendor/adler2/.cargo-checksum.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"files":{".cargo_vcs_info.json":"08071a308400282ad18ca2de0df6d1f2dc270475f69aa483740ce311af3f63e0","CHANGELOG.md":"04fa29ec6eb6b05b706247ecac2cbc7075792dbfcea0bf52715782cf42132e94","Cargo.lock":"3e44ced212a9e7ddc0a5450bcebb48ec67f32a058529856458efa36415554e53","Cargo.toml":"8f30dbd092f3acc475b9d339736cd7b64c6489bc47cd234a7f2232fc52e2d490","Cargo.toml.orig":"077df9094ac86443a4d05305f74782bd237c1f15fa39640463e4c62e9e4a310a","LICENSE-0BSD":"861399f8c21c042b110517e76dc6b63a2b334276c8cf17412fc3c8908ca8dc17","LICENSE-APACHE":"8ada45cd9f843acf64e4722ae262c622a2b3b3007c7310ef36ac1061a30f6adb","LICENSE-MIT":"23f18e03dc49df91622fe2a76176497404e46ced8a715d9d2b67a7446571cca3","README.md":"cd955d5d6a49161e6f7a04df4a5963581b66ed43fd5096b2dedca8e295efe4f9","RELEASE_PROCESS.md":"a86cd10fc70f167f8d00e9e4ce0c6b4ebdfa1865058390dffd1e0ad4d3e68d9d","benches/bench.rs":"d67bef1c7f36ed300a8fbcf9d50b9dfdead1fd340bf87a4d47d99a0c1c042c04","src/algo.rs":"932c2bc591d13fe4470185125617b5aaa660a3898f23b553acc85df0bf49dded","src/lib.rs":"4acd41668fe30daffa37084e7e223f268957b816afc1864ffb3f5d6d7adf0890"},"package":"320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa"}
6 changes: 6 additions & 0 deletions vendor/adler2/.cargo_vcs_info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "89a031a0f42eeff31c70dc598b398cbf31f1680f"
},
"path_in_vcs": ""
}
84 changes: 84 additions & 0 deletions vendor/adler2/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Changelog

All notable changes to this project will be documented in this file.

---
## [2.0.1](https://github.com/Frommi/miniz_oxide/compare/2.0.0..2.0.1) - 2025-06-09

### Other

- Remove `compiler-builtins` from `rustc-dep-of-std` dependencies - ([7cdbd39](https://github.com/Frommi/miniz_oxide/commit/7cdbd3925a7f61cc075f44367b5d383861571b0a)) - Trevor Gross

---
## [2.0.0](https://github.com/Frommi/miniz_oxide/compare/1.0.2..2.0.0) - 2024-08-04

First release of adler2 - fork of adler crate as the original is unmaintained and archived

##### Changes since last version of Adler:

### Bug Fixes

- **(core)** change to rust 2021 edition, update repository info and links, update author info - ([867b115](https://github.com/Frommi/miniz_oxide/commit/867b115bad79bf62098f2acccc81bf53ec5a125d)) - oyvindln
- **(core)** simplify some code and fix benches - ([128fb9c](https://github.com/Frommi/miniz_oxide/commit/128fb9cb6cad5c3a54fb0b6c68549d80b79a1fe0)) - oyvindln

### Changelog of original adler crate

---

## [1.0.2 - 2021-02-26](https://github.com/jonas-schievink/adler/releases/tag/v1.0.2)

- Fix doctest on big-endian systems ([#9]).

[#9]: https://github.com/jonas-schievink/adler/pull/9

## [1.0.1 - 2020-11-08](https://github.com/jonas-schievink/adler/releases/tag/v1.0.1)

### Fixes

- Fix documentation on docs.rs.

## [1.0.0 - 2020-11-08](https://github.com/jonas-schievink/adler/releases/tag/v1.0.0)

### Fixes

- Fix `cargo test --no-default-features` ([#5]).

### Improvements

- Extended and clarified documentation.
- Added more rustdoc examples.
- Extended CI to test the crate with `--no-default-features`.

### Breaking Changes

- `adler32_reader` now takes its generic argument by value instead of as a `&mut`.
- Renamed `adler32_reader` to `adler32`.

## [0.2.3 - 2020-07-11](https://github.com/jonas-schievink/adler/releases/tag/v0.2.3)

- Process 4 Bytes at a time, improving performance by up to 50% ([#2]).

## [0.2.2 - 2020-06-27](https://github.com/jonas-schievink/adler/releases/tag/v0.2.2)

- Bump MSRV to 1.31.0.

## [0.2.1 - 2020-06-27](https://github.com/jonas-schievink/adler/releases/tag/v0.2.1)

- Add a few `#[inline]` annotations to small functions.
- Fix CI badge.
- Allow integration into libstd.

## [0.2.0 - 2020-06-27](https://github.com/jonas-schievink/adler/releases/tag/v0.2.0)

- Support `#![no_std]` when using `default-features = false`.
- Improve performance by around 7x.
- Support Rust 1.8.0.
- Improve API naming.

## [0.1.0 - 2020-06-26](https://github.com/jonas-schievink/adler/releases/tag/v0.1.0)

Initial release.


[#2]: https://github.com/jonas-schievink/adler/pull/2
[#5]: https://github.com/jonas-schievink/adler/pull/5
91 changes: 91 additions & 0 deletions vendor/adler2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.

[package]
edition = "2021"
name = "adler2"
version = "2.0.1"
authors = [
"Jonas Schievink <jonasschievink@gmail.com>",
"oyvindln <oyvindln@users.noreply.github.com>",
]
build = false
exclude = [".*"]
autolib = false
autobins = false
autoexamples = false
autotests = false
autobenches = false
description = "A simple clean-room implementation of the Adler-32 checksum"
documentation = "https://docs.rs/adler2/"
readme = "README.md"
keywords = [
"checksum",
"integrity",
"hash",
"adler32",
"zlib",
]
categories = ["algorithms"]
license = "0BSD OR MIT OR Apache-2.0"
repository = "https://github.com/oyvindln/adler2"

[package.metadata.docs.rs]
rustdoc-args = ["--cfg=docsrs"]

[package.metadata.release]
no-dev-version = true
pre-release-commit-message = "Release {{version}}"
tag-message = "{{version}}"

[[package.metadata.release.pre-release-replacements]]
file = "CHANGELOG.md"
replace = """
## Unreleased

No changes.

## [{{version}} - {{date}}](https://github.com/jonas-schievink/adler/releases/tag/v{{version}})
"""
search = """
## Unreleased
"""

[[package.metadata.release.pre-release-replacements]]
file = "README.md"
replace = 'adler = "{{version}}"'
search = 'adler = "[a-z0-9\\.-]+"'

[[package.metadata.release.pre-release-replacements]]
file = "src/lib.rs"
replace = "https://docs.rs/adler/{{version}}"
search = 'https://docs.rs/adler/[a-z0-9\.-]+'

[features]
default = ["std"]
rustc-dep-of-std = ["core"]
std = []

[lib]
name = "adler2"
path = "src/lib.rs"

[[bench]]
name = "bench"
path = "benches/bench.rs"
harness = false

[dependencies.core]
version = "1.0.0"
optional = true
package = "rustc-std-workspace-core"

[dev-dependencies]
Loading