jam_std_common/
keyset.rs

1use jam_types::FixedVec;
2use scale::{ConstEncodedLen, Decode, Encode, MaxEncodedLen};
3
4use crate::{bandersnatch, ed25519, PeerAddr, PeerDetails, PeerId};
5
6#[derive(Copy, Clone, Eq, PartialEq, Encode, Decode, MaxEncodedLen)]
7pub struct ValidatorMetadata {
8	pub addr: PeerAddr,
9	pub rest1: [u8; 14],
10	pub rest2: [u32; 24],
11}
12impl ConstEncodedLen for ValidatorMetadata {}
13
14impl Default for ValidatorMetadata {
15	fn default() -> Self {
16		Self {
17			addr: PeerAddr { ip: Default::default(), port: 0 },
18			rest1: Default::default(),
19			rest2: Default::default(),
20		}
21	}
22}
23
24impl std::fmt::Debug for ValidatorMetadata {
25	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26		write!(f, "{}", self.addr)
27	}
28}
29
30/// BLS Placeholder
31pub mod bls {
32	use scale::{Decode, Encode, MaxEncodedLen};
33
34	pub const BLS_PUBLIC_SERIALIZED_LEN: usize = 144;
35
36	#[derive(Copy, Clone, Encode, Decode, Debug, Eq, PartialEq, MaxEncodedLen)]
37	pub struct Public([u8; BLS_PUBLIC_SERIALIZED_LEN]);
38
39	impl Default for Public {
40		fn default() -> Self {
41			Public([0u8; BLS_PUBLIC_SERIALIZED_LEN])
42		}
43	}
44}
45
46#[derive(Copy, Clone, Encode, Decode, Debug, Eq, PartialEq, MaxEncodedLen, Default)]
47pub struct ValKeyset {
48	pub bandersnatch: bandersnatch::Public,
49	pub ed25519: ed25519::Public,
50	pub bls: bls::Public,
51	pub metadata: ValidatorMetadata,
52}
53
54impl From<&ValKeyset> for PeerDetails {
55	fn from(keyset: &ValKeyset) -> PeerDetails {
56		PeerDetails { id: PeerId(keyset.ed25519), addr: keyset.metadata.addr }
57	}
58}
59
60pub type ValKeysets = FixedVec<ValKeyset, jam_types::ValCount>;
61pub type EdKeys = FixedVec<ed25519::Public, jam_types::ValCount>;
62
63pub struct SecretKeyset {
64	pub bandersnatch: bandersnatch::Secret,
65	pub ed25519: ed25519::Secret,
66}
67
68impl SecretKeyset {
69	pub fn public(&self, metadata: ValidatorMetadata) -> ValKeyset {
70		ValKeyset {
71			ed25519: self.ed25519.public(),
72			bandersnatch: self.bandersnatch.public(),
73			bls: bls::Public::default(),
74			metadata,
75		}
76	}
77}