Skip to content

Commit

Permalink
Add Service Endpoints to Cli Did Web and Dht (#400)
Browse files Browse the repository at this point in the history
* update cli with service endpoints

* update
  • Loading branch information
nitro-neal authored Oct 22, 2024
1 parent 6f6c336 commit fc8564f
Showing 1 changed file with 69 additions and 14 deletions.
83 changes: 69 additions & 14 deletions crates/web5_cli/src/dids/create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clap::Subcommand;
use std::sync::Arc;
use url::Url;
use web5::{
crypto::key_managers::in_memory_key_manager::InMemoryKeyManager,
dids::{
Expand All @@ -11,7 +12,7 @@ use web5::{
portable_did::PortableDid,
},
};

use web5::dids::data_model::service::Service;
use crate::utils::warn_if_not_root;

#[derive(Subcommand, Debug)]
Expand All @@ -25,11 +26,25 @@ pub enum Commands {
Web {
domain: String,
#[arg(long)]
service_endpoint: Option<String>,
#[arg(
long = "service-endpoint-type",
default_value = "LinkedDomains"
)]
service_endpoint_type: String,
#[arg(long)]
no_indent: bool,
#[arg(long)]
json_escape: bool,
},
Dht {
#[arg(long)]
service_endpoint: Option<String>,
#[arg(
long = "service-endpoint-type",
default_value = "LinkedDomains"
)]
service_endpoint_type: String,
#[arg(long)]
no_publish: bool,
#[arg(long)]
Expand Down Expand Up @@ -75,27 +90,55 @@ impl Commands {
}
Commands::Web {
domain,
service_endpoint,
service_endpoint_type,
no_indent,
json_escape,
} => {
// Check if the current process has root privileges because the InMemoryKeyManager may require root privileges
warn_if_not_root();
let key_manager = Arc::new(InMemoryKeyManager::new());

let bearer_did = DidWeb::create(
domain,
Some(DidWebCreateOptions {
key_manager: Some(key_manager.clone()),
..Default::default()
}),
)
.unwrap();
let mut did_web_create_options = DidWebCreateOptions {
key_manager: Some(key_manager.clone()),
..Default::default()
};

// Parse the domain to extract the host without the protocol
let domain_host = match Url::parse(domain) {
Ok(parsed_url) => parsed_url.host_str().unwrap_or(domain).to_string(),
Err(_) => {
// Try adding "https://" if parsing fails
match Url::parse(&format!("https://{}", domain)) {
Ok(parsed_url) => parsed_url.host_str().unwrap_or(domain).to_string(),
Err(_) => {
eprintln!("Error: Invalid domain provided.");
return;
}
}
}
};

// If a service endpoint is provided, add it to the DID document
if let Some(service_endpoint_url) = service_endpoint {
let service = Service {
id: format!("did:web:{}#service-1", domain_host),
r#type: service_endpoint_type.clone(),
service_endpoint: vec![service_endpoint_url.clone()],
};
did_web_create_options.service = Some(vec![service]);
}

let bearer_did = DidWeb::create(domain, Some(did_web_create_options))
.unwrap();

let portable_did = bearer_did.to_portable_did(key_manager).unwrap();

print_portable_did(portable_did, no_indent, json_escape)
print_portable_did(portable_did, no_indent, json_escape);
}
Commands::Dht {
service_endpoint,
service_endpoint_type,
no_publish,
no_indent,
json_escape,
Expand All @@ -104,13 +147,25 @@ impl Commands {
warn_if_not_root();
let key_manager = Arc::new(InMemoryKeyManager::new());

let bearer_did = DidDht::create(Some(DidDhtCreateOptions {
let mut did_dht_create_options = DidDhtCreateOptions {
publish: Some(!no_publish),
key_manager: Some(key_manager.clone()),
..Default::default()
}))
.await
.unwrap();
};

// If a service endpoint is provided, add it to the DID document
if let Some(service_endpoint_url) = service_endpoint {
let service = Service {
id: "did:dht:#service-1".to_string(),
r#type: service_endpoint_type.clone(),
service_endpoint: vec![service_endpoint_url.clone()],
};
did_dht_create_options.service = Some(vec![service]);
}

let bearer_did = DidDht::create(Some(did_dht_create_options))
.await
.unwrap();

let portable_did = bearer_did.to_portable_did(key_manager).unwrap();

Expand Down

0 comments on commit fc8564f

Please sign in to comment.