diff --git a/Cargo.lock b/Cargo.lock index aa98be7..57f890f 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=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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 435bdaa..e856e17 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/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", @@ -1058,8 +1058,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", @@ -1606,7 +1606,7 @@ dependencies = [ [[package]] name = "lightning-storage-server" version = "0.3.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", "async-trait", @@ -1691,7 +1691,7 @@ dependencies = [ [[package]] name = "lss-connector" version = "0.1.0" -source = "git+https://github.com/stakwork/sphinx-rs?rev=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs?rev=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs?rev=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs?rev=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs?rev=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" 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=af50ee91e3600b7b4862e8f00772601c8a342110#af50ee91e3600b7b4862e8f00772601c8a342110" +source = "git+https://github.com/stakwork/sphinx-rs?rev=768189358fd8ea647e34b250c396daf3479ebbfe#768189358fd8ea647e34b250c396daf3479ebbfe" dependencies = [ "anyhow", "bip39", @@ -4006,6 +4006,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "uuid" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.0" @@ -4021,7 +4030,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", "backtrace", @@ -4047,7 +4056,7 @@ dependencies = [ [[package]] name = "vls-frontend" 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 = [ "async-trait", "lightning-storage-server", @@ -4061,7 +4070,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", @@ -4069,13 +4078,14 @@ dependencies = [ "serde", "serde_json", "serde_with", + "uuid", "vls-core", ] [[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", @@ -4088,7 +4098,7 @@ dependencies = [ [[package]] name = "vls-protocol-client" 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", "async-trait", @@ -4103,7 +4113,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", @@ -4114,7 +4124,7 @@ dependencies = [ [[package]] name = "vls-proxy" 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", "as-any", diff --git a/broker/Cargo.toml b/broker/Cargo.toml index 7037456..a64beec 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/irriden/validating-lightning-signer.git", rev = "ce04684b312c95fa7a62329ff198092a50e9a088" } -vls-protocol = { git = "https://gitlab.com/irriden/validating-lightning-signer.git", rev = "ce04684b312c95fa7a62329ff198092a50e9a088" } -vls-protocol-client = { git = "https://gitlab.com/irriden/validating-lightning-signer.git", rev = "ce04684b312c95fa7a62329ff198092a50e9a088" } -vls-proxy = { git = "https://gitlab.com/irriden/validating-lightning-signer.git", rev = "ce04684b312c95fa7a62329ff198092a50e9a088" } +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 = "af50ee91e3600b7b4862e8f00772601c8a342110" } -sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "af50ee91e3600b7b4862e8f00772601c8a342110" } +lss-connector = { git = "https://github.com/stakwork/sphinx-rs", rev = "768189358fd8ea647e34b250c396daf3479ebbfe" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs", rev = "768189358fd8ea647e34b250c396daf3479ebbfe" } # lss-connector = { path = "../../sphinx-rs/lss-connector" } # sphinx-signer = { path = "../../sphinx-rs/signer" } diff --git a/broker/src/chain_tracker.rs b/broker/src/chain_tracker.rs index c4f5331..be7be56 100644 --- a/broker/src/chain_tracker.rs +++ b/broker/src/chain_tracker.rs @@ -49,7 +49,7 @@ impl MqttSignerPort { if res_topic == topics::LSS_RES { // send LSS instead let lss_reply = self.send_lss(res).await?; - let (res_topic2, res2) = self.send_request_wait(topics::LSS_MSG, lss_reply).await?; + let (res_topic2, res2) = self.send_request_wait(&lss_reply.0, lss_reply.1).await?; if res_topic2 != topics::VLS_RES { log::warn!("ChainTracker got a topic NOT on {}", topics::VLS_RES); } @@ -70,8 +70,8 @@ impl MqttSignerPort { Ok((reply.topic_end, reply.reply)) } - async fn send_lss(&self, message: Vec) -> Result> { - let (request, reply_rx) = LssReq::new(message); + async fn send_lss(&self, message: Vec) -> Result<(String, Vec)> { + let (request, reply_rx) = LssReq::new(topics::LSS_MSG.to_string(), message); self.lss_tx.send(request).await?; let res = reply_rx.await?; Ok(res) diff --git a/broker/src/conn.rs b/broker/src/conn.rs index c9ed0e5..5c615aa 100644 --- a/broker/src/conn.rs +++ b/broker/src/conn.rs @@ -143,13 +143,14 @@ impl ChannelReply { /// Responses are received on the oneshot sender #[derive(Debug)] pub struct LssReq { + pub topic: String, pub message: Vec, - pub reply_tx: oneshot::Sender>, + pub reply_tx: oneshot::Sender<(String, Vec)>, } impl LssReq { - pub fn new(message: Vec) -> (Self, oneshot::Receiver>) { + pub fn new(topic: String, message: Vec) -> (Self, oneshot::Receiver<(String, Vec)>) { let (reply_tx, reply_rx) = oneshot::channel(); - let cr = Self { message, reply_tx }; + let cr = Self { topic, message, reply_tx }; (cr, reply_rx) } } diff --git a/broker/src/looper.rs b/broker/src/looper.rs index ffe280a..6c1b942 100644 --- a/broker/src/looper.rs +++ b/broker/src/looper.rs @@ -180,12 +180,12 @@ impl SignerLoop { 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(res)?; - log::info!("LSS REPLY LEN {}", &lss_reply.len()); + 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 {}", topics::LSS_MSG); - let (res_topic2, res2) = self.send_request_wait(topics::LSS_MSG, lss_reply)?; - log::info!("GOT ON {}, send to CLN", res_topic2); + 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); } @@ -237,9 +237,9 @@ impl SignerLoop { Ok((reply.topic_end, reply.reply)) } - fn send_lss(&mut self, message: Vec) -> Result> { + fn send_lss(&mut self, topic: String, message: Vec) -> Result<(String, Vec)> { // Send a request to the LSS server - let (request, reply_rx) = LssReq::new(message); + let (request, reply_rx) = LssReq::new(topic, message); self.lss_tx.blocking_send(request).map_err(|_| Error::Eof)?; let res = reply_rx.blocking_recv().map_err(|_| Error::Eof)?; Ok(res) diff --git a/broker/src/lss.rs b/broker/src/lss.rs index 0ecbf69..b7ba210 100644 --- a/broker/src/lss.rs +++ b/broker/src/lss.rs @@ -112,7 +112,7 @@ async fn dance_step_2( ) -> Result<()> { let state_bytes = lss_conn.get_created_state_msg(ir).await?; let cr = send_created(cid, state_bytes, mqtt_tx).await?; - lss_conn.handle(Response::Created(cr)).await?; + lss_conn.handle(Response::Created(cr)).await; Ok(()) } diff --git a/broker/src/mqtt.rs b/broker/src/mqtt.rs index d0caa4a..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); @@ -266,17 +265,9 @@ fn pub_timeout( } fn subs(cid: &str, mut ltx: LinkTx) { - ltx.subscribe(format!("{}/{}", cid, topics::VLS_RES)) - .unwrap(); - ltx.subscribe(format!("{}/{}", cid, topics::CONTROL_RES)) - .unwrap(); - ltx.subscribe(format!("{}/{}", cid, topics::ERROR)).unwrap(); - ltx.subscribe(format!("{}/{}", cid, topics::LSS_RES)) - .unwrap(); - ltx.subscribe(format!("{}/{}", cid, topics::INIT_1_RES)) - .unwrap(); - ltx.subscribe(format!("{}/{}", cid, topics::INIT_2_RES)) - .unwrap(); + for t in topics::BROKER_SUBS { + ltx.subscribe(format!("{}/{}", cid, t)).unwrap(); + } } fn unsubs(_cid: &str, mut _ltx: LinkTx) { diff --git a/sphinx-key/Cargo.toml b/sphinx-key/Cargo.toml index d44eadf..78f5d80 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 = "768189358fd8ea647e34b250c396daf3479ebbfe" } +sphinx-crypter = { git = "https://github.com/stakwork/sphinx-rs.git", rev = "768189358fd8ea647e34b250c396daf3479ebbfe" } +sphinx-signer = { git = "https://github.com/stakwork/sphinx-rs.git", optional = true, rev = "768189358fd8ea647e34b250c396daf3479ebbfe" } # 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..2888ec1 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); @@ -149,7 +143,7 @@ pub fn make_event_loop( }; // store the previous msgs processed, for LSS last step - let mut msgs: Option<(Vec, Vec)> = None; + let mut msgs: Option<(Vec, [u8; 32])> = None; // signing loop log::info!("=> starting the main signing loop..."); @@ -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 => { @@ -180,24 +174,28 @@ pub fn make_event_loop( expected_sequence, do_log, ) { - 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); - restart_esp_if_memory_low(); - } else { + Ok((vls_b, lss_b, sequence, _cmd, server_hmac_opt)) => { + if let Some(server_hmac) = server_hmac_opt { // 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) + .set_raw("vls_b", &vls_b) .map_err(|_e| anyhow::anyhow!("failed to put vls_b"))?; msg_store - .put("lss_b", &lss_b) + .set_raw("lss_b", &server_hmac) .map_err(|_e| anyhow::anyhow!("failed to put lss_b"))?; - msgs = Some((vls_b, lss_b)); + msgs = Some((vls_b, server_hmac)); + } else { + // no muts, respond directly back! + 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(); } 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()[..]); } }, }; @@ -230,32 +228,41 @@ pub fn make_event_loop( if msgs.is_none() { log::warn!("Restoring previous message from sd card"); let vls_b = msg_store - .get("vls_b") - .map_err(|e| anyhow::anyhow!("failed to get vls_b: {:?}", e))? - .ok_or(anyhow::anyhow!("vls_b is none"))? - .1; + .get_raw("vls_b") + .map_err(|e| anyhow::anyhow!("failed to get vls_b: {:?}", e))?; let lss_b = msg_store - .get("lss_b") - .map_err(|e| anyhow::anyhow!("failed to get lss_b: {:?}", e))? - .ok_or(anyhow::anyhow!("lss_b is none"))? - .1; - msgs = Some((vls_b, lss_b)); + .get_raw("lss_b") + .map_err(|e| anyhow::anyhow!("failed to get lss_b: {:?}", e))?; + let server_hmac: [u8; 32] = lss_b + .try_into() + .map_err(|e| anyhow::anyhow!("lss_b is not 32 bytes: {:?}", e))?; + msgs = Some((vls_b, server_hmac)); } match lss::handle_lss_msg(&msg_bytes, msgs, &lss_signer) { 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 { + log::info!("HMACs matched! commit now..."); + // 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 +}