redgold_schema/
proof.rs

1use itertools::Itertools;
2use crate::structs::{Address, ErrorCode as RGError, ErrorInfo, Proof};
3
4use crate::proto_serde::ProtoSerde;
5use crate::{error_message, structs, HashClear, RgResult, SafeOption};
6
7impl HashClear for Proof {
8    // TODO: Separate the hashclear method for those that don't require clears
9    fn hash_clear(&mut self) {}
10}
11
12impl Proof {
13
14    pub fn signature_hex(&self) -> RgResult<String> {
15        Ok(hex::encode(self.signature_bytes()?))
16    }
17    // pub fn public_key_bytes(&self) -> Result<Vec<u8>, ErrorInfo> {
18    //     // Ok(self.public_key.safe_get_msg("Missing public key")?.bytes()?)
19    //     Ok(self
20    //         .public_key
21    //         .as_ref()
22    //         .ok_or(error_message(RGError::MissingField, "public_key"))?
23    //         .clone()
24    //         .bytes
25    //         .as_ref()
26    //         .ok_or(error_message(
27    //             RGError::MissingField,
28    //             "public_key bytes data",
29    //         ))?
30    //         .value
31    //         .clone())
32    // }
33    pub fn public_key_direct_bytes(&self) -> Result<Vec<u8>, ErrorInfo> {
34        // Ok(self.public_key.safe_get_msg("Missing public key")?.bytes()?)
35        Ok(self
36            .public_key
37            .as_ref()
38            .ok_or(error_message(RGError::MissingField, "public_key"))?
39            .clone()
40            .bytes
41            .as_ref()
42            .ok_or(error_message(
43                RGError::MissingField,
44                "public_key bytes data",
45            ))?
46            .value
47            .clone())
48    }
49    pub fn public_key_proto_bytes(&self) -> Result<Vec<u8>, ErrorInfo> {
50        Ok(self.public_key.safe_get_msg("Missing public key")?.vec())
51    }
52
53    pub fn signature_bytes(&self) -> Result<Vec<u8>, ErrorInfo> {
54        Ok(self
55            .signature
56            .as_ref()
57            .ok_or(error_message(RGError::MissingField, "signature"))?
58            .clone()
59            .bytes
60            .as_ref()
61            .ok_or(error_message(RGError::MissingField, "signature bytes data"))?
62            .value
63            .clone())
64    }
65
66
67    pub fn from(public_key: structs::PublicKey, signature: structs::Signature) -> Self {
68        Self {
69            signature: Some(signature),
70            public_key: Some(public_key),
71        }
72    }
73}