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
use super::{ed25519_dalek as ed25519, EnrKey, EnrPublicKey, SigningError};
use rlp::DecoderError;
use std::collections::BTreeMap;
pub const ENR_KEY: &str = "ed25519";
impl EnrKey for ed25519::Keypair {
type PublicKey = ed25519::PublicKey;
fn sign_v4(&self, msg: &[u8]) -> Result<Vec<u8>, SigningError> {
Ok(self.sign(msg).to_bytes().to_vec())
}
fn public(&self) -> Self::PublicKey {
self.public
}
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
if let Some(pubkey_bytes) = content.get(ENR_KEY) {
ed25519::PublicKey::from_bytes(pubkey_bytes)
.map_err(|_| DecoderError::Custom("Invalid ed25519 Signature"))
} else {
Err(DecoderError::Custom("Unknown signature"))
}
}
}
impl EnrPublicKey for ed25519::PublicKey {
fn verify_v4(&self, msg: &[u8], sig: &[u8]) -> bool {
ed25519::Signature::from_bytes(sig)
.and_then(|s| self.verify(msg, &s))
.is_ok()
}
fn encode(&self) -> Vec<u8> {
self.to_bytes().to_vec()
}
fn encode_uncompressed(&self) -> Vec<u8> {
self.encode()
}
fn enr_key(&self) -> String {
ENR_KEY.into()
}
}