Skip to content

Commit

Permalink
feat: Add @ dialect to p2p key parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
aidan46 committed Jan 10, 2025
1 parent 56b859a commit 13b34bf
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/p2p-config/bootstrap.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
address = "/ip4/127.0.0.1/tcp/62649"
keypair = "examples/p2p-keys/private.pem"
keypair = "@examples/p2p-keys/private.pem"
2 changes: 1 addition & 1 deletion examples/p2p-config/register.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
keypair = "storage-provider/server/p2p-example-config/private.pem"
keypair = "@storage-provider/server/p2p-example-config/private.pem"
rendezvous_point = "12D3KooWS8tjUoiMgh2hZ12EyDjnJuEoa1zxq7G7EQtqafRUT8fr"
rendezvous_point_address = "/ip4/127.0.0.1/tcp/62649"
33 changes: 14 additions & 19 deletions storage-provider/server/src/p2p.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
fmt::Display,
path::{Path, PathBuf},
};
use std::{fmt::Display, path::PathBuf, str::FromStr};

use bootstrap::{BootstrapBehaviour, BootstrapBehaviourEvent};
use clap::ValueEnum;
Expand Down Expand Up @@ -35,10 +32,6 @@ impl Display for NodeType {

#[derive(Debug, thiserror::Error)]
pub enum P2PError {
#[error(transparent)]
SigningKeyError(#[from] ed25519_dalek::pkcs8::Error),
#[error(transparent)]
DecodingError(#[from] libp2p::identity::DecodingError),
#[error(transparent)]
Io(#[from] std::io::Error),
#[error(transparent)]
Expand All @@ -55,17 +48,19 @@ pub enum P2PError {
P2PTransportError(#[from] libp2p::TransportError<std::io::Error>),
}

pub fn create_keypair<P: AsRef<Path> + std::fmt::Debug>(path: P) -> Result<Keypair, P2PError> {
info!("Creating keypair from pem file at {path:?}");
let key = SigningKey::read_pkcs8_pem_file(path)?;
let keypair = Keypair::ed25519_from_bytes(key.to_bytes())?;

Ok(keypair)
}

fn path_to_keypair<'de, D: de::Deserializer<'de>>(d: D) -> Result<Keypair, D::Error> {
let path: PathBuf = de::Deserialize::deserialize(d)?;
create_keypair(path).map_err(de::Error::custom)
fn deser_keypair<'de, D: de::Deserializer<'de>>(d: D) -> Result<Keypair, D::Error> {
let src: String = de::Deserialize::deserialize(d)?;
let key = if let Some(stripped) = src.strip_prefix('@') {
let path = PathBuf::from_str(stripped)
.map_err(de::Error::custom)?
.canonicalize()
.map_err(de::Error::custom)?;
SigningKey::read_pkcs8_pem_file(path).map_err(de::Error::custom)?
} else {
let hex_key = hex::decode(src).map_err(de::Error::custom)?;
SigningKey::try_from(hex_key.as_slice()).map_err(de::Error::custom)?
};
Keypair::ed25519_from_bytes(key.to_bytes()).map_err(de::Error::custom)
}

pub async fn run_bootstrap_node(
Expand Down
4 changes: 2 additions & 2 deletions storage-provider/server/src/p2p/bootstrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libp2p::{
};
use serde::Deserialize;

use super::{path_to_keypair, P2PError};
use super::{deser_keypair, P2PError};

#[derive(NetworkBehaviour)]
pub struct BootstrapBehaviour {
Expand All @@ -17,7 +17,7 @@ pub struct BootstrapBehaviour {
#[derive(Deserialize)]
pub struct BootstrapConfig {
address: Multiaddr,
#[serde(deserialize_with = "path_to_keypair")]
#[serde(deserialize_with = "deser_keypair")]
keypair: Keypair,
}

Expand Down
4 changes: 2 additions & 2 deletions storage-provider/server/src/p2p/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use libp2p::{
};
use serde::{de, Deserialize};

use super::{path_to_keypair, P2PError};
use super::{deser_keypair, P2PError};

#[derive(NetworkBehaviour)]
pub struct RegisterBehaviour {
Expand All @@ -21,7 +21,7 @@ fn string_to_peer_id<'de, D: de::Deserializer<'de>>(d: D) -> Result<PeerId, D::E

#[derive(Deserialize)]
pub struct RegisterConfig {
#[serde(deserialize_with = "path_to_keypair")]
#[serde(deserialize_with = "deser_keypair")]
keypair: Keypair,
rendezvous_point_address: Multiaddr,
#[serde(deserialize_with = "string_to_peer_id")]
Expand Down
7 changes: 1 addition & 6 deletions storagext/cli/src/cmd/storage_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,7 @@ impl StorageProviderCommand {
Client: StorageProviderClientExt,
{
let submission_result = client
.register_storage_provider(
&account_keypair,
peer_id,
post_proof,
wait_for_finalization,
)
.register_storage_provider(&account_keypair, peer_id, post_proof, wait_for_finalization)
.await?
.inspect(|result| {
tracing::debug!(
Expand Down

0 comments on commit 13b34bf

Please sign in to comment.