mirror of
https://github.com/stakwork/sphinx-key.git
synced 2026-02-04 15:24:35 +01:00
controller to parse and validate incoming ControlMessage bytes
This commit is contained in:
@@ -8,7 +8,7 @@ vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning
|
||||
serde = { version = "1.0", default-features = false }
|
||||
rmp-serde = "1.1.0"
|
||||
serde_bolt = { version = "0.2", default-features = false }
|
||||
sphinx-auther = "0.1.8"
|
||||
sphinx-auther = "0.1.9"
|
||||
anyhow = "1"
|
||||
|
||||
[features]
|
||||
|
||||
@@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize};
|
||||
use sphinx_auther::nonce;
|
||||
use sphinx_auther::secp256k1::{PublicKey, SecretKey};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum ControlMessage {
|
||||
Nonce,
|
||||
QueryPolicy,
|
||||
@@ -10,7 +10,7 @@ pub enum ControlMessage {
|
||||
Ota(OtaParams),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub enum ControlResponse {
|
||||
Nonce(u64),
|
||||
PolicyCurrent(Policy),
|
||||
@@ -18,12 +18,12 @@ pub enum ControlResponse {
|
||||
OtaConfirm(OtaParams),
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct Policy {
|
||||
pub sats_per_day: u64,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
#[derive(Serialize, Deserialize, Debug, Clone)]
|
||||
pub struct OtaParams {
|
||||
pub version: u64,
|
||||
pub url: String,
|
||||
@@ -38,15 +38,18 @@ impl Controller {
|
||||
}
|
||||
pub fn build_msg(&mut self, msg: ControlMessage) -> anyhow::Result<Vec<u8>> {
|
||||
let data = rmp_serde::to_vec(&msg)?;
|
||||
let ret = nonce::build_msg(&data, &self.0, self.2)?;
|
||||
self.2 = self.2 + 1;
|
||||
Ok(nonce::build_msg(data, &self.0, self.2)?)
|
||||
Ok(ret)
|
||||
}
|
||||
pub fn build_response(&self, msg: ControlResponse) -> anyhow::Result<Vec<u8>> {
|
||||
Ok(rmp_serde::to_vec(&msg)?)
|
||||
}
|
||||
pub fn parse_msg(&mut self, input: Vec<u8>) -> anyhow::Result<ControlMessage> {
|
||||
pub fn parse_msg(&mut self, input: &[u8]) -> anyhow::Result<ControlMessage> {
|
||||
let msg = nonce::parse_msg(input, &self.1, self.2)?;
|
||||
Ok(rmp_serde::from_slice(&msg)?)
|
||||
let ret = rmp_serde::from_slice(&msg)?;
|
||||
self.2 = self.2 + 1;
|
||||
Ok(ret)
|
||||
}
|
||||
pub fn parse_response(&self, input: &[u8]) -> anyhow::Result<ControlResponse> {
|
||||
Ok(rmp_serde::from_slice(input)?)
|
||||
@@ -1,4 +1,4 @@
|
||||
pub mod policy;
|
||||
pub mod control;
|
||||
|
||||
use serde::ser;
|
||||
use std::cmp::min;
|
||||
|
||||
@@ -18,7 +18,7 @@ use vls_protocol_signer::lightning_signer::bitcoin::Network;
|
||||
pub use vls_protocol_signer::vls_protocol;
|
||||
|
||||
pub use derive::node_keys as derive_node_keys;
|
||||
pub use sphinx_key_parser::MsgDriver;
|
||||
pub use sphinx_key_parser::{control, MsgDriver};
|
||||
pub use sphinx_key_persister::FsPersister;
|
||||
pub struct InitResponse {
|
||||
pub root_handler: RootHandler,
|
||||
|
||||
16
sphinx-key/Cargo.lock
generated
16
sphinx-key/Cargo.lock
generated
@@ -1969,6 +1969,19 @@ dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sphinx-auther"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07ba95c8bd0600a9853ed6320701423362bfeac8d69034ed9585cb289d849701"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"base64",
|
||||
"hex",
|
||||
"log",
|
||||
"secp256k1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "sphinx-crypter"
|
||||
version = "0.1.0"
|
||||
@@ -2007,8 +2020,11 @@ dependencies = [
|
||||
name = "sphinx-key-parser"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"rmp-serde",
|
||||
"serde",
|
||||
"serde_bolt",
|
||||
"sphinx-auther",
|
||||
"vls-protocol",
|
||||
]
|
||||
|
||||
|
||||
@@ -14,7 +14,6 @@ use std::thread;
|
||||
|
||||
pub const VLS_TOPIC: &str = "sphinx";
|
||||
pub const CONTROL_TOPIC: &str = "sphinx-control";
|
||||
pub const OTA_TOPIC: &str = "sphinx-ota";
|
||||
pub const RETURN_TOPIC: &str = "sphinx-return";
|
||||
pub const USERNAME: &str = "sphinx-key";
|
||||
pub const PASSWORD: &str = "sphinx-key-pass";
|
||||
@@ -90,9 +89,6 @@ pub fn start_listening(
|
||||
CONTROL_TOPIC => tx
|
||||
.send(CoreEvent::Control(msg.data().to_vec()))
|
||||
.expect("couldnt send Event::Control"),
|
||||
OTA_TOPIC => tx
|
||||
.send(CoreEvent::Ota(msg.data().to_vec()))
|
||||
.expect("couldnt send Event::Ota"),
|
||||
_ => log::warn!("unrecognized topic {}", topic),
|
||||
};
|
||||
} else {
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use crate::conn::mqtt::{CONTROL_TOPIC, OTA_TOPIC, QOS, RETURN_TOPIC, VLS_TOPIC};
|
||||
use crate::conn::mqtt::{CONTROL_TOPIC, QOS, RETURN_TOPIC, VLS_TOPIC};
|
||||
use crate::core::config::Config;
|
||||
use crate::core::init::make_init_msg;
|
||||
|
||||
use sphinx_key_signer::control::Controller;
|
||||
use sphinx_key_signer::lightning_signer::bitcoin::Network;
|
||||
use sphinx_key_signer::vls_protocol::model::PubKey;
|
||||
use sphinx_key_signer::{self, InitResponse};
|
||||
@@ -19,7 +20,6 @@ pub enum Event {
|
||||
Connected,
|
||||
Disconnected,
|
||||
VlsMessage(Vec<u8>),
|
||||
Ota(Vec<u8>),
|
||||
Control(Vec<u8>),
|
||||
}
|
||||
|
||||
@@ -36,6 +36,12 @@ pub enum Status {
|
||||
Signing,
|
||||
}
|
||||
|
||||
// the controller validated Control messages
|
||||
pub fn controller_from_seed(network: &Network, seed: &[u8]) -> Controller {
|
||||
let (pk, sk) = sphinx_key_signer::derive_node_keys(network, seed);
|
||||
Controller::new(sk, pk, 0)
|
||||
}
|
||||
|
||||
// the main event loop
|
||||
#[cfg(not(feature = "pingpong"))]
|
||||
pub fn make_event_loop(
|
||||
@@ -56,8 +62,6 @@ pub fn make_event_loop(
|
||||
.expect("could not MQTT subscribe");
|
||||
mqtt.subscribe(CONTROL_TOPIC, QOS)
|
||||
.expect("could not MQTT subscribe");
|
||||
mqtt.subscribe(OTA_TOPIC, QOS)
|
||||
.expect("could not MQTT subscribe");
|
||||
led_tx.send(Status::Connected).unwrap();
|
||||
break;
|
||||
}
|
||||
@@ -71,6 +75,10 @@ pub fn make_event_loop(
|
||||
root_handler,
|
||||
init_reply: _,
|
||||
} = sphinx_key_signer::init(init_msg, network).expect("failed to init signer");
|
||||
|
||||
// make the controller to validate Control messages
|
||||
let mut ctrlr = controller_from_seed(&network, &config.seed[..]);
|
||||
|
||||
// signing loop
|
||||
let dummy_peer = PubKey([0; 33]);
|
||||
while let Ok(event) = rx.recv() {
|
||||
@@ -81,8 +89,6 @@ pub fn make_event_loop(
|
||||
.expect("could not MQTT subscribe");
|
||||
mqtt.subscribe(CONTROL_TOPIC, QOS)
|
||||
.expect("could not MQTT subscribe");
|
||||
mqtt.subscribe(OTA_TOPIC, QOS)
|
||||
.expect("could not MQTT subscribe");
|
||||
led_tx.send(Status::Connected).unwrap();
|
||||
}
|
||||
Event::Disconnected => {
|
||||
@@ -107,8 +113,15 @@ pub fn make_event_loop(
|
||||
}
|
||||
};
|
||||
}
|
||||
Event::Control(_) => (),
|
||||
Event::Ota(_) => (),
|
||||
Event::Control(ref msg_bytes) => {
|
||||
match ctrlr.parse_msg(msg_bytes) {
|
||||
Ok(msg) => {
|
||||
log::info!("CONTROL MSG {:?}", msg);
|
||||
// create a response and mqtt pub here
|
||||
},
|
||||
Err(e) => log::warn!("error parsing ctrl msg {:?}", e),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -147,7 +160,6 @@ pub fn make_event_loop(
|
||||
log::info!("GOT A Event::Disconnected msg!");
|
||||
}
|
||||
Event::Control(_) => (),
|
||||
Event::Ota(_) => (),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user