mirror of
https://github.com/stakwork/sphinx-key.git
synced 2025-12-18 15:54:31 +01:00
full tester bin
This commit is contained in:
@@ -14,3 +14,5 @@ log = "0.4"
|
|||||||
rumqttc = "0.12.0"
|
rumqttc = "0.12.0"
|
||||||
tokio = { version = "1.4.0", features = ["rt", "rt-multi-thread", "macros"] }
|
tokio = { version = "1.4.0", features = ["rt", "rt-multi-thread", "macros"] }
|
||||||
pretty_env_logger = "0.4.0"
|
pretty_env_logger = "0.4.0"
|
||||||
|
clap = "=3.0.0-beta.2"
|
||||||
|
clap_derive = "=3.0.0-beta.5"
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
use sphinx_key_parser as parser;
|
use sphinx_key_parser as parser;
|
||||||
|
|
||||||
|
use clap::{App, AppSettings, Arg};
|
||||||
use rumqttc::{self, AsyncClient, Event, MqttOptions, Packet, QoS};
|
use rumqttc::{self, AsyncClient, Event, MqttOptions, Packet, QoS};
|
||||||
|
use sphinx_key_signer::{self, InitResponse, PubKey};
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use vls_protocol::msgs;
|
use vls_protocol::msgs;
|
||||||
@@ -13,7 +15,11 @@ const PASSWORD: &str = "sphinx-key-pass";
|
|||||||
#[tokio::main(worker_threads = 1)]
|
#[tokio::main(worker_threads = 1)]
|
||||||
async fn main() -> Result<(), Box<dyn Error>> {
|
async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
// color_backtrace::install();
|
|
||||||
|
let app = App::new("tester")
|
||||||
|
.setting(AppSettings::NoAutoVersion)
|
||||||
|
.about("CLN:mqtt-tester - MQTT client signer")
|
||||||
|
.arg(Arg::from("--test run a test against the embedded device"));
|
||||||
|
|
||||||
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
|
let mut mqttoptions = MqttOptions::new("test-1", "localhost", 1883);
|
||||||
mqttoptions.set_credentials(USERNAME, PASSWORD);
|
mqttoptions.set_credentials(USERNAME, PASSWORD);
|
||||||
@@ -36,12 +42,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
.await
|
.await
|
||||||
.expect("could not pub");
|
.expect("could not pub");
|
||||||
|
|
||||||
|
let matches = app.get_matches();
|
||||||
|
if matches.is_present("test") {
|
||||||
loop {
|
loop {
|
||||||
let event = eventloop.poll().await;
|
let event = eventloop.poll().await.expect("failed to unwrap event");
|
||||||
// println!("{:?}", event.unwrap());
|
// println!("{:?}", event);
|
||||||
if let Some(bs) = incoming_bytes(event.expect("failed to unwrap event")) {
|
if let Some(ping_bytes) = incoming_bytes(event) {
|
||||||
let (ping, sequence, dbid): (msgs::Ping, u16, u64) =
|
let (ping, sequence, dbid): (msgs::Ping, u16, u64) =
|
||||||
parser::request_from_bytes(bs).expect("read ping header");
|
parser::request_from_bytes(ping_bytes).expect("read ping header");
|
||||||
println!("sequence {}", sequence);
|
println!("sequence {}", sequence);
|
||||||
println!("dbid {}", dbid);
|
println!("dbid {}", dbid);
|
||||||
println!("INCOMING: {:?}", ping);
|
println!("INCOMING: {:?}", ping);
|
||||||
@@ -49,13 +57,48 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
|||||||
id: ping.id,
|
id: ping.id,
|
||||||
message: ping.message,
|
message: ping.message,
|
||||||
};
|
};
|
||||||
let bytes = parser::raw_response_from_msg(pong, sequence)?;
|
let bytes = parser::raw_response_from_msg(pong, sequence)
|
||||||
|
.expect("couldnt parse raw response");
|
||||||
client
|
client
|
||||||
.publish(PUB_TOPIC, QoS::AtMostOnce, false, bytes)
|
.publish(PUB_TOPIC, QoS::AtMostOnce, false, bytes)
|
||||||
.await
|
.await
|
||||||
.expect("could not mqtt publish");
|
.expect("could not mqtt publish");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// once the init loop is done, the root_handler is returned
|
||||||
|
let root_handler = loop {
|
||||||
|
let init_event = eventloop.poll().await.expect("failed to unwrap event");
|
||||||
|
// this may be another kind of message like MQTT ConnAck
|
||||||
|
// loop around again and wait for the init
|
||||||
|
if let Some(init_msg_bytes) = incoming_bytes(init_event) {
|
||||||
|
let InitResponse {
|
||||||
|
root_handler,
|
||||||
|
init_reply,
|
||||||
|
} = sphinx_key_signer::init(init_msg_bytes).expect("failed to init signer");
|
||||||
|
client
|
||||||
|
.publish(PUB_TOPIC, QoS::AtMostOnce, false, init_reply)
|
||||||
|
.await
|
||||||
|
.expect("could not publish init response");
|
||||||
|
// return the root_handler and finish the init loop
|
||||||
|
break root_handler;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// the actual loop
|
||||||
|
loop {
|
||||||
|
let event = eventloop.poll().await.expect("failed to unwrap event");
|
||||||
|
let dummy_peer = PubKey([0; 33]);
|
||||||
|
if let Some(msg_bytes) = incoming_bytes(event) {
|
||||||
|
match sphinx_key_signer::handle(&root_handler, msg_bytes, dummy_peer.clone()) {
|
||||||
|
Ok(b) => client
|
||||||
|
.publish(PUB_TOPIC, QoS::AtMostOnce, false, b)
|
||||||
|
.await
|
||||||
|
.expect("could not publish init response"),
|
||||||
|
Err(e) => panic!("HANDLE FAILED {:?}", e),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn incoming_bytes(event: Event) -> Option<Vec<u8>> {
|
fn incoming_bytes(event: Event) -> Option<Vec<u8>> {
|
||||||
|
|||||||
Reference in New Issue
Block a user