Skip to content

Commit

Permalink
Change rlp to alloy-rlp (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
armaganyildirak authored Nov 29, 2023
1 parent a06ab8a commit 77bde01
Show file tree
Hide file tree
Showing 9 changed files with 247 additions and 193 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ license = "MIT"
exclude = [".gitignore", ".github/*"]

[dependencies]
alloy-rlp = { version = "0.3.3", default-features = true }
base64 = "0.21.0"
bytes = "1"
hex = { version = "0.4.2" }
log = "0.4.8"
rand = "0.8"
rlp = "0.5"
zeroize = "1.1.0"
sha3 = "0.10"
k256 = { version = "0.13", features = ["ecdsa"], optional = true }
Expand Down
33 changes: 22 additions & 11 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Enr, EnrKey, EnrPublicKey, Error, Key, NodeId, MAX_ENR_SIZE};
use alloy_rlp::{Decodable, Encodable, Header};
use bytes::{Bytes, BytesMut};
use rlp::{Encodable, RlpStream};
use std::{
collections::BTreeMap,
marker::PhantomData,
Expand Down Expand Up @@ -44,7 +44,9 @@ impl<K: EnrKey> Builder<K> {

/// Adds an arbitrary key-value to the `ENRBuilder`.
pub fn add_value<T: Encodable>(&mut self, key: impl AsRef<[u8]>, value: &T) -> &mut Self {
self.add_value_rlp(key, rlp::encode(value).freeze())
let mut out = BytesMut::new();
value.encode(&mut out);
self.add_value_rlp(key, out.freeze())
}

/// Adds an arbitrary key-value where the value is raw RLP encoded bytes.
Expand Down Expand Up @@ -110,15 +112,22 @@ impl<K: EnrKey> Builder<K> {

/// Generates the rlp-encoded form of the ENR specified by the builder config.
fn rlp_content(&self) -> BytesMut {
let mut stream = RlpStream::new_with_buffer(BytesMut::with_capacity(MAX_ENR_SIZE));
stream.begin_list(self.content.len() * 2 + 1);
stream.append(&self.seq);
let mut list = Vec::<u8>::with_capacity(MAX_ENR_SIZE);
self.seq.encode(&mut list);
for (k, v) in &self.content {
stream.append(k);
// The values are stored as raw RLP encoded bytes
stream.append_raw(v, 1);
// Keys are bytes
k.as_slice().encode(&mut list);
// Values are raw RLP encoded data
list.extend_from_slice(v);
}
stream.out()
let header = Header {
list: true,
payload_length: list.len(),
};
let mut out = BytesMut::new();
header.encode(&mut out);
out.extend_from_slice(&list);
out
}

/// Signs record based on the identity scheme. Currently only "v4" is supported.
Expand Down Expand Up @@ -149,10 +158,12 @@ impl<K: EnrKey> Builder<K> {

// Sanitize all data, ensuring all RLP data is correctly formatted.
for value in self.content.values() {
rlp::Rlp::new(value).data()?;
Bytes::decode(&mut value.as_ref())?;
}

self.add_value_rlp("id", rlp::encode(&self.id.as_bytes()).freeze());
let mut id_bytes = BytesMut::with_capacity(3);
self.id.as_bytes().encode(&mut id_bytes);
self.add_value_rlp("id", id_bytes.freeze());

self.add_public_key(&key.public());
let rlp_content = self.rlp_content();
Expand Down
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ pub enum Error {
/// The identity scheme is not supported.
UnsupportedIdentityScheme,
/// Failed decoding the RLP data.
InvalidRlpData(rlp::DecoderError),
InvalidRlpData(alloy_rlp::Error),
}

impl From<rlp::DecoderError> for Error {
fn from(decode_error: rlp::DecoderError) -> Self {
impl From<alloy_rlp::Error> for Error {
fn from(decode_error: alloy_rlp::Error) -> Self {
Error::InvalidRlpData(decode_error)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/keys/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
//! Currently only `secp256k1` and `ed25519` key types are supported.
use super::{ed25519_dalek as ed25519, EnrKey, EnrPublicKey, SigningError};
use alloy_rlp::Error as DecoderError;
use bytes::Bytes;
pub use k256;
use rlp::DecoderError;
use std::{collections::BTreeMap, convert::TryFrom};
use zeroize::Zeroize;

Expand Down
6 changes: 3 additions & 3 deletions src/keys/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use super::{
EnrKey, EnrKeyUnambiguous, EnrPublicKey, SigningError,
};
use crate::Key;
use alloy_rlp::{Decodable, Error as DecoderError};
use bytes::Bytes;
use rlp::DecoderError;
use std::{collections::BTreeMap, convert::TryFrom};

/// The ENR key that stores the public key in the ENR record.
Expand Down Expand Up @@ -33,9 +33,9 @@ impl EnrKey for ed25519::SigningKey {
.ok_or(DecoderError::Custom("Unknown signature"))?;

// Decode the RLP
let pubkey_bytes = rlp::Rlp::new(pubkey_bytes).data()?;
let pubkey_bytes = Bytes::decode(&mut pubkey_bytes.as_ref())?;

Self::decode_public(pubkey_bytes)
Self::decode_public(&pubkey_bytes)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/keys/k256_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use super::{EnrKey, EnrKeyUnambiguous, EnrPublicKey, SigningError};
use crate::Key;
use alloy_rlp::{Decodable, Error as DecoderError};
use bytes::Bytes;
use k256::{
ecdsa::{
Expand All @@ -16,7 +17,6 @@ use k256::{
AffinePoint, CompressedPoint, EncodedPoint,
};
use rand::rngs::OsRng;
use rlp::DecoderError;
use sha3::{Digest, Keccak256};
use std::{collections::BTreeMap, convert::TryFrom};

Expand Down Expand Up @@ -46,9 +46,9 @@ impl EnrKey for SigningKey {
.ok_or(DecoderError::Custom("Unknown signature"))?;

// Decode the RLP
let pubkey_bytes = rlp::Rlp::new(pubkey_bytes).data()?;
let pubkey_bytes = Bytes::decode(&mut pubkey_bytes.as_ref())?;

Self::decode_public(pubkey_bytes)
Self::decode_public(&pubkey_bytes)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/keys/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub use k256;
pub use secp256k1;

use crate::Key;
use alloy_rlp::Error as DecoderError;
use bytes::Bytes;
use rlp::DecoderError;
use std::{
collections::BTreeMap,
error::Error,
Expand Down
6 changes: 3 additions & 3 deletions src/keys/rust_secp256k1.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use super::{EnrKey, EnrKeyUnambiguous, EnrPublicKey, SigningError};
use crate::{digest, Key};
use alloy_rlp::{Decodable, Error as DecoderError};
use bytes::Bytes;
use rand::RngCore;
use rlp::DecoderError;
use secp256k1::SECP256K1;
use std::collections::BTreeMap;

Expand Down Expand Up @@ -40,9 +40,9 @@ impl EnrKey for secp256k1::SecretKey {
.get(ENR_KEY.as_bytes())
.ok_or(DecoderError::Custom("Unknown signature"))?;
// Decode the RLP
let pubkey_bytes = rlp::Rlp::new(pubkey_bytes).data()?;
let pubkey_bytes = Bytes::decode(&mut pubkey_bytes.as_ref())?;

Self::decode_public(pubkey_bytes)
Self::decode_public(&pubkey_bytes)
}
}

Expand Down
Loading

0 comments on commit 77bde01

Please sign in to comment.