Skip to content

Commit

Permalink
Added which is invalid in AddressError::InvalidBech32String
Browse files Browse the repository at this point in the history
  • Loading branch information
gfusee committed Nov 12, 2023
1 parent dc91ef2 commit 2a777fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
2 changes: 1 addition & 1 deletion data/src/error/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub enum AddressError {
/// Represents an error case where an invalid Bech32 string is provided.
InvalidBech32String,
InvalidBech32String { invalid_value: String },
/// Represents an error case where an `Address` cannot be converted to its Bech32 string representation.
CannotConvertToBech32String,
}
Expand Down
30 changes: 28 additions & 2 deletions data/src/types/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Address {
/// let address = Address::from_bech32_string("erd1qqqqqqqqqqqqqpgq7ykazrzd905zvnlr88dpfw06677lxe9w0n4suz00uh").unwrap();
/// ```
pub fn from_bech32_string(bech32: &str) -> Result<Address, DataError> {
let Ok(address) = SDKAddress::from_bech32_string(bech32) else { return Err(AddressError::InvalidBech32String.into()) };
let Ok(address) = SDKAddress::from_bech32_string(bech32) else { return Err(AddressError::InvalidBech32String { invalid_value: bech32.to_string() }.into()) };

Ok(Address(address))
}
Expand Down Expand Up @@ -235,7 +235,7 @@ impl<'a> Deserialize<'a> for Address {
mod tests {
use multiversx_sc::types::ManagedAddress;
use multiversx_sc_scenario::api::StaticApi;
use crate::Address;
use crate::{Address, AddressError, DataError};
use crate::types::managed::ManagedConvertible;
use crate::types::native::NativeConvertible;

Expand All @@ -261,4 +261,30 @@ mod tests {
managed.to_byte_array()
)
}

#[test]
fn test_from_bech32_string_valid_address() {
Address::from_bech32_string("erd1an4xpn58j7ymd58m2jznr32t0vmas75egrdfa8mta6fzvqn9tkxq4jvghn").unwrap();
}

#[test]
fn test_from_bech32_string_invalid_address() {
let str = "erd1an4xpn58j7ymd58m2jznr32t0vmas75egrdfa8mta6fzvqn9tkxq4jvghm";
let error = Address::from_bech32_string(str).unwrap_err();

let expected = DataError::Address(AddressError::InvalidBech32String { invalid_value: str.to_string() });

assert_eq!(error, expected);
}

#[test]
fn test_from_bech32_string_invalid_address_bad_length() {
let str = "erd1an4xpn58j7ymd58m2jznr32t";
let error = Address::from_bech32_string(str).unwrap_err();

let expected = DataError::Address(AddressError::InvalidBech32String { invalid_value: str.to_string() });

assert_eq!(error, expected);
}

}

0 comments on commit 2a777fc

Please sign in to comment.