From 620fe09ef06f033619737085dec8b8fcd11a84f2 Mon Sep 17 00:00:00 2001 From: Dongri Jin Date: Sat, 16 Sep 2023 19:09:20 +0900 Subject: [PATCH] Update network.json --- examples/nodejs/package.json | 3 +- examples/nodejs/src/network.ts | 14 +++++ src/controllers/internal/mod.rs | 1 + src/controllers/internal/network.rs | 47 ++++++++++++++ src/network.rs | 97 ++++++++++++++++++++++++++++- src/server.rs | 4 ++ 6 files changed, 162 insertions(+), 4 deletions(-) create mode 100644 examples/nodejs/src/network.ts create mode 100644 src/controllers/internal/network.rs diff --git a/examples/nodejs/package.json b/examples/nodejs/package.json index 62d7d4f3..794c563f 100644 --- a/examples/nodejs/package.json +++ b/examples/nodejs/package.json @@ -21,7 +21,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/network.rs b/src/network.rs index 02d0313b..8117c452 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_id: 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 { @@ -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 4c9fd157..e84007ce 100644 --- a/src/server.rs +++ b/src/server.rs @@ -53,6 +53,10 @@ pub fn new_server(sock_path: &PathBuf, sender: Sender) -> 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(