Skip to content

Commit

Permalink
Merge pull request #485 from radixdlt/bugfix/non-fungible-address-string
Browse files Browse the repository at this point in the history
Fix `NonFungibleAddress` from and to string
  • Loading branch information
iamyulong authored Sep 8, 2022
2 parents 6a431b4 + 109a519 commit 7783f3d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
60 changes: 51 additions & 9 deletions scrypto/src/resource/non_fungible_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -41,9 +41,9 @@ impl NonFungibleAddress {
}
}

//========
// binary
//========
//=======
// error
//=======

/// Represents an error when parsing non-fungible address.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -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;

Expand Down Expand Up @@ -100,18 +114,15 @@ impl FromStr for NonFungibleAddress {
fn from_str(s: &str) -> Result<Self, Self::Err> {
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())
}
}

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()))
}
}

Expand All @@ -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));
}
}
2 changes: 1 addition & 1 deletion transaction/src/signing/ecdsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down

0 comments on commit 7783f3d

Please sign in to comment.