Skip to content

Commit fce0863

Browse files
committed
slight naming fixes [skip ci]
1 parent 67e3573 commit fce0863

File tree

4 files changed

+29
-26
lines changed

4 files changed

+29
-26
lines changed

compute/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use eyre::{eyre, Result};
44
use libsecp256k1::{PublicKey, SecretKey};
55
use std::{env, str::FromStr};
66

7-
use crate::utils::crypto::{secret_to_keypair, to_address};
7+
use crate::utils::crypto::{public_key_to_address, secret_to_keypair};
88

99
const DEFAULT_TASK_BATCH_SIZE: usize = 5;
1010
const DEFAULT_P2P_LISTEN_ADDR: &str = "/ip4/0.0.0.0/tcp/4001";
@@ -64,7 +64,7 @@ impl DriaComputeNodeConfig {
6464
hex::encode(public_key.serialize_compressed())
6565
);
6666

67-
let address = to_address(&public_key);
67+
let address = public_key_to_address(&public_key);
6868
log::info!("Node Address: 0x{}", hex::encode(address));
6969

7070
// to this here to log the peer id at start

compute/src/node/gossipsub.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use dkn_p2p::libp2p::gossipsub::{Message, MessageAcceptance, MessageId};
1+
use dkn_p2p::libp2p::gossipsub::{Message, MessageAcceptance, MessageId, TopicSubscriptionFilter};
22
use dkn_p2p::libp2p::PeerId;
33
use eyre::Result;
44

@@ -53,7 +53,7 @@ impl DriaComputeNode {
5353
/// Handles a GossipSub message received from the network.
5454
pub(crate) async fn handle_message(
5555
&mut self,
56-
(peer_id, message_id, gossipsub_message): (PeerId, &MessageId, Message),
56+
(propagation_peer_id, message_id, gossipsub_message): (PeerId, &MessageId, Message),
5757
) -> MessageAcceptance {
5858
// handle message with respect to its topic
5959
match gossipsub_message.topic.as_str() {
@@ -63,7 +63,7 @@ impl DriaComputeNode {
6363
log::warn!(
6464
"Received {} message from {} without source.",
6565
gossipsub_message.topic,
66-
peer_id
66+
propagation_peer_id
6767
);
6868
return MessageAcceptance::Ignore;
6969
};
@@ -98,7 +98,7 @@ impl DriaComputeNode {
9898
"Received {} message ({}) from {}\n{}",
9999
gossipsub_message.topic,
100100
message_id,
101-
peer_id,
101+
propagation_peer_id,
102102
message
103103
);
104104

compute/src/utils/crypto.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use dkn_p2p::{libp2p::PeerId, libp2p_identity};
2-
use ecies::PublicKey;
3-
use libsecp256k1::SecretKey;
42
use sha2::{Digest, Sha256};
53
use sha3::Keccak256;
64

@@ -16,33 +14,33 @@ pub fn keccak256hash(data: impl AsRef<[u8]>) -> [u8; 32] {
1614
Keccak256::digest(data).into()
1715
}
1816

17+
/// Converts a `libsecp256k1::SecretKey` to a `libp2p_identity::secp256k1::Keypair`.
18+
/// To do this, we serialize the secret key and create a new keypair from it.
19+
#[inline]
20+
pub fn secret_to_keypair(secret_key: &libsecp256k1::SecretKey) -> libp2p_identity::Keypair {
21+
let bytes = secret_key.serialize();
22+
23+
let secret_key = dkn_p2p::libp2p_identity::secp256k1::SecretKey::try_from_bytes(bytes)
24+
.expect("Failed to create secret key");
25+
libp2p_identity::secp256k1::Keypair::from(secret_key).into()
26+
}
27+
1928
/// Given a secp256k1 public key, finds the corresponding Ethereum address.
2029
///
2130
/// Internally, the public key is serialized in uncompressed format at 65 bytes (0x04 || x || y),
2231
/// and then (x || y) is hashed using Keccak256. The last 20 bytes of this hash is taken as the address.
2332
#[inline]
24-
pub fn to_address(public_key: &PublicKey) -> [u8; 20] {
33+
pub fn public_key_to_address(public_key: &libsecp256k1::PublicKey) -> [u8; 20] {
2534
let public_key_xy = &public_key.serialize()[1..];
2635
let mut addr = [0u8; 20];
2736
addr.copy_from_slice(&keccak256hash(public_key_xy)[12..32]);
2837
addr
2938
}
3039

31-
/// Converts a `libsecp256k1::SecretKey` to a `libp2p_identity::secp256k1::Keypair`.
32-
/// To do this, we serialize the secret key and create a new keypair from it.
33-
#[inline]
34-
pub fn secret_to_keypair(secret_key: &SecretKey) -> libp2p_identity::Keypair {
35-
let bytes = secret_key.serialize();
36-
37-
let secret_key = dkn_p2p::libp2p_identity::secp256k1::SecretKey::try_from_bytes(bytes)
38-
.expect("Failed to create secret key");
39-
libp2p_identity::secp256k1::Keypair::from(secret_key).into()
40-
}
41-
4240
/// Converts a `libsecp256k1::PublicKey` to a `libp2p_identity::PeerId`.
4341
/// To do this, we serialize the secret key and create a new keypair from it.
4442
#[inline]
45-
pub fn public_key_to_peer_id(public_key: &PublicKey) -> libp2p_identity::PeerId {
43+
pub fn public_key_to_peer_id(public_key: &libsecp256k1::PublicKey) -> libp2p_identity::PeerId {
4644
let bytes = public_key.serialize_compressed();
4745

4846
let public_key = dkn_p2p::libp2p_identity::secp256k1::PublicKey::try_from_bytes(&bytes)
@@ -73,7 +71,7 @@ mod tests {
7371
fn test_address() {
7472
let sk = SecretKey::parse_slice(DUMMY_SECRET_KEY).expect("Should parse key.");
7573
let pk = PublicKey::from_secret_key(&sk);
76-
let addr = to_address(&pk);
74+
let addr = public_key_to_address(&pk);
7775
assert_eq!(
7876
"D79Fdf178547614CFdd0dF6397c53569716Bd596".to_lowercase(),
7977
hex::encode(addr)

compute/src/utils/message.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ impl DriaMessage {
7878

7979
/// Checks if the payload is signed by the owner of one of the given peer ids.
8080
pub(crate) fn is_signed(&self, authorized_peerids: &HashSet<PeerId>) -> Result<bool> {
81+
let recovered_public_key = self.get_origin()?;
82+
let recovered_peer_id = public_key_to_peer_id(&recovered_public_key);
83+
84+
Ok(authorized_peerids.contains(&recovered_peer_id))
85+
}
86+
87+
/// Recovers the public key of the message origin, i.e. the owner of the signature.
88+
pub(crate) fn get_origin(&self) -> Result<libsecp256k1::PublicKey> {
8189
let signature_bytes =
8290
hex::decode(&self.signature).wrap_err("could not decode signature hex")?;
8391
let signature = Signature::parse_standard_slice(&signature_bytes)
@@ -89,10 +97,7 @@ impl DriaMessage {
8997
// verify signature w.r.t the payload and the given public key
9098
let message = Message::parse(&sha256hash(&self.payload));
9199

92-
let recovered_public_key = recover(&message, &signature, &recovery_id)?;
93-
let recovered_peer_id = public_key_to_peer_id(&recovered_public_key);
94-
95-
Ok(authorized_peerids.contains(&recovered_peer_id))
100+
recover(&message, &signature, &recovery_id).wrap_err("could not recover public key")
96101
}
97102

98103
/// Converts the message to bytes.

0 commit comments

Comments
 (0)