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
use std::fmt::Display;

use filament_crypto::VerificationKey;
use filament_encoding::{FromBech32, ToBech32};
use serde::{Deserialize, Serialize};

const BECH32_ADDRESS_PREFIX: &str = "fltaddr";

/// A unique identifier for a state entry.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
pub struct Address(VerificationKey);

impl Display for Address {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for byte in &self.0.to_bytes() {
            write!(f, "{byte:02x}")?;
        }
        Ok(())
    }
}

impl AsRef<[u8]> for Address {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl From<VerificationKey> for Address {
    fn from(vk: VerificationKey) -> Self {
        Self(vk)
    }
}

impl TryFrom<&[u8]> for Address {
    type Error = eyre::Report;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        VerificationKey::try_from(bytes)
            .map(Address)
            .map_err(|e| eyre::eyre!(e))
    }
}

impl ToBech32 for Address {
    const PREFIX: &'static str = BECH32_ADDRESS_PREFIX;
}

impl FromBech32 for Address {
    const PREFIX: &'static str = BECH32_ADDRESS_PREFIX;
}

#[cfg(test)]
mod tests {
    use filament_crypto::{SigningKey, VerificationKey};
    use pretty_assertions::assert_eq;
    use rand::thread_rng;

    use super::*;

    #[test]
    fn address_bech32_roundtrip() {
        let address = Address(VerificationKey::from(&SigningKey::new(thread_rng())));
        let encoded = address.to_bech32().unwrap();
        let decoded = Address::from_bech32(encoded).unwrap();
        assert_eq!(address, decoded);
    }
}