diff --git a/Cargo.lock b/Cargo.lock index ffa09db..3efa4ea 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1199,7 +1199,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "lss-connector" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "log", @@ -1571,7 +1571,7 @@ dependencies = [ [[package]] name = "rmp-utils" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "log", @@ -1778,7 +1778,7 @@ dependencies = [ [[package]] name = "sphinx-auther" version = "0.1.12" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "base64 0.21.2", @@ -1790,7 +1790,7 @@ dependencies = [ [[package]] name = "sphinx-crypter" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "chacha20poly1305", @@ -1801,7 +1801,7 @@ dependencies = [ [[package]] name = "sphinx-glyph" version = "0.1.2" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "hex", @@ -1852,7 +1852,7 @@ dependencies = [ [[package]] name = "sphinx-signer" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "bip39", diff --git a/broker/Cargo.lock b/broker/Cargo.lock index efb02b3..94991c3 100644 --- a/broker/Cargo.lock +++ b/broker/Cargo.lock @@ -1700,7 +1700,7 @@ dependencies = [ [[package]] name = "lss-connector" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "lightning-storage-server", @@ -2694,7 +2694,7 @@ dependencies = [ [[package]] name = "rmp-utils" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "log", @@ -3308,7 +3308,7 @@ dependencies = [ [[package]] name = "sphinx-auther" version = "0.1.12" -source = "git+https://github.com/stakwork/sphinx-rs?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "base64 0.21.2", @@ -3320,7 +3320,7 @@ dependencies = [ [[package]] name = "sphinx-glyph" version = "0.1.2" -source = "git+https://github.com/stakwork/sphinx-rs?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "hex", @@ -3366,7 +3366,7 @@ dependencies = [ [[package]] name = "sphinx-signer" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=253b45a659b1b395e3ff4435f4b7a3e4036da521#253b45a659b1b395e3ff4435f4b7a3e4036da521" +source = "git+https://github.com/stakwork/sphinx-rs?rev=31f4305caa37f98e3db181a00543947d6d856e6c#31f4305caa37f98e3db181a00543947d6d856e6c" dependencies = [ "anyhow", "bip39", diff --git a/broker/Cargo.toml b/broker/Cargo.toml index ed1e9a8..6d1987e 100644 --- a/broker/Cargo.toml +++ b/broker/Cargo.toml @@ -39,8 +39,8 @@ vls-proxy = { git = "https://gitlab.com/lightning-signer/validating-li # vls-protocol-client = { path = "../../vls/vls-protocol-client" } # vls-proxy = { path = "../../vls/vls-proxy" } -lss-connector = { git = "https://github.com/stakwork/sphinx-rs", rev = "253b45a659b1b395e3ff4435f4b7a3e4036da521" } -sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "253b45a659b1b395e3ff4435f4b7a3e4036da521" } +lss-connector = { git = "https://github.com/stakwork/sphinx-rs", rev = "31f4305caa37f98e3db181a00543947d6d856e6c" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "31f4305caa37f98e3db181a00543947d6d856e6c" } # lss-connector = { path = "../../sphinx-rs/lss-connector" } # sphinx-signer = { path = "../../sphinx-rs/signer" } diff --git a/broker/src/conn.rs b/broker/src/conn.rs index 9c02d2a..9ad3899 100644 --- a/broker/src/conn.rs +++ b/broker/src/conn.rs @@ -1,38 +1,38 @@ use anyhow::Result; use rocket::tokio::sync::{mpsc, oneshot}; use serde::{Deserialize, Serialize}; +use std::collections::HashMap; #[derive(Debug, Serialize, Deserialize)] pub struct Connections { pub pubkey: Option, - pub clients: Vec, + pub clients: HashMap, + pub current: String, } impl Connections { pub fn new() -> Self { Self { pubkey: None, - clients: Vec::new(), + clients: HashMap::new(), + current: String::default(), } } pub fn set_pubkey(&mut self, pk: &str) { self.pubkey = Some(pk.to_string()) } + pub fn set_current(&mut self, cid: String) { + self.current = cid; + } pub fn add_client(&mut self, cid: &str) { - let cids = cid.to_string(); - if !self.clients.contains(&cids) { - // new client is added to beginning of Vec - self.clients.insert(0, cids); - } + self.clients.insert(cid.to_string(), false); } pub fn remove_client(&mut self, cid: &str) { - let cids = cid.to_string(); - if self.clients.contains(&cids) { - self.clients.retain(|x| x != cid) - } + self.clients.remove(cid); } pub fn client_action(&mut self, cid: &str, connected: bool) { if connected { + self.current = cid.to_string(); self.add_client(cid); } else { self.remove_client(cid); diff --git a/broker/src/mqtt.rs b/broker/src/mqtt.rs index 0fbed70..d8533b1 100644 --- a/broker/src/mqtt.rs +++ b/broker/src/mqtt.rs @@ -159,11 +159,11 @@ fn pub_and_wait( let reply = if let Some(cid) = msg.cid.clone() { // for a specific client log::debug!("publishing to a specific client"); - let res_opt = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx); - res_opt + pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx) } else { log::debug!("publishing to all clients"); let cs = conns_.lock().unwrap(); + let current = cs.current.clone(); let client_list = cs.clients.clone(); log::debug!("got the list lock!"); drop(cs); @@ -173,11 +173,19 @@ fn pub_and_wait( std::thread::sleep(Duration::from_secs(1)); None } else { - let mut rep = None; - for cid in client_list.iter() { - rep = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx); - if let Some(_) = &rep { - break; + // Try the current connection + let mut rep = pub_timeout(¤t, &msg.topic, &msg.message, &msg_rx, link_tx); + // If that failed, try looking for some other signer + if rep.is_none() { + for cid in client_list.into_keys().filter(|k| k != ¤t) { + rep = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx); + if rep.is_some() { + let mut cs = conns_.lock().unwrap(); + log::debug!("got the list lock!"); + cs.set_current(cid.to_string()); + drop(cs); + break; + } } } rep @@ -192,13 +200,16 @@ fn pub_and_wait( } else { log::debug!("couldn't reach any clients..."); } - if let Some(attempt) = retries { - if counter == attempt { + if let Some(max) = retries { + log::debug!("counter: {}, retries: {}", counter, max); + if counter == max { if let Err(_) = msg.reply_tx.send(ChannelReply::empty()) { log::warn!("could not send on reply_tx"); } break; } + } else { + log::debug!("retrying indefinitely"); } counter = counter + 1; } diff --git a/sphinx-key/1_up.sh b/sphinx-key/1_up.sh new file mode 100755 index 0000000..9513f6e --- /dev/null +++ b/sphinx-key/1_up.sh @@ -0,0 +1,73 @@ +# MODE=debug +MODE=release +check_exists() { + command -v "$1" > /dev/null +} +check_port() { + cargo espflash board-info --port "$1" &> /dev/null +} +if ! check_exists esptool.py +then + echo "esptool.py not installed!" + echo "install with this command: pip install esptool" + exit 1 +fi +if ! check_exists ldproxy +then + echo "ldproxy not installed!" + echo "install with this command: cargo install ldproxy" + exit 1 +fi +if ! check_exists cargo-espflash +then + echo "cargo-espflash not installed!" + echo "install with this command: cargo install cargo-espflash" + exit 1 +fi +if [ -z "$SSID" ] +then + echo "Please set environment variable SSID to the SSID of the wifi you'll use to configure your sphinx-key." + exit 1 +fi +if [ -z "$PASS" ] +then + echo "Please set environment variable PASS to the password of the wifi you'll use to configure your sphinx-key." + exit 1 +fi +if [ ${#PASS} -lt 8 ] +then + echo "Please set PASS to a password longer than 7 characters." + exit 1 +fi +for FILE in /dev/tty.* +do + # Check for port on macOS + if check_port $FILE + then + PORT=$FILE + break + fi +done +if [ -z "$PORT" ] +then + # Check for port on linux + if check_port /dev/ttyUSB1 + then + PORT=/dev/ttyUSB1 + fi +fi +if [ -z "$PORT" ] +then + echo "ESP likely not connected! Exiting now." + echo "Make sure the ESP is connected with a data USB cable, and try again." + exit 1 +fi +if [ $MODE = "release" ] +then + cargo build --release +else + cargo build +fi && +esptool.py --chip esp32-c3 elf2image ../target/riscv32imc-esp-espidf/$MODE/sphinx-key && +esptool.py --chip esp32c3 -b 460800 --before=default_reset --after=hard_reset write_flash --flash_mode dio --flash_freq 40m --flash_size 4MB 0x90000 ../target/riscv32imc-esp-espidf/$MODE/sphinx-key.bin && +cargo espflash monitor --port $PORT diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index 652ef88..34e508d 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -18,11 +18,11 @@ serde_json = { version = "1.0.81", default-features = false } serde_urlencoded = "0.7.1" url = "2" -lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, rev = "253b45a659b1b395e3ff4435f4b7a3e4036da521" } +lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, rev = "31f4305caa37f98e3db181a00543947d6d856e6c" } # lss-connector = { path = "../../sphinx-rs/lss-connector", default-features = false } -sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "253b45a659b1b395e3ff4435f4b7a3e4036da521" } +sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "31f4305caa37f98e3db181a00543947d6d856e6c" } # sphinx-crypter = { path = "../../sphinx-rs/crypter" } -sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "253b45a659b1b395e3ff4435f4b7a3e4036da521" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "31f4305caa37f98e3db181a00543947d6d856e6c" } # sphinx-signer = { path = "../../sphinx-rs/signer", optional = true } anyhow.workspace = true diff --git a/sphinx-key/src/core/control.rs b/sphinx-key/src/core/control.rs index 5d86e50..dcd9243 100644 --- a/sphinx-key/src/core/control.rs +++ b/sphinx-key/src/core/control.rs @@ -1,3 +1,4 @@ +use crate::ID_LEN; use anyhow::{anyhow, Context, Result}; use embedded_svc::storage::RawStorage; use esp_idf_svc::nvs::{EspDefaultNvs, EspDefaultNvsPartition}; @@ -79,6 +80,19 @@ impl ControlPersist for FlashPersister { self.0.remove(FlashKey::Seed.as_str())?; Ok(()) } + fn write_id(&mut self, id: String) -> Result<()> { + let id = id.into_bytes(); + self.0.set_raw(FlashKey::Id.as_str(), &id[..])?; + Ok(()) + } + fn read_id(&self) -> Result { + let mut buf = [0u8; ID_LEN]; + let existing = self + .0 + .get_raw(FlashKey::Id.as_str(), &mut buf)? + .ok_or(anyhow!("no existing id"))?; + Ok(String::from_utf8(existing.to_vec())?) + } fn read_policy(&self) -> Result { let mut buf = [0u8; 250]; let existing = self diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 4927e09..8f9ac76 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -32,6 +32,8 @@ const CLIENT_ID: &str = "sphinx-1"; #[cfg(feature = "pingpong")] const CLIENT_ID: &str = "test-1"; +const ID_LEN: usize = 12; + fn main() -> Result<()> { // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // or else some patches to the runtime implemented by esp-idf-sys might not link properly. @@ -67,6 +69,7 @@ fn main() -> Result<()> { let mut flash = FlashPersister::new(default_nvs.clone()); if let Ok(exist) = flash.read_config() { let seed = flash.read_seed().expect("no seed..."); + let id = flash.read_id().expect("no id..."); let policy = flash.read_policy().unwrap_or_default(); let velocity = flash.read_velocity().ok(); println!( @@ -102,6 +105,7 @@ fn main() -> Result<()> { if let Ok(()) = make_and_launch_client( exist.clone(), seed, + id.clone(), &policy, &velocity, led_tx.clone(), @@ -121,6 +125,9 @@ fn main() -> Result<()> { Ok((_wifi, config, seed)) => { flash.write_config(config).expect("could not store config"); flash.write_seed(seed).expect("could not store seed"); + flash + .write_id(random_word(ID_LEN)) + .expect("could not store id"); println!("CONFIG SAVED"); unsafe { esp_idf_sys::esp_restart() }; } @@ -134,6 +141,7 @@ fn main() -> Result<()> { fn make_and_launch_client( config: Config, seed: [u8; 32], + client_id: String, policy: &Policy, velocity: &Option, led_tx: mpsc::Sender, @@ -158,7 +166,6 @@ fn make_and_launch_client( let token = ctrlr.make_auth_token().expect("couldnt make auth token"); log::info!("PUBKEY {} TOKEN {}", &pubkey_str, &token); - let client_id = random_word(8); let mqtt_client = conn::mqtt::make_client(&config.broker, &client_id, &pubkey_str, &token, tx.clone())?; // let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;