mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
chore: move pk2id and id2pk to primitives (#7382)
This commit is contained in:
@ -3,7 +3,7 @@
|
||||
use crate::{
|
||||
error::ECIESErrorImpl,
|
||||
mac::{HeaderBytes, MAC},
|
||||
util::{hmac_sha256, id2pk, pk2id, sha256},
|
||||
util::{hmac_sha256, sha256},
|
||||
ECIESError,
|
||||
};
|
||||
use aes::{cipher::StreamCipher, Aes128, Aes256};
|
||||
@ -15,7 +15,7 @@ use educe::Educe;
|
||||
use rand::{thread_rng, Rng};
|
||||
use reth_primitives::{
|
||||
bytes::{BufMut, Bytes, BytesMut},
|
||||
B128, B256, B512 as PeerId,
|
||||
id2pk, pk2id, B128, B256, B512 as PeerId,
|
||||
};
|
||||
use secp256k1::{
|
||||
ecdsa::{RecoverableSignature, RecoveryId},
|
||||
|
||||
@ -175,7 +175,7 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::util::pk2id;
|
||||
use reth_primitives::pk2id;
|
||||
use secp256k1::SECP256K1;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
|
||||
|
||||
@ -1,8 +1,7 @@
|
||||
//! Utility functions for hashing and encoding.
|
||||
|
||||
use hmac::{Hmac, Mac};
|
||||
use reth_primitives::{B256, B512 as PeerId};
|
||||
use secp256k1::PublicKey;
|
||||
use reth_primitives::B256;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
/// Hashes the input data with SHA256.
|
||||
@ -21,35 +20,3 @@ pub(crate) fn hmac_sha256(key: &[u8], input: &[&[u8]], auth_data: &[u8]) -> B256
|
||||
hmac.update(auth_data);
|
||||
B256::from_slice(&hmac.finalize().into_bytes())
|
||||
}
|
||||
|
||||
/// 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].
|
||||
pub fn pk2id(pk: &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.
|
||||
pub(crate) fn id2pk(id: PeerId) -> Result<PublicKey, secp256k1::Error> {
|
||||
// NOTE: B512 is used as a PeerId not because it represents a hash, but because 512 bits is
|
||||
// enough to represent an uncompressed public key.
|
||||
let mut s = [0u8; 65];
|
||||
// SECP256K1_TAG_PUBKEY_UNCOMPRESSED = 0x04
|
||||
// see: https://github.com/bitcoin-core/secp256k1/blob/master/include/secp256k1.h#L211
|
||||
s[0] = 4;
|
||||
s[1..].copy_from_slice(id.as_slice());
|
||||
PublicKey::from_slice(&s)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use secp256k1::{SecretKey, SECP256K1};
|
||||
|
||||
#[test]
|
||||
fn pk2id2pk() {
|
||||
let prikey = SecretKey::new(&mut secp256k1::rand::thread_rng());
|
||||
let pubkey = PublicKey::from_secret_key(SECP256K1, &prikey);
|
||||
assert_eq!(pubkey, id2pk(pk2id(&pubkey)).unwrap());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user