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
use crate::error::{Error, Result};
use crate::model::ByteString;
use crate::util::Base58;
use serde_json::Value;
use std::fmt;

#[derive(Clone, Eq, PartialEq, Hash)]
pub struct AssetId {
    bytes: Vec<u8>,
}

impl fmt::Debug for AssetId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "AssetId {{ {} }}", self.encoded())
    }
}

impl AssetId {
    pub fn from_string(asset_id: &str) -> Result<AssetId> {
        Ok(Self::from_bytes(Base58::decode(asset_id)?))
    }

    pub fn from_bytes(bytes: Vec<u8>) -> AssetId {
        AssetId { bytes }
    }
}

impl ByteString for AssetId {
    fn bytes(&self) -> Vec<u8> {
        self.bytes.clone()
    }

    fn encoded(&self) -> String {
        Base58::encode(&self.bytes, false)
    }

    fn encoded_with_prefix(&self) -> String {
        Base58::encode(&self.bytes, true)
    }
}

impl TryFrom<&str> for AssetId {
    type Error = Error;

    fn try_from(value: &str) -> Result<Self> {
        AssetId::from_string(value)
    }
}

impl From<AssetId> for Value {
    fn from(value: AssetId) -> Self {
        Value::String(value.encoded())
    }
}

#[cfg(test)]
mod tests {
    use crate::error::Result;
    use crate::model::{AssetId, ByteString};
    use serde_json::Value;

    #[test]
    fn test_byte_string_for_asset_id() -> Result<()> {
        let asset_id: AssetId = "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6".try_into()?;
        let expected_bytes = vec![
            112, 241, 63, 143, 135, 230, 99, 221, 151, 63, 90, 167, 36, 212, 184, 216, 209, 139,
            159, 7, 132, 61, 191, 30, 148, 44, 33, 177, 250, 231, 193, 117,
        ];
        assert_eq!(expected_bytes, asset_id.bytes());
        assert_eq!(
            "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6",
            asset_id.encoded()
        );
        assert_eq!(
            "base58:8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6",
            asset_id.encoded_with_prefix()
        );
        Ok(())
    }

    #[test]
    fn test_asset_id_to_json() -> Result<()> {
        let asset_id: AssetId = "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6".try_into()?;
        let json_value: Value = asset_id.into();
        assert_eq!(
            "8bt2MZjuUCJPmfucPfaZPTXqrxmoCHCC8gVnbjZ7bhH6",
            json_value.as_str().unwrap_or("")
        );
        Ok(())
    }
}