Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change rlp to alloy-rlp #46

Merged
merged 25 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
4d04cc3
change rlp to alloy-rlp
armaganyildirak Aug 9, 2023
06373c5
clippy
armaganyildirak Aug 9, 2023
2160b4b
Working on decode function
armaganyildirak Aug 11, 2023
2f82a44
Working on rlp_content, still has problems
armaganyildirak Aug 14, 2023
795fbb8
fix the rlp_content
armaganyildirak Aug 14, 2023
733287d
implement the encode still have a problem with test_rlp_list_value
armaganyildirak Aug 16, 2023
332149c
fix tests
armaganyildirak Aug 17, 2023
417d1b9
fix the clippy issues
armaganyildirak Aug 25, 2023
3584010
fmt
armaganyildirak Aug 25, 2023
31d7011
prepared for discv5
armaganyildirak Sep 1, 2023
979ffed
remove unnecessary change
armaganyildirak Sep 1, 2023
8c445fb
fix merge confilicts
armaganyildirak Sep 15, 2023
7e9224c
remove unused dependency
armaganyildirak Sep 15, 2023
a48d546
use less to_vec
armaganyildirak Sep 25, 2023
21a779e
fmt and clippy
armaganyildirak Sep 25, 2023
66ecdf4
working on encode
armaganyildirak Sep 25, 2023
779b85e
don't use octects
armaganyildirak Sep 25, 2023
68c7af0
updates after review
armaganyildirak Oct 9, 2023
e74ecbd
fix merge conflicts
armaganyildirak Oct 9, 2023
b2b35ba
updates after review
armaganyildirak Oct 9, 2023
f2effcc
Merge branch 'master' into alloy-rlp
armaganyildirak Oct 13, 2023
9151474
changes after review
armaganyildirak Oct 20, 2023
106b0d7
Merge branch 'sigp:master' into alloy-rlp
armaganyildirak Oct 23, 2023
34fb134
Merge branch 'master' into alloy-rlp
armaganyildirak Nov 6, 2023
1ba4728
fix merge conflicts
armaganyildirak Nov 29, 2023
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
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