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
use crate::error::Result;
use crate::model::ByteString;
use crate::util::Base58;
use std::fmt;

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

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

impl Id {
    pub fn from_string(id: &str) -> Result<Id> {
        Ok(Id {
            bytes: Base58::decode(id)?,
        })
    }

    pub fn from_bytes(id: &[u8]) -> Id {
        Id { bytes: id.into() }
    }
}

impl ByteString for Id {
    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)
    }
}