Skip to content

Commit

Permalink
chore: replace secp256k1 with fully qualified path (#8701)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jun 9, 2024
1 parent c7fd507 commit 2216b4b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
20 changes: 9 additions & 11 deletions crates/net/peers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<PublicKey, secp256k1::Error> {
pub fn id2pk(id: PeerId) -> Result<secp256k1::PublicKey, secp256k1::Error> {
// 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.
Expand All @@ -99,7 +98,7 @@ pub enum AnyNode {
/// An "enode:" peer with full ip
NodeRecord(NodeRecord),
/// An "enr:"
Enr(Enr<SecretKey>),
Enr(Enr<secp256k1::SecretKey>),
/// An incomplete "enode" with only a peer id
PeerId(PeerId),
}
Expand Down Expand Up @@ -139,8 +138,8 @@ impl From<NodeRecord> for AnyNode {
}
}

impl From<Enr<SecretKey>> for AnyNode {
fn from(value: Enr<SecretKey>) -> Self {
impl From<Enr<secp256k1::SecretKey>> for AnyNode {
fn from(value: Enr<secp256k1::SecretKey>) -> Self {
Self::Enr(value)
}
}
Expand Down Expand Up @@ -235,7 +234,6 @@ impl<T> WithPeerId<Option<T>> {
#[cfg(test)]
mod tests {
use super::*;
use secp256k1::SECP256K1;

#[test]
fn test_node_record_parse() {
Expand Down Expand Up @@ -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());
}
}
12 changes: 5 additions & 7 deletions crates/net/peers/src/node_record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -170,10 +169,10 @@ impl FromStr for NodeRecord {
}
}

impl TryFrom<&Enr<SecretKey>> for NodeRecord {
impl TryFrom<&Enr<secp256k1::SecretKey>> for NodeRecord {
type Error = NodeRecordParseError;

fn try_from(enr: &Enr<SecretKey>) -> Result<Self, Self::Error> {
fn try_from(enr: &Enr<secp256k1::SecretKey>) -> Result<Self, Self::Error> {
let Some(address) = enr.ip4().map(IpAddr::from).or_else(|| enr.ip6().map(IpAddr::from))
else {
return Err(NodeRecordParseError::InvalidUrl("ip missing".to_string()))
Expand All @@ -195,10 +194,9 @@ impl TryFrom<&Enr<SecretKey>> 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::*;

Expand Down
10 changes: 4 additions & 6 deletions crates/net/peers/src/trusted_peer.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
//! `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,
net::IpAddr,
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
Expand All @@ -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)
}
Expand Down

0 comments on commit 2216b4b

Please sign in to comment.