redgold_schema/
public_key.rs

1use crate::proto_serde::ProtoSerde;
2use crate::structs::{Address, ErrorInfo, PublicKey, PublicKeyType};
3use crate::{bytes_data, RgResult, SafeOption, ShortString};
4use std::fmt::{Display, Formatter};
5
6
7impl Display for PublicKey {
8    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
9        write!(f, "{}", self.hex())
10    }
11}
12
13impl PublicKey {
14    pub fn from_bytes_direct_ecdsa(bytes: Vec<u8>) -> Self {
15        Self {
16            bytes: bytes_data(bytes),
17            key_type: PublicKeyType::Secp256k1 as i32,
18            aux_data: None
19        }
20    }
21    pub fn from_bytes_direct_ed25519(bytes: Vec<u8>) -> Self {
22        Self {
23            bytes: bytes_data(bytes),
24            key_type: PublicKeyType::Ed25519 as i32,
25            aux_data: None
26        }
27    }
28    pub fn from_bytes_direct_ed25519_aux(bytes: Vec<u8>, aux: Vec<u8>) -> Self {
29        Self {
30            bytes: bytes_data(bytes),
31            key_type: PublicKeyType::Ed25519 as i32,
32            aux_data: bytes_data(aux)
33        }
34    }
35
36    pub fn raw_bytes(&self) -> Result<Vec<u8>, ErrorInfo> {
37        Ok(self.bytes.safe_get().cloned()?.value)
38    }
39
40    pub fn short_id(&self) -> String {
41        self.hex().short_string().expect("worked")
42    }
43
44    pub fn address(&self) -> Result<Address, ErrorInfo> {
45        Address::from_struct_public(self)
46    }
47
48    pub fn from_hex_direct(hex: impl Into<String>) -> RgResult<Self> {
49        let bytes = crate::from_hex(hex.into())?;
50        let key = Self::from_bytes_direct_ecdsa(bytes);
51        Ok(key)
52    }
53
54    pub fn to_hex_direct_ecdsa(&self) -> RgResult<String> {
55        self.raw_bytes().map(|b| hex::encode(b))
56    }
57
58}