From 84f5ddc936961f843ba4d8f0a9e9b1cde3c6daea Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 21 Jul 2022 11:25:38 -0700 Subject: [PATCH 1/2] update all esp-rs deps, release/4.4 --- persister/Cargo.toml | 2 +- persister/src/lib.rs | 1 + signer/src/lib.rs | 3 +- sphinx-key/.cargo/config.toml | 2 +- sphinx-key/Cargo.toml | 7 +-- sphinx-key/src/clear.rs | 7 +-- sphinx-key/src/conn/wifi.rs | 80 ++++++++++++++++++----------------- sphinx-key/src/core/events.rs | 11 +++-- sphinx-key/src/main.rs | 2 +- 9 files changed, 60 insertions(+), 55 deletions(-) diff --git a/persister/Cargo.toml b/persister/Cargo.toml index e2b516a..3e52cc0 100644 --- a/persister/Cargo.toml +++ b/persister/Cargo.toml @@ -10,5 +10,5 @@ lightning-signer-core = { git = "https://gitlab.com/Evanfeenstra/validating-ligh lightning-signer-server = { git = "https://gitlab.com/Evanfeenstra/validating-lightning-signer", branch = "patch-sign-chan", default-features = false, features = ["persist", "secp-lowmemory"] } serde = { version = "1.0.105" } hex = "0.4.3" -fsdb = "0.1.10" +fsdb = "0.1.11" log = "0.4" diff --git a/persister/src/lib.rs b/persister/src/lib.rs index 3e67bed..5d42b61 100644 --- a/persister/src/lib.rs +++ b/persister/src/lib.rs @@ -175,6 +175,7 @@ impl Persist for FsPersister { Ok(ns) => ns, Err(_) => return res, }; + log::info!("NODE LIST LEN {}", list.len()); for pk in list { if let Ok(pubkey) = self.pubkeys.get(&pk) { if let Ok(node) = self.nodes.get(&pk) { diff --git a/signer/src/lib.rs b/signer/src/lib.rs index 34cd0ca..9a401a7 100644 --- a/signer/src/lib.rs +++ b/signer/src/lib.rs @@ -1,4 +1,5 @@ -use lightning_signer::persist::{DummyPersister, Persist}; +use lightning_signer::persist::Persist; +// use lightning_signer::persist::DummyPersister; use std::sync::Arc; use vls_protocol::model::PubKey; use vls_protocol::msgs::{self, read_serial_request_header, write_serial_response_header, Message}; diff --git a/sphinx-key/.cargo/config.toml b/sphinx-key/.cargo/config.toml index d34d4dd..33f5399 100644 --- a/sphinx-key/.cargo/config.toml +++ b/sphinx-key/.cargo/config.toml @@ -29,6 +29,6 @@ build-std-features = ["panic_immediate_abort"] # Required for older ESP-IDF vers [env] # Note: these variables are not used when using pio builder # Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF stable (v4.4) -ESP_IDF_VERSION = { value = "tag:v4.4.1" } +ESP_IDF_VERSION = { value = "release/v4.4" } # Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF master (mainline) #ESP_IDF_VERSION = { value = "master" } diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index 892e2b0..49325bd 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -17,15 +17,16 @@ default = ["std"] std = ["sphinx-key-signer"] pio = ["esp-idf-sys/pio"] pingpong = [] +no_persist = [] [dependencies] bitflags = "1.3.2" esp-idf-sys = { version = "0.31.6", features = ["binstart"] } sphinx-key-signer = { path = "../signer", optional = true } sphinx-key-crypter = { path = "../crypter" } -embedded-svc = { version = "0.21.2" } -esp-idf-svc = "0.41" -esp-idf-hal = "0.37" +embedded-svc = "0.22.0" +esp-idf-svc = "0.42.1" +esp-idf-hal = "0.38.0" embedded-hal = "=1.0.0-alpha.8" anyhow = {version = "1", features = ["backtrace"]} log = "0.4" diff --git a/sphinx-key/src/clear.rs b/sphinx-key/src/clear.rs index 8292bd3..92b87ad 100644 --- a/sphinx-key/src/clear.rs +++ b/sphinx-key/src/clear.rs @@ -1,15 +1,16 @@ use esp_idf_sys as _; // If using the `binstart` feature of `esp-idf-sys`, always keep this module imported +use embedded_svc::storage::StorageBase; use esp_idf_svc::nvs::*; use esp_idf_svc::nvs_storage::EspNvsStorage; -use embedded_svc::storage::Storage; use std::sync::Arc; fn main() -> anyhow::Result<()> { let default_nvs = Arc::new(EspDefaultNvs::new()?); - let mut store = EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage"); + let mut store = + EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage"); store.remove("config").expect("couldnt remove config 1"); println!("NVS cleared!"); Ok(()) -} \ No newline at end of file +} diff --git a/sphinx-key/src/conn/wifi.rs b/sphinx-key/src/conn/wifi.rs index 8000f21..e822fad 100644 --- a/sphinx-key/src/conn/wifi.rs +++ b/sphinx-key/src/conn/wifi.rs @@ -1,25 +1,23 @@ use crate::core::config::Config; -use esp_idf_svc::wifi::*; -use esp_idf_svc::sysloop::*; use esp_idf_svc::netif::*; use esp_idf_svc::nvs::EspDefaultNvs; use esp_idf_svc::ping; +use esp_idf_svc::sysloop::*; +use esp_idf_svc::wifi::*; -use embedded_svc::wifi::*; use embedded_svc::httpd::Result; -use embedded_svc::ping::Ping; use embedded_svc::ipv4; +use embedded_svc::ping::Ping; +use embedded_svc::wifi::Wifi; +use embedded_svc::wifi::*; use log::*; -use std::time::Duration; use std::sync::Arc; use std::thread; +use std::time::Duration; -pub fn start_client( - default_nvs: Arc, - config: &Config, -) -> Result> { +pub fn start_client(default_nvs: Arc, config: &Config) -> Result> { let netif_stack = Arc::new(EspNetifStack::new()?); let sys_loop_stack = Arc::new(EspSysLoopStack::new()?); @@ -30,24 +28,29 @@ pub fn start_client( let ours = ap_infos.into_iter().find(|a| a.ssid == ssid); let channel = if let Some(ours) = ours { - info!("Found configured access point {} on channel {}", ssid, ours.channel); + info!( + "Found configured access point {} on channel {}", + ssid, ours.channel + ); Some(ours.channel) } else { - info!("Configured access point {} not found during scanning, will go with unknown channel", ssid); + info!( + "Configured access point {} not found during scanning, will go with unknown channel", + ssid + ); None }; - wifi.set_configuration(&Configuration::Client( - ClientConfiguration { - ssid: ssid.into(), - password: pass.into(), - channel, - ..Default::default() - } - ))?; + wifi.set_configuration(&Configuration::Client(ClientConfiguration { + ssid: ssid.into(), + password: pass.into(), + channel, + ..Default::default() + }))?; info!("...Wifi client configuration set, get status"); - match wifi.wait_status_with_timeout(Duration::from_secs(20), |status| !status.is_transitional()) { + match wifi.wait_status_with_timeout(Duration::from_secs(20), |status| !status.is_transitional()) + { Ok(_) => (), Err(e) => warn!("Unexpected Wifi status: {:?}", e), }; @@ -58,13 +61,17 @@ pub fn start_client( if let Status( ClientStatus::Started(ClientConnectionStatus::Connected(ClientIpStatus::Done(ip_settings))), ApStatus::Stopped, - ) = status { + ) = status + { info!("Wifi started!"); ping(&ip_settings)?; } else { thread::sleep(Duration::from_secs(13)); // bail!("Unexpected Client Wifi status: {:?}", status); - return Err(anyhow::anyhow!("Unexpected Client Wifi status: {:?}", status)); + return Err(anyhow::anyhow!( + "Unexpected Client Wifi status: {:?}", + status + )); } info!("wifi::start_client Ok(())"); @@ -72,30 +79,22 @@ pub fn start_client( Ok(wifi) } -pub fn start_access_point( - default_nvs: Arc, -) -> Result> { +pub fn start_access_point(default_nvs: Arc) -> Result> { let netif_stack = Arc::new(EspNetifStack::new()?); let sys_loop_stack = Arc::new(EspSysLoopStack::new()?); - let mut wifi = Box::new(EspWifi::new(netif_stack, sys_loop_stack, default_nvs)?); - wifi.set_configuration(&Configuration::AccessPoint( - AccessPointConfiguration { - ssid: "sphinxkey".into(), - channel: 6, - ..Default::default() - }, - ))?; + wifi.set_configuration(&Configuration::AccessPoint(AccessPointConfiguration { + ssid: "sphinxkey".into(), + channel: 6, + ..Default::default() + }))?; info!("Wifi configuration set, about to get status"); wifi.wait_status_with_timeout(Duration::from_secs(20), |status| !status.is_transitional()) .map_err(|e| anyhow::anyhow!("Unexpected Wifi status: {:?}", e))?; let status = wifi.get_status(); - if let Status( - ClientStatus::Stopped, - ApStatus::Started(ApIpStatus::Done), - ) = status { + if let Status(ClientStatus::Stopped, ApStatus::Started(ApIpStatus::Done)) = status { info!("Wifi started!"); } else { return Err(anyhow::anyhow!("Unexpected AP Wifi status: {:?}", status)); @@ -110,10 +109,13 @@ fn ping(ip_settings: &ipv4::ClientSettings) -> Result<()> { let ping_summary = ping::EspPing::default().ping(ip_settings.subnet.gateway, &Default::default())?; if ping_summary.transmitted != ping_summary.received { - return Err(anyhow::anyhow!("Pinging gateway {} resulted in timeouts", ip_settings.subnet.gateway)); + return Err(anyhow::anyhow!( + "Pinging gateway {} resulted in timeouts", + ip_settings.subnet.gateway + )); } info!("Pinging done"); Ok(()) -} \ No newline at end of file +} diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index ca932e9..cbc9dcf 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -1,9 +1,9 @@ use crate::conn::mqtt::{QOS, RETURN_TOPIC, TOPIC}; use crate::core::init::make_init_msg; +use sphinx_key_signer::lightning_signer::bitcoin::Network; use sphinx_key_signer::vls_protocol::model::PubKey; use sphinx_key_signer::{self, InitResponse}; -use sphinx_key_signer::lightning_signer::bitcoin::Network; use std::sync::mpsc; use embedded_svc::httpd::Result; @@ -39,7 +39,7 @@ pub fn make_event_loop( network: Network, do_log: bool, led_tx: mpsc::Sender, - seed: [u8; 32] + seed: [u8; 32], ) -> Result<()> { while let Ok(event) = rx.recv() { match event { @@ -66,7 +66,6 @@ pub fn make_event_loop( root_handler, init_reply: _, } = sphinx_key_signer::init(init_msg, network).expect("failed to init signer"); - // signing loop let dummy_peer = PubKey([0; 33]); while let Ok(event) = rx.recv() { @@ -86,9 +85,9 @@ pub fn make_event_loop( do_log, ) { Ok(b) => { - mqtt.publish(RETURN_TOPIC, QOS, false, b) + mqtt.publish(RETURN_TOPIC, QOS, false, &b) .expect("could not publish init response"); - }, + } Err(e) => { log::error!("HANDLE FAILED {:?}", e); // panic!("HANDLE FAILED {:?}", e); @@ -112,7 +111,7 @@ pub fn make_event_loop( _network: Network, do_log: bool, led_tx: mpsc::Sender, - _seed: [u8; 32] + _seed: [u8; 32], ) -> Result<()> { log::info!("About to subscribe to the mpsc channel"); while let Ok(event) = rx.recv() { diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index df8dd6b..854d30a 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -13,7 +13,7 @@ use std::sync::{mpsc, Arc}; use std::thread; use std::time::Duration; -use embedded_svc::storage::Storage; +use embedded_svc::storage::StorageBase; use esp_idf_hal::peripherals::Peripherals; use esp_idf_svc::nvs::*; use esp_idf_svc::nvs_storage::EspNvsStorage; From b6fdb3f367dee6e4b862958182cebf2b6b99c0a4 Mon Sep 17 00:00:00 2001 From: Evan Feenstra Date: Thu, 21 Jul 2022 11:55:37 -0700 Subject: [PATCH 2/2] use rmp-serde to store config in NVS --- sphinx-key/Cargo.toml | 1 + sphinx-key/src/main.rs | 12 ++++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index 49325bd..9ce3505 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -35,6 +35,7 @@ serde_urlencoded = "0.7.1" serde = { version = "1.0.137", default-features = false } serde_json = { version = "1.0.81", default-features = false } hex = "0.4.3" +rmp-serde = "1.1.0" [patch.crates-io] # updates the "rand" create to use esp RNG diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 854d30a..c2338f5 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -13,7 +13,7 @@ use std::sync::{mpsc, Arc}; use std::thread; use std::time::Duration; -use embedded_svc::storage::StorageBase; +use embedded_svc::storage::RawStorage; use esp_idf_hal::peripherals::Peripherals; use esp_idf_svc::nvs::*; use esp_idf_svc::nvs_storage::EspNvsStorage; @@ -49,8 +49,11 @@ fn main() -> Result<()> { let default_nvs = Arc::new(EspDefaultNvs::new()?); let mut store = EspNvsStorage::new_default(default_nvs.clone(), "sphinx", true).expect("no storage"); - let existing: Option = store.get("config").expect("failed"); - if let Some(exist) = existing { + let mut buf = [0u8; 250]; + // let existing: Option = store.get_raw("config", buf).expect("failed"); + let existing = store.get_raw("config", &mut buf).expect("failed"); + if let Some((exist_bytes, _)) = existing { + let exist: Config = rmp_serde::from_slice(exist_bytes).expect("failed to parse Config"); println!( "=============> START CLIENT NOW <============== {:?}", exist @@ -82,8 +85,9 @@ fn main() -> Result<()> { led_tx.send(Status::WifiAccessPoint).unwrap(); println!("=============> START SERVER NOW AND WAIT <=============="); if let Ok((wifi, config)) = start_config_server_and_wait(default_nvs.clone()) { + let conf = rmp_serde::to_vec(&config).expect("couldnt rmp Config"); store - .put("config", &config) + .put_raw("config", &conf[..]) .expect("could not store config"); println!("CONFIG SAVED"); drop(wifi);