zink/primitives/
address.rs

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
use crate::{
    ffi,
    primitives::Bytes20,
    storage::{StorageValue, TransientStorageValue},
    Asm,
};

/// Account address
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Address(Bytes20);

impl Address {
    /// Returns empty address
    pub const fn empty() -> Self {
        Address(Bytes20::empty())
    }

    /// Returns empty address
    #[inline(always)]
    pub fn caller() -> Self {
        unsafe { ffi::evm::caller() }
    }

    /// if self equal to another
    ///
    /// NOTE: not using core::cmp because it uses registers in wasm
    #[allow(clippy::should_implement_trait)]
    #[inline(always)]
    pub fn eq(self, other: Self) -> bool {
        self.0.eq(other.0)
    }
}

impl Asm for Address {
    fn push(self) {
        unsafe { ffi::bytes::push_bytes20(self.0) }
    }

    #[cfg(not(target_family = "wasm"))]
    fn bytes32(&self) -> [u8; 32] {
        self.0.bytes32()
    }
}

impl StorageValue for Address {
    fn sload() -> Self {
        Self(unsafe { ffi::bytes::sload_bytes20() })
    }
}

impl From<Bytes20> for Address {
    fn from(value: Bytes20) -> Self {
        Address(value)
    }
}

#[cfg(not(target_family = "wasm"))]
impl From<[u8; 20]> for Address {
    fn from(value: [u8; 20]) -> Self {
        Address(Bytes20(value))
    }
}

impl TransientStorageValue for Address {
    fn tload() -> Self {
        Address(unsafe { ffi::bytes::tload_bytes20() })
    }
}