From 2216b4b53d643edde4919714c3ba71c951803a29 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Sun, 9 Jun 2024 16:23:17 +0200 Subject: [PATCH] chore: replace secp256k1 with fully qualified path (#8701) --- crates/net/peers/src/lib.rs | 20 +++++++++----------- crates/net/peers/src/node_record.rs | 12 +++++------- crates/net/peers/src/trusted_peer.rs | 10 ++++------ 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/crates/net/peers/src/lib.rs b/crates/net/peers/src/lib.rs index 40e8b638af8a..9815b51968ac 100644 --- a/crates/net/peers/src/lib.rs +++ b/crates/net/peers/src/lib.rs @@ -49,7 +49,6 @@ #![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] use alloy_primitives::B512; -use secp256k1::{constants::UNCOMPRESSED_PUBLIC_KEY_SIZE, PublicKey, SecretKey}; use std::{net::IpAddr, str::FromStr}; // Re-export PeerId for ease of use. @@ -75,20 +74,20 @@ const SECP256K1_TAG_PUBKEY_UNCOMPRESSED: u8 = 4; /// Converts a [`secp256k1::PublicKey`] to a [`PeerId`] by stripping the /// `SECP256K1_TAG_PUBKEY_UNCOMPRESSED` tag and storing the rest of the slice in the [`PeerId`]. #[inline] -pub fn pk2id(pk: &PublicKey) -> PeerId { +pub fn pk2id(pk: &secp256k1::PublicKey) -> PeerId { PeerId::from_slice(&pk.serialize_uncompressed()[1..]) } /// Converts a [`PeerId`] to a [`secp256k1::PublicKey`] by prepending the [`PeerId`] bytes with the /// `SECP256K1_TAG_PUBKEY_UNCOMPRESSED` tag. #[inline] -pub fn id2pk(id: PeerId) -> Result { +pub fn id2pk(id: PeerId) -> Result { // NOTE: B512 is used as a PeerId because 512 bits is enough to represent an uncompressed // public key. - let mut s = [0u8; UNCOMPRESSED_PUBLIC_KEY_SIZE]; + let mut s = [0u8; secp256k1::constants::UNCOMPRESSED_PUBLIC_KEY_SIZE]; s[0] = SECP256K1_TAG_PUBKEY_UNCOMPRESSED; s[1..].copy_from_slice(id.as_slice()); - PublicKey::from_slice(&s) + secp256k1::PublicKey::from_slice(&s) } /// A peer that can come in ENR or [`NodeRecord`] form. @@ -99,7 +98,7 @@ pub enum AnyNode { /// An "enode:" peer with full ip NodeRecord(NodeRecord), /// An "enr:" - Enr(Enr), + Enr(Enr), /// An incomplete "enode" with only a peer id PeerId(PeerId), } @@ -139,8 +138,8 @@ impl From for AnyNode { } } -impl From> for AnyNode { - fn from(value: Enr) -> Self { +impl From> for AnyNode { + fn from(value: Enr) -> Self { Self::Enr(value) } } @@ -235,7 +234,6 @@ impl WithPeerId> { #[cfg(test)] mod tests { use super::*; - use secp256k1::SECP256K1; #[test] fn test_node_record_parse() { @@ -278,8 +276,8 @@ mod tests { #[test] fn pk2id2pk() { - let prikey = SecretKey::new(&mut secp256k1::rand::thread_rng()); - let pubkey = PublicKey::from_secret_key(SECP256K1, &prikey); + let prikey = secp256k1::SecretKey::new(&mut rand::thread_rng()); + let pubkey = secp256k1::PublicKey::from_secret_key(secp256k1::SECP256K1, &prikey); assert_eq!(pubkey, id2pk(pk2id(&pubkey)).unwrap()); } } diff --git a/crates/net/peers/src/node_record.rs b/crates/net/peers/src/node_record.rs index ec1435670f83..eb3fe373e2b0 100644 --- a/crates/net/peers/src/node_record.rs +++ b/crates/net/peers/src/node_record.rs @@ -11,7 +11,6 @@ use std::{ use crate::{pk2id, PeerId}; use alloy_rlp::{RlpDecodable, RlpEncodable}; use enr::Enr; -use secp256k1::{SecretKey, SECP256K1}; use serde_with::{DeserializeFromStr, SerializeDisplay}; /// Represents a ENR in discovery. @@ -42,8 +41,8 @@ pub struct NodeRecord { impl NodeRecord { /// Derive the [`NodeRecord`] from the secret key and addr - pub fn from_secret_key(addr: SocketAddr, sk: &SecretKey) -> Self { - let pk = secp256k1::PublicKey::from_secret_key(SECP256K1, sk); + pub fn from_secret_key(addr: SocketAddr, sk: &secp256k1::SecretKey) -> Self { + let pk = secp256k1::PublicKey::from_secret_key(secp256k1::SECP256K1, sk); let id = PeerId::from_slice(&pk.serialize_uncompressed()[1..]); Self::new(addr, id) } @@ -170,10 +169,10 @@ impl FromStr for NodeRecord { } } -impl TryFrom<&Enr> for NodeRecord { +impl TryFrom<&Enr> for NodeRecord { type Error = NodeRecordParseError; - fn try_from(enr: &Enr) -> Result { + fn try_from(enr: &Enr) -> Result { let Some(address) = enr.ip4().map(IpAddr::from).or_else(|| enr.ip6().map(IpAddr::from)) else { return Err(NodeRecordParseError::InvalidUrl("ip missing".to_string())) @@ -195,10 +194,9 @@ impl TryFrom<&Enr> for NodeRecord { #[cfg(test)] mod tests { - use std::net::Ipv6Addr; - use alloy_rlp::Decodable; use rand::{thread_rng, Rng, RngCore}; + use std::net::Ipv6Addr; use super::*; diff --git a/crates/net/peers/src/trusted_peer.rs b/crates/net/peers/src/trusted_peer.rs index 7a973f056644..210b9b9b08ca 100644 --- a/crates/net/peers/src/trusted_peer.rs +++ b/crates/net/peers/src/trusted_peer.rs @@ -1,5 +1,7 @@ //! `NodeRecord` type that uses a domain instead of an IP. +use crate::{NodeRecord, PeerId}; +use serde_with::{DeserializeFromStr, SerializeDisplay}; use std::{ fmt::{self, Write}, io::Error, @@ -7,10 +9,6 @@ use std::{ num::ParseIntError, str::FromStr, }; - -use crate::{NodeRecord, PeerId}; -use secp256k1::{SecretKey, SECP256K1}; -use serde_with::{DeserializeFromStr, SerializeDisplay}; use url::Host; /// Represents the node record of a trusted peer. The only difference between this and a @@ -35,8 +33,8 @@ pub struct TrustedPeer { impl TrustedPeer { /// Derive the [`NodeRecord`] from the secret key and addr - pub fn from_secret_key(host: Host, port: u16, sk: &SecretKey) -> Self { - let pk = secp256k1::PublicKey::from_secret_key(SECP256K1, sk); + pub fn from_secret_key(host: Host, port: u16, sk: &secp256k1::SecretKey) -> Self { + let pk = secp256k1::PublicKey::from_secret_key(secp256k1::SECP256K1, sk); let id = PeerId::from_slice(&pk.serialize_uncompressed()[1..]); Self::new(host, port, id) }