1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! 20-byte Ethereum addresses.

use crate::{
    conv,
    ffi::{
        boxed::{AscBox, AscRef},
        str::AscString,
        sys,
        types::AscAddress,
    },
};
use std::{
    convert::Infallible,
    fmt::{self, Debug, Display, Formatter},
    str::FromStr,
};

/// An Ethereum address.
#[derive(Clone, Copy, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Address(pub [u8; 20]);

impl Address {
    /// Returns an address from its raw byte representation.
    pub(crate) fn from_raw(bytes: &AscRef<AscAddress>) -> Self {
        Self(bytes.as_slice().try_into().unwrap())
    }

    /// Returns the address as an AssemblyScript value.
    pub(crate) fn to_raw(self) -> AscBox<AscAddress> {
        AscAddress::from_bytes(&self.0)
    }

    /// Returns a new address from its string reprensentation.
    pub fn parse(s: impl AsRef<str>) -> Self {
        let str = AscString::new(s.as_ref());
        let bytes = unsafe { &*sys::type_conversion__string_to_h160(str.as_ptr()) };
        Self::from_raw(bytes)
    }

    /// Creates an address from a slice of bytes.
    ///
    /// # Panics
    ///
    /// Panics if the slice is not exactly 20-bytes long.
    pub fn from_slice(bytes: &[u8]) -> Self {
        let mut result = Self::default();
        result.0.copy_from_slice(bytes);
        result
    }
}

impl Debug for Address {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        f.debug_tuple("Address")
            .field(&format_args!("{self}"))
            .finish()
    }
}

impl Display for Address {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        let hex = conv::hex(self.0.as_slice());
        f.write_str(&hex)
    }
}

impl FromStr for Address {
    type Err = Infallible;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Self::parse(s))
    }
}