Skip to content
This repository has been archived by the owner on Sep 21, 2024. It is now read-only.

Commit

Permalink
feat: Expose ipfs-api-url to orb-ns to integrate IPFS cid resolution …
Browse files Browse the repository at this point in the history
…in NS validation. (#265)
  • Loading branch information
jsantell authored Mar 9, 2023
1 parent b79872a commit d1bdc29
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 36 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions rust/noosphere-ns/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ ucan-key-support = { version = "0.1.0" }
tokio = { version = "1.15", features = ["io-util", "io-std", "sync", "macros", "rt", "rt-multi-thread"] }
noosphere-storage = { version = "0.4.2", path = "../noosphere-storage" }
noosphere-core = { version = "0.6.3", path = "../noosphere-core" }
libp2p = { version = "0.51.0", default-features = false, features = [ "identify", "dns", "kad", "macros", "mplex", "noise", "serde", "tcp", "tokio", "yamux" ] }

# noosphere_ns::bin
noosphere = { version = "0.6.3", path = "../noosphere", optional = true }
noosphere-ipfs = { version = "0.1.2", path = "../noosphere-ipfs", optional = true }
clap = { version = "^4.1", features = ["derive"], optional = true }
home = { version = "~0.5", optional = true }
toml = { version = "~0.5", optional = true }

# noosphere_ns::server
axum = { version = "~0.5", features = ["json", "headers", "macros"], optional = true }
reqwest = { version = "~0.11", default-features = false, features = ["json", "rustls-tls"], optional = true }
tracing-subscriber = { version = "~0.3", features = ["env-filter"], optional = true }
tower-http = { version = "~0.3", features = ["trace"], optional = true }
url = { version = "^2", features = [ "serde" ], optional = true }
libp2p = { version = "0.51.0", default-features = false, features = [ "identify", "dns", "kad", "macros", "mplex", "noise", "serde", "tcp", "tokio", "yamux" ] }

[dev-dependencies]

Expand All @@ -61,7 +64,7 @@ tempdir = { version = "~0.3" }
[features]
default = ["orb-ns", "api-server"]
api-server = ["axum", "reqwest", "url", "tracing-subscriber", "tower-http"]
orb-ns = ["clap", "noosphere", "home", "toml"]
orb-ns = ["clap", "noosphere", "home", "toml", "noosphere-ipfs"]

[[bin]]
name = "orb-ns"
Expand Down
41 changes: 24 additions & 17 deletions rust/noosphere-ns/src/bin/orb-ns/cli/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,46 @@ pub fn parse_cli_address<T: TryFrom<CLIAddress>>(input: &str) -> Result<T, Strin
.map_err(|_| String::from("invalid conversion"))
}

/// Parses a string in [CLIAddress] form into a [SocketAddr] for
/// serde deserialization.
/// Parses a string in [CLIAddress] form into a [SocketAddr] for serde deserialization.
pub fn deserialize_socket_addr<'de, D>(deserializer: D) -> Result<Option<SocketAddr>, D::Error>
where
D: Deserializer<'de>,
{
match Option::<CLIAddress>::deserialize(deserializer) {
Ok(address) => match address {
Some(addr) => match addr.try_into() {
Ok(socket) => Ok(Some(socket)),
Err(e) => Err(de::Error::custom(e.to_string())),
},
None => Ok(None),
},
Err(e) => Err(de::Error::custom(e.to_string())),
}
deserialize_cliaddress::<'de, D, SocketAddr>(deserializer)
}

/// Parses a string in [CLIAddress] form into a [Multiaddr] for
/// serde deserialization.
/// Parses a string in [CLIAddress] form into a [Multiaddr] for serde deserialization.
pub fn deserialize_multiaddr<'de, D>(deserializer: D) -> Result<Option<Multiaddr>, D::Error>
where
D: Deserializer<'de>,
{
deserialize_cliaddress::<'de, D, Multiaddr>(deserializer)
}

/// Parses a string in [CLIAddress] form into a [Url] for serde deserialization.
pub fn deserialize_url<'de, D>(deserializer: D) -> Result<Option<Url>, D::Error>
where
D: Deserializer<'de>,
{
deserialize_cliaddress::<'de, D, Url>(deserializer)
}

