Skip to content

Commit

Permalink
Update network.json
Browse files Browse the repository at this point in the history
  • Loading branch information
Dongri Jin committed Sep 16, 2023
1 parent 3709a9a commit 620fe09
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
14 changes: 14 additions & 0 deletions examples/nodejs/src/network.ts
Original file line number Diff line number Diff line change
@@ -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)
})()
1 change: 1 addition & 0 deletions src/controllers/internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
47 changes: 47 additions & 0 deletions src/controllers/internal/network.rs
Original file line number Diff line number Diff line change
@@ -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<MessageContainer>,
) -> actix_web::Result<HttpResponse> {
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<String> = 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"))
}
97 changes: 94 additions & 3 deletions src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@ use crate::nodex::errors::NodeXError;
#[serde(default)]
#[derive(Default)]
pub struct ConfigNetwork {
secret_key: Option<String>,
project_id: Option<String>,
pub secret_key: Option<String>,
pub project_id: Option<String>,
pub recipient_dids: Option<Vec<String>>,
pub hub_endpoint: Option<String>,
pub heartbeat: Option<u64>,
pub trm: Option<String>,
}

#[derive(Debug)]
pub struct Network {
config: HomeConfig,
root: ConfigNetwork,
pub root: ConfigNetwork,
}

impl Network {
Expand Down Expand Up @@ -115,4 +119,91 @@ impl Network {
}
}
}

// NOTE: recipient_dids
#[allow(dead_code)]
pub fn get_recipient_dids(&self) -> Option<Vec<String>> {
self.root.recipient_dids.clone()
}

#[allow(dead_code)]
pub fn save_recipient_dids(&mut self, value: Vec<String>) {
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<String> {
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<u64> {
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<String> {
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!()
}
}
}
}
4 changes: 4 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub fn new_server(sock_path: &PathBuf, sender: Sender<Command>) -> 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(
Expand Down

0 comments on commit 620fe09

Please sign in to comment.