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)]
16pub struct AccountPublicKey(NonZeroCurvePoint);
17
18impl From<PubKey> for AccountPublicKey {
19    fn from(value: PubKey) -> Self {
20        value.into_compressed().into()
21    }
22}
23
24impl From<CompressedPubKey> for AccountPublicKey {
25    fn from(value: CompressedPubKey) -> Self {
26        Self(
27            NonZeroCurvePointUncompressedStableV1 {
28                x: value.x.into(),
29                is_odd: value.is_odd,
30            }
31            .into(),
32        )
33    }
34}
35
36impl TryFrom<AccountPublicKey> for CompressedPubKey {
37    type Error = ();
38
39    fn try_from(value: AccountPublicKey) -> Result<Self, Self::Error> {
40        Ok(Self {
41            is_odd: value.0.is_odd,
42            x: value.0.into_inner().x.try_into().map_err(|_| ())?,
43        })
44    }
45}
46
47impl From<NonZeroCurvePoint> for AccountPublicKey {
48    fn from(value: NonZeroCurvePoint) -> Self {
49        Self(value)
50    }
51}
52
53impl From<AccountPublicKey> for NonZeroCurvePoint {
54    fn from(value: AccountPublicKey) -> Self {
55        value.0
56    }
57}
58
59impl FromStr for AccountPublicKey {
60    type Err = FromBase58CheckError;
61
62    fn from_str(s: &str) -> Result<Self, Self::Err> {
63        Ok(Self(s.parse()?))
64    }
65}
66
67impl AsRef<NonZeroCurvePoint> for AccountPublicKey {
68    fn as_ref(&self) -> &NonZeroCurvePoint {
69        &self.0
70    }
71}
72
73impl fmt::Display for AccountPublicKey {
74    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
75        let p2p_key = NonZeroCurvePoint::from(self.clone());
76        write!(f, "{p2p_key}")
77    }
78}
79
80// for a simple hashmap or btree map use the hash of the string representation
81impl std::hash::Hash for AccountPublicKey {
82    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
83        self.to_string().hash(state);
84    }
85}