p2panda_rs/entry/
signature.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3use std::fmt;
4use std::hash::Hash as StdHash;
5
6use bamboo_rs_core_ed25519_yasmf::Signature as BambooSignature;
7use ed25519_dalek::Signature as Ed25519Signature;
8
9/// Ed25519 signature.
10#[derive(Copy, Clone, Debug)]
11pub struct Signature(Ed25519Signature);
12
13impl Signature {
14    /// Returns signature as bytes.
15    pub fn into_bytes(&self) -> Vec<u8> {
16        self.0.to_bytes().to_vec()
17    }
18}
19
20impl From<BambooSignature<&[u8]>> for Signature {
21    fn from(signature: BambooSignature<&[u8]>) -> Self {
22        // Unwrap here as the signature from bamboo should already be checked
23        Signature(Ed25519Signature::from_bytes(signature.0).unwrap())
24    }
25}
26
27impl From<Ed25519Signature> for Signature {
28    fn from(signature: Ed25519Signature) -> Self {
29        Self(signature)
30    }
31}
32
33impl From<&Signature> for Ed25519Signature {
34    fn from(signature: &Signature) -> Self {
35        signature.0.to_owned()
36    }
37}
38
39impl StdHash for Signature {
40    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
41        self.into_bytes().hash(state)
42    }
43}
44
45impl PartialEq for Signature {
46    fn eq(&self, other: &Self) -> bool {
47        self.0 == other.0
48    }
49}
50
51impl Eq for Signature {}
52
53impl fmt::Display for Signature {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        write!(f, "{}", hex::encode(self.into_bytes()))
56    }
57}
58
59#[cfg(test)]
60impl Signature {
61    pub fn from_bytes(bytes: &[u8]) -> Self {
62        Self(Ed25519Signature::from_bytes(bytes).unwrap())
63    }
64}