Skip to main content

mina_node_account/
public_key.rs

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