openmina_node_account/
public_key.rs1use std::{fmt, str::FromStr};
2
3use mina_p2p_messages::{
4 b58::FromBase58CheckError,
5 binprot::{
6 self,
7 macros::{BinProtRead, BinProtWrite},
8 },
9 v2::{NonZeroCurvePoint, NonZeroCurvePointUncompressedStableV1},
10};
11use serde::{Deserialize, Serialize};
12
13use mina_signer::{CompressedPubKey, PubKey};
14
15#[derive(
16 BinProtWrite, BinProtRead, Serialize, Deserialize, Debug, Ord, PartialOrd, Eq, PartialEq, Clone,
17)]
18pub struct AccountPublicKey(NonZeroCurvePoint);
19
20impl From<PubKey> for AccountPublicKey {
21 fn from(value: PubKey) -> Self {
22 value.into_compressed().into()
23 }
24}
25
26impl From<CompressedPubKey> for AccountPublicKey {
27 fn from(value: CompressedPubKey) -> Self {
28 Self(
29 NonZeroCurvePointUncompressedStableV1 {
30 x: value.x.into(),
31 is_odd: value.is_odd,
32 }
33 .into(),
34 )
35 }
36}
37
38impl TryFrom<AccountPublicKey> for CompressedPubKey {
39 type Error = ();
40
41 fn try_from(value: AccountPublicKey) -> Result<Self, Self::Error> {
42 Ok(Self {
43 is_odd: value.0.is_odd,
44 x: value.0.into_inner().x.try_into().map_err(|_| ())?,
45 })
46 }
47}
48
49impl From<NonZeroCurvePoint> for AccountPublicKey {
50 fn from(value: NonZeroCurvePoint) -> Self {
51 Self(value)
52 }
53}
54
55impl From<AccountPublicKey> for NonZeroCurvePoint {
56 fn from(value: AccountPublicKey) -> Self {
57 value.0
58 }
59}
60
61impl FromStr for AccountPublicKey {
62 type Err = FromBase58CheckError;
63
64 fn from_str(s: &str) -> Result<Self, Self::Err> {
65 Ok(Self(s.parse()?))
66 }
67}
68
69impl AsRef<NonZeroCurvePoint> for AccountPublicKey {
70 fn as_ref(&self) -> &NonZeroCurvePoint {
71 &self.0
72 }
73}
74
75impl fmt::Display for AccountPublicKey {
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 let p2p_key = NonZeroCurvePoint::from(self.clone());
78 write!(f, "{p2p_key}")
79 }
80}
81
82impl std::hash::Hash for AccountPublicKey {
84 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
85 self.to_string().hash(state);
86 }
87}