sdkt-xdr 2.5.0

XDR encoding and decoding pipelines customized for Soroban smart contracts.
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
//! Typed Rust <-> [`ScVal`] conversion and typed contract-invocation helpers.
//!
//! The [`IntoScVal`] and [`FromScVal`] traits let callers map idiomatic Rust
//! values to/from Soroban [`stellar_xdr::ScVal`] without hand-assembling XDR
//! union variants. [`encode_scvals`]/[`decode_scvals`] batch a list of values.
//!
//! Conversion is lossless for all supported types; out-of-range numeric
//! truncation is rejected at the scalar boundary (see [`FromScVal`]).

use stellar_xdr::{Int128Parts, ReadXdr, ScAddress, ScBytes, ScString, ScVal, UInt128Parts};

/// Error produced when a Rust value cannot be represented as a [`ScVal`] or
/// back.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ScValError {
    /// The byte buffer exceeded the Soroban `BytesM`/`StringM` limit.
    TooLong,
    /// The value could not be decoded as the requested Rust type.
    TypeMismatch(&'static str),
    /// Numeric value out of range for the target scalar.
    OutOfRange,
}

impl std::fmt::Display for ScValError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ScValError::TooLong => write!(f, "value exceeds Soroban length limit"),
            ScValError::TypeMismatch(t) => write!(f, "expected {t}, got a different ScVal kind"),
            ScValError::OutOfRange => write!(f, "numeric value out of range"),
        }
    }
}
impl std::error::Error for ScValError {}

/// Converts an owned Rust value into a [`stellar_xdr::ScVal`].
pub trait IntoScVal {
    /// Consume `self` and produce an [`ScVal`].
    fn into_scval(self) -> Result<ScVal, ScValError>;
}

/// Converts a [`stellar_xdr::ScVal`] reference back into a Rust value.
pub trait FromScVal: Sized {
    /// Try to interpret `v` as `Self`.
    fn from_scval(v: &ScVal) -> Result<Self, ScValError>;
}

// --- Scalars ---------------------------------------------------------------

impl IntoScVal for bool {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::Bool(self))
    }
}
impl FromScVal for bool {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::Bool(b) => Ok(*b),
            _ => Err(ScValError::TypeMismatch("bool")),
        }
    }
}

impl IntoScVal for u32 {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::U32(self))
    }
}
impl FromScVal for u32 {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::U32(n) => Ok(*n),
            _ => Err(ScValError::TypeMismatch("u32")),
        }
    }
}

impl IntoScVal for i32 {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::I32(self))
    }
}
impl FromScVal for i32 {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::I32(n) => Ok(*n),
            _ => Err(ScValError::TypeMismatch("i32")),
        }
    }
}

impl IntoScVal for u64 {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::U64(self))
    }
}
impl FromScVal for u64 {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::U64(n) => Ok(*n),
            _ => Err(ScValError::TypeMismatch("u64")),
        }
    }
}

impl IntoScVal for i64 {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::I64(self))
    }
}
impl FromScVal for i64 {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::I64(n) => Ok(*n),
            _ => Err(ScValError::TypeMismatch("i64")),
        }
    }
}

impl IntoScVal for u128 {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::U128(UInt128Parts {
            hi: (self >> 64) as u64,
            lo: self as u64,
        }))
    }
}
impl FromScVal for u128 {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::U128(p) => Ok(((p.hi as u128) << 64) | p.lo as u128),
            _ => Err(ScValError::TypeMismatch("u128")),
        }
    }
}

impl IntoScVal for i128 {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::I128(Int128Parts {
            hi: (self >> 64) as i64,
            lo: self as u64,
        }))
    }
}
impl FromScVal for i128 {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::I128(p) => Ok(((p.hi as i128) << 64) | p.lo as i128),
            _ => Err(ScValError::TypeMismatch("i128")),
        }
    }
}

// --- Strings ---------------------------------------------------------------

impl IntoScVal for String {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        let str_m = self
            .into_bytes()
            .try_into()
            .map_err(|_| ScValError::TooLong)?;
        Ok(ScVal::String(ScString(str_m)))
    }
}
impl FromScVal for String {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::String(s) => Ok(s.to_utf8_string_lossy()),
            _ => Err(ScValError::TypeMismatch("String")),
        }
    }
}

impl IntoScVal for &str {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        self.to_string().into_scval()
    }
}

// --- Bytes -----------------------------------------------------------------

impl IntoScVal for Vec<u8> {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        let bytes = self.try_into().map_err(|_| ScValError::TooLong)?;
        Ok(ScVal::Bytes(ScBytes(bytes)))
    }
}
impl FromScVal for Vec<u8> {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::Bytes(b) => Ok(b.to_vec()),
            _ => Err(ScValError::TypeMismatch("Vec<u8>")),
        }
    }
}

impl IntoScVal for &[u8] {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        self.to_vec().into_scval()
    }
}

/// 32-byte buffers map to Soroban `Bytes` (the on-wire `BytesN` wrapper is not a
/// distinct [`ScVal`] variant — fixed-length byte arrays are represented as
/// `Bytes` on host interfaces).
impl IntoScVal for [u8; 32] {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        self.to_vec().into_scval()
    }
}
impl FromScVal for [u8; 32] {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        let bytes = Vec::<u8>::from_scval(v)?;
        bytes
            .try_into()
            .map_err(|_| ScValError::TypeMismatch("[u8; 32]"))
    }
}

// --- Addresses (StrKey) ----------------------------------------------------

/// A Soroban `Address`. Supports conversion from/to a `C...` contract or
/// `G...` account StrKey by delegating to `stellar-strkey`.
///
/// Kept as a thin wrapper so callers can pass a contract/account address without
/// reaching into low-level XDR variants.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Address(pub ScAddress);

impl IntoScVal for Address {
    fn into_scval(self) -> Result<ScVal, ScValError> {
        Ok(ScVal::Address(self.0))
    }
}
impl FromScVal for Address {
    fn from_scval(v: &ScVal) -> Result<Self, ScValError> {
        match v {
            ScVal::Address(a) => Ok(Address(a.clone())),
            _ => Err(ScValError::TypeMismatch("Address")),
        }
    }
}

impl Address {
    /// Build an `Address` from a Stellar public key (`G...`) or contract (`C...`)
    /// strkey.
    pub fn from_strkey(key: &str) -> Result<Self, ScValError> {
        let parsed = stellar_strkey::Strkey::from_string(key)
            .map_err(|_| ScValError::TypeMismatch("address strkey"))?;
        let sc = match parsed {
            stellar_strkey::Strkey::PublicKeyEd25519(pk) => {
                ScAddress::Account(stellar_xdr::AccountId(
                    stellar_xdr::PublicKey::PublicKeyTypeEd25519(stellar_xdr::Uint256(pk.0)),
                ))
            }
            stellar_strkey::Strkey::Contract(c) => {
                ScAddress::Contract(stellar_xdr::ContractId(stellar_xdr::Hash(c.0)))
            }
            _ => return Err(ScValError::TypeMismatch("unsupported strkey")),
        };
        Ok(Address(sc))
    }
}

// --- Batches ---------------------------------------------------------------

/// Convert a list of typed values into `ScVal`s.
pub fn encode_scvals<I, T>(values: I) -> Result<Vec<ScVal>, ScValError>
where
    I: IntoIterator<Item = T>,
    T: IntoScVal,
{
    values.into_iter().map(|v| v.into_scval()).collect()
}

/// Encode an [`ScVal`] to its base64 XDR representation for transport.
pub fn scval_to_base64(v: &ScVal) -> Result<String, ScValError> {
    use base64::Engine;
    let mut buf = Vec::new();
    use stellar_xdr::WriteXdr;
    let mut limited = stellar_xdr::Limited::new(&mut buf, stellar_xdr::Limits::none());
    v.write_xdr(&mut limited).map_err(|_| ScValError::TooLong)?;
    Ok(base64::engine::general_purpose::STANDARD.encode(&buf))
}

/// Decode a base64-encoded XDR [`ScVal`] back into the typed representation.
///
/// Returns `None` when the input is empty or not valid base64 XDR `ScVal`
/// bytes (e.g. an unrelated topic payload) so callers can fall back to the
/// raw string instead of erroring out mid-stream.
pub fn scval_from_base64(raw: &str) -> Option<ScVal> {
    use base64::Engine;
    let buf = base64::engine::general_purpose::STANDARD
        .decode(raw.trim())
        .ok()?;
    let mut cursor = std::io::Cursor::new(buf);
    let mut limited = stellar_xdr::Limited::new(&mut cursor, stellar_xdr::Limits::none());
    stellar_xdr::ScVal::read_xdr(&mut limited).ok()
}

/// Estimate serialized XDR size for a generic serializable object.
pub fn estimate_xdr_size<T: stellar_xdr::WriteXdr>(val: &T) -> Result<usize, ScValError> {
    let mut buf = Vec::new();
    let mut l = stellar_xdr::Limited::new(&mut buf, stellar_xdr::Limits::none());
    val.write_xdr(&mut l).map_err(|_| ScValError::TooLong)?;
    Ok(buf.len())
}

/// Decode a list of `ScVal`s back into typed values.
pub fn decode_scvals<T, I>(vals: I) -> Result<Vec<T>, ScValError>
where
    I: IntoIterator<Item = ScVal>,
    T: FromScVal,
{
    vals.into_iter().map(|v| T::from_scval(&v)).collect()
}

/// Decode `ScVal`s by reference.
pub fn decode_scvals_ref<T, I>(vals: I) -> Result<Vec<T>, ScValError>
where
    I: IntoIterator<Item = ScVal>,
    T: FromScVal,
{
    decode_scvals(vals)
}

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

    #[test]
    fn bool_roundtrip() {
        let sv = true.into_scval().unwrap();
        assert!(bool::from_scval(&sv).unwrap());
    }

    #[test]
    fn integer_roundtrip() {
        assert_eq!(42u32.into_scval().unwrap(), ScVal::U32(42));
        assert_eq!((-7i32).into_scval().unwrap(), ScVal::I32(-7));

        // Bounds
        assert_eq!(u32::MAX.into_scval().unwrap(), ScVal::U32(u32::MAX));
        assert_eq!(u32::MIN.into_scval().unwrap(), ScVal::U32(u32::MIN));
        assert_eq!(i32::MAX.into_scval().unwrap(), ScVal::I32(i32::MAX));
        assert_eq!(i32::MIN.into_scval().unwrap(), ScVal::I32(i32::MIN));

        assert_eq!(
            9_000_000_000u64.into_scval().unwrap(),
            ScVal::U64(9_000_000_000)
        );
        assert_eq!((-5i64).into_scval().unwrap(), ScVal::I64(-5));
        // wide integers
        let u = 0x_0123_4567_89ab_cdef_0000_0000_0000_0001u128;
        let sv = u.into_scval().unwrap();
        assert_eq!(u128::from_scval(&sv).unwrap(), u);

        let i = -(0x_0123_4567_89ab_cdef_0000_0000_0000_0001i128);
        let sv = i.into_scval().unwrap();
        assert_eq!(i128::from_scval(&sv).unwrap(), i);
    }

    #[test]
    fn string_roundtrip() {
        let sv = "hello".into_scval().unwrap();
        assert_eq!(String::from_scval(&sv).unwrap(), "hello");
        let own = String::from("world");
        let sv = own.into_scval().unwrap();
        assert_eq!(String::from_scval(&sv).unwrap(), "world");
    }

    #[test]
    fn bytes_roundtrip() {
        let data = vec![1u8, 2, 3, 255];
        let sv = data.clone().into_scval().unwrap();
        assert_eq!(Vec::<u8>::from_scval(&sv).unwrap(), data);

        let arr = [7u8; 32];
        let sv = arr.into_scval().unwrap();
        assert_eq!(<[u8; 32]>::from_scval(&sv).unwrap(), arr);
    }

    #[test]
    fn invalid_typed_value() {
        // Can't decode a u32 from an i32.
        let sv = ScVal::I32(5);
        assert_eq!(u32::from_scval(&sv), Err(ScValError::TypeMismatch("u32")));
    }

    #[test]
    fn encode_decode_batch() {
        let scs = encode_scvals([1u32, 2, 3]).unwrap();
        assert_eq!(scs.len(), 3);
        let back = decode_scvals::<u32, _>(scs).unwrap();
        assert_eq!(back, vec![1, 2, 3]);
    }

    #[test]
    fn address_from_strkey() {
        // Test account G...
        let addr = Address::from_strkey("GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWHF")
            .unwrap();
        assert!(matches!(addr.0, ScAddress::Account(_)));
        // Test contract C...
        let addr2 =
            Address::from_strkey("CAAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQCAIBAEAQC526")
                .unwrap();
        assert!(matches!(addr2.0, ScAddress::Contract(_)));
    }
}