Skip to content
This repository was archived by the owner on Nov 20, 2023. It is now read-only.

Commit bee1e03

Browse files
authored
Generalise ENR key type (#12)
* Allow arbitrary bytes as keys * Clippy lints
1 parent 866957b commit bee1e03

File tree

8 files changed

+70
-51
lines changed

8 files changed

+70
-51
lines changed

src/builder.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::Key;
12
use crate::{Enr, EnrError, EnrKey, EnrPublicKey, NodeId, MAX_ENR_SIZE};
23
use rlp::RlpStream;
34
use std::{collections::BTreeMap, marker::PhantomData, net::IpAddr};
@@ -11,7 +12,7 @@ pub struct EnrBuilder<K: EnrKey> {
1112
seq: u64,
1213

1314
/// The key-value pairs for the ENR record.
14-
content: BTreeMap<String, Vec<u8>>,
15+
content: BTreeMap<Key, Vec<u8>>,
1516

1617
/// Pins the generic key types.
1718
phantom: PhantomData<K>,
@@ -37,21 +38,19 @@ impl<K: EnrKey> EnrBuilder<K> {
3738
}
3839

3940
/// Adds an arbitrary key-value to the `ENRBuilder`.
40-
pub fn add_value(&mut self, key: String, value: Vec<u8>) -> &mut Self {
41-
self.content.insert(key, value);
41+
pub fn add_value(&mut self, key: impl AsRef<[u8]>, value: Vec<u8>) -> &mut Self {
42+
self.content.insert(key.as_ref().to_vec(), value);
4243
self
4344
}
4445

4546
/// Adds an `ip` field to the `ENRBuilder`.
4647
pub fn ip(&mut self, ip: IpAddr) -> &mut Self {
4748
match ip {
4849
IpAddr::V4(addr) => {
49-
self.content
50-
.insert(String::from("ip"), addr.octets().to_vec());
50+
self.content.insert(b"ip".to_vec(), addr.octets().to_vec());
5151
}
5252
IpAddr::V6(addr) => {
53-
self.content
54-
.insert(String::from("ip6"), addr.octets().to_vec());
53+
self.content.insert(b"ip6".to_vec(), addr.octets().to_vec());
5554
}
5655
}
5756
self

src/keys/combined.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub use secp256k1;
1010
use std::collections::BTreeMap;
1111
use zeroize::Zeroize;
1212

13+
use crate::Key;
14+
1315
/// A standard implementation of the `EnrKey` trait used to sign and modify ENR records. The variants here represent the currently
1416
/// supported in-built signing schemes.
1517
pub enum CombinedKey {
@@ -64,7 +66,7 @@ impl EnrKey for CombinedKey {
6466
}
6567

6668
/// Decodes the raw bytes of an ENR's content into a public key if possible.
67-
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
69+
fn enr_to_public(content: &BTreeMap<Key, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
6870
secp256k1::SecretKey::enr_to_public(content)
6971
.map(CombinedPublicKey::Secp256k1)
7072
.or_else(|_| ed25519::Keypair::enr_to_public(content).map(CombinedPublicKey::from))
@@ -178,7 +180,7 @@ impl EnrPublicKey for CombinedPublicKey {
178180
}
179181

180182
/// Generates the ENR public key string associated with the key type.
181-
fn enr_key(&self) -> String {
183+
fn enr_key(&self) -> Key {
182184
match self {
183185
Self::Secp256k1(key) => key.enr_key(),
184186
Self::Ed25519(key) => key.enr_key(),

src/keys/ed25519.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
ed25519_dalek::{self as ed25519, Signer as _, Verifier as _},
33
EnrKey, EnrPublicKey, SigningError,
44
};
5+
use crate::Key;
56
use rlp::DecoderError;
67
use std::{collections::BTreeMap, convert::TryFrom};
78

@@ -25,9 +26,9 @@ impl EnrKey for ed25519::Keypair {
2526
}
2627

2728
/// Decodes the raw bytes of an ENR's content into a public key if possible.
28-
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
29+
fn enr_to_public(content: &BTreeMap<Key, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
2930
let pubkey_bytes = content
30-
.get(ENR_KEY)
31+
.get(ENR_KEY.as_bytes())
3132
.ok_or_else(|| DecoderError::Custom("Unknown signature"))?;
3233
ed25519::PublicKey::from_bytes(pubkey_bytes)
3334
.map_err(|_| DecoderError::Custom("Invalid ed25519 Signature"))
@@ -54,7 +55,7 @@ impl EnrPublicKey for ed25519::PublicKey {
5455
}
5556

5657
/// Generates the ENR public key string associated with the ed25519 key type.
57-
fn enr_key(&self) -> String {
58+
fn enr_key(&self) -> Key {
5859
ENR_KEY.into()
5960
}
6061
}

src/keys/k256.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! An implementation for `EnrKey` for `k256::SecretKey`
22
33
use super::{EnrKey, EnrPublicKey, SigningError};
4+
use crate::Key;
45
use k256_crate::{
56
ecdsa::signature::{DigestVerifier, RandomizedDigestSigner, Signature},
67
elliptic_curve::weierstrass::public_key::FromPublicKey,
@@ -36,9 +37,9 @@ impl EnrKey for k256_crate::SecretKey {
3637
self.try_into().unwrap()
3738
}
3839

39-
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
40+
fn enr_to_public(content: &BTreeMap<Key, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
4041
let pubkey_bytes = content
41-
.get(ENR_KEY)
42+
.get(ENR_KEY.as_bytes())
4243
.ok_or_else(|| DecoderError::Custom("Unknown signature"))?;
4344

4445
// should be encoded in compressed form, i.e 33 byte raw secp256k1 public key
@@ -75,7 +76,7 @@ impl EnrPublicKey for k256_crate::PublicKey {
7576
.to_vec()
7677
}
7778

78-
fn enr_key(&self) -> String {
79+
fn enr_key(&self) -> Key {
7980
ENR_KEY.into()
8081
}
8182
}

src/keys/libsecp256k1.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use super::{secp256k1, EnrKey, EnrPublicKey, SigningError};
44
use crate::digest;
5+
use crate::Key;
56
use rlp::DecoderError;
67
use std::collections::BTreeMap;
78

@@ -30,9 +31,9 @@ impl EnrKey for secp256k1::SecretKey {
3031
}
3132

3233
/// Decodes the raw bytes of an ENR's content into a public key if possible.
33-
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
34+
fn enr_to_public(content: &BTreeMap<Key, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
3435
let pubkey_bytes = content
35-
.get(ENR_KEY)
36+
.get(ENR_KEY.as_bytes())
3637
.ok_or_else(|| DecoderError::Custom("Unknown signature"))?;
3738
// should be encoded in compressed form, i.e 33 byte raw secp256k1 public key
3839
secp256k1::PublicKey::parse_slice(
@@ -69,7 +70,7 @@ impl EnrPublicKey for secp256k1::PublicKey {
6970
}
7071

7172
/// Generates the ENR public key string associated with the secp256k1 key type.
72-
fn enr_key(&self) -> String {
73+
fn enr_key(&self) -> Key {
7374
ENR_KEY.into()
7475
}
7576
}

src/keys/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub use ed25519_dalek;
2828
#[cfg(any(feature = "libsecp256k1", doc))]
2929
pub use secp256k1;
3030

31+
use crate::Key;
3132
use rlp::DecoderError;
3233
use std::{
3334
collections::BTreeMap,
@@ -51,7 +52,7 @@ pub trait EnrKey {
5152
/// `EnrPublicKey`. It takes the ENR's `BTreeMap` and returns a public key.
5253
///
5354
/// Note: This specifies the supported key schemes for an ENR.
54-
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError>;
55+
fn enr_to_public(content: &BTreeMap<Key, Vec<u8>>) -> Result<Self::PublicKey, DecoderError>;
5556
}
5657

5758
/// The trait required for a `PublicKey` to verify an ENR record.
@@ -68,7 +69,7 @@ pub trait EnrPublicKey {
6869

6970
/// Returns the ENR key identifier for the public key type. For `secp256k1` keys this
7071
/// is `secp256k1`.
71-
fn enr_key(&self) -> String;
72+
fn enr_key(&self) -> Key;
7273
}
7374

7475
/// An error during signing of a message.

src/keys/rust_secp256k1.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use super::{EnrKey, EnrPublicKey, SigningError};
22
use crate::digest;
3+
use crate::Key;
34
use rlp::DecoderError;
45
use std::collections::BTreeMap;
56

@@ -25,9 +26,9 @@ impl EnrKey for c_secp256k1::SecretKey {
2526
Self::PublicKey::from_secret_key(&c_secp256k1::Secp256k1::new(), self)
2627
}
2728

28-
fn enr_to_public(content: &BTreeMap<String, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
29+
fn enr_to_public(content: &BTreeMap<Key, Vec<u8>>) -> Result<Self::PublicKey, DecoderError> {
2930
let pubkey_bytes = content
30-
.get(ENR_KEY)
31+
.get(ENR_KEY.as_bytes())
3132
.ok_or_else(|| DecoderError::Custom("Unknown signature"))?;
3233
// should be encoded in compressed form, i.e 33 byte raw secp256k1 public key
3334
c_secp256k1::PublicKey::from_slice(pubkey_bytes)
@@ -54,7 +55,7 @@ impl EnrPublicKey for c_secp256k1::PublicKey {
5455
self.serialize_uncompressed()[1..].to_vec()
5556
}
5657

57-
fn enr_key(&self) -> String {
58+
fn enr_key(&self) -> Key {
5859
ENR_KEY.into()
5960
}
6061
}

0 commit comments

Comments
 (0)