Skip to content

Commit

Permalink
Revert "[account-address] Input and output with a 0x for hex output (…
Browse files Browse the repository at this point in the history
…#7)"

This reverts commit ba7c064.
  • Loading branch information
gregnazario committed Apr 20, 2022
1 parent 43f1e15 commit 5c0d768
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
28 changes: 11 additions & 17 deletions language/move-core/types/src/account_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl AccountAddress {

let hex_len = literal.len() - 2;

// If the string is too short, pad it because it needs to be exactly the right number of bytes
// If the string is too short, pad it
if hex_len < Self::LENGTH * 2 {
let mut hex_str = String::with_capacity(Self::LENGTH * 2);
for _ in 0..Self::LENGTH * 2 - hex_len {
Expand All @@ -92,7 +92,7 @@ impl AccountAddress {
}

pub fn to_hex(&self) -> String {
self.short_str_lossless()
format!("{:x}", self)
}

pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Self, AccountAddressParseError> {
Expand Down Expand Up @@ -206,23 +206,23 @@ impl From<&AccountAddress> for [u8; AccountAddress::LENGTH] {

impl From<&AccountAddress> for String {
fn from(addr: &AccountAddress) -> String {
addr.to_hex_literal()
::hex::encode(addr.as_ref())
}
}

impl TryFrom<String> for AccountAddress {
type Error = AccountAddressParseError;

fn try_from(s: String) -> Result<AccountAddress, AccountAddressParseError> {
AccountAddress::from_str(&s)
Self::from_hex(s)
}
}

impl FromStr for AccountAddress {
type Err = AccountAddressParseError;

fn from_str(s: &str) -> Result<Self, AccountAddressParseError> {
Self::from_hex_literal(s)
Self::from_hex(s)
}
}

Expand All @@ -233,7 +233,7 @@ impl<'de> Deserialize<'de> for AccountAddress {
{
if deserializer.is_human_readable() {
let s = <String>::deserialize(deserializer)?;
AccountAddress::from_hex_literal(&s).map_err(D::Error::custom)
AccountAddress::from_hex(s).map_err(D::Error::custom)
} else {
// In order to preserve the Serde data model and help analysis tools,
// make sure to wrap our value in a container with the same name
Expand All @@ -254,7 +254,7 @@ impl Serialize for AccountAddress {
S: Serializer,
{
if serializer.is_human_readable() {
self.to_hex_literal().serialize(serializer)
self.to_hex().serialize(serializer)
} else {
// See comment in deserialize.
serializer.serialize_newtype_struct("AccountAddress", &self.0)
Expand Down Expand Up @@ -346,16 +346,8 @@ mod tests {
assert_eq!(address_from_literal, address);
assert_eq!(hex_literal, address.to_hex_literal());

// Check other variations of 0x1
assert_eq!(AccountAddress::from_str("0x01").unwrap(), address);
assert_eq!(
AccountAddress::from_str("0x00000000000000000000000000000001").unwrap(),
address
);

// Missing '0x' should fail
// Missing '0x'
AccountAddress::from_hex_literal(hex).unwrap_err();

// Too long
AccountAddress::from_hex_literal("0x100000000000000000000000000000001").unwrap_err();
}
Expand Down Expand Up @@ -384,12 +376,14 @@ mod tests {
#[test]
fn test_serde_json() {
let hex = "ca843279e3427144cead5e4d5999a3d0";
let json_hex = "\"0xca843279e3427144cead5e4d5999a3d0\"";
let json_hex = "\"ca843279e3427144cead5e4d5999a3d0\"";

let address = AccountAddress::from_hex(hex).unwrap();

let json = serde_json::to_string(&address).unwrap();
let json_address: AccountAddress = serde_json::from_str(json_hex).unwrap();

assert_eq!(json, json_hex);
assert_eq!(address, json_address);
}

Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 5c0d768

Please sign in to comment.