p2p/network/identify/
keys_proto.rs1#![allow(non_snake_case)]
4#![allow(non_upper_case_globals)]
5#![allow(non_camel_case_types)]
6#![allow(unused_imports)]
7#![allow(unknown_lints)]
8#![allow(clippy::all)]
9#![cfg_attr(rustfmt, rustfmt_skip)]
10
11
12use std::borrow::Cow;
13use quick_protobuf::{MessageInfo, MessageRead, MessageWrite, BytesReader, Writer, WriterBackend, Result};
14use quick_protobuf::sizeofs::*;
15use super::*;
16
17#[derive(Debug, PartialEq, Eq, Clone, Copy)]
18pub enum KeyType {
19 RSA = 0,
20 Ed25519 = 1,
21 Secp256k1 = 2,
22 ECDSA = 3,
23}
24
25impl Default for KeyType {
26 fn default() -> Self {
27 KeyType::RSA
28 }
29}
30
31impl From<i32> for KeyType {
32 fn from(i: i32) -> Self {
33 match i {
34 0 => KeyType::RSA,
35 1 => KeyType::Ed25519,
36 2 => KeyType::Secp256k1,
37 3 => KeyType::ECDSA,
38 _ => Self::default(),
39 }
40 }
41}
42
43impl<'a> From<&'a str> for KeyType {
44 fn from(s: &'a str) -> Self {
45 match s {
46 "RSA" => KeyType::RSA,
47 "Ed25519" => KeyType::Ed25519,
48 "Secp256k1" => KeyType::Secp256k1,
49 "ECDSA" => KeyType::ECDSA,
50 _ => Self::default(),
51 }
52 }
53}
54
55#[allow(clippy::derive_partial_eq_without_eq)]
56#[derive(Debug, Default, PartialEq, Clone)]
57pub struct PublicKey<'a> {
58 pub Type: keys_proto::KeyType,
59 pub Data: Cow<'a, [u8]>,
60}
61
62impl<'a> MessageRead<'a> for PublicKey<'a> {
63 fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
64 let mut msg = Self::default();
65 while !r.is_eof() {
66 match r.next_tag(bytes) {
67 Ok(8) => msg.Type = r.read_enum(bytes)?,
68 Ok(18) => msg.Data = r.read_bytes(bytes).map(Cow::Borrowed)?,
69 Ok(t) => { r.read_unknown(bytes, t)?; }
70 Err(e) => return Err(e),
71 }
72 }
73 Ok(msg)
74 }
75}
76
77impl<'a> MessageWrite for PublicKey<'a> {
78 fn get_size(&self) -> usize {
79 0
80 + 1 + sizeof_varint(*(&self.Type) as u64)
81 + 1 + sizeof_len((&self.Data).len())
82 }
83
84 fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
85 w.write_with_tag(8, |w| w.write_enum(*&self.Type as i32))?;
86 w.write_with_tag(18, |w| w.write_bytes(&**&self.Data))?;
87 Ok(())
88 }
89}
90
91#[allow(clippy::derive_partial_eq_without_eq)]
92#[derive(Debug, Default, PartialEq, Clone)]
93pub struct PrivateKey<'a> {
94 pub Type: keys_proto::KeyType,
95 pub Data: Cow<'a, [u8]>,
96}
97
98impl<'a> MessageRead<'a> for PrivateKey<'a> {
99 fn from_reader(r: &mut BytesReader, bytes: &'a [u8]) -> Result<Self> {
100 let mut msg = Self::default();
101 while !r.is_eof() {
102 match r.next_tag(bytes) {
103 Ok(8) => msg.Type = r.read_enum(bytes)?,
104 Ok(18) => msg.Data = r.read_bytes(bytes).map(Cow::Borrowed)?,
105 Ok(t) => { r.read_unknown(bytes, t)?; }
106 Err(e) => return Err(e),
107 }
108 }
109 Ok(msg)
110 }
111}
112
113impl<'a> MessageWrite for PrivateKey<'a> {
114 fn get_size(&self) -> usize {
115 0
116 + 1 + sizeof_varint(*(&self.Type) as u64)
117 + 1 + sizeof_len((&self.Data).len())
118 }
119
120 fn write_message<W: WriterBackend>(&self, w: &mut Writer<W>) -> Result<()> {
121 w.write_with_tag(8, |w| w.write_enum(*&self.Type as i32))?;
122 w.write_with_tag(18, |w| w.write_bytes(&**&self.Data))?;
123 Ok(())
124 }
125}
126