/// Implementation for the serde deserialization methods e.g. `deserialize_url`.
fn deserialize_cliaddress<'de, D, T>(deserializer: D) -> Result<Option<T>, D::Error>
where
D: Deserializer<'de>,
T: TryFrom<CLIAddress>,
<T as TryFrom<CLIAddress>>::Error: ToString,
{
match Option::<CLIAddress>::deserialize(deserializer) {
Ok(address) => match address {
Some(addr) => match addr.try_into() {
Ok(maddr) => Ok(Some(maddr)),
Some(cli_addr) => match cli_addr.try_into() {
Ok(t_addr) => Ok(Some(t_addr)),
Err(e) => Err(de::Error::custom(e.to_string())),
},
None => Ok(None),
},
Err(e) => Err(de::Error::custom(e.to_string())),
Err(e) => Err(e),
}
}

Expand Down
14 changes: 13 additions & 1 deletion rust/noosphere-ns/src/bin/orb-ns/cli/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![cfg(not(target_arch = "wasm32"))]

use crate::cli::address::{deserialize_multiaddr, deserialize_socket_addr, parse_cli_address};
use crate::cli::address::{
deserialize_multiaddr, deserialize_socket_addr, deserialize_url, parse_cli_address,
};
use clap::{Parser, Subcommand};
use noosphere_core::data::Did;
use noosphere_ns::{DHTConfig, Multiaddr, NSRecord};
Expand Down Expand Up @@ -55,6 +57,14 @@ pub enum CLICommand {
/// the default bootstrap peers.
#[arg(long, default_value_t = false)]
no_default_peers: bool,

/// If no configuration path provided, the URL to a Kubo HTTP RPC
/// service to resolve content-addressable content.
#[arg(
long,
value_parser = parse_cli_address::<Url>
)]
ipfs_api_url: Option<Url>,
},

/// Utility to create keys compatible with Noosphere.
Expand Down Expand Up @@ -116,4 +126,6 @@ pub struct CLIConfigFile {
pub no_default_peers: bool,
#[serde(default)]
pub dht_config: DHTConfig,
#[serde(default, deserialize_with = "deserialize_url")]
pub ipfs_api_url: Option<Url>,
}
1 change: 1 addition & 0 deletions rust/noosphere-ns/src/bin/orb-ns/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ mod test {
api_address: Some("127.0.0.1:0".parse().unwrap()),
peers: None,
no_default_peers: true,
ipfs_api_url: None,
},
&key_storage,
)
Expand Down
7 changes: 2 additions & 5 deletions rust/noosphere-ns/src/bin/orb-ns/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ mod inner {
pub use crate::cli;
pub use anyhow::{anyhow, Result};
pub use noosphere::key::{InsecureKeyStorage, KeyStorage};
pub use noosphere_core::tracing::initialize_tracing;
pub use tokio;
pub use tracing::*;
pub use tracing_subscriber::{fmt, prelude::*, EnvFilter};
}

