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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use fuel_types::{Address, ContractId};
use fuels_macros::{Parameterize, Tokenizable, TryFrom};
use serde::{Deserialize, Serialize};

use crate::types::bech32::{Bech32Address, Bech32ContractId};

#[derive(
    Debug,
    Copy,
    Clone,
    PartialEq,
    Eq,
    Hash,
    Parameterize,
    Tokenizable,
    TryFrom,
    Serialize,
    Deserialize,
)]
#[FuelsCorePath = "crate"]
#[FuelsTypesPath = "crate::types"]
pub enum Identity {
    Address(Address),
    ContractId(ContractId),
}

impl Default for Identity {
    fn default() -> Self {
        Self::Address(Address::default())
    }
}

impl AsRef<[u8]> for Identity {
    fn as_ref(&self) -> &[u8] {
        match self {
            Identity::Address(address) => address.as_ref(),
            Identity::ContractId(contract_id) => contract_id.as_ref(),
        }
    }
}

impl From<&Address> for Identity {
    fn from(address: &Address) -> Self {
        Self::Address(*address)
    }
}
impl From<Address> for Identity {
    fn from(address: Address) -> Self {
        Self::Address(address)
    }
}

impl From<&ContractId> for Identity {
    fn from(contract_id: &ContractId) -> Self {
        Self::ContractId(*contract_id)
    }
}
impl From<ContractId> for Identity {
    fn from(contract_id: ContractId) -> Self {
        Self::ContractId(contract_id)
    }
}

impl From<&Bech32Address> for Identity {
    fn from(data: &Bech32Address) -> Self {
        Self::Address(data.into())
    }
}
impl From<Bech32Address> for Identity {
    fn from(data: Bech32Address) -> Self {
        Self::Address(data.into())
    }
}

impl From<&Bech32ContractId> for Identity {
    fn from(data: &Bech32ContractId) -> Self {
        Self::ContractId(data.into())
    }
}
impl From<Bech32ContractId> for Identity {
    fn from(data: Bech32ContractId) -> Self {
        Self::ContractId(data.into())
    }
}

#[cfg(test)]
mod test {
    use std::str::FromStr;

    use super::*;

    #[test]
    fn test_bech32() {
        let b32_str = "fuel1dved7k25uxadatl7l5kql309jnw07dcn4t3a6x9hm9nxyjcpqqns50p7n2";
        let bech32_contract_id = Bech32ContractId::from_str(b32_str).unwrap();

        let bech32_contract_id_borrowed = &bech32_contract_id.clone();
        let identity: Identity = bech32_contract_id_borrowed.into();
        assert_eq!(
            identity,
            Identity::ContractId(bech32_contract_id.clone().into())
        );

        let identity: Identity = bech32_contract_id.clone().into();
        assert_eq!(identity, Identity::ContractId(bech32_contract_id.into()));

        let bech32_address = Bech32Address::from_str(b32_str).unwrap();

        let bech32_address_borrowed = &bech32_address.clone();
        let identity: Identity = bech32_address_borrowed.into();
        assert_eq!(identity, Identity::Address(bech32_address.clone().into()));

        let identity: Identity = bech32_address.clone().into();
        assert_eq!(identity, Identity::Address(bech32_address.clone().into()));
    }
}