use thruput_xdr::types::ContractAddress;
use zeroize::Zeroizing;
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub enum Error {
#[error("contract address must be a Z StrKey or base58 encoding of exactly 32 bytes")]
ContractAddress,
#[error("Ed25519 public key must be a G StrKey or base58 encoding of exactly 32 bytes")]
PublicKey,
#[error("expected an S StrKey or base58 encoding of a 32-byte Ed25519 seed")]
SecretSeed,
}
pub fn parse_contract_address(value: &str) -> Result<ContractAddress, Error> {
thruput_encoding::parse_address(value)
.map(Into::into)
.map_err(|_| Error::ContractAddress)
}
pub fn format_contract_address(address: &ContractAddress) -> String {
let mut bytes = [0; 32];
bytes.copy_from_slice(address.as_ref());
thruput_encoding::address_strkey(&bytes)
}
pub fn parse_public_key(value: &str) -> Result<[u8; 32], Error> {
thruput_encoding::parse_public_key(value).map_err(|_| Error::PublicKey)
}
pub fn format_public_key(key: &[u8; 32]) -> String {
thruput_encoding::public_key_strkey(key)
}
pub fn parse_secret_seed(value: &str) -> Result<Zeroizing<[u8; 32]>, Error> {
thruput_encoding::parse_secret_seed(value).map_err(|_| Error::SecretSeed)
}
pub fn format_secret_seed(seed: &[u8; 32]) -> Zeroizing<String> {
thruput_encoding::secret_seed_strkey(seed)
}