Struct dcl_crypto::account::Address
source · pub struct Address(/* private fields */);
Implementations§
source§impl Address
impl Address
sourcepub fn zero() -> Self
pub fn zero() -> Self
Creates an instance of the zero address
use dcl_crypto::account::Address;
assert_eq!(Address::zero().to_string(), "0x0000000000000000000000000000000000000000")
sourcepub fn checksum(&self) -> String
pub fn checksum(&self) -> String
Calculate ERC-55 version of the address
use dcl_crypto::account::Address;
assert_eq!(Address::try_from("0x0f5d2fb29fb7d3cfee444a200298f468908cc942").unwrap().checksum(), "0x0F5D2fB29fb7d3CFeE444a200298f468908cC942");
assert_eq!(Address::try_from("0x554bb6488ba955377359bed16b84ed0822679cdc").unwrap().checksum(), "0x554BB6488bA955377359bED16b84Ed0822679CDC");
assert_eq!(Address::try_from("0x1784ef41af86e97f8d28afe95b573a24aeda966e").unwrap().checksum(), "0x1784Ef41af86e97f8D28aFe95b573a24aEDa966e");
assert_eq!(Address::from([255; 20]).checksum(), "0xFFfFfFffFFfffFFfFFfFFFFFffFFFffffFfFFFfF");
assert_eq!(Address::from([0; 20]).checksum(), "0x0000000000000000000000000000000000000000");
Methods from Deref<Target = H160>§
pub fn as_fixed_bytes(&self) -> &[u8; 20]
pub fn as_fixed_bytes(&self) -> &[u8; 20]
Extracts a reference to the byte array containing the entire fixed hash.
pub fn to_low_u64_be(&self) -> u64
pub fn to_low_u64_be(&self) -> u64
Returns the lowest 8 bytes interpreted as big-endian.
Note
For hash type with less than 8 bytes the missing bytes are interpreted as being zero.
pub fn to_low_u64_le(&self) -> u64
pub fn to_low_u64_le(&self) -> u64
Returns the lowest 8 bytes interpreted as little-endian.
Note
For hash type with less than 8 bytes the missing bytes are interpreted as being zero.
pub fn to_low_u64_ne(&self) -> u64
pub fn to_low_u64_ne(&self) -> u64
Returns the lowest 8 bytes interpreted as native-endian.
Note
For hash type with less than 8 bytes the missing bytes are interpreted as being zero.
Trait Implementations§
source§impl<'de> Deserialize<'de> for Address
impl<'de> Deserialize<'de> for Address
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Display for Address
impl Display for Address
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Format an Address into it string representation
use dcl_crypto::account::Address;
assert_eq!(Address::from([0; 20]).to_string(), "0x0000000000000000000000000000000000000000");
assert_eq!(Address::from([255; 20]).to_string(), "0xffffffffffffffffffffffffffffffffffffffff");
source§impl LowerHex for Address
impl LowerHex for Address
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the Address
into its hexadecimal lowercase representation
use dcl_crypto::account::Address;
let address = Address::from([255; 20]);
let zero = Address::from([0; 20]);
assert_eq!(format!("{zero:x}"), "0000000000000000000000000000000000000000");
assert_eq!(format!("{address:x}"), "ffffffffffffffffffffffffffffffffffffffff");
assert_eq!(format!("{zero:#x}"), "0x0000000000000000000000000000000000000000");
assert_eq!(format!("{address:#x}"), "0xffffffffffffffffffffffffffffffffffffffff");
source§impl PartialEq<H160> for &Address
impl PartialEq<H160> for &Address
source§impl PartialEq<H160> for Address
impl PartialEq<H160> for Address
source§impl PartialEq for Address
impl PartialEq for Address
source§impl TryFrom<&str> for Address
impl TryFrom<&str> for Address
source§fn try_from(value: &str) -> Result<Self, Self::Error>
fn try_from(value: &str) -> Result<Self, Self::Error>
Converts an hexadecimal representation into an Address
use dcl_crypto::account::Address;
let lower_address = Address::try_from("0xf15fd08462c3248b2bfe9c39b48af7801fc303db");
let upper_address = Address::try_from("0xF15FD08462C3248B2BFE9C39B48AF7801FC303DB");
let address_expected: [u8; 20] = [ 241, 95, 208, 132, 98, 195, 36, 139, 43, 254, 156, 57, 180, 138, 247, 128, 31, 195, 3, 219];
assert_eq!(lower_address.unwrap(), Address::from(address_expected));
assert_eq!(upper_address.unwrap(), Address::from(address_expected));
It requires the address string to be prefixed with 0x
use dcl_crypto::account::{Address, DecodeHexError};
let not_prefixed_address = Address::try_from("f15fd08462c3248b2bfe9c39b48af7801fc303db");
assert_eq!(not_prefixed_address.is_err(), true);
assert_eq!(not_prefixed_address, Err(DecodeHexError::MissingPrefix));
It requires the address to be 42
characters long
use dcl_crypto::account::{Address, DecodeHexError};
let len_41_address = Address::try_from("0xf15fd08462c3248b2bfe9c39b48af7801fc303d");
assert_eq!(len_41_address.is_err(), true);
assert_eq!(len_41_address, Err(DecodeHexError::InvalidLength));
let len_43_address = Address::try_from("0xf15fd08462c3248b2bfe9c39b48af7801fc303dbb");
assert_eq!(len_43_address.is_err(), true);
assert_eq!(len_43_address, Err(DecodeHexError::InvalidLength));
It requires all characters to be hexadecimals
use dcl_crypto::account::{Address, DecodeHexError};
let not_hex_address = Address::try_from("0xf15fd08462c3248b2bfe9c39b48af7801fc303dx");
assert_eq!(not_hex_address.is_err(), true);
assert_eq!(not_hex_address, Err(DecodeHexError::InvalidHexCharacter{ c: 'x', index: 41}));
§type Error = DecodeHexError
type Error = DecodeHexError
source§impl UpperHex for Address
impl UpperHex for Address
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the Address
into its hexadecimal uppercase representation
use dcl_crypto::account::Address;
let address = Address::from([255; 20]);
let zero = Address::from([0; 20]);
assert_eq!(format!("{zero:X}"), "0000000000000000000000000000000000000000");
assert_eq!(format!("{address:X}"), "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
assert_eq!(format!("{zero:#X}"), "0X0000000000000000000000000000000000000000");
assert_eq!(format!("{address:#X}"), "0XFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");