hyli_model/node/
crypto.rs1use blst::min_pk::Signature as BlstSignature;
2use serde::{Deserialize, Serialize};
3use std::fmt::{self, Display};
4
5use crate::{staking::*, *};
6
7#[derive(
8 Serialize,
9 Deserialize,
10 Clone,
11 borsh::BorshSerialize,
12 borsh::BorshDeserialize,
13 PartialEq,
14 Eq,
15 Hash,
16 Ord,
17 PartialOrd,
18)]
19pub struct Signed<T: borsh::BorshSerialize, V: borsh::BorshSerialize> {
20 pub msg: T,
21 pub signature: V,
22}
23
24#[derive(
25 Serialize,
26 Deserialize,
27 Clone,
28 borsh::BorshSerialize,
29 borsh::BorshDeserialize,
30 Default,
31 PartialEq,
32 Eq,
33 Hash,
34 Ord,
35 PartialOrd,
36)]
37pub struct Signature(pub Vec<u8>);
38
39#[derive(
40 Debug,
41 Serialize,
42 Deserialize,
43 Clone,
44 borsh::BorshSerialize,
45 borsh::BorshDeserialize,
46 PartialEq,
47 Eq,
48 Hash,
49 Ord,
50 PartialOrd,
51)]
52pub struct ValidatorSignature {
53 pub signature: Signature,
54 pub validator: ValidatorPublicKey,
55}
56pub type SignedByValidator<T> = Signed<T, ValidatorSignature>;
57
58#[derive(
59 Debug,
60 Default,
61 Serialize,
62 Deserialize,
63 Clone,
64 borsh::BorshSerialize,
65 borsh::BorshDeserialize,
66 PartialEq,
67 Eq,
68 Hash,
69 Ord,
70 PartialOrd,
71)]
72pub struct AggregateSignature {
73 pub signature: Signature,
74 pub validators: Vec<ValidatorPublicKey>,
75}
76
77impl From<BlstSignature> for Signature {
78 fn from(sig: BlstSignature) -> Self {
79 Signature(sig.compress().as_slice().to_vec())
80 }
81}
82
83impl fmt::Debug for Signature {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 f.debug_tuple("Signature")
86 .field(&hex::encode(&self.0))
87 .finish()
88 }
89}
90
91impl fmt::Display for Signature {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 write!(
94 f,
95 "{}",
96 &hex::encode(self.0.get(..HASH_DISPLAY_SIZE).unwrap_or(&self.0))
97 )
98 }
99}
100
101impl<T: Display + borsh::BorshSerialize> Display for SignedByValidator<T> {
102 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103 _ = write!(f, " --> from validator {}", self.signature.validator);
104 write!(f, "")
105 }
106}
107
108impl<T: std::fmt::Debug + borsh::BorshSerialize> std::fmt::Debug for SignedByValidator<T> {
109 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
110 f.debug_struct("Signed")
111 .field("msg", &self.msg)
112 .field("validator", &format!("{}", self.signature.validator))
113 .field("sig", &{
114 format!("{}...", {
115 let mut hash = hex::encode(&self.signature.signature.0);
116 hash.truncate(4);
117 hash
118 })
119 })
120 .finish()
121 }
122}