iota_stardust_types/block/address/
ed25519.rs

1// Copyright (c) 2026 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4use core::str::FromStr;
5
6use derive_more::{AsRef, Deref, From};
7
8use crate::block::Error;
9
10/// An Ed25519 address.
11#[derive(
12    Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable,
13)]
14#[as_ref(forward)]
15pub struct Ed25519Address([u8; Self::LENGTH]);
16
17impl Ed25519Address {
18    /// The [`Address`](crate::block::address::Address) kind of an
19    /// [`Ed25519Address`].
20    pub const KIND: u8 = 0;
21    /// The length of an [`Ed25519Address`].
22    pub const LENGTH: usize = 32;
23
24    /// Creates a new [`Ed25519Address`].
25    #[inline(always)]
26    pub fn new(address: [u8; Self::LENGTH]) -> Self {
27        Self::from(address)
28    }
29}
30
31#[cfg(feature = "serde")]
32string_serde_impl!(Ed25519Address);
33
34impl FromStr for Ed25519Address {
35    type Err = Error;
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        Ok(Self::new(prefix_hex::decode(s).map_err(Error::Hex)?))
39    }
40}
41
42impl core::fmt::Display for Ed25519Address {
43    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
44        write!(f, "{}", prefix_hex::encode(self.0))
45    }
46}
47
48impl core::fmt::Debug for Ed25519Address {
49    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
50        write!(f, "Ed25519Address({self})")
51    }
52}