#[cfg(not(target_arch = "wasm32"))]
Expand All @@ -28,10 +28,7 @@ use inner::*;
#[cfg(not(target_arch = "wasm32"))]
#[tokio::main]
async fn main() -> Result<()> {
tracing_subscriber::registry()
.with(fmt::layer())
.with(EnvFilter::from_default_env())
.init();
initialize_tracing();

let key_storage = InsecureKeyStorage::new(&utils::get_keys_dir()?)?;
cli::process_args(&key_storage)
Expand Down
20 changes: 14 additions & 6 deletions rust/noosphere-ns/src/bin/orb-ns/runner/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use anyhow::{anyhow, Result};
use noosphere::key::InsecureKeyStorage;
use noosphere_ns::{DHTConfig, Multiaddr, BOOTSTRAP_PEERS};
use std::net::SocketAddr;

use ucan_key_support::ed25519::Ed25519KeyMaterial;
use url::Url;

/// Configuration for [NameSystemRunner], hydrated/resolved from CLI.
pub struct RunnerNodeConfig {
Expand All @@ -14,6 +14,7 @@ pub struct RunnerNodeConfig {
pub listening_address: Option<Multiaddr>,
pub peers: Vec<Multiaddr>,
pub dht_config: DHTConfig,
pub ipfs_api_url: Option<Url>,
}

impl RunnerNodeConfig {
Expand All @@ -26,6 +27,7 @@ impl RunnerNodeConfig {
let dht_config = config.dht_config;
let listening_address = config.listening_address;
let api_address = config.api_address;
let ipfs_api_url = config.ipfs_api_url;
let mut peers = config.peers;
if !config.no_default_peers {
peers.extend_from_slice(&BOOTSTRAP_PEERS[..]);
Expand All @@ -37,6 +39,7 @@ impl RunnerNodeConfig {
listening_address,
peers,
dht_config,
ipfs_api_url,
})
}

Expand All @@ -53,6 +56,7 @@ impl RunnerNodeConfig {
no_default_peers,
listening_address,
api_address,
ipfs_api_url,
} => match config {
Some(config_path) => {
let toml_str = tokio::fs::read_to_string(&config_path).await?;
Expand All @@ -69,11 +73,7 @@ impl RunnerNodeConfig {
vec![]
};

let dht_config = if cfg!(test) {
DHTConfig::default()
} else {
DHTConfig::default()
};
let dht_config = DHTConfig::default();

let config = CLIConfigFile {
key: key_name.clone(),
Expand All @@ -82,6 +82,7 @@ impl RunnerNodeConfig {
peers: bootstrap_peers,
no_default_peers,
dht_config,
ipfs_api_url,
};
Ok(RunnerNodeConfig::try_from_config(key_storage, config).await?)
}
Expand Down Expand Up @@ -152,6 +153,7 @@ mod tests {
listening_address: Some("/ip4/127.0.0.1/tcp/6666".parse()?),
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
&env.key_storage,
)
Expand Down Expand Up @@ -199,6 +201,7 @@ peers = [
listening_address: None,
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
&env.key_storage,
)
Expand Down Expand Up @@ -250,6 +253,7 @@ no_default_peers = true
listening_address: None,
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
&env.key_storage,
)
Expand Down Expand Up @@ -282,6 +286,7 @@ listening_address = 10000
listening_address: Some("/ip4/127.0.0.1/tcp/6666".parse()?),
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
CLICommand::Run {
api_address: None,
Expand All @@ -290,6 +295,7 @@ listening_address = 10000
listening_address: Some("/ip4/127.0.0.1/tcp/6666".parse()?),
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
CLICommand::Run {
api_address: None,
Expand All @@ -298,6 +304,7 @@ listening_address = 10000
listening_address: None,
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
CLICommand::Run {
api_address: None,
Expand All @@ -306,6 +313,7 @@ listening_address = 10000
listening_address: None,
peers: None,
no_default_peers: false,
ipfs_api_url: None,
},
];

Expand Down
18 changes: 13 additions & 5 deletions rust/noosphere-ns/src/bin/orb-ns/runner/runner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::runner::config::RunnerNodeConfig;
use anyhow::Result;
use noosphere_ipfs::{IpfsStorage, KuboClient};
#[cfg(feature = "api-server")]
use noosphere_ns::server::APIServer;
use noosphere_ns::{Multiaddr, NameSystem, NameSystemClient, PeerId};
use noosphere_storage::{MemoryStorage, SphereDb};
use serde::Serialize;
Expand All @@ -13,9 +16,6 @@ use std::{
use tokio::sync::Mutex;
use url::Url;

#[cfg(feature = "api-server")]
use noosphere_ns::server::APIServer;

#[cfg(not(feature = "api-server"))]
struct APIServer;
#[cfg(not(feature = "api-server"))]
Expand Down Expand Up @@ -44,8 +44,16 @@ pub struct NameSystemRunner {

impl NameSystemRunner {
pub(crate) async fn try_from_config(mut config: RunnerNodeConfig) -> Result<Self> {
let store = SphereDb::new(&MemoryStorage::default()).await?;
let node = NameSystem::new(&config.key_material, store, config.dht_config.to_owned())?;
let node = if let Some(ipfs_api_url) = config.ipfs_api_url {
let kubo = KuboClient::new(&ipfs_api_url)?;
let store =
SphereDb::new(&IpfsStorage::new(MemoryStorage::default(), Some(kubo))).await?;
NameSystem::new(&config.key_material, store, config.dht_config.to_owned())?
} else {
let store = SphereDb::new(&MemoryStorage::default()).await?;
NameSystem::new(&config.key_material, store, config.dht_config.to_owned())?
};

let peer_id = node.peer_id().to_owned();

let listening_address = if let Some(requested_addr) = config.listening_address.take() {
Expand Down

0 comments on commit d1bdc29

Please sign in to comment.