Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/build/guides/conversions/address-conversions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,19 @@ Smart contracts don't need to explicitly interact with the XDR types, as all the

Note, that XDR conversions are an advanced feature and are not necessary for most Stellar smart contracts.

Deserializing XDR bytes back into a contract type can fail in two ways. If the bytes hold valid XDR for a value of a different type, the conversion returns an error that the contract can handle. If the bytes are not valid XDR for any value at all, the conversion panics, and the SDK offers no in-contract recovery for that case. Contracts that receive XDR bytes from untrusted sources, such as a custom authentication scheme, should handle the error case and validate the input before relying on it: prefer typed arguments such as `Address` where possible, or verify the byte format in the client that submits the transaction.

```rust
use soroban_sdk::{
xdr::{FromXdr, ToXdr},
Address, Bytes, Env,
Address, Bytes, ConversionError, Env,
};

pub fn address_to_xdr_bytes(env: Env, address: Address) -> Bytes {
address.to_xdr(&env)
}

pub fn address_from_xdr_bytes(env: Env, bytes: Bytes) -> Address {
Address::from_xdr(&env, &bytes).unwrap()
pub fn address_from_xdr_bytes(env: Env, bytes: Bytes) -> Result<Address, ConversionError> {
Address::from_xdr(&env, &bytes)
Comment thread
Copilot marked this conversation as resolved.
}
```
Loading