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
use std::borrow::Borrow;
use std::convert::TryFrom;
use everscale_crypto::{ed25519, tl};
use rand::Rng;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct AdnlNodeIdFull(ed25519::PublicKey);
impl AdnlNodeIdFull {
pub const fn new(public_key: ed25519::PublicKey) -> Self {
Self(public_key)
}
#[inline(always)]
pub const fn public_key(&self) -> &ed25519::PublicKey {
&self.0
}
#[inline(always)]
pub fn as_tl(&self) -> tl::PublicKey {
self.0.as_tl()
}
pub fn verify<T: tl_proto::TlWrite<Repr = tl_proto::Boxed>>(
&self,
message: T,
other_signature: &[u8],
) -> Result<(), AdnlNodeIdFullError> {
let other_signature = <[u8; 64]>::try_from(other_signature)
.map_err(|_| AdnlNodeIdFullError::InvalidSignature)?;
if self.0.verify(message, &other_signature) {
Ok(())
} else {
Err(AdnlNodeIdFullError::InvalidSignature)
}
}
pub fn compute_short_id(&self) -> AdnlNodeIdShort {
AdnlNodeIdShort::new(tl_proto::hash(self.0.as_tl()))
}
}
impl From<ed25519::PublicKey> for AdnlNodeIdFull {
fn from(key: ed25519::PublicKey) -> Self {
Self::new(key)
}
}
impl<'a> TryFrom<tl::PublicKey<'a>> for AdnlNodeIdFull {
type Error = AdnlNodeIdFullError;
fn try_from(value: tl::PublicKey<'a>) -> Result<Self, Self::Error> {
match value {
tl::PublicKey::Ed25519 { key } => {
let public_key = ed25519::PublicKey::from_bytes(*key)
.ok_or(AdnlNodeIdFullError::InvalidPublicKey)?;
Ok(Self::new(public_key))
}
_ => Err(AdnlNodeIdFullError::UnsupportedPublicKey),
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum AdnlNodeIdFullError {
#[error("Unsupported public key")]
UnsupportedPublicKey,
#[error("Invalid public key")]
InvalidPublicKey,
#[error("Invalid signature")]
InvalidSignature,
}
#[derive(Default, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[repr(transparent)]
pub struct AdnlNodeIdShort([u8; 32]);
impl AdnlNodeIdShort {
pub const fn new(hash: [u8; 32]) -> Self {
Self(hash)
}
pub fn random() -> Self {
Self(rand::thread_rng().gen())
}
pub const fn as_slice(&self) -> &[u8; 32] {
&self.0
}
pub fn is_zero(&self) -> bool {
self == &[0; 32]
}
}
impl std::fmt::Display for AdnlNodeIdShort {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut output = [0u8; 64];
hex::encode_to_slice(&self.0, &mut output).ok();
let output = unsafe { std::str::from_utf8_unchecked(&output) };
f.write_str(output)
}
}
impl std::fmt::Debug for AdnlNodeIdShort {
#[inline(always)]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl PartialEq<[u8]> for AdnlNodeIdShort {
fn eq(&self, other: &[u8]) -> bool {
self.0.eq(other)
}
}
impl PartialEq<[u8; 32]> for AdnlNodeIdShort {
fn eq(&self, other: &[u8; 32]) -> bool {
self.0.eq(other)
}
}
impl From<AdnlNodeIdShort> for [u8; 32] {
fn from(id: AdnlNodeIdShort) -> Self {
id.0
}
}
impl Borrow<[u8; 32]> for AdnlNodeIdShort {
fn borrow(&self) -> &[u8; 32] {
&self.0
}
}
impl<'a> Borrow<[u8; 32]> for &'a AdnlNodeIdShort {
fn borrow(&self) -> &[u8; 32] {
&self.0
}
}
pub trait ComputeNodeIds {
fn compute_node_ids(&self) -> (AdnlNodeIdFull, AdnlNodeIdShort);
}
impl ComputeNodeIds for ed25519::SecretKey {
fn compute_node_ids(&self) -> (AdnlNodeIdFull, AdnlNodeIdShort) {
let public_key = ed25519::PublicKey::from(self);
let full_id = AdnlNodeIdFull::new(public_key);
let short_id = full_id.compute_short_id();
(full_id, short_id)
}
}
impl ComputeNodeIds for ed25519::PublicKey {
fn compute_node_ids(&self) -> (AdnlNodeIdFull, AdnlNodeIdShort) {
let full_id = AdnlNodeIdFull::new(*self);
let short_id = full_id.compute_short_id();
(full_id, short_id)
}
}
pub struct StoredAdnlNodeKey {
short_id: AdnlNodeIdShort,
full_id: AdnlNodeIdFull,
private_key: ed25519::ExpandedSecretKey,
}
impl StoredAdnlNodeKey {
pub fn from_id_and_private_key(
short_id: AdnlNodeIdShort,
full_id: AdnlNodeIdFull,
private_key: &ed25519::SecretKey,
) -> Self {
let private_key = ed25519::ExpandedSecretKey::from(private_key);
Self {
short_id,
full_id,
private_key,
}
}
#[inline(always)]
pub fn id(&self) -> &AdnlNodeIdShort {
&self.short_id
}
#[inline(always)]
pub fn full_id(&self) -> &AdnlNodeIdFull {
&self.full_id
}
#[inline(always)]
pub fn private_key(&self) -> &ed25519::ExpandedSecretKey {
&self.private_key
}
#[inline(always)]
pub fn sign<T: tl_proto::TlWrite<Repr = tl_proto::Boxed>>(&self, data: T) -> [u8; 64] {
self.private_key.sign(data, self.full_id.public_key())
}
}