reth_cli_commands/p2p/
enode.rs

1//! Enode identifier command
2
3use clap::Parser;
4use reth_cli_util::get_secret_key;
5use reth_network_peers::NodeRecord;
6use std::{
7    net::{IpAddr, Ipv4Addr, SocketAddr},
8    path::PathBuf,
9};
10
11/// Print the enode identifier for a given secret key.
12#[derive(Parser, Debug)]
13pub struct Command {
14    /// Path to the secret key file for discovery.
15    pub discovery_secret: PathBuf,
16
17    /// Optional IP address to include in the enode URL.
18    ///
19    /// If not provided, defaults to 0.0.0.0.
20    #[arg(long)]
21    pub ip: Option<IpAddr>,
22}
23
24impl Command {
25    /// Execute the enode command.
26    pub fn execute(self) -> eyre::Result<()> {
27        let sk = get_secret_key(&self.discovery_secret)?;
28        let ip = self.ip.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
29        let addr = SocketAddr::new(ip, 30303);
30        let enr = NodeRecord::from_secret_key(addr, &sk);
31        println!("{enr}");
32        Ok(())
33    }
34}