Skip to content

Commit

Permalink
chore: Added additional tool and cleanup (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
dariusc93 authored Nov 13, 2023
1 parent 994690e commit 6a042d7
Show file tree
Hide file tree
Showing 9 changed files with 340 additions and 20 deletions.
3 changes: 2 additions & 1 deletion extensions/warp-ipfs/examples/identity-interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use warp::crypto::DID;
use warp::multipass::identity::{Identifier, IdentityProfile, IdentityStatus, IdentityUpdate};
use warp::multipass::{IdentityImportOption, ImportLocation, MultiPass};
use warp::tesseract::Tesseract;
use warp_ipfs::config::{Config, Discovery, DiscoveryType};
use warp_ipfs::config::{Bootstrap, Config, Discovery, DiscoveryType};
use warp_ipfs::WarpIpfsBuilder;

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -96,6 +96,7 @@ async fn account(
}
if opt.no_discovery {
config.store_setting.discovery = Discovery::None;
config.bootstrap = Bootstrap::None;
config.ipfs_setting.bootstrap = false;
}

Expand Down
5 changes: 1 addition & 4 deletions extensions/warp-ipfs/src/behaviour/phonebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,7 @@ impl NetworkBehaviour for Behaviour {
}
}

fn poll(
&mut self,
cx: &mut Context,
) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
fn poll(&mut self, cx: &mut Context) -> Poll<ToSwarm<Self::ToSwarm, THandlerInEvent<Self>>> {
if let Some(event) = self.events.pop_front() {
return Poll::Ready(event);
}
Expand Down
7 changes: 4 additions & 3 deletions extensions/warp-ipfs/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
str::FromStr,
time::Duration,
};
use warp::{multipass::identity::Identity, constellation::file::FileType};
use warp::{constellation::file::FileType, multipass::identity::Identity};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -249,8 +249,9 @@ pub enum UpdateEvents {
Disable,
}

pub type DefaultPfpFn =
std::sync::Arc<dyn Fn(&Identity) -> Result<(Vec<u8>, FileType), std::io::Error> + Send + Sync + 'static>;
pub type DefaultPfpFn = std::sync::Arc<
dyn Fn(&Identity) -> Result<(Vec<u8>, FileType), std::io::Error> + Send + Sync + 'static,
>;

#[derive(Clone, Serialize, Deserialize)]
pub struct StoreSetting {
Expand Down
23 changes: 17 additions & 6 deletions extensions/warp-ipfs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,8 +321,10 @@ impl WarpIpfs {
uninitialized = uninitialized.set_path(path);
}

for addr in config.bootstrap.address() {
uninitialized = uninitialized.add_bootstrap(addr);
if config.ipfs_setting.bootstrap {
for addr in config.bootstrap.address() {
uninitialized = uninitialized.add_bootstrap(addr);
}
}

if config.ipfs_setting.memory_transport {
Expand Down Expand Up @@ -398,10 +400,19 @@ impl WarpIpfs {
}

for relay_peer in relay_peers {
if let Err(e) = ipfs.enable_relay(Some(relay_peer)).await {
error!("Failed to use {relay_peer} as a relay: {e}");
continue;
}
match tokio::time::timeout(Duration::from_secs(15), ipfs.enable_relay(Some(relay_peer)))
.await
{
Ok(Ok(_)) => {}
Ok(Err(e)) => {
error!("Failed to use {relay_peer} as a relay: {e}");
continue;
}
Err(_) => {
error!("Relay connection timed out");
continue;
}
};

let list = ipfs.list_relays(true).await.unwrap_or_default();
for addr in list
Expand Down
8 changes: 3 additions & 5 deletions extensions/warp-ipfs/tests/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -766,7 +766,6 @@ mod test {
assert!(conversation.recipients().contains(&did_b));
assert!(conversation.recipients().contains(&did_c));


let mut conversation_a = chat_a.get_conversation_stream(id_a).await?;
let mut conversation_b = chat_b.get_conversation_stream(id_b).await?;

Expand Down Expand Up @@ -918,15 +917,15 @@ mod test {
assert!(conversation.recipients().contains(&did_b));
assert!(conversation.recipients().contains(&did_c));


let mut conversation_a = chat_a.get_conversation_stream(id_a).await?;
let mut conversation_b = chat_b.get_conversation_stream(id_b).await?;

_account_c.block(&did_a).await?;

tokio::time::timeout(Duration::from_secs(60), async {
loop {
if let Some(MultiPassEventKind::BlockedBy { did }) = account_subscribe_a.next().await
if let Some(MultiPassEventKind::BlockedBy { did }) =
account_subscribe_a.next().await
{
assert_eq!(did, did_c);
break;
Expand Down Expand Up @@ -967,8 +966,7 @@ mod test {

tokio::time::timeout(Duration::from_secs(60), async {
loop {
if let Some(MultiPassEventKind::Blocked { did }) =
account_subscribe_c.next().await
if let Some(MultiPassEventKind::Blocked { did }) = account_subscribe_c.next().await
{
assert_eq!(did, did_a);
break;
Expand Down
28 changes: 28 additions & 0 deletions tools/relay-server/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "relay-server"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
repository.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
rust-ipfs = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tokio-util = { workspace = true, features = ["full"] }
tokio-stream = { workspace = true, features = ["net"] }
futures.workspace = true
futures-timer.workspace = true
async-trait.workspace = true
async-stream.workspace = true
anyhow.workspace = true
serde.workspace = true
serde_json.workspace = true
void.workspace = true
tracing.workspace = true
clap = { version = "4.4", features = ["derive"] }
zeroize = "1"
dotenv = "0.15"
base64 = "0.21"
47 changes: 47 additions & 0 deletions tools/relay-server/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::{error::Error, path::Path};

use base64::{
alphabet::STANDARD,
engine::{general_purpose::PAD, GeneralPurpose},
Engine,
};
use rust_ipfs::{Keypair, PeerId};
use serde::Deserialize;
use zeroize::Zeroizing;

#[derive(Clone, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct IpfsConfig {
pub identity: Identity,
}

impl IpfsConfig {
pub fn load<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn Error>> {
let file = std::fs::File::open(path)?;
let config = serde_json::from_reader(file)?;
Ok(config)
}
}

#[derive(Deserialize, Clone)]
#[serde(rename_all = "PascalCase")]
pub struct Identity {
#[serde(rename = "PeerID")]
pub peer_id: PeerId,
pub priv_key: String,
}

impl Identity {
pub fn keypair(&self) -> Result<Keypair, Box<dyn Error>> {
let engine = GeneralPurpose::new(&STANDARD, PAD);
let keypair_bytes = Zeroizing::new(engine.decode(self.priv_key.as_bytes())?);
let keypair = Keypair::from_protobuf_encoding(&keypair_bytes)?;
Ok(keypair)
}
}

impl zeroize::Zeroize for IpfsConfig {
fn zeroize(&mut self) {
self.identity.priv_key.zeroize();
}
}
Loading

0 comments on commit 6a042d7

Please sign in to comment.