casper_types/transaction/
approval.rs

1use alloc::vec::Vec;
2use core::fmt::{self, Display, Formatter};
3
4#[cfg(feature = "datasize")]
5use datasize::DataSize;
6#[cfg(feature = "json-schema")]
7use schemars::JsonSchema;
8use serde::{Deserialize, Serialize};
9
10#[cfg(any(all(feature = "std", feature = "testing"), test))]
11use crate::testing::TestRng;
12use crate::{
13    bytesrepr::{self, FromBytes, ToBytes},
14    crypto, PublicKey, SecretKey, Signature,
15};
16
17use super::TransactionHash;
18
19/// A struct containing a signature of a transaction hash and the public key of the signer.
20#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize, Debug)]
21#[cfg_attr(feature = "datasize", derive(DataSize))]
22#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
23#[serde(deny_unknown_fields)]
24pub struct Approval {
25    signer: PublicKey,
26    signature: Signature,
27}
28
29impl Approval {
30    /// Creates an approval by signing the given transaction hash using the given secret key.
31    pub fn create(hash: &TransactionHash, secret_key: &SecretKey) -> Self {
32        let signer = PublicKey::from(secret_key);
33        let signature = crypto::sign(hash, secret_key, &signer);
34        Self { signer, signature }
35    }
36
37    /// Returns a new approval.
38    pub fn new(signer: PublicKey, signature: Signature) -> Self {
39        Self { signer, signature }
40    }
41
42    /// Returns the public key of the approval's signer.
43    pub fn signer(&self) -> &PublicKey {
44        &self.signer
45    }
46
47    /// Returns the approval signature.
48    pub fn signature(&self) -> &Signature {
49        &self.signature
50    }
51
52    /// Returns a random `Approval`.
53    #[cfg(any(all(feature = "std", feature = "testing"), test))]
54    pub fn random(rng: &mut TestRng) -> Self {
55        let hash = TransactionHash::random(rng);
56        let secret_key = SecretKey::random(rng);
57        Approval::create(&hash, &secret_key)
58    }
59}
60
61impl Display for Approval {
62    fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
63        write!(formatter, "approval({})", self.signer)
64    }
65}
66
67impl ToBytes for Approval {
68    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
69        self.signer.write_bytes(writer)?;
70        self.signature.write_bytes(writer)
71    }
72
73    fn to_bytes(&self) -> Result<Vec<u8>, bytesrepr::Error> {
74        let mut buffer = bytesrepr::allocate_buffer(self)?;
75        self.write_bytes(&mut buffer)?;
76        Ok(buffer)
77    }
78
79    fn serialized_length(&self) -> usize {
80        self.signer.serialized_length() + self.signature.serialized_length()
81    }
82}
83
84impl FromBytes for Approval {
85    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), bytesrepr::Error> {
86        let (signer, remainder) = PublicKey::from_bytes(bytes)?;
87        let (signature, remainder) = Signature::from_bytes(remainder)?;
88        let approval = Approval { signer, signature };
89        Ok((approval, remainder))
90    }
91}
92
93#[cfg(test)]
94mod tests {
95    use super::*;
96
97    #[test]
98    fn bytesrepr_roundtrip() {
99        let rng = &mut TestRng::new();
100        let approval = Approval::random(rng);
101        bytesrepr::test_serialization_roundtrip(&approval);
102    }
103}