bee_block/signature/
mod.rs

1// Copyright 2020-2021 IOTA Stiftung
2// SPDX-License-Identifier: Apache-2.0
3
4mod ed25519;
5
6use derive_more::From;
7
8pub use self::ed25519::Ed25519Signature;
9use crate::Error;
10
11/// A `Signature` contains a signature which is used to unlock a transaction input.
12///
13/// This is defined as part of the Unspent Transaction Output (UTXO) transaction protocol.
14///
15/// RFC: <https://github.com/luca-moser/protocol-rfcs/blob/signed-tx-payload/text/0000-transaction-payload/0000-transaction-payload.md#signature-unlock-block>
16#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
17#[cfg_attr(
18    feature = "serde",
19    derive(serde::Serialize, serde::Deserialize),
20    serde(tag = "type", content = "data")
21)]
22#[packable(unpack_error = Error)]
23#[packable(tag_type = u8, with_error = Error::InvalidSignatureKind)]
24pub enum Signature {
25    /// An Ed25519 signature.
26    #[packable(tag = Ed25519Signature::KIND)]
27    Ed25519(Ed25519Signature),
28}
29
30impl Signature {
31    /// Returns the signature kind of a `Signature`.
32    pub fn kind(&self) -> u8 {
33        match self {
34            Self::Ed25519(_) => Ed25519Signature::KIND,
35        }
36    }
37}
38
39#[cfg(feature = "dto")]
40#[allow(missing_docs)]
41pub mod dto {
42    use serde::{Deserialize, Serialize};
43
44    pub use super::ed25519::dto::Ed25519SignatureDto;
45    use super::*;
46    use crate::error::dto::DtoError;
47
48    /// Describes all the different signature types.
49    #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize, From)]
50    #[serde(untagged)]
51    pub enum SignatureDto {
52        Ed25519(Ed25519SignatureDto),
53    }
54
55    impl From<&Signature> for SignatureDto {
56        fn from(value: &Signature) -> Self {
57            match value {
58                Signature::Ed25519(s) => SignatureDto::Ed25519(s.into()),
59            }
60        }
61    }
62
63    impl TryFrom<&SignatureDto> for Signature {
64        type Error = DtoError;
65
66        fn try_from(value: &SignatureDto) -> Result<Self, Self::Error> {
67            match value {
68                SignatureDto::Ed25519(s) => Ok(Signature::Ed25519(s.try_into()?)),
69            }
70        }
71    }
72}