diff --git a/Cargo.lock b/Cargo.lock index aa98be7..c48b7a0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -249,7 +249,7 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bolt-derive" version = "0.2.0" -source = "git+https://gitlab.com/irriden/validating-lightning-signer.git?rev=ce04684b312c95fa7a62329ff198092a50e9a088#ce04684b312c95fa7a62329ff198092a50e9a088" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "proc-macro2", "quote", @@ -818,8 +818,8 @@ dependencies = [ [[package]] name = "fsdb" -version = "0.1.17" -source = "git+https://github.com/Evanfeenstra/fsdb.git?rev=634125776c14be45ab0618f2fa2b7f1aff4adb85#634125776c14be45ab0618f2fa2b7f1aff4adb85" +version = "0.1.18" +source = "git+https://github.com/Evanfeenstra/fsdb.git?rev=7d0db454133bf37b16d9cf5500111491e67c81e5#7d0db454133bf37b16d9cf5500111491e67c81e5" dependencies = [ "rmp-serde", "serde", @@ -1219,7 +1219,7 @@ checksum = "b06a4cde4c0f271a446782e3eff8de789548ce57dbc8eca9292c27f4a42004b4" [[package]] name = "lss-connector" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=7861557d15ec03d1d07ea117c550a69453d487e5#7861557d15ec03d1d07ea117c550a69453d487e5" dependencies = [ "anyhow", "log", @@ -1591,7 +1591,7 @@ dependencies = [ [[package]] name = "rmp-utils" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=7861557d15ec03d1d07ea117c550a69453d487e5#7861557d15ec03d1d07ea117c550a69453d487e5" dependencies = [ "anyhow", "log", @@ -1798,7 +1798,7 @@ dependencies = [ [[package]] name = "sphinx-auther" version = "0.1.12" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=7861557d15ec03d1d07ea117c550a69453d487e5#7861557d15ec03d1d07ea117c550a69453d487e5" dependencies = [ "anyhow", "base64 0.21.2", @@ -1810,7 +1810,7 @@ dependencies = [ [[package]] name = "sphinx-crypter" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=7861557d15ec03d1d07ea117c550a69453d487e5#7861557d15ec03d1d07ea117c550a69453d487e5" dependencies = [ "anyhow", "chacha20poly1305", @@ -1821,7 +1821,7 @@ dependencies = [ [[package]] name = "sphinx-glyph" version = "0.1.2" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=7861557d15ec03d1d07ea117c550a69453d487e5#7861557d15ec03d1d07ea117c550a69453d487e5" dependencies = [ "anyhow", "hex", @@ -1872,7 +1872,7 @@ dependencies = [ [[package]] name = "sphinx-signer" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs.git?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs.git?rev=7861557d15ec03d1d07ea117c550a69453d487e5#7861557d15ec03d1d07ea117c550a69453d487e5" dependencies = [ "anyhow", "bip39", @@ -2154,7 +2154,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "vls-core" version = "0.10.0" -source = "git+https://gitlab.com/irriden/validating-lightning-signer.git?rev=ce04684b312c95fa7a62329ff198092a50e9a088#ce04684b312c95fa7a62329ff198092a50e9a088" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "anyhow", "bitcoin", @@ -2178,7 +2178,7 @@ dependencies = [ [[package]] name = "vls-persist" version = "0.10.0" -source = "git+https://gitlab.com/irriden/validating-lightning-signer.git?rev=ce04684b312c95fa7a62329ff198092a50e9a088#ce04684b312c95fa7a62329ff198092a50e9a088" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "hex", "log", @@ -2191,7 +2191,7 @@ dependencies = [ [[package]] name = "vls-protocol" version = "0.10.0" -source = "git+https://gitlab.com/irriden/validating-lightning-signer.git?rev=ce04684b312c95fa7a62329ff198092a50e9a088#ce04684b312c95fa7a62329ff198092a50e9a088" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "as-any", "bitcoin-consensus-derive", @@ -2204,7 +2204,7 @@ dependencies = [ [[package]] name = "vls-protocol-signer" version = "0.10.0" -source = "git+https://gitlab.com/irriden/validating-lightning-signer.git?rev=ce04684b312c95fa7a62329ff198092a50e9a088#ce04684b312c95fa7a62329ff198092a50e9a088" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "bit-vec", "log", diff --git a/broker/Cargo.lock b/broker/Cargo.lock index f9e217b..e4034ab 100644 --- a/broker/Cargo.lock +++ b/broker/Cargo.lock @@ -442,7 +442,7 @@ dependencies = [ [[package]] name = "bolt-derive" version = "0.2.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "proc-macro2", "quote", @@ -1606,7 +1606,7 @@ dependencies = [ [[package]] name = "lightning-storage-server" version = "0.3.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "anyhow", "async-trait", @@ -1691,7 +1691,7 @@ dependencies = [ [[package]] name = "lss-connector" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=66d7032705918146552473dc27ef1bcfae45aa4b#66d7032705918146552473dc27ef1bcfae45aa4b" +source = "git+https://github.com/stakwork/sphinx-rs?rev=dd11b74164a330a4dd3ceac4ee7601f8b3210b71#dd11b74164a330a4dd3ceac4ee7601f8b3210b71" dependencies = [ "anyhow", "lightning-storage-server", @@ -2693,7 +2693,7 @@ dependencies = [ [[package]] name = "rmp-utils" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=66d7032705918146552473dc27ef1bcfae45aa4b#66d7032705918146552473dc27ef1bcfae45aa4b" +source = "git+https://github.com/stakwork/sphinx-rs?rev=dd11b74164a330a4dd3ceac4ee7601f8b3210b71#dd11b74164a330a4dd3ceac4ee7601f8b3210b71" dependencies = [ "anyhow", "log", @@ -3268,7 +3268,7 @@ dependencies = [ [[package]] name = "sphinx-auther" version = "0.1.12" -source = "git+https://github.com/stakwork/sphinx-rs?rev=66d7032705918146552473dc27ef1bcfae45aa4b#66d7032705918146552473dc27ef1bcfae45aa4b" +source = "git+https://github.com/stakwork/sphinx-rs?rev=dd11b74164a330a4dd3ceac4ee7601f8b3210b71#dd11b74164a330a4dd3ceac4ee7601f8b3210b71" dependencies = [ "anyhow", "base64 0.21.2", @@ -3280,7 +3280,7 @@ dependencies = [ [[package]] name = "sphinx-glyph" version = "0.1.2" -source = "git+https://github.com/stakwork/sphinx-rs?rev=66d7032705918146552473dc27ef1bcfae45aa4b#66d7032705918146552473dc27ef1bcfae45aa4b" +source = "git+https://github.com/stakwork/sphinx-rs?rev=dd11b74164a330a4dd3ceac4ee7601f8b3210b71#dd11b74164a330a4dd3ceac4ee7601f8b3210b71" dependencies = [ "anyhow", "hex", @@ -3326,7 +3326,7 @@ dependencies = [ [[package]] name = "sphinx-signer" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=66d7032705918146552473dc27ef1bcfae45aa4b#66d7032705918146552473dc27ef1bcfae45aa4b" +source = "git+https://github.com/stakwork/sphinx-rs?rev=dd11b74164a330a4dd3ceac4ee7601f8b3210b71#dd11b74164a330a4dd3ceac4ee7601f8b3210b71" dependencies = [ "anyhow", "bip39", @@ -4030,7 +4030,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "vls-core" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "anyhow", "backtrace", @@ -4056,7 +4056,7 @@ dependencies = [ [[package]] name = "vls-frontend" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "async-trait", "lightning-storage-server", @@ -4070,7 +4070,7 @@ dependencies = [ [[package]] name = "vls-persist" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "hex", "log", @@ -4085,7 +4085,7 @@ dependencies = [ [[package]] name = "vls-protocol" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "as-any", "bitcoin-consensus-derive", @@ -4098,7 +4098,7 @@ dependencies = [ [[package]] name = "vls-protocol-client" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "anyhow", "async-trait", @@ -4113,7 +4113,7 @@ dependencies = [ [[package]] name = "vls-protocol-signer" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "bit-vec", "log", @@ -4124,7 +4124,7 @@ dependencies = [ [[package]] name = "vls-proxy" version = "0.10.0" -source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=66b77ac71363ac3ea4504d4fce3f4813a52acd84#66b77ac71363ac3ea4504d4fce3f4813a52acd84" +source = "git+https://gitlab.com/lightning-signer/validating-lightning-signer.git?rev=0370e6a124ac5f64c91b755d7ab1a9e9cc27597d#0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" dependencies = [ "anyhow", "as-any", diff --git a/broker/Cargo.toml b/broker/Cargo.toml index cec2aa2..9869d61 100644 --- a/broker/Cargo.toml +++ b/broker/Cargo.toml @@ -30,17 +30,17 @@ thiserror = "1.0.31" toml = "0.5.9" url = { version = "2.2" } -vls-frontend = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "66b77ac71363ac3ea4504d4fce3f4813a52acd84" } -vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "66b77ac71363ac3ea4504d4fce3f4813a52acd84" } -vls-protocol-client = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "66b77ac71363ac3ea4504d4fce3f4813a52acd84" } -vls-proxy = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "66b77ac71363ac3ea4504d4fce3f4813a52acd84" } +vls-frontend = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" } +vls-protocol = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" } +vls-protocol-client = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" } +vls-proxy = { git = "https://gitlab.com/lightning-signer/validating-lightning-signer.git", rev = "0370e6a124ac5f64c91b755d7ab1a9e9cc27597d" } # vls-frontend = { path = "../../vls/vls-frontend" } # vls-protocol = { path = "../../vls/vls-protocol" } # vls-protocol-client = { path = "../../vls/vls-protocol-client" } # vls-proxy = { path = "../../vls/vls-proxy" } -lss-connector = { git = "https://github.com/stakwork/sphinx-rs", rev = "66d7032705918146552473dc27ef1bcfae45aa4b" } -sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "66d7032705918146552473dc27ef1bcfae45aa4b" } +lss-connector = { git = "https://github.com/stakwork/sphinx-rs", rev = "dd11b74164a330a4dd3ceac4ee7601f8b3210b71" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "dd11b74164a330a4dd3ceac4ee7601f8b3210b71" } # lss-connector = { path = "../../sphinx-rs/lss-connector" } # sphinx-signer = { path = "../../sphinx-rs/signer" } diff --git a/broker/src/looper.rs b/broker/src/looper.rs index 710461a..6c1b942 100644 --- a/broker/src/looper.rs +++ b/broker/src/looper.rs @@ -175,29 +175,23 @@ impl SignerLoop { .unwrap_or([0u8; 33]); let md = parser::raw_request_from_bytes(message, ticket, peer_id, dbid)?; // send to signer - let the_res = loop { - log::info!("SEND ON {}", topics::VLS); - let (res_topic, res) = self.send_request_wait(topics::VLS, md.clone())?; - log::info!("GOT ON {}", res_topic); - if res_topic == topics::LSS_RES { - // send reply to LSS to store muts - let lss_reply = self.send_lss(topics::LSS_MSG.to_string(), res)?; - log::info!("LSS REPLY LEN {}", &lss_reply.1.len()); - // send to signer for HMAC validation, and get final reply - log::info!("SEND ON {}", lss_reply.0); - let (res_topic2, res2) = self.send_request_wait(&lss_reply.0, lss_reply.1)?; - log::info!("GOT ON {}, send to CLN?", res_topic2); - if res_topic2 != topics::VLS_RES { - log::warn!("got a topic NOT on {}", topics::VLS_RES); - } - if res_topic2 == topics::LSS_CONFLICT_RES { - // try again... - continue; - } - break res2; - } else { - break res; - }; + log::info!("SEND ON {}", topics::VLS); + let (res_topic, res) = self.send_request_wait(topics::VLS, md)?; + log::info!("GOT ON {}", res_topic); + let the_res = if res_topic == topics::LSS_RES { + // send reply to LSS to store muts + let lss_reply = self.send_lss(topics::LSS_MSG.to_string(), res)?; + log::info!("LSS REPLY LEN {}", &lss_reply.1.len()); + // send to signer for HMAC validation, and get final reply + log::info!("SEND ON {}", lss_reply.0); + let (res_topic2, res2) = self.send_request_wait(&lss_reply.0, lss_reply.1)?; + log::info!("GOT ON {}, send to CLN?", res_topic2); + if res_topic2 != topics::VLS_RES { + log::warn!("got a topic NOT on {}", topics::VLS_RES); + } + res2 + } else { + res }; // create reply bytes for CLN let reply = parser::raw_response_from_bytes(the_res, ticket)?; diff --git a/broker/src/mqtt.rs b/broker/src/mqtt.rs index 9127213..442df5b 100644 --- a/broker/src/mqtt.rs +++ b/broker/src/mqtt.rs @@ -22,7 +22,6 @@ pub fn start_broker( ) -> anyhow::Result<()> { let conf = config(settings); // println!("CONF {:?}", conf); - // let client_id = expected_client_id.to_string(); let mut broker = Broker::new(conf); diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index d44eadf..eae73e8 100644 --- a/sphinx-key/Cargo.toml +++ b/sphinx-key/Cargo.toml @@ -20,9 +20,9 @@ serde_urlencoded = "0.7.1" url = "2" # sphinx-rs -lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, rev = "af50ee91e3600b7b4862e8f00772601c8a342110" } -sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "af50ee91e3600b7b4862e8f00772601c8a342110" } -sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "af50ee91e3600b7b4862e8f00772601c8a342110" } +lss-connector = { git = "https://github.com/stakwork/sphinx-rs.git", default-features = false, rev = "7861557d15ec03d1d07ea117c550a69453d487e5" } +sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "7861557d15ec03d1d07ea117c550a69453d487e5" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "7861557d15ec03d1d07ea117c550a69453d487e5" } # local # lss-connector = { path = "../../sphinx-rs/lss-connector", default-features = false } # sphinx-crypter = { path = "../../sphinx-rs/crypter" } diff --git a/sphinx-key/src/btn.rs b/sphinx-key/src/btn.rs index c50e121..6ded5e8 100644 --- a/sphinx-key/src/btn.rs +++ b/sphinx-key/src/btn.rs @@ -20,7 +20,7 @@ use std::sync::{Arc, Mutex}; use std::thread; use std::time::Duration; -const ID_LEN: usize = 12; +const ID_LEN: usize = 16; fn main() -> anyhow::Result<()> { esp_idf_sys::link_patches(); diff --git a/sphinx-key/src/clear.rs b/sphinx-key/src/clear.rs index c4ed222..4d34f67 100644 --- a/sphinx-key/src/clear.rs +++ b/sphinx-key/src/clear.rs @@ -17,7 +17,7 @@ use std::fs; use std::path::Path; pub const ROOT_STORE: &str = "/sdcard/store"; -pub const ID_LEN: usize = 8usize; +pub const ID_LEN: usize = 16usize; fn main() -> anyhow::Result<()> { // NvsDefault::new(); diff --git a/sphinx-key/src/conn/mqtt.rs b/sphinx-key/src/conn/mqtt.rs index 847848a..9d075f7 100644 --- a/sphinx-key/src/conn/mqtt.rs +++ b/sphinx-key/src/conn/mqtt.rs @@ -18,14 +18,16 @@ pub const QOS: QoS = QoS::AtMostOnce; pub fn make_client( broker: &str, - client_id: &str, + signer_id: &[u8; 16], username: &str, password: &str, tx: mpsc::Sender, ) -> Result>> { + let client_id = hex::encode(signer_id); log::info!("make_client with id {}", client_id); + let mut conf = MqttClientConfiguration { - client_id: Some(client_id), + client_id: Some(&client_id), buffer_size: 4096, task_stack: 12288, username: Some(username), @@ -119,6 +121,7 @@ pub fn make_client( } else if topic.ends_with(topics::LSS_MSG) || topic.ends_with(topics::INIT_1_MSG) || topic.ends_with(topics::INIT_2_MSG) + || topic.ends_with(topics::LSS_CONFLICT) { log::debug!("received data len {}", data.len()); tx.send(CoreEvent::LssMessage(data)) diff --git a/sphinx-key/src/core/control.rs b/sphinx-key/src/core/control.rs index 9c314e2..4426774 100644 --- a/sphinx-key/src/core/control.rs +++ b/sphinx-key/src/core/control.rs @@ -79,18 +79,17 @@ 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(); + fn write_id(&mut self, id: [u8; ID_LEN]) -> Result<()> { self.0.set_raw(FlashKey::Id.as_str(), &id[..])?; Ok(()) } - fn read_id(&self) -> Result { + fn read_id(&self) -> Result<[u8; ID_LEN]> { 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())?) + Ok(existing.try_into()?) } fn read_policy(&self) -> Result { let mut buf = [0u8; 250]; diff --git a/sphinx-key/src/core/events.rs b/sphinx-key/src/core/events.rs index 56e082f..f116290 100644 --- a/sphinx-key/src/core/events.rs +++ b/sphinx-key/src/core/events.rs @@ -39,14 +39,6 @@ pub enum Event { pub const ROOT_STORE: &str = "/sdcard/store"; -pub const SUB_TOPICS: &[&str] = &[ - topics::INIT_1_MSG, - topics::INIT_2_MSG, - topics::LSS_MSG, - topics::VLS, - topics::CONTROL, -]; - fn mqtt_sub( mqtt: &mut EspMqttClient>, client_id: &str, @@ -84,23 +76,25 @@ pub fn make_event_loop( policy: &Policy, velocity: &Option, mut ctrlr: Controller, - client_id: &str, + signer_id: &[u8; 16], node_id: &PublicKey, ) -> Result<()> { + let client_id = hex::encode(signer_id); + while let Ok(event) = rx.recv() { log::info!("BROKER IP AND PORT: {}", config.broker); // wait for a Connection first. match event { Event::Connected => { - mqtt_sub(&mut mqtt, client_id, SUB_TOPICS); + mqtt_sub(&mut mqtt, &client_id, topics::SIGNER_SUBS); break; } _ => (), } } - let kvv_store = FsKVVStore::new(&ROOT_STORE, None).0; - let msg_store = FsKVVStore::new(&ROOT_STORE, None).0; + let kvv_store = FsKVVStore::new(&ROOT_STORE, signer_id.clone(), None).0; + let msg_store = FsKVVStore::new(&ROOT_STORE, signer_id.clone(), None).0; let fs_persister = CloudKVVStore::new(kvv_store); let _ = fs_persister.enter(); @@ -138,9 +132,9 @@ pub fn make_event_loop( thread::sleep(std::time::Duration::from_secs(1)); // send the initial HELLO - mqtt_pub(&mut mqtt, client_id, topics::HELLO, &[]); + mqtt_pub(&mut mqtt, &client_id, topics::HELLO, &[]); - let (root_handler, lss_signer) = match lss::init_lss(client_id, &rx, rhb, &mut mqtt) { + let (root_handler, lss_signer) = match lss::init_lss(signer_id, &rx, rhb, &mut mqtt) { Ok(rl) => rl, Err(e) => { log::error!("failed to init lss {:?}", e); @@ -159,10 +153,10 @@ pub fn make_event_loop( match event { Event::Connected => { log::info!("GOT A Event::Connected msg!"); - mqtt_sub(&mut mqtt, client_id, SUB_TOPICS); + mqtt_sub(&mut mqtt, &client_id, topics::SIGNER_SUBS); thread::sleep(std::time::Duration::from_secs(1)); // send the initial HELLO again - mqtt_pub(&mut mqtt, client_id, topics::HELLO, &[]); + mqtt_pub(&mut mqtt, &client_id, topics::HELLO, &[]); led_tx.send(Status::Connected).unwrap(); } Event::Disconnected => { @@ -183,11 +177,16 @@ pub fn make_event_loop( Ok((vls_b, lss_b, sequence, _cmd)) => { if lss_b.len() == 0 { // no muts, respond directly back! - mqtt_pub(&mut mqtt, client_id, topics::VLS_RES, &vls_b); + mqtt_pub(&mut mqtt, &client_id, topics::VLS_RES, &vls_b); + // and commit + if let Err(e) = root_handler.node().get_persister().commit() { + log::error!("LOCAL COMMIT ERROR! {:?}", e); + unsafe { esp_idf_sys::esp_restart() }; + } restart_esp_if_memory_low(); } else { // muts! send LSS first! - mqtt_pub(&mut mqtt, client_id, topics::LSS_RES, &lss_b); + mqtt_pub(&mut mqtt, &client_id, topics::LSS_RES, &lss_b); msg_store .put("vls_b", &vls_b) .map_err(|_e| anyhow::anyhow!("failed to put vls_b"))?; @@ -197,7 +196,6 @@ pub fn make_event_loop( msgs = Some((vls_b, lss_b)); } expected_sequence = Some(sequence + 1); - root_handler.commit(); } Err(e) => match e { VlsHandlerError::BadSequence(current, expected) => unsafe { @@ -207,12 +205,12 @@ pub fn make_event_loop( expected ); log::info!("restarting esp!"); - esp_idf_sys::esp_restart(); + unsafe { esp_idf_sys::esp_restart() }; }, _ => { let err_msg = GlyphError::new(1, &e.to_string()); log::error!("HANDLE FAILED {:?}", e); - mqtt_pub(&mut mqtt, client_id, topics::ERROR, &err_msg.to_vec()[..]); + mqtt_pub(&mut mqtt, &client_id, topics::ERROR, &err_msg.to_vec()[..]); } }, }; @@ -245,17 +243,26 @@ pub fn make_event_loop( Ok((ret_topic, bytes)) => { // set msgs back to None msgs = None; - mqtt_pub(&mut mqtt, client_id, &ret_topic, &bytes); + mqtt_pub(&mut mqtt, &client_id, &ret_topic, &bytes); if ret_topic == topics::VLS_RES { + // and commit + if let Err(e) = root_handler.node().get_persister().commit() { + log::error!("LOCAL COMMIT ERROR AFTER LSS! {:?}", e); + unsafe { esp_idf_sys::esp_restart() }; + } restart_esp_if_memory_low(); } + if ret_topic == topics::LSS_CONFLICT_RES { + log::error!("LSS PUT CONFLICT! RESTART..."); + unsafe { esp_idf_sys::esp_restart() }; + } } Err(e) => { log::error!("LSS MESSAGE FAILED!"); log::error!("{}", &e.to_string()); msgs = None; let err_msg = GlyphError::new(1, &e.to_string()); - mqtt_pub(&mut mqtt, client_id, topics::ERROR, &err_msg.to_vec()[..]); + mqtt_pub(&mut mqtt, &client_id, topics::ERROR, &err_msg.to_vec()[..]); } } } @@ -267,7 +274,7 @@ pub fn make_event_loop( { let mut bb = ByteBuf::new(); serialize_controlresponse(&mut bb, &res).expect("failed serialize_lssresponse"); - mqtt_pub(&mut mqtt, client_id, topics::CONTROL_RES, bb.as_slice()); + mqtt_pub(&mut mqtt, &client_id, topics::CONTROL_RES, bb.as_slice()); } } } diff --git a/sphinx-key/src/core/lss.rs b/sphinx-key/src/core/lss.rs index f18e7e8..4ecdda1 100644 --- a/sphinx-key/src/core/lss.rs +++ b/sphinx-key/src/core/lss.rs @@ -14,11 +14,13 @@ use std::time::Duration; pub use lss_connector::handle_lss_msg; pub fn init_lss( - client_id: &str, + signer_id: &[u8; 16], rx: &mpsc::Receiver, handler_builder: RootHandlerBuilder, mqtt: &mut EspMqttClient>, ) -> Result<(RootHandler, LssSigner)> { + let client_id = hex::encode(signer_id); + let server_pubkey = loop { let event = rx.recv_timeout(Duration::from_secs(30))?; match server_pubkey_from_event(event) { diff --git a/sphinx-key/src/main.rs b/sphinx-key/src/main.rs index 5a85aa6..9d47094 100644 --- a/sphinx-key/src/main.rs +++ b/sphinx-key/src/main.rs @@ -25,7 +25,7 @@ use std::thread; use std::time::Duration; use std::time::SystemTime; -const ID_LEN: usize = 12; +const ID_LEN: usize = 16; fn main() -> Result<()> { // Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once, @@ -137,9 +137,7 @@ fn main() -> Result<()> { Some(s) => flash.write_seed(s).expect("could not store seed"), None => panic!("SEED REQUIRED!!!"), } - flash - .write_id(random_word(ID_LEN)) - .expect("could not store id"); + flash.write_id(random_16()).expect("could not store id"); } drop(flash); println!("CONFIG SAVED"); @@ -158,7 +156,7 @@ fn main() -> Result<()> { fn make_and_launch_client( config: Config, seed: [u8; 32], - client_id: String, + signer_id: [u8; ID_LEN], policy: &Policy, velocity: &Option, led_tx: mpsc::Sender, @@ -184,7 +182,7 @@ fn make_and_launch_client( log::info!("PUBKEY {} TOKEN {}", &pubkey_str, &token); let mqtt_client = - conn::mqtt::make_client(&config.broker, &client_id, &pubkey_str, &token, tx.clone())?; + conn::mqtt::make_client(&config.broker, &signer_id, &pubkey_str, &token, tx.clone())?; // let mqtt_client = conn::mqtt::start_listening(mqtt, connection, tx)?; // this blocks forever... the "main thread" @@ -204,7 +202,7 @@ fn make_and_launch_client( policy, velocity, ctrlr, - &client_id, + &signer_id, &pubkey, )?; Ok(()) @@ -218,3 +216,10 @@ pub fn random_word(n: usize) -> String { .map(char::from) .collect() } + +pub fn random_16() -> [u8; 16] { + use sphinx_crypter::secp256k1::rand::{thread_rng, RngCore}; + let mut signer_id = [0u8; 16]; + thread_rng().fill_bytes(&mut signer_id); + signer_id +}