mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: replace secp256k1 with fully qualified path (#8701)
This commit is contained in:
@ -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<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.
|
||||
@ -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),
|
||||
}
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
@ -235,7 +234,6 @@ impl<T> WithPeerId<Option<T>> {
|
||||
#[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());
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<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()))
|
||||
@ -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::*;
|
||||
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user