From e859908e502caa2d3f53fd67610559a02d253179 Mon Sep 17 00:00:00 2001 From: 0xOmarA Date: Tue, 6 Sep 2022 15:36:38 +0300 Subject: [PATCH 1/5] Fix `NonFungibleAddress` from and to string --- scrypto/src/resource/non_fungible_address.rs | 57 +++++++++++++++++--- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/scrypto/src/resource/non_fungible_address.rs b/scrypto/src/resource/non_fungible_address.rs index db47145dc21..9ff36bbe75f 100644 --- a/scrypto/src/resource/non_fungible_address.rs +++ b/scrypto/src/resource/non_fungible_address.rs @@ -6,6 +6,7 @@ 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::*; @@ -41,9 +42,9 @@ impl NonFungibleAddress { } } -//======== -// binary -//======== +//======= +// error +//======= /// Represents an error when parsing non-fungible address. #[derive(Debug, Clone, PartialEq, Eq)] @@ -55,6 +56,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 +115,10 @@ 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) { + if bytes.get(0) != Some(&EntityType::Resource.id()) { return Err(ParseNonFungibleAddressError::InvalidPrefix); } - Self::try_from(&bytes[1..]) + Self::try_from(bytes.as_ref()) } } @@ -111,7 +126,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 +135,33 @@ impl fmt::Debug for NonFungibleAddress { write!(f, "{}", self) } } + +//====== +// test +//====== + +#[cfg(test)] +mod tests { + use super::*; + + #[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)); + } +} From 0cdb0465c4bd2a748b4b80f24d3a8c5ca2d9f26e Mon Sep 17 00:00:00 2001 From: 0xOmarA Date: Tue, 6 Sep 2022 15:40:45 +0300 Subject: [PATCH 2/5] Fix tests --- transaction/src/signing/ecdsa.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 = From 2af3374242227cffd04ccbbb92108d7a1a4f8c1f Mon Sep 17 00:00:00 2001 From: 0xOmarA Date: Tue, 6 Sep 2022 15:58:38 +0300 Subject: [PATCH 3/5] Fix tests --- scrypto/src/resource/non_fungible_address.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrypto/src/resource/non_fungible_address.rs b/scrypto/src/resource/non_fungible_address.rs index 9ff36bbe75f..f6757975907 100644 --- a/scrypto/src/resource/non_fungible_address.rs +++ b/scrypto/src/resource/non_fungible_address.rs @@ -9,7 +9,6 @@ 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. @@ -142,6 +141,7 @@ impl fmt::Debug for NonFungibleAddress { #[cfg(test)] mod tests { + use crate::sbor::rust::string::ToString; use super::*; #[test] From fbe2790e2225300ba277a8d4159d45679f49f8f3 Mon Sep 17 00:00:00 2001 From: 0xOmarA Date: Tue, 6 Sep 2022 16:00:14 +0300 Subject: [PATCH 4/5] Formatting --- scrypto/src/resource/non_fungible_address.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrypto/src/resource/non_fungible_address.rs b/scrypto/src/resource/non_fungible_address.rs index f6757975907..7622980f1a5 100644 --- a/scrypto/src/resource/non_fungible_address.rs +++ b/scrypto/src/resource/non_fungible_address.rs @@ -141,8 +141,8 @@ impl fmt::Debug for NonFungibleAddress { #[cfg(test)] mod tests { - use crate::sbor::rust::string::ToString; use super::*; + use crate::sbor::rust::string::ToString; #[test] pub fn non_fungible_address_from_and_to_string_succeeds() { From 109a5192f88c3e59b2cb268c3a153c9f526eef3c Mon Sep 17 00:00:00 2001 From: 0xOmarA Date: Thu, 8 Sep 2022 03:45:43 +0300 Subject: [PATCH 5/5] Remove check --- scrypto/src/resource/non_fungible_address.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/scrypto/src/resource/non_fungible_address.rs b/scrypto/src/resource/non_fungible_address.rs index 7622980f1a5..307a913baa8 100644 --- a/scrypto/src/resource/non_fungible_address.rs +++ b/scrypto/src/resource/non_fungible_address.rs @@ -114,9 +114,6 @@ 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(&EntityType::Resource.id()) { - return Err(ParseNonFungibleAddressError::InvalidPrefix); - } Self::try_from(bytes.as_ref()) } }