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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
use alloc::boxed::Box;
use alloc::string::String;
use alloc::string::ToString;
use core::fmt;
use k256::ecdsa::recoverable;
use k256::ecdsa::signature::Signature as SignatureTrait;
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use sha3::{Digest, Keccak256};
use umbral_pre::{PublicKey, SerializableToArray, Signature, Signer};
use crate::address::Address;
use crate::arrays_as_bytes::{self, DeserializeAsBytes, SerializeAsBytes};
use crate::fleet_state::FleetStateChecksum;
use crate::versioning::{
messagepack_deserialize, messagepack_serialize, ProtocolObject, ProtocolObjectInner,
};
use crate::VerificationError;
impl SerializeAsBytes for recoverable::Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(self.as_ref())
}
}
impl<'de> DeserializeAsBytes<'de> for recoverable::Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct BytesVisitor;
impl<'de> de::Visitor<'de> for BytesVisitor {
type Value = recoverable::Signature;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Recoverable signature bytes")
}
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: de::Error,
{
recoverable::Signature::from_bytes(v).map_err(de::Error::custom)
}
}
deserializer.deserialize_bytes(BytesVisitor)
}
}
pub enum AddressDerivationError {
NoSignatureInPayload,
RecoveryFailed(signature::Error),
}
impl fmt::Display for AddressDerivationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::NoSignatureInPayload => write!(f, "Signature is missing from the payload"),
Self::RecoveryFailed(err) => write!(
f,
"Failed to recover the public key from the signature: {}",
err
),
}
}
}
fn encode_defunct(message: &[u8]) -> Keccak256 {
Keccak256::new()
.chain(b"\x19")
.chain(b"E")
.chain(b"thereum Signed Message:\n")
.chain(message.len().to_string().as_bytes())
.chain(message)
}
pub const RECOVERABLE_SIGNATURE_SIZE: usize = recoverable::SIZE;
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct NodeMetadataPayload {
pub staking_provider_address: Address,
pub domain: String,
pub timestamp_epoch: u32,
pub verifying_key: PublicKey,
pub encrypting_key: PublicKey,
#[serde(with = "serde_bytes")]
pub certificate_der: Box<[u8]>,
pub host: String,
pub port: u16,
#[serde(with = "arrays_as_bytes")]
pub operator_signature: Option<recoverable::Signature>,
}
impl NodeMetadataPayload {
fn to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(self)
}
pub fn derive_operator_address(&self) -> Result<Address, AddressDerivationError> {
let signature = self
.operator_signature
.ok_or(AddressDerivationError::NoSignatureInPayload)?;
let message = encode_defunct(&self.verifying_key.to_array());
let key = signature
.recover_verify_key_from_digest(message)
.map_err(AddressDerivationError::RecoveryFailed)?;
Ok(Address::from_k256_public_key(&key))
}
}
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct NodeMetadata {
signature: Signature,
pub payload: NodeMetadataPayload,
}
impl NodeMetadata {
pub fn new(signer: &Signer, payload: &NodeMetadataPayload) -> Self {
Self {
signature: signer.sign(&payload.to_bytes()),
payload: payload.clone(),
}
}
pub fn verify(&self) -> bool {
self.signature
.verify(&self.payload.verifying_key, &self.payload.to_bytes())
}
}
impl<'a> ProtocolObjectInner<'a> for NodeMetadata {
fn brand() -> [u8; 4] {
*b"NdMd"
}
fn version() -> (u16, u16) {
(1, 0)
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for NodeMetadata {}
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct MetadataRequest {
pub fleet_state_checksum: FleetStateChecksum,
pub announce_nodes: Box<[NodeMetadata]>,
}
impl MetadataRequest {
pub fn new(fleet_state_checksum: &FleetStateChecksum, announce_nodes: &[NodeMetadata]) -> Self {
Self {
fleet_state_checksum: *fleet_state_checksum,
announce_nodes: announce_nodes.to_vec().into_boxed_slice(),
}
}
}
impl<'a> ProtocolObjectInner<'a> for MetadataRequest {
fn brand() -> [u8; 4] {
*b"MdRq"
}
fn version() -> (u16, u16) {
(1, 0)
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for MetadataRequest {}
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct MetadataResponsePayload {
pub timestamp_epoch: u32,
pub announce_nodes: Box<[NodeMetadata]>,
}
impl MetadataResponsePayload {
pub fn new(timestamp_epoch: u32, announce_nodes: &[NodeMetadata]) -> Self {
Self {
timestamp_epoch,
announce_nodes: announce_nodes.to_vec().into_boxed_slice(),
}
}
fn to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(self)
}
}
#[derive(PartialEq, Debug, Serialize, Deserialize, Clone)]
pub struct MetadataResponse {
signature: Signature,
payload: MetadataResponsePayload,
}
impl MetadataResponse {
pub fn new(signer: &Signer, payload: &MetadataResponsePayload) -> Self {
Self {
signature: signer.sign(&payload.to_bytes()),
payload: payload.clone(),
}
}
pub fn verify(
self,
verifying_pk: &PublicKey,
) -> Result<MetadataResponsePayload, VerificationError> {
if self
.signature
.verify(verifying_pk, &self.payload.to_bytes())
{
Ok(self.payload)
} else {
Err(VerificationError)
}
}
}
impl<'a> ProtocolObjectInner<'a> for MetadataResponse {
fn brand() -> [u8; 4] {
*b"MdRs"
}
fn version() -> (u16, u16) {
(1, 0)
}
fn unversioned_to_bytes(&self) -> Box<[u8]> {
messagepack_serialize(&self)
}
fn unversioned_from_bytes(minor_version: u16, bytes: &[u8]) -> Option<Result<Self, String>> {
if minor_version == 0 {
Some(messagepack_deserialize(bytes))
} else {
None
}
}
}
impl<'a> ProtocolObject<'a> for MetadataResponse {}