vialabs-stellar-common 0.1.3

Common interfaces, types, and utilities for Stellar contracts in the VIA cross-chain messaging system
Documentation
use crate::storage::ZERO_BYTES;
use soroban_sdk::{xdr::ToXdr, Address, Bytes, Env};

/// Checks if an address is the zero address.
///
/// # Arguments
///
/// * `env` - The Soroban environment
/// * `address` - Address in bytes format to check
///
/// # Returns
///
/// Returns `true` if the address is the zero address, `false` otherwise.
pub fn is_zero_address(env: &Env, address: Bytes) -> bool {
  let zero_bytes = Bytes::from_array(env, &ZERO_BYTES);

  zero_bytes == address
}

/// Validates that an address's XDR representation matches the provided public key bytes.
///
/// # Arguments
///
/// * `env` - The Soroban environment
/// * `signer` - The address to validate
/// * `public_key` - The public key bytes to match against
///
/// # Returns
///
/// Returns `true` if the address's public key matches the provided public key bytes, `false` otherwise.
pub fn validate_address_bytes(env: &Env, signer: Address, public_key: Bytes) -> bool {
  let pub_from_address = signer.to_xdr(env);
  let pub_from_address_buffer = pub_from_address.to_buffer::<1024>();
  let pub_from_address_slice = pub_from_address_buffer.as_slice();

  if pub_from_address_slice.len() < 32 {
    return false;
  }

  let start = pub_from_address_slice.len() - 32;
  let raw_bytes_from_xdr = &pub_from_address_slice[start..pub_from_address_slice.len()];
  let raw_bytes_from_address = Bytes::from_slice(env, raw_bytes_from_xdr);

  raw_bytes_from_address == public_key
}