Skip to content

Commit

Permalink
backup identity handler
Browse files Browse the repository at this point in the history
  • Loading branch information
rkdud007 committed Apr 28, 2024
1 parent 39913dd commit 4586826
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 12 deletions.
31 changes: 31 additions & 0 deletions crates/common/src/identity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use starknet::{core::types::FieldElement, signers::SigningKey};

pub struct IdentityHandler {
/// Key pair for the p2p network.
/// This represents the identity of the node in the network.
pub p2p_keypair: libp2p::identity::Keypair,
/// The signing key for the StarkNet network.
/// This is used to sign messages and transactions.
pub signing_key: SigningKey,
}

impl IdentityHandler {
pub fn new(private_key: Vec<u8>) -> Self {
let secret_key = libp2p::identity::ecdsa::SecretKey::try_from_bytes(private_key.as_slice())
.expect("Failed to create secret key from private key.");
let p2p_keypair =
libp2p::identity::Keypair::from(libp2p::identity::ecdsa::Keypair::from(secret_key));
let signing_key = SigningKey::from_secret_scalar(
FieldElement::from_byte_slice_be(private_key.as_slice()).unwrap(),
);
Self { p2p_keypair, signing_key }
}

pub fn get_keypair(&self) -> libp2p::identity::Keypair {
self.p2p_keypair.clone()
}

pub fn get_signing_key(&self) -> SigningKey {
self.signing_key.clone()
}
}
1 change: 1 addition & 0 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod hash_macro;
pub mod identity;
pub mod job;
pub mod job_record;
pub mod job_trace;
Expand Down
12 changes: 6 additions & 6 deletions crates/delegator/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![deny(unused_crate_dependencies)]

use futures::StreamExt;
use libp2p::{
gossipsub::Event,
identity::{ecdsa, Keypair},
};
use libp2p::gossipsub::Event;
use sharp_p2p_common::{
hash,
identity::IdentityHandler,
job::Job,
network::Network,
topic::{gossipsub_ident_topic, Topic},
Expand All @@ -28,8 +28,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let private_key =
hex::decode("018ef9563461ec2d88236d59039babf44c97d8bf6200d01d81170f1f60a78f32")?;
let p2p_local_keypair =
Keypair::from(ecdsa::Keypair::from(ecdsa::SecretKey::try_from_bytes(&private_key)?));
let identity_handler = IdentityHandler::new(private_key);
let p2p_local_keypair = identity_handler.get_keypair();

// Generate topic
let new_job_topic = gossipsub_ident_topic(Network::Sepolia, Topic::NewJob);
Expand Down
10 changes: 4 additions & 6 deletions crates/executor/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#![deny(unused_crate_dependencies)]

use futures::{stream::FuturesUnordered, StreamExt};
use libp2p::{
gossipsub::Event,
identity::{ecdsa, Keypair},
};
use libp2p::gossipsub::Event;
use sharp_p2p_common::{
hash,
identity::IdentityHandler,
job::Job,
job_record::JobRecord,
job_trace::JobTrace,
Expand Down Expand Up @@ -44,8 +42,8 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {

let private_key =
hex::decode("07c7a41c77c7a3b19e7c77485854fc88b09ed7041361595920009f81236d55d2")?;
let p2p_local_keypair =
Keypair::from(ecdsa::Keypair::from(ecdsa::SecretKey::try_from_bytes(&private_key)?));
let identity_handler = IdentityHandler::new(private_key);
let p2p_local_keypair = identity_handler.get_keypair();

// Generate topic
let new_job_topic = gossipsub_ident_topic(Network::Sepolia, Topic::NewJob);
Expand Down

0 comments on commit 4586826

Please sign in to comment.