Merge branch 'fixed-prefix'

This commit is contained in:
irriden
2023-07-27 15:12:51 +00:00
9 changed files with 142 additions and 37 deletions

12
Cargo.lock generated
View File

@@ -1199,7 +1199,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4"
[[package]] [[package]]
name = "lss-connector" name = "lss-connector"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"log", "log",
@@ -1571,7 +1571,7 @@ dependencies = [
[[package]] [[package]]
name = "rmp-utils" name = "rmp-utils"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"log", "log",
@@ -1778,7 +1778,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-auther" name = "sphinx-auther"
version = "0.1.12" 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 = [ dependencies = [
"anyhow", "anyhow",
"base64 0.21.2", "base64 0.21.2",
@@ -1790,7 +1790,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-crypter" name = "sphinx-crypter"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"chacha20poly1305", "chacha20poly1305",
@@ -1801,7 +1801,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-glyph" name = "sphinx-glyph"
version = "0.1.2" 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 = [ dependencies = [
"anyhow", "anyhow",
"hex", "hex",
@@ -1852,7 +1852,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-signer" name = "sphinx-signer"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"bip39", "bip39",

10
broker/Cargo.lock generated
View File

@@ -1700,7 +1700,7 @@ dependencies = [
[[package]] [[package]]
name = "lss-connector" name = "lss-connector"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"lightning-storage-server", "lightning-storage-server",
@@ -2694,7 +2694,7 @@ dependencies = [
[[package]] [[package]]
name = "rmp-utils" name = "rmp-utils"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"log", "log",
@@ -3308,7 +3308,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-auther" name = "sphinx-auther"
version = "0.1.12" 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 = [ dependencies = [
"anyhow", "anyhow",
"base64 0.21.2", "base64 0.21.2",
@@ -3320,7 +3320,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-glyph" name = "sphinx-glyph"
version = "0.1.2" 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 = [ dependencies = [
"anyhow", "anyhow",
"hex", "hex",
@@ -3366,7 +3366,7 @@ dependencies = [
[[package]] [[package]]
name = "sphinx-signer" name = "sphinx-signer"
version = "0.1.0" 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 = [ dependencies = [
"anyhow", "anyhow",
"bip39", "bip39",

View File

@@ -39,8 +39,8 @@ vls-proxy = { git = "https://gitlab.com/lightning-signer/validating-li
# vls-protocol-client = { path = "../../vls/vls-protocol-client" } # vls-protocol-client = { path = "../../vls/vls-protocol-client" }
# vls-proxy = { path = "../../vls/vls-proxy" } # vls-proxy = { path = "../../vls/vls-proxy" }
lss-connector = { 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 = "253b45a659b1b395e3ff4435f4b7a3e4036da521" } sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "31f4305caa37f98e3db181a00543947d6d856e6c" }
# lss-connector = { path = "../../sphinx-rs/lss-connector" } # lss-connector = { path = "../../sphinx-rs/lss-connector" }
# sphinx-signer = { path = "../../sphinx-rs/signer" } # sphinx-signer = { path = "../../sphinx-rs/signer" }

View File

@@ -1,38 +1,38 @@
use anyhow::Result; use anyhow::Result;
use rocket::tokio::sync::{mpsc, oneshot}; use rocket::tokio::sync::{mpsc, oneshot};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Connections { pub struct Connections {
pub pubkey: Option<String>, pub pubkey: Option<String>,
pub clients: Vec<String>, pub clients: HashMap<String, bool>,
pub current: String,
} }
impl Connections { impl Connections {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
pubkey: None, pubkey: None,
clients: Vec::new(), clients: HashMap::new(),
current: String::default(),
} }
} }
pub fn set_pubkey(&mut self, pk: &str) { pub fn set_pubkey(&mut self, pk: &str) {
self.pubkey = Some(pk.to_string()) self.pubkey = Some(pk.to_string())
} }
pub fn add_client(&mut self, cid: &str) { pub fn set_current(&mut self, cid: String) {
let cids = cid.to_string(); self.current = cid;
if !self.clients.contains(&cids) {
// new client is added to beginning of Vec
self.clients.insert(0, cids);
} }
pub fn add_client(&mut self, cid: &str) {
self.clients.insert(cid.to_string(), false);
} }
pub fn remove_client(&mut self, cid: &str) { pub fn remove_client(&mut self, cid: &str) {
let cids = cid.to_string(); self.clients.remove(cid);
if self.clients.contains(&cids) {
self.clients.retain(|x| x != cid)
}
} }
pub fn client_action(&mut self, cid: &str, connected: bool) { pub fn client_action(&mut self, cid: &str, connected: bool) {
if connected { if connected {
self.current = cid.to_string();
self.add_client(cid); self.add_client(cid);
} else { } else {
self.remove_client(cid); self.remove_client(cid);

View File

@@ -159,11 +159,11 @@ fn pub_and_wait(
let reply = if let Some(cid) = msg.cid.clone() { let reply = if let Some(cid) = msg.cid.clone() {
// for a specific client // for a specific client
log::debug!("publishing to a specific client"); log::debug!("publishing to a specific client");
let res_opt = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx); pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx)
res_opt
} else { } else {
log::debug!("publishing to all clients"); log::debug!("publishing to all clients");
let cs = conns_.lock().unwrap(); let cs = conns_.lock().unwrap();
let current = cs.current.clone();
let client_list = cs.clients.clone(); let client_list = cs.clients.clone();
log::debug!("got the list lock!"); log::debug!("got the list lock!");
drop(cs); drop(cs);
@@ -173,13 +173,21 @@ fn pub_and_wait(
std::thread::sleep(Duration::from_secs(1)); std::thread::sleep(Duration::from_secs(1));
None None
} else { } else {
let mut rep = None; // Try the current connection
for cid in client_list.iter() { let mut rep = pub_timeout(&current, &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 != &current) {
rep = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx); rep = pub_timeout(&cid, &msg.topic, &msg.message, &msg_rx, link_tx);
if let Some(_) = &rep { 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; break;
} }
} }
}
rep rep
} }
}; };
@@ -192,13 +200,16 @@ fn pub_and_wait(
} else { } else {
log::debug!("couldn't reach any clients..."); log::debug!("couldn't reach any clients...");
} }
if let Some(attempt) = retries { if let Some(max) = retries {
if counter == attempt { log::debug!("counter: {}, retries: {}", counter, max);
if counter == max {
if let Err(_) = msg.reply_tx.send(ChannelReply::empty()) { if let Err(_) = msg.reply_tx.send(ChannelReply::empty()) {
log::warn!("could not send on reply_tx"); log::warn!("could not send on reply_tx");
} }
break; break;
} }
} else {
log::debug!("retrying indefinitely");
} }
counter = counter + 1; counter = counter + 1;
} }

73
sphinx-key/1_up.sh Executable file
View File

@@ -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

View File

@@ -18,11 +18,11 @@ serde_json = { version = "1.0.81", default-features = false }
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"
url = "2" 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 } # 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-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 } # sphinx-signer = { path = "../../sphinx-rs/signer", optional = true }
anyhow.workspace = true anyhow.workspace = true

View File

@@ -1,3 +1,4 @@
use crate::ID_LEN;
use anyhow::{anyhow, Context, Result}; use anyhow::{anyhow, Context, Result};
use embedded_svc::storage::RawStorage; use embedded_svc::storage::RawStorage;
use esp_idf_svc::nvs::{EspDefaultNvs, EspDefaultNvsPartition}; use esp_idf_svc::nvs::{EspDefaultNvs, EspDefaultNvsPartition};
@@ -79,6 +80,19 @@ impl ControlPersist for FlashPersister {
self.0.remove(FlashKey::Seed.as_str())?; self.0.remove(FlashKey::Seed.as_str())?;
Ok(()) 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<String> {
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<Policy> { fn read_policy(&self) -> Result<Policy> {
let mut buf = [0u8; 250]; let mut buf = [0u8; 250];
let existing = self let existing = self

View File

@@ -32,6 +32,8 @@ const CLIENT_ID: &str = "sphinx-1";
#[cfg(feature = "pingpong")] #[cfg(feature = "pingpong")]
const CLIENT_ID: &str = "test-1"; const CLIENT_ID: &str = "test-1";
const ID_LEN: usize = 12;
fn main() -> Result<()> { fn main() -> Result<()> {
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, // 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. // 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()); let mut flash = FlashPersister::new(default_nvs.clone());
if let Ok(exist) = flash.read_config() { if let Ok(exist) = flash.read_config() {
let seed = flash.read_seed().expect("no seed..."); 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 policy = flash.read_policy().unwrap_or_default();
let velocity = flash.read_velocity().ok(); let velocity = flash.read_velocity().ok();
println!( println!(
@@ -102,6 +105,7 @@ fn main() -> Result<()> {
if let Ok(()) = make_and_launch_client( if let Ok(()) = make_and_launch_client(
exist.clone(), exist.clone(),
seed, seed,
id.clone(),
&policy, &policy,
&velocity, &velocity,
led_tx.clone(), led_tx.clone(),
@@ -121,6 +125,9 @@ fn main() -> Result<()> {
Ok((_wifi, config, seed)) => { Ok((_wifi, config, seed)) => {
flash.write_config(config).expect("could not store config"); flash.write_config(config).expect("could not store config");
flash.write_seed(seed).expect("could not store seed"); flash.write_seed(seed).expect("could not store seed");
flash
.write_id(random_word(ID_LEN))
.expect("could not store id");
println!("CONFIG SAVED"); println!("CONFIG SAVED");
unsafe { esp_idf_sys::esp_restart() }; unsafe { esp_idf_sys::esp_restart() };
} }
@@ -134,6 +141,7 @@ fn main() -> Result<()> {
fn make_and_launch_client( fn make_and_launch_client(
config: Config, config: Config,
seed: [u8; 32], seed: [u8; 32],
client_id: String,
policy: &Policy, policy: &Policy,
velocity: &Option<Velocity>, velocity: &Option<Velocity>,
led_tx: mpsc::Sender<Status>, led_tx: mpsc::Sender<Status>,
@@ -158,7 +166,6 @@ fn make_and_launch_client(
let token = ctrlr.make_auth_token().expect("couldnt make auth token"); let token = ctrlr.make_auth_token().expect("couldnt make auth token");
log::info!("PUBKEY {} TOKEN {}", &pubkey_str, &token); log::info!("PUBKEY {} TOKEN {}", &pubkey_str, &token);
let client_id = random_word(8);
let mqtt_client = let mqtt_client =
conn::mqtt::make_client(&config.broker, &client_id, &pubkey_str, &token, tx.clone())?; conn::mqtt::make_client(&config.broker, &client_id, &pubkey_str, &token, tx.clone())?;
// let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; // let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?;