Skip to content

Commit f7ef256

Browse files
committed
feat: Replaced generated code with stellar-xdr
Removed the dependencies xdr-rs-serialize and xdr-rs-serialize-derive, since serialization is already provided by stellar-xdr. For compatibility reasons and convenience, the existing XDRSerialize and XDRDeserialize traits are now implemented on top of the WriteXdr and ReadXdr traits from stellar-xdr. Added a bunch of new data types and enum values. Removed enum types where they added nothing beyond the same type in stellar-xdr. No support was added for Preconditions::V2 and TransactionExt::V1, both part of the new stellar_xdr_curr::Transaction type. When trying to convert transactions which use these values an UnsupportedFeature error is returned for now. Also, not all new types have been wrapped. For example, the inner data of the new LedgerKey values instead refers directly to the types from the stellar-xdr crate. This can be changed when there is demand, but it seems futile to wrap all these types in verbose API and to keep this updated. Closes #10 Signed-off-by: Thorbjørn Lindeijer <bjorn@lindeijer.nl>
1 parent 98f7202 commit f7ef256

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1415
-8340
lines changed

Cargo.lock

Lines changed: 70 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ serde = { version = "1.0", features = ["derive"] }
2727
serde_json = "1.0"
2828
sha2 = "0.10.1"
2929
thiserror = "2.0.12"
30-
xdr-rs-serialize = "0.3.0"
31-
xdr-rs-serialize-derive = "0.3.0"
30+
stellar-xdr = "22.2.0"
3231

3332
[dependencies.ed25519-dalek]
3433
version = "2.1.1"

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,17 @@ You can join the discussion on our mailing list at
2424

2525
## Features
2626

27-
* Working XDR definitions for all Stellar types.
28-
* Seamlessy convert monetary amounts between decimal representation
29-
and stroops.
27+
* Wraps generated XDR definitions from `stellar-xdr` in a nicer API.
28+
29+
* Seamlessy convert monetary amounts between decimal representation
30+
and stroops.
3031

3132

3233
## Documentation
3334

3435
You can find the documentation on [docs.rs](https://docs.rs/stellar-base).
3536

3637

37-
## Generating XDR types
38-
39-
You can generated XDR types using [our fork of `xdrgen`](https://github.com/aurora-rs/xdrgen).
40-
4138
## Changelog
4239

4340
[You can find a changelog here.](https://github.com/aurora-rs/stellar-base-rs/blob/master/CHANGELOG.md)

src/account.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ bitflags! {
66
/// Account flags.
77
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
88
pub struct AccountFlags: u32 {
9-
const AUTH_REQUIRED = xdr::AccountFlags::AuthRequiredFlag as u32;
10-
const AUTH_REVOCABLE = xdr::AccountFlags::AuthRevocableFlag as u32;
11-
const AUTH_IMMUTABLE = xdr::AccountFlags::AuthImmutableFlag as u32;
12-
const AUTH_CLAWBACK_ENABLED = xdr::AccountFlags::AuthClawbackEnabledFlag as u32;
9+
const AUTH_REQUIRED = xdr::AccountFlags::RequiredFlag as u32;
10+
const AUTH_REVOCABLE = xdr::AccountFlags::RevocableFlag as u32;
11+
const AUTH_IMMUTABLE = xdr::AccountFlags::ImmutableFlag as u32;
12+
const AUTH_CLAWBACK_ENABLED = xdr::AccountFlags::ClawbackEnabledFlag as u32;
1313
}
1414
}
1515

@@ -53,12 +53,14 @@ impl DataValue {
5353

5454
/// Returns the DataValue xdr object.
5555
pub fn to_xdr(&self) -> Result<xdr::DataValue> {
56-
let inner = self.as_bytes().to_vec();
57-
Ok(xdr::DataValue::new(inner))
56+
let inner = self.as_bytes();
57+
Ok(xdr::DataValue(
58+
inner.try_into().map_err(|_| Error::XdrError)?,
59+
))
5860
}
5961

6062
/// Creates a DataValue from xdr object.
6163
pub fn from_xdr(x: &xdr::DataValue) -> Result<DataValue> {
62-
DataValue::from_slice(&x.value)
64+
DataValue::from_slice(&x)
6365
}
6466
}

src/amount.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
//! Represent monetary values and prices.
22
use crate::error::{Error, Result};
33
use crate::xdr;
4-
use crate::xdr::{XDRDeserialize, XDRSerialize};
54
use num_rational::Ratio;
65
use num_traits::cast::ToPrimitive;
76
use rust_decimal::Decimal;
87
use std::convert::TryFrom;
98
use std::fmt;
9+
use std::io::{Read, Write};
1010
use std::str::FromStr;
11-
use xdr_rs_serialize::de::XDRIn;
12-
use xdr_rs_serialize::ser::XDROut;
1311

1412
const STELLAR_SCALE: u32 = 7;
1513

@@ -140,26 +138,26 @@ impl Stroops {
140138

141139
/// Returns stroops amount as xdr object.
142140
pub fn to_xdr_int64(&self) -> Result<xdr::Int64> {
143-
Ok(xdr::Int64::new(self.0))
141+
Ok(self.0)
144142
}
145143

146144
/// Returns stroops amount as xdr object.
147145
pub fn to_xdr_uint32(&self) -> Result<xdr::Uint32> {
148146
if self.0 >= 0 {
149-
Ok(xdr::Uint32::new(self.0 as u32))
147+
Ok(self.0 as u32)
150148
} else {
151149
Err(Error::NegativeStroops)
152150
}
153151
}
154152

155153
/// Creates from xdr object.
156-
pub fn from_xdr_int64(x: &xdr::Int64) -> Result<Stroops> {
157-
Ok(Stroops::new(x.value))
154+
pub fn from_xdr_int64(x: xdr::Int64) -> Result<Stroops> {
155+
Ok(Stroops(x))
158156
}
159157

160158
/// Creates from xdr object.
161-
pub fn from_xdr_uint32(x: &xdr::Uint32) -> Result<Stroops> {
162-
Ok(Stroops::new(x.value as i64))
159+
pub fn from_xdr_uint32(x: xdr::Uint32) -> Result<Stroops> {
160+
Ok(Stroops::new(x as i64))
163161
}
164162
}
165163

@@ -189,14 +187,14 @@ impl Price {
189187
/// Returns price as xdr object.
190188
pub fn to_xdr(&self) -> Result<xdr::Price> {
191189
Ok(xdr::Price {
192-
n: xdr::Int32::new(self.numerator()),
193-
d: xdr::Int32::new(self.denominator()),
190+
n: self.numerator(),
191+
d: self.denominator(),
194192
})
195193
}
196194

197195
/// Creates price from xdr object.
198196
pub fn from_xdr(x: &xdr::Price) -> Result<Price> {
199-
Ok(Price::new(x.n.value, x.d.value))
197+
Ok(Price::new(x.n, x.d))
200198
}
201199
}
202200

@@ -265,18 +263,17 @@ impl TryFrom<Stroops> for Amount {
265263
}
266264
}
267265

268-
impl XDRSerialize for Price {
269-
fn write_xdr(&self, out: &mut Vec<u8>) -> Result<u64> {
270-
let xdr_price = self.to_xdr()?;
271-
xdr_price.write_xdr(out).map_err(Error::XdrError)
266+
impl xdr::WriteXdr for Price {
267+
fn write_xdr<W: Write>(&self, w: &mut xdr::Limited<W>) -> xdr::Result<()> {
268+
let xdr_price = self.to_xdr().map_err(|_| xdr::Error::Invalid)?;
269+
xdr_price.write_xdr(w)
272270
}
273271
}
274272

275-
impl XDRDeserialize for Price {
276-
fn from_xdr_bytes(buffer: &[u8]) -> Result<(Self, u64)> {
277-
let (xdr_price, bytes_read) = xdr::Price::read_xdr(buffer).map_err(Error::XdrError)?;
278-
let res = Price::from_xdr(&xdr_price)?;
279-
Ok((res, bytes_read))
273+
impl xdr::ReadXdr for Price {
274+
fn read_xdr<R: Read>(r: &mut xdr::Limited<R>) -> xdr::Result<Self> {
275+
let xdr_result = xdr::Price::read_xdr(r)?;
276+
Self::from_xdr(&xdr_result).map_err(|_| xdr::Error::Invalid)
280277
}
281278
}
282279

0 commit comments

Comments
 (0)