stellar_base/
account.rs

1//! Account data and flags.
2use crate::error::{Error, Result};
3use crate::xdr;
4
5bitflags! {
6    /// Account flags.
7    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
8    pub struct AccountFlags: 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;
13    }
14}
15
16bitflags! {
17    /// Account trust line flags.
18    #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
19    pub struct TrustLineFlags: u32 {
20        const AUTHORIZED = xdr::TrustLineFlags::AuthorizedFlag as u32;
21        const AUTHORIZED_TO_MAINTAIN_LIABILITIES = xdr::TrustLineFlags::AuthorizedToMaintainLiabilitiesFlag as u32;
22        const TRUSTLINE_CLAWBACK_ENABLED = xdr::TrustLineFlags::TrustlineClawbackEnabledFlag as u32;
23    }
24}
25
26/// Data associated with a Stellar account.
27#[derive(Debug, Clone, PartialEq, Eq)]
28pub struct DataValue(Vec<u8>);
29
30impl DataValue {
31    /// Converts a slice of bytes to a DataValue.
32    ///
33    /// Returns Err if the slice is longer than 64 bytes.
34    pub fn from_slice(value: &[u8]) -> Result<DataValue> {
35        if value.len() > 64 {
36            return Err(Error::InvalidDataValue);
37        }
38        Ok(DataValue(value.to_vec()))
39    }
40
41    /// Converts bytes encoded as base64 to a DataValue.
42    ///
43    /// Returns Err if the encoded data is longer than 64 bytes.
44    pub fn from_base64(encoded: &str) -> Result<DataValue> {
45        let decoded = base64::Engine::decode(&base64::engine::general_purpose::STANDARD, encoded)?;
46        DataValue::from_slice(&decoded)
47    }
48
49    /// Returns the DataValue content as bytes.
50    pub fn as_bytes(&self) -> &[u8] {
51        &self.0
52    }
53
54    /// Returns the DataValue xdr object.
55    pub fn to_xdr(&self) -> Result<xdr::DataValue> {
56        let inner = self.as_bytes();
57        Ok(xdr::DataValue(
58            inner.try_into().map_err(|_| Error::XdrError)?,
59        ))
60    }
61
62    /// Creates a DataValue from xdr object.
63    pub fn from_xdr(x: &xdr::DataValue) -> Result<DataValue> {
64        DataValue::from_slice(&x)
65    }
66}