1
//! See [`PublicKey`].
2

            
3
use arrayvec::ArrayVec;
4
use serde::{Deserialize, Serialize};
5
use serde_big_array::BigArray;
6

            
7
use crate::{Config, Group};
8

            
9
/// Public key, used to verify the server by the client. See
10
/// [`ClientRegistration::register()`](crate::ClientRegistration::register).
11
2305
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
12
pub struct PublicKey {
13
	/// [`Config`] this [`PublicKey`] was created with.
14
	pub(crate) config: Config,
15
	/// Public key bytes.
16
	#[serde(with = "BigArray")]
17
	pub(crate) key: [u8; 33],
18
}
19

            
20
impl PublicKey {
21
	/// Create a [`PublicKey`] from a `[u8; 33]`.
22
837
	pub(crate) const fn new(config: Config, key: [u8; 33]) -> Self {
23
837
		Self { config, key }
24
837
	}
25

            
26
	/// Returns the [`Config`] associated with this [`PublicKey`].
27
	#[must_use]
28
1
	pub const fn config(self) -> Config {
29
1
		self.config
30
1
	}
31

            
32
	/// Returns an [`ArrayVec`] of this key.
33
	#[must_use]
34
576
	pub fn to_bytes(&self) -> ArrayVec<u8, 33> {
35
576
		let mut bytes = ArrayVec::from(self.key);
36
576

            
37
576
		match self.config.group() {
38
288
			Group::Ristretto255 => bytes.truncate(32),
39
			#[cfg(feature = "p256")]
40
288
			Group::P256 => bytes.truncate(33),
41
		}
42

            
43
576
		bytes
44
576
	}
45
}