This crate contains an implementation of an Ethereum Node Record (ENR) as specified by EIP-778 extended to allow for the use of ed25519 keys.
An ENR is a signed, key-value record which has an associated NodeId
(a 32-byte identifier).
Updating/modifying an ENR requires an EnrKey
in order to re-sign the record with the
associated key-pair.
ENR's are identified by their sequence number. When updating an ENR, the sequence number is increased.
Different identity schemes can be used to define the node id and signatures. Currently only the "v4" identity is supported and is set by default.
User's wishing to implement their own signing algorithms simply need to
implement the EnrKey
trait and apply it to an Enr
.
By default, k256::SigningKey
implement EnrKey
and can be used to sign and
verify ENR records. This library also implements EnrKey
for ed25519_dalek::Keypair
via the ed25519
feature flag.
Furthermore, a CombinedKey
is provided if the ed25519
feature flag is set, which provides an
ENR type that can support both secp256k1
and ed25519
signed ENR records. Examples of the
use of each of these key types is given below.
This crate supports a number of features.
serde
: Allows for serde serialization and deserialization for ENRs.ed25519
: Provides support fored25519_dalek
keypair types.rust-secp256k1
: Usesc-secp256k1
for secp256k1 keys.
These can be enabled via adding the feature flag in your Cargo.toml
enr = { version = "*", features = ["serde", "ed25519", "rust-secp256k1"] }
use enr::{Enr, k256};
use std::net::Ipv4Addr;
use rand::thread_rng;
// generate a random secp256k1 key
let mut rng = thread_rng();
let key = k256::ecdsa::SigningKey::random(&mut rng);
let ip = Ipv4Addr::new(192,168,0,1);
let enr = Enr::builder().ip4(ip).tcp4(8000).build(&key).unwrap();
assert_eq!(enr.ip4(), Some("192.168.0.1".parse().unwrap()));
assert_eq!(enr.id(), Some("v4".into()));
Note the ed25519
feature flag must be set. This makes use of the
builder::Builder
struct.
use enr::{Enr, CombinedKey};
use std::net::Ipv4Addr;
// create a new secp256k1 key
let key = CombinedKey::generate_secp256k1();
// or create a new ed25519 key
let key = CombinedKey::generate_ed25519();
let ip = Ipv4Addr::new(192,168,0,1);
let enr = Enr::builder().ip4(ip).tcp4(8000).build(&key).unwrap();
assert_eq!(enr.ip4(), Some("192.168.0.1".parse().unwrap()));
assert_eq!(enr.id(), Some("v4".into()));
ENR fields can be added and modified using the getters/setters on Enr
. A custom field
can be added using insert
and retrieved with get
.
use enr::{k256::ecdsa::SigningKey, Enr};
use std::net::Ipv4Addr;
use rand::thread_rng;
// specify the type of ENR
type DefaultEnr = Enr<SigningKey>;
// generate a random secp256k1 key
let mut rng = thread_rng();
let key = SigningKey::random(&mut rng);
let ip = Ipv4Addr::new(192,168,0,1);
let mut enr = Enr::builder().ip4(ip).tcp4(8000).build(&key).unwrap();
enr.set_tcp4(8001, &key);
// set a custom key
enr.insert("custom_key", &vec![0,0,1],