1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use crate::{account, vote, PublicKey};
#[cfg(feature = "serde")]
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
#[cfg(feature = "rpc")]
use subtle_encoding::base64;
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Info {
pub address: account::Id,
pub pub_key: PublicKey,
pub voting_power: vote::Power,
pub proposer_priority: Option<ProposerPriority>,
}
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
pub struct ProposerPriority(i64);
impl ProposerPriority {
pub fn value(self) -> i64 {
self.0
}
}
impl From<ProposerPriority> for i64 {
fn from(priority: ProposerPriority) -> i64 {
priority.value()
}
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for ProposerPriority {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(ProposerPriority(
String::deserialize(deserializer)?
.parse()
.map_err(|e| D::Error::custom(format!("{}", e)))?,
))
}
}
#[cfg(feature = "serde")]
impl Serialize for ProposerPriority {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.0.to_string().serialize(serializer)
}
}
#[cfg(feature = "rpc")]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Update {
#[serde(deserialize_with = "deserialize_public_key")]
pub pub_key: PublicKey,
pub power: vote::Power,
}
#[cfg(feature = "rpc")]
#[derive(Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
enum PK {
#[serde(rename = "ed25519")]
Ed25519(String),
}
#[cfg(feature = "rpc")]
fn deserialize_public_key<'de, D>(deserializer: D) -> Result<PublicKey, D::Error>
where
D: Deserializer<'de>,
{
match &PK::deserialize(deserializer)? {
PK::Ed25519(base64_value) => {
let bytes =
base64::decode(base64_value).map_err(|e| D::Error::custom(format!("{}", e)))?;
PublicKey::from_raw_ed25519(&bytes)
.ok_or_else(|| D::Error::custom("error parsing Ed25519 key"))
}
}
}