1use codec::{ConstEncodedLen, Decode, Encode, MaxEncodedLen};
2use jam_types::FixedVec;
3
4use crate::{bandersnatch, ed25519, hash_raw_concat, 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
30pub mod bls {
32 use codec::{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 from_seed(seed: &[u8; 32]) -> Self {
70 Self {
71 bandersnatch: bandersnatch::Secret::from_seed(hash_raw_concat([
72 b"jam_val_key_bandersnatch".as_slice(),
73 seed.as_slice(),
74 ])),
75 ed25519: ed25519::Secret::from_seed(hash_raw_concat([
76 b"jam_val_key_ed25519".as_slice(),
77 seed.as_slice(),
78 ])),
79 }
80 }
81
82 pub fn trivial(id: u32) -> Self {
83 [id; 8].using_encoded(|data| {
84 Self::from_seed(data.try_into().expect("data is always 32 bytes"))
85 })
86 }
87
88 pub fn public(&self, metadata: ValidatorMetadata) -> ValKeyset {
89 ValKeyset {
90 ed25519: self.ed25519.public(),
91 bandersnatch: self.bandersnatch.public(),
92 bls: bls::Public::default(),
93 metadata,
94 }
95 }
96}