diff --git a/examples/nodejs/package.json b/examples/nodejs/package.json index c65f7831..7a1595eb 100644 --- a/examples/nodejs/package.json +++ b/examples/nodejs/package.json @@ -23,7 +23,8 @@ "verify_didcomm_encrypted_message": "npm run exec src/verify_didcomm_encrypted_message.ts", "verify_didcomm_plaintext_message": "npm run exec src/verify_didcomm_plaintext_message.ts", "verify_didcomm_signed_message": "npm run exec src/verify_didcomm_signed_message.ts", - "verify_vc": "npm run exec src/verify_vc.ts" + "verify_vc": "npm run exec src/verify_vc.ts", + "network": "npm run exec src/network.ts" }, "dependencies": { "got": "^12.5.3", diff --git a/examples/nodejs/src/network.ts b/examples/nodejs/src/network.ts new file mode 100644 index 00000000..e7cc3252 --- /dev/null +++ b/examples/nodejs/src/network.ts @@ -0,0 +1,14 @@ +import { post } from './sock.js' + +(async () => { + const json = await post('/internal/network', { + message: { + "recipient_dids": ["123", "456"], + "hub_endpoint": "https://hub.example.com", + "heartbeat": 123, + "trm": "true", + } + }) + + console.log(json) +})() \ No newline at end of file diff --git a/src/controllers/internal/mod.rs b/src/controllers/internal/mod.rs index bc992576..a5a5b768 100644 --- a/src/controllers/internal/mod.rs +++ b/src/controllers/internal/mod.rs @@ -8,3 +8,4 @@ pub mod didcomm_generate_signed; pub mod didcomm_verify_encrypted; pub mod didcomm_verify_plaintext; pub mod didcomm_verify_signed; +pub mod network; diff --git a/src/controllers/internal/network.rs b/src/controllers/internal/network.rs new file mode 100644 index 00000000..8e16634f --- /dev/null +++ b/src/controllers/internal/network.rs @@ -0,0 +1,47 @@ +use crate::network::Network; +use actix_web::{web, HttpRequest, HttpResponse}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +// NOTE: POST /internal/version +#[derive(Deserialize, Serialize)] +pub struct MessageContainer { + message: Value, +} + +pub async fn handler( + _req: HttpRequest, + web::Json(json): web::Json, +) -> actix_web::Result { + let recipient_dids = match json.message["recipient_dids"].as_array() { + Some(url) => url, + None => return Ok(HttpResponse::BadRequest().json("recipient_dids is required")), + }; + let recipient_dids: Vec = recipient_dids.iter().map(|v| v.to_string()).collect(); + + let hub_endpoint = match json.message["hub_endpoint"].as_str() { + Some(p) => p, + None => return Ok(HttpResponse::BadRequest().json("hub_endpoint is required")), + }; + let heartbeat: u64 = match json.message["heartbeat"].as_u64() { + Some(h) => h, + None => return Ok(HttpResponse::BadRequest().json("heartbeat is required")), + }; + let trm = match json.message["trm"].as_str() { + Some(t) => t, + None => return Ok(HttpResponse::BadRequest().json("trm is required")), + }; + let mut network_config = Network::new(); + network_config.root.recipient_dids = Some(recipient_dids); + network_config.root.hub_endpoint = Some(hub_endpoint.to_string()); + network_config.root.heartbeat = Some(heartbeat); + network_config.root.trm = Some(trm.to_string()); + match network_config.save() { + Ok(_) => {} + Err(e) => { + log::error!("{:?}", e); + return Ok(HttpResponse::InternalServerError().json("Internal Server Error")); + } + }; + Ok(HttpResponse::Ok().json("ok")) +} diff --git a/src/main.rs b/src/main.rs index c9691ef8..d7128ea8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,20 +170,20 @@ async fn main() -> std::io::Result<()> { // NOTE: generate Key Chain let node_x = NodeX::new(); - let did = node_x.create_identifier().await.unwrap(); + let device_did = node_x.create_identifier().await.unwrap(); // NOTE: CLI match cli.config { true => { - use_cli(cli.command, did.did_document.id.clone()); + use_cli(cli.command, device_did.did_document.id.clone()); return Ok(()); } false => (), } // NOTE: hub initilize - hub_initilize(did.did_document.id.clone()).await; - send_device_info(did.did_document.id.clone()).await; + hub_initilize(device_did.did_document.id.clone()).await; + send_device_info(device_did.did_document.id.clone()).await; let sock_path = runtime_dir.clone().join("nodex.sock"); @@ -192,7 +192,7 @@ async fn main() -> std::io::Result<()> { let mqtt_port = 1883; let mqtt_client_id = cuid::cuid2(); - let did_id = did.did_document.id; + let did_id = device_did.did_document.id; let mqtt_topic = format!("nodex/{}", did_id); let mut mqtt_options = MqttOptions::new(&mqtt_client_id, mqtt_host, mqtt_port); @@ -263,7 +263,7 @@ async fn main() -> std::io::Result<()> { fn use_cli(command: Option, did: String) { let mut network_config = Network::new(); const SECRET_KEY: &str = "secret_key"; - const PROJECT_ID: &str = "project_id"; + const PROJECT_DID: &str = "project_did"; if let Some(command) = command { match command { @@ -276,9 +276,9 @@ fn use_cli(command: Option, did: String) { network_config.save_secretk_key(&value); print!("Network {} is set", SECRET_KEY); } - PROJECT_ID => { - network_config.save_project_id(&value); - print!("Network {} is set", PROJECT_ID); + PROJECT_DID => { + network_config.save_project_did(&value); + print!("Network {} is set", PROJECT_DID); } _ => { print!("key is not found"); @@ -292,12 +292,12 @@ fn use_cli(command: Option, did: String) { }; print!("Network {} is not set", SECRET_KEY); } - PROJECT_ID => { - if let Some(v) = network_config.get_project_id() { - println!("Network {}: {}", PROJECT_ID, v); + PROJECT_DID => { + if let Some(v) = network_config.get_project_did() { + println!("Network {}: {}", PROJECT_DID, v); return; }; - print!("Network {} is not set", PROJECT_ID); + print!("Network {} is not set", PROJECT_DID); } _ => { print!("key is not found"); @@ -308,9 +308,9 @@ fn use_cli(command: Option, did: String) { } } -async fn hub_initilize(did: String) { +async fn hub_initilize(my_did: String) { let network_config = Network::new(); - // NOTE: check network secret_key and project_id + // NOTE: check network secret_key and project_did match network_config.get_secretk_key() { Some(_) => (), None => { @@ -318,10 +318,10 @@ async fn hub_initilize(did: String) { panic!() } } - match network_config.get_project_id() { + match network_config.get_project_did() { Some(_) => (), None => { - log::error!("Network project_id is not set. Please set project_id use cli"); + log::error!("Network project_did is not set. Please set project_did use cli"); panic!() } } @@ -329,7 +329,7 @@ async fn hub_initilize(did: String) { // NOTE: register device let hub = Hub::new(); match hub - .register_device(did, network_config.get_project_id().unwrap()) + .register_device(my_did, network_config.get_project_did().unwrap()) .await { Ok(()) => (), @@ -340,7 +340,7 @@ async fn hub_initilize(did: String) { }; } -async fn send_device_info(did: String) { +async fn send_device_info(device_did: String) { const VERSION: &str = env!("CARGO_PKG_VERSION"); const OS: &str = env::consts::OS; let mac_address: String = match get_mac_address() { @@ -350,7 +350,7 @@ async fn send_device_info(did: String) { let hub = Hub::new(); match hub - .send_device_info(did, mac_address, VERSION.to_string(), OS.to_string()) + .send_device_info(device_did, mac_address, VERSION.to_string(), OS.to_string()) .await { Ok(()) => (), diff --git a/src/network.rs b/src/network.rs index 02d0313b..0b4551d3 100644 --- a/src/network.rs +++ b/src/network.rs @@ -13,14 +13,18 @@ use crate::nodex::errors::NodeXError; #[serde(default)] #[derive(Default)] pub struct ConfigNetwork { - secret_key: Option, - project_id: Option, + pub secret_key: Option, + pub project_did: Option, + pub recipient_dids: Option>, + pub hub_endpoint: Option, + pub heartbeat: Option, + pub trm: Option, } #[derive(Debug)] pub struct Network { config: HomeConfig, - root: ConfigNetwork, + pub root: ConfigNetwork, } impl Network { @@ -99,13 +103,13 @@ impl Network { } } - // NOTE: project_id - pub fn get_project_id(&self) -> Option { - self.root.project_id.clone() + // NOTE: project_did + pub fn get_project_did(&self) -> Option { + self.root.project_did.clone() } - pub fn save_project_id(&mut self, value: &str) { - self.root.project_id = Some(value.to_string()); + pub fn save_project_did(&mut self, value: &str) { + self.root.project_did = Some(value.to_string()); match self.write() { Ok(_) => {} @@ -115,4 +119,91 @@ impl Network { } } } + + // NOTE: recipient_dids + #[allow(dead_code)] + pub fn get_recipient_dids(&self) -> Option> { + self.root.recipient_dids.clone() + } + + #[allow(dead_code)] + pub fn save_recipient_dids(&mut self, value: Vec) { + self.root.recipient_dids = Some(value); + + match self.write() { + Ok(_) => {} + Err(e) => { + log::error!("{:?}", e); + panic!() + } + } + } + + // NOTE: hub_endpoint + #[allow(dead_code)] + pub fn get_hub_endpoint(&self) -> Option { + self.root.hub_endpoint.clone() + } + + #[allow(dead_code)] + pub fn save_hub_endpoint(&mut self, value: &str) { + self.root.hub_endpoint = Some(value.to_string()); + + match self.write() { + Ok(_) => {} + Err(e) => { + log::error!("{:?}", e); + panic!() + } + } + } + + // NOTE: heartbeat + #[allow(dead_code)] + pub fn get_heartbeat(&self) -> Option { + self.root.heartbeat + } + + #[allow(dead_code)] + pub fn save_heartbeat(&mut self, value: u64) { + self.root.heartbeat = Some(value); + + match self.write() { + Ok(_) => {} + Err(e) => { + log::error!("{:?}", e); + panic!() + } + } + } + + // NOTE: trm + #[allow(dead_code)] + pub fn get_trm(&self) -> Option { + self.root.trm.clone() + } + + #[allow(dead_code)] + pub fn save_trm(&mut self, value: &str) { + self.root.trm = Some(value.to_string()); + + match self.write() { + Ok(_) => {} + Err(e) => { + log::error!("{:?}", e); + panic!() + } + } + } + + // NOTE: write + pub fn save(&mut self) -> Result<(), NodeXError> { + match self.write() { + Ok(v) => Ok(v), + Err(e) => { + log::error!("{:?}", e); + panic!() + } + } + } } diff --git a/src/server.rs b/src/server.rs index 3fcbde9e..6a12d05d 100644 --- a/src/server.rs +++ b/src/server.rs @@ -63,6 +63,10 @@ pub fn new_server( "/verifiable-presentations/verify", web::post().to(controllers::internal::did_verify_vp::handler), ) + .route( + "/network", + web::post().to(controllers::internal::network::handler), + ) .service( web::scope("/didcomm") .route( diff --git a/src/services/hub.rs b/src/services/hub.rs index f696829f..22225493 100644 --- a/src/services/hub.rs +++ b/src/services/hub.rs @@ -27,7 +27,7 @@ pub struct Hub { #[derive(Debug, Serialize, Deserialize)] struct RegisterDeviceRequest { device_did: String, - project_id: String, + project_did: String, } #[derive(Debug, Serialize, Deserialize)] @@ -58,10 +58,14 @@ impl Hub { } } - pub async fn register_device(&self, did: String, project_id: String) -> Result<(), NodeXError> { + pub async fn register_device( + &self, + device_did: String, + project_did: String, + ) -> Result<(), NodeXError> { let request = RegisterDeviceRequest { - device_did: did, - project_id, + device_did, + project_did, }; let payload = serde_json::to_string(&request).expect("failed to serialize"); let res = match self.http_client.post("/v1/device", &payload).await {