diff --git a/scrypto/src/resource/non_fungible_address.rs b/scrypto/src/resource/non_fungible_address.rs index db47145dc21..307a913baa8 100644 --- a/scrypto/src/resource/non_fungible_address.rs +++ b/scrypto/src/resource/non_fungible_address.rs @@ -6,9 +6,9 @@ use sbor::rust::vec::Vec; use sbor::*; use crate::abi::*; +use crate::address::EntityType; use crate::constants::ECDSA_TOKEN; use crate::crypto::EcdsaPublicKey; -use crate::misc::*; use crate::resource::*; /// Identifier for a non-fungible unit. @@ -41,9 +41,9 @@ impl NonFungibleAddress { } } -//======== -// binary -//======== +//======= +// error +//======= /// Represents an error when parsing non-fungible address. #[derive(Debug, Clone, PartialEq, Eq)] @@ -55,6 +55,20 @@ pub enum ParseNonFungibleAddressError { InvalidPrefix, } +#[cfg(not(feature = "alloc"))] +impl std::error::Error for ParseNonFungibleAddressError {} + +#[cfg(not(feature = "alloc"))] +impl fmt::Display for ParseNonFungibleAddressError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{:?}", self) + } +} + +//======== +// binary +//======== + impl TryFrom<&[u8]> for NonFungibleAddress { type Error = ParseNonFungibleAddressError; @@ -100,10 +114,7 @@ impl FromStr for NonFungibleAddress { fn from_str(s: &str) -> Result { let bytes = hex::decode(s).map_err(|_| ParseNonFungibleAddressError::InvalidHex(s.to_owned()))?; - if bytes.get(0) != Some(&3u8) { - return Err(ParseNonFungibleAddressError::InvalidPrefix); - } - Self::try_from(&bytes[1..]) + Self::try_from(bytes.as_ref()) } } @@ -111,7 +122,7 @@ impl fmt::Display for NonFungibleAddress { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { // Note that if the non-fungible ID is empty, the non-fungible address won't be distinguishable from resource address. // TODO: figure out what's best for the users - write!(f, "{}", hex::encode(combine(3, &self.to_vec()))) + write!(f, "{}", hex::encode(&self.to_vec())) } } @@ -120,3 +131,34 @@ impl fmt::Debug for NonFungibleAddress { write!(f, "{}", self) } } + +//====== +// test +//====== + +#[cfg(test)] +mod tests { + use super::*; + use crate::sbor::rust::string::ToString; + + #[test] + pub fn non_fungible_address_from_and_to_string_succeeds() { + // Arrange + let resource_address = ResourceAddress::from_str( + "resource_sim1qzntya3nlyju8zsj8h86fz8ma5yl8smwjlg9tckkqvrs520k2p", + ) + .expect("Resource address from str failed."); + let non_fungible_id = NonFungibleId( + hex::decode("30071000000071dba5dd36e30de857049805fd1553cd") + .expect("Invalid NonFungibleId hex"), + ); + let non_fungible_address = NonFungibleAddress::new(resource_address, non_fungible_id); + + // Act + let converted_non_fungible_address = + NonFungibleAddress::from_str(&non_fungible_address.to_string()); + + // Assert + assert_eq!(converted_non_fungible_address, Ok(non_fungible_address)); + } +} diff --git a/transaction/src/signing/ecdsa.rs b/transaction/src/signing/ecdsa.rs index b54e7b22fb2..64f2f35dd35 100644 --- a/transaction/src/signing/ecdsa.rs +++ b/transaction/src/signing/ecdsa.rs @@ -65,7 +65,7 @@ mod tests { #[test] fn test_non_fungible_address_codec() { - let expected = "03000000000000000000000000000000000000000000000000000002300721000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f"; + let expected = "000000000000000000000000000000000000000000000000000002300721000000031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f"; let private_key = EcdsaPrivateKey::from_bytes(&[1u8; 32]).unwrap(); let public_key = private_key.public_key(); let auth_address =