Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Age Manning <Age@AgeManning.com>"]
edition = "2018"
name = "enr-cli"
version = "0.5.1"
version = "0.6.0"
description = "Simple utility to read Ethereum Node Records (ENR)"
readme = "./README.md"
keywords = ["ethereum", "enr", "record", "EIP778", "node"]
Expand All @@ -19,12 +19,15 @@ name = "enr-cli"
path = "src/main.rs"

[dependencies]
enr = { version = "0.8.1", features = ["ed25519"] }
enr = { version = "0.12", features = ["ed25519"] }
clap = "4.0.18"
libp2p-core = "0.40.1"
libp2p-core = "0.41"
hex = "0.4.3"
eth2_ssz = "0.4.0"
eth2_ssz_derive = "0.3.0"
tiny-keccak = "2.0.2"
bytes = "1"
# TODO: remove 'rand' feature when <https://github.com/libp2p/rust-libp2p/pull/5212> is merged
libp2p-identity = { version = "0.2.8", features = ['ecdsa', 'ed25519', 'peerid', 'secp256k1', 'rand'] }

[dev-dependencies]
tiny-keccak = { version = "2.0.2", features = ["keccak"] }
2 changes: 1 addition & 1 deletion src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn build(matches: &clap::ArgMatches) -> Result<(), &'static str> {

// Build the ENR:

let mut enr_builder = enr::EnrBuilder::new("v4");
let mut enr_builder = enr::Builder::default();

if let Some(seq) = matches.get_one::<String>("seq") {
enr_builder.seq(seq.parse::<u64>().map_err(|_| "Invalid sequence number")?);
Expand Down
7 changes: 5 additions & 2 deletions src/enr_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub const QUIC_ENR_KEY: &str = "quic";
pub const QUIC6_ENR_KEY: &str = "quic6";

/// Extend ENR for libp2p types.
#[allow(dead_code)]
pub trait EnrExt {
/// The libp2p `PeerId` for the record.
fn peer_id(&self) -> PeerId;
Expand Down Expand Up @@ -52,6 +53,7 @@ pub trait CombinedKeyPublicExt {
}

/// Extend ENR CombinedKey for conversion to libp2p keys.
#[allow(dead_code)]
pub trait CombinedKeyExt {
/// Converts a libp2p key into an ENR combined key.
fn from_libp2p(key: Keypair) -> Result<CombinedKey, &'static str>;
Expand Down Expand Up @@ -350,6 +352,7 @@ impl CombinedKeyExt for CombinedKey {
// peer_ids
#[cfg(test)]
pub fn peer_id_to_node_id(peer_id: &PeerId) -> Result<enr::NodeId, String> {
use tiny_keccak::{Hasher, Keccak};
// A libp2p peer id byte representation should be 2 length bytes + 4 protobuf bytes + compressed pk bytes
// if generated from a PublicKey with Identity multihash.
let pk_bytes = &peer_id.to_bytes()[2..];
Expand Down Expand Up @@ -407,7 +410,7 @@ mod tests {
let libp2p_kp: Keypair = secp256k1_kp.into();
let peer_id = libp2p_kp.public().to_peer_id();

let enr = enr::EnrBuilder::new("v4").build(&secret_key).unwrap();
let enr = enr::Builder::default().build(&secret_key).unwrap();
let node_id = peer_id_to_node_id(&peer_id).unwrap();

assert_eq!(enr.node_id(), node_id);
Expand All @@ -425,7 +428,7 @@ mod tests {
let libp2p_kp: Keypair = secp256k1_kp.into();
let peer_id = libp2p_kp.public().to_peer_id();

let enr = enr::EnrBuilder::new("v4").build(&secret_key).unwrap();
let enr = enr::Builder::default().build(&secret_key).unwrap();
let node_id = peer_id_to_node_id(&peer_id).unwrap();

assert_eq!(enr.node_id(), node_id);
Expand Down
12 changes: 9 additions & 3 deletions src/eth2_ext.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::Enr;
use bytes::Bytes;
use ssz::Decode;
use ssz_derive::{Decode, Encode};

Expand All @@ -23,12 +24,17 @@ pub trait Eth2Enr {

impl Eth2Enr for Enr {
fn bitfield(&self) -> Option<Vec<u8>> {
self.get(BITFIELD_ENR_KEY).map(|v| v.to_vec())
self.get_decodable(BITFIELD_ENR_KEY)?
.ok()
.map(|v: Bytes| v.to_vec())
}

fn eth2(&self) -> Result<EnrForkId, &'static str> {
let eth2_bytes = self.get(ETH2_ENR_KEY).ok_or("ENR has no eth2 field")?;
let eth2_bytes = self
.get_decodable::<Bytes>(ETH2_ENR_KEY)
.ok_or("ENR has no eth2 field")?
.map_err(|_| "Could not decode fork id")?;

EnrForkId::from_ssz_bytes(eth2_bytes).map_err(|_| "Could not decode EnrForkId")
EnrForkId::from_ssz_bytes(&eth2_bytes).map_err(|_| "Could not decode EnrForkId")
}
}