Skip to content

Commit

Permalink
Merge branch 'develop' into call-get-message-endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rabe1028 committed Sep 20, 2023
2 parents 4b6b7d5 + 9638de5 commit e61d594
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 33 deletions.
3 changes: 2 additions & 1 deletion examples/nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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"))
}
40 changes: 20 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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);
Expand Down Expand Up @@ -263,7 +263,7 @@ async fn main() -> std::io::Result<()> {
fn use_cli(command: Option<Commands>, 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 {
Expand All @@ -276,9 +276,9 @@ fn use_cli(command: Option<Commands>, 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");
Expand All @@ -292,12 +292,12 @@ fn use_cli(command: Option<Commands>, 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");
Expand All @@ -308,28 +308,28 @@ fn use_cli(command: Option<Commands>, 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 => {
log::error!("Network secret_key is not set. Please set secret_key use cli");
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!()
}
}

// 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(()) => (),
Expand All @@ -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() {
Expand All @@ -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(()) => (),
Expand Down
107 changes: 99 additions & 8 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_did: 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 @@ -99,13 +103,13 @@ impl Network {
}
}

// NOTE: project_id
pub fn get_project_id(&self) -> Option<String> {
self.root.project_id.clone()
// NOTE: project_did
pub fn get_project_did(&self) -> Option<String> {
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(_) => {}
Expand All @@ -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 @@ -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(
Expand Down
12 changes: 8 additions & 4 deletions src/services/hub.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e61d594

Please sign in to comment.