Skip to content

Commit

Permalink
Improve builder ergonomics, remove prefix (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
divagant-martian authored Oct 12, 2023
1 parent ca06c83 commit ee6a8d9
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 104 deletions.
22 changes: 10 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,10 @@ enr = { version = "*", features = ["serde", "ed25519", "rust-secp256k1"] }

## Examples

To build an ENR, an `EnrBuilder` is provided.

#### Building an ENR with the default `k256` key type

```rust
use enr::{EnrBuilder, k256};
use enr::{Enr, k256};
use std::net::Ipv4Addr;
use rand::thread_rng;

Expand All @@ -69,7 +67,7 @@ let mut rng = thread_rng();
let key = k256::ecdsa::SigningKey::random(&mut rng);

let ip = Ipv4Addr::new(192,168,0,1);
let enr = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
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()));
Expand All @@ -78,10 +76,10 @@ assert_eq!(enr.id(), Some("v4".into()));
#### Building an ENR with the `CombinedKey` type (support for multiple signing algorithms).

Note the `ed25519` feature flag must be set. This makes use of the
`EnrBuilder` struct.
`builder::Builder` struct.

```rust
use enr::{EnrBuilder, CombinedKey};
use enr::{Enr, CombinedKey};
use std::net::Ipv4Addr;

// create a new secp256k1 key
Expand All @@ -91,7 +89,7 @@ let key = CombinedKey::generate_secp256k1();
let key = CombinedKey::generate_ed25519();

let ip = Ipv4Addr::new(192,168,0,1);
let enr = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
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()));
Expand All @@ -103,7 +101,7 @@ ENR fields can be added and modified using the getters/setters on `Enr`. A custo
can be added using `insert` and retrieved with `get`.

```rust
use enr::{EnrBuilder, k256::ecdsa::SigningKey, Enr};
use enr::{k256::ecdsa::SigningKey, Enr};
use std::net::Ipv4Addr;
use rand::thread_rng;

Expand All @@ -115,7 +113,7 @@ let mut rng = thread_rng();
let key = SigningKey::random(&mut rng);

let ip = Ipv4Addr::new(192,168,0,1);
let mut enr = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
let mut enr = Enr::builder().ip4(ip).tcp4(8000).build(&key).unwrap();

enr.set_tcp4(8001, &key);
// set a custom key
Expand All @@ -136,7 +134,7 @@ assert_eq!(decoded_enr.get("custom_key"), Some(vec![0,0,1].as_slice()));
#### Encoding/Decoding ENR's of various key types

```rust
use enr::{EnrBuilder, k256::ecdsa::SigningKey, Enr, ed25519_dalek::Keypair, CombinedKey};
use enr::{k256::ecdsa::SigningKey, Enr, ed25519_dalek::Keypair, CombinedKey};
use std::net::Ipv4Addr;
use rand::thread_rng;
use rand::Rng;
Expand All @@ -145,15 +143,15 @@ use rand::Rng;
let mut rng = thread_rng();
let key = SigningKey::random(&mut rng);
let ip = Ipv4Addr::new(192,168,0,1);
let enr_secp256k1 = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
let enr_secp256k1 = Enr::builder().ip4(ip).tcp4(8000).build(&key).unwrap();

// encode to base64
let base64_string_secp256k1 = enr_secp256k1.to_base64();

// generate a random ed25519 key
let mut rng = rand_07::thread_rng();
let key = Keypair::generate(&mut rng);
let enr_ed25519 = EnrBuilder::new("v4").ip4(ip).tcp4(8000).build(&key).unwrap();
let enr_ed25519 = Enr::builder().ip4(ip).tcp4(8000).build(&key).unwrap();

// encode to base64
let base64_string_ed25519 = enr_ed25519.to_base64();
Expand Down
16 changes: 8 additions & 8 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
};

/// The base builder for generating ENR records with arbitrary signing algorithms.
pub struct EnrBuilder<K: EnrKey> {
pub struct Builder<K: EnrKey> {
/// The identity scheme used to build the ENR record.
id: String,

Expand All @@ -23,19 +23,19 @@ pub struct EnrBuilder<K: EnrKey> {
phantom: PhantomData<K>,
}

impl<K: EnrKey> EnrBuilder<K> {
/// Constructs a minimal `EnrBuilder` providing only a sequence number.
/// Currently only supports the id v4 scheme and therefore disallows creation of any other
/// scheme.
pub fn new(id: impl Into<String>) -> Self {
impl<K: EnrKey> Default for Builder<K> {
/// Constructs a minimal [`Builder`] for the v4 identity scheme.
fn default() -> Self {
Self {
id: id.into(),
id: String::from("v4"),
seq: 1,
content: BTreeMap::new(),
phantom: PhantomData,
}
}
}

impl<K: EnrKey> Builder<K> {
/// Modifies the sequence number of the builder.
pub fn seq(&mut self, seq: u64) -> &mut Self {
self.seq = seq;
Expand Down Expand Up @@ -137,7 +137,7 @@ impl<K: EnrKey> EnrBuilder<K> {
self.add_value(key.enr_key(), &key.encode().as_ref());
}

/// Constructs an ENR from the `EnrBuilder`.
/// Constructs an ENR from the [`Builder`].
///
/// # Errors
/// Fails if the identity scheme is not supported, or the record size exceeds `MAX_ENR_SIZE`.
Expand Down
Loading

0 comments on commit ee6a8d9

Please sign in to comment.