From 590b26a8bfd120e416268121f97ef4e1f730b126 Mon Sep 17 00:00:00 2001 From: arkanoider Date: Thu, 2 May 2024 12:30:23 +0200 Subject: [PATCH 1/2] first idea of mostro message on request --- Cargo.lock | 279 ++++++++++++++++++++++++++------------------------- src/app.rs | 7 ++ src/nip33.rs | 59 +++++++++++ src/util.rs | 11 ++ 4 files changed, 220 insertions(+), 136 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c441c1..3fa7840 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -202,9 +202,9 @@ dependencies = [ [[package]] name = "async-wsocket" -version = "0.3.0" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d253e375ea899cb131b92a474587e217634e7ea927c24d8098eecbcad0c5c97a" +checksum = "5c38341e6ee670913fb9dc3aba40c22d616261da4dc0928326d3168ebf576fb0" dependencies = [ "async-utility", "futures-util", @@ -336,6 +336,12 @@ version = "0.21.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9d297deb1925b89f2ccc13d7635fa0714f12c87adce1c75356b39ca9b7178567" +[[package]] +name = "base64" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" + [[package]] name = "base64ct" version = "1.6.0" @@ -348,6 +354,12 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d86b93f97252c47b41663388e6d155714a9d0c398b99f1005cbc5f978b29f445" +[[package]] +name = "bech32" +version = "0.10.0-beta" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98f7eed2b2781a6f0b5c903471d48e15f56fb4e1165df8a9a2337fd1a59d45ea" + [[package]] name = "bip39" version = "2.0.0" @@ -365,11 +377,35 @@ version = "0.30.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1945a5048598e4189e239d3f809b19bdad4845c4b2ba400d304d2dcf26d2c462" dependencies = [ - "bech32", + "bech32 0.9.1", "bitcoin-private", "bitcoin_hashes 0.12.0", "hex_lit", - "secp256k1", + "secp256k1 0.27.0", + "serde", +] + +[[package]] +name = "bitcoin" +version = "0.31.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c85783c2fe40083ea54a33aa2f0ba58831d90fcd190f5bdc47e74e84d2a96ae" +dependencies = [ + "bech32 0.10.0-beta", + "bitcoin-internals", + "bitcoin_hashes 0.13.0", + "hex-conservative", + "hex_lit", + "secp256k1 0.28.2", + "serde", +] + +[[package]] +name = "bitcoin-internals" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9425c3bf7089c983facbae04de54513cce73b41c7f9ff8c845b54e7bc64ebbfb" +dependencies = [ "serde", ] @@ -395,6 +431,17 @@ dependencies = [ "serde", ] +[[package]] +name = "bitcoin_hashes" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1930a4dabfebb8d7d9992db18ebe3ae2876f0a305fab206fd168df931ede293b" +dependencies = [ + "bitcoin-internals", + "hex-conservative", + "serde", +] + [[package]] name = "bitflags" version = "1.3.2" @@ -1383,16 +1430,19 @@ dependencies = [ [[package]] name = "hyper-rustls" -version = "0.24.2" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ec3efd23720e2049821a693cbc7e65ea87c72f1c58ff2f9522ff332b1491e590" +checksum = "a0bea761b46ae2b24eb4aef630d8d1c398157b6fc29e6350ecf090a0b70c952c" dependencies = [ "futures-util", - "http 0.2.12", - "hyper 0.14.28", - "rustls 0.21.10", + "http 1.1.0", + "hyper 1.2.0", + "hyper-util", + "rustls 0.22.3", + "rustls-pki-types", "tokio", - "tokio-rustls 0.24.1", + "tokio-rustls 0.25.0", + "tower-service", ] [[package]] @@ -1407,19 +1457,6 @@ dependencies = [ "tokio-io-timeout", ] -[[package]] -name = "hyper-tls" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d6183ddfa99b85da61a140bea0efc93fdf56ceaa041b37d553518030827f9905" -dependencies = [ - "bytes", - "hyper 0.14.28", - "native-tls", - "tokio", - "tokio-native-tls", -] - [[package]] name = "hyper-tls" version = "0.6.0" @@ -1615,25 +1652,25 @@ dependencies = [ [[package]] name = "lightning" -version = "0.0.121" +version = "0.0.122" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b0c1f811ae288f86c6767055c55b5f7a721ca1e61bf1897a9ae2ec663e8aba1" +checksum = "0d9b36ae12b379905bfc429ce5d4e8ca4a55c8dd3de73074309bd0bcc053bcac" dependencies = [ - "bitcoin", + "bitcoin 0.30.2", "hex-conservative", ] [[package]] name = "lightning-invoice" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b186aca4a605d4db3b85979922be287b9ebd5dedd8132963bb9dbeb8f7d2a04" +checksum = "106fdb897e69df697480f45bf0a564b425af488fb0f7407e770a770c39b19a21" dependencies = [ - "bech32", - "bitcoin", + "bech32 0.9.1", + "bitcoin 0.30.2", "lightning", "num-traits", - "secp256k1", + "secp256k1 0.27.0", ] [[package]] @@ -1659,30 +1696,30 @@ checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" [[package]] name = "lnurl-pay" -version = "0.3.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b628658116d331c9567f6cb22415d726125ff6e328d1fb1b422b1b58afeaec21" +checksum = "02c042191c2e3f27147decfad8182eea2c7dd1c6c1733562e25d3d401369669d" dependencies = [ - "bech32", - "reqwest 0.11.27", + "bech32 0.10.0-beta", + "reqwest", "serde", "serde_json", ] [[package]] name = "lnurl-rs" -version = "0.4.1" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29742339d2d88bd3ea1f4305e11b22d3efada9f86010ccbd7b6646837cc57e85" +checksum = "043935963e3454227165b4daf62e8fdecd6273857e0e6fc00422aea4d9258673" dependencies = [ "aes", "anyhow", - "base64 0.13.1", - "bech32", - "bitcoin", + "base64 0.22.0", + "bech32 0.9.1", + "bitcoin 0.30.2", "cbc", "email_address", - "reqwest 0.11.27", + "reqwest", "serde", "serde_json", "ureq", @@ -1809,8 +1846,7 @@ dependencies = [ "lnurl-rs", "mostro-core", "nostr-sdk", - "openssl", - "reqwest 0.12.2", + "reqwest", "serde", "serde_json", "sqlx", @@ -1824,9 +1860,7 @@ dependencies = [ [[package]] name = "mostro-core" -version = "0.5.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3ae55c1c489c71622adbc7bdd472e6eeaccb2c9dd9f4f3967147403561310972" +version = "0.5.8" dependencies = [ "anyhow", "serde", @@ -1878,14 +1912,14 @@ dependencies = [ [[package]] name = "nostr" -version = "0.29.1" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25e4e34578e8cc2b4050c6224a0c422b23ba1e61f2602b4e320c221ac3cbbc2e" +checksum = "a27223888faca0c4ba9b97c2b7dc776e9a33d5f54e3558887471cf17798b5fbf" dependencies = [ "aes", "base64 0.21.7", "bip39", - "bitcoin", + "bitcoin 0.31.2", "cbc", "chacha20", "chacha20poly1305", @@ -1894,7 +1928,7 @@ dependencies = [ "js-sys", "negentropy", "once_cell", - "reqwest 0.11.27", + "reqwest", "scrypt", "serde", "serde_json", @@ -1908,9 +1942,9 @@ dependencies = [ [[package]] name = "nostr-database" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e15ab55f96ea5e560af0c75f1d942b1064266d443d11b2afbe51ca9ad78a018" +checksum = "f726b8c0904a838f64b51a931a1bf39e341f5584a5e04f06310fbfb847e2e924" dependencies = [ "async-trait", "lru", @@ -1922,9 +1956,9 @@ dependencies = [ [[package]] name = "nostr-relay-pool" -version = "0.29.2" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1b306bc99d49064950a16a06d35c7c19af94d8b4052fad0dfe02e41e529d5d3" +checksum = "52f0ccf9e81aa747abdfa130007651248b37c3699d37029bad701e68902257ce" dependencies = [ "async-utility", "async-wsocket", @@ -1938,9 +1972,9 @@ dependencies = [ [[package]] name = "nostr-sdk" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81ed0ab9cbc3b20d3dba99337f2e0739f052ebe32133d690e212022a06a22044" +checksum = "d1ffedac7ab488e0dfea52804d0c43fafc7e3eefc62d97726d3927a1390db05b" dependencies = [ "async-utility", "lnurl-pay", @@ -1957,22 +1991,23 @@ dependencies = [ [[package]] name = "nostr-signer" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "307bdc7c26887d7e65632e66872989a19892dfe9f2c6dbd9a1d3f959c5c524d5" +checksum = "22e568670664cf5cc14a794ae32dfc04bde385d63ff0f5b1c3745dd3ea69f73a" dependencies = [ "async-utility", "nostr", "nostr-relay-pool", "thiserror", "tokio", + "tracing", ] [[package]] name = "nostr-zapper" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "061d5eb00b430747a984ea9e41cd82c849832151b4263d8230c9c220dc2c62f8" +checksum = "420a7c6458d5c1dc502b3d36fb9f8598837743a737b84adb4ef8ea36b98c5e07" dependencies = [ "async-trait", "nostr", @@ -2010,9 +2045,9 @@ dependencies = [ [[package]] name = "nwc" -version = "0.29.0" +version = "0.30.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d1894ffe54a1e5adf8dbb22b5a290c0748ec4a88aa07fa69c4359010edea49ed" +checksum = "e236611ea96d3545138f7b2152f2e4571e3c93436ddc91d1c458f366e5c6430f" dependencies = [ "async-utility", "nostr", @@ -2081,15 +2116,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" -[[package]] -name = "openssl-src" -version = "300.2.3+3.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cff92b6f71555b61bb9315f7c64da3ca43d87531622120fea0195fc761b4843" -dependencies = [ - "cc", -] - [[package]] name = "openssl-sys" version = "0.9.102" @@ -2098,7 +2124,6 @@ checksum = "c597637d56fbc83893a35eb0dd04b2b8e7a50c91e64e9493e398b5df4fb45fa2" dependencies = [ "cc", "libc", - "openssl-src", "pkg-config", "vcpkg", ] @@ -2519,56 +2544,11 @@ checksum = "adad44e29e4c806119491a7f06f03de4d1af22c3a680dd47f1e6e179439d1f56" [[package]] name = "reqwest" -version = "0.11.27" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd67538700a17451e7cba03ac727fb961abb7607553461627b97de0b89cf4a62" -dependencies = [ - "base64 0.21.7", - "bytes", - "encoding_rs", - "futures-core", - "futures-util", - "h2 0.3.25", - "http 0.2.12", - "http-body 0.4.6", - "hyper 0.14.28", - "hyper-rustls", - "hyper-tls 0.5.0", - "ipnet", - "js-sys", - "log", - "mime", - "native-tls", - "once_cell", - "percent-encoding", - "pin-project-lite", - "rustls 0.21.10", - "rustls-pemfile", - "serde", - "serde_json", - "serde_urlencoded", - "sync_wrapper", - "system-configuration", - "tokio", - "tokio-native-tls", - "tokio-rustls 0.24.1", - "tokio-socks", - "tower-service", - "url", - "wasm-bindgen", - "wasm-bindgen-futures", - "web-sys", - "webpki-roots 0.25.4", - "winreg", -] - -[[package]] -name = "reqwest" -version = "0.12.2" +version = "0.12.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d66674f2b6fb864665eea7a3c1ac4e3dfacd2fda83cf6f935a612e01b0e3338" +checksum = "566cafdd92868e0939d3fb961bd0dc25fcfaaed179291093b3d43e6b3150ea10" dependencies = [ - "base64 0.21.7", + "base64 0.22.0", "bytes", "encoding_rs", "futures-core", @@ -2578,7 +2558,8 @@ dependencies = [ "http-body 1.0.0", "http-body-util", "hyper 1.2.0", - "hyper-tls 0.6.0", + "hyper-rustls", + "hyper-tls", "hyper-util", "ipnet", "js-sys", @@ -2588,7 +2569,9 @@ dependencies = [ "once_cell", "percent-encoding", "pin-project-lite", - "rustls-pemfile", + "rustls 0.22.3", + "rustls-pemfile 2.1.2", + "rustls-pki-types", "serde", "serde_json", "serde_urlencoded", @@ -2596,11 +2579,14 @@ dependencies = [ "system-configuration", "tokio", "tokio-native-tls", + "tokio-rustls 0.25.0", + "tokio-socks", "tower-service", "url", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", + "webpki-roots 0.26.1", "winreg", ] @@ -2731,6 +2717,16 @@ dependencies = [ "base64 0.21.7", ] +[[package]] +name = "rustls-pemfile" +version = "2.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29993a25686778eb88d4189742cd713c9bce943bc54251a33509dc63cbacf73d" +dependencies = [ + "base64 0.22.0", + "rustls-pki-types", +] + [[package]] name = "rustls-pki-types" version = "1.4.1" @@ -2818,7 +2814,19 @@ checksum = "25996b82292a7a57ed3508f052cfff8640d38d32018784acd714758b43da9c8f" dependencies = [ "bitcoin_hashes 0.12.0", "rand", - "secp256k1-sys", + "secp256k1-sys 0.8.1", + "serde", +] + +[[package]] +name = "secp256k1" +version = "0.28.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d24b59d129cdadea20aea4fb2352fa053712e5d713eee47d700cd4b2bc002f10" +dependencies = [ + "bitcoin_hashes 0.12.0", + "rand", + "secp256k1-sys 0.9.2", "serde", ] @@ -2831,6 +2839,15 @@ dependencies = [ "cc", ] +[[package]] +name = "secp256k1-sys" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5d1746aae42c19d583c3c1a8c646bfad910498e2051c551a7f2e3c0c9fbb7eb" +dependencies = [ + "cc", +] + [[package]] name = "security-framework" version = "2.10.0" @@ -3106,7 +3123,7 @@ dependencies = [ "paste", "percent-encoding", "rustls 0.20.9", - "rustls-pemfile", + "rustls-pemfile 1.0.4", "serde", "sha2 0.10.8", "smallvec", @@ -3397,16 +3414,6 @@ dependencies = [ "webpki", ] -[[package]] -name = "tokio-rustls" -version = "0.24.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c28327cf380ac148141087fbfb9de9d7bd4e84ab5d2c28fbc911d753de8a7081" -dependencies = [ - "rustls 0.21.10", - "tokio", -] - [[package]] name = "tokio-rustls" version = "0.25.0" @@ -4222,9 +4229,9 @@ dependencies = [ [[package]] name = "winreg" -version = "0.50.0" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" +checksum = "a277a57398d4bfa075df44f501a17cfdf8542d224f0d36095a2adc7aee4ef0a5" dependencies = [ "cfg-if", "windows-sys 0.48.0", diff --git a/src/app.rs b/src/app.rs index 570c635..9837d01 100644 --- a/src/app.rs +++ b/src/app.rs @@ -175,6 +175,13 @@ pub async fn run( warning_msg(&action, e) } } + Action::Info => { + if let Err(e) = + crate::util::send_mostro_info(&my_keys).await + { + warning_msg(&action, e) + } + } _ => info!("Received message with action {:?}", action), } } diff --git a/src/nip33.rs b/src/nip33.rs index 9dec490..3136908 100644 --- a/src/nip33.rs +++ b/src/nip33.rs @@ -1,3 +1,4 @@ +use crate::Settings; use chrono::Duration; use mostro_core::order::Order; use mostro_core::NOSTR_REPLACEABLE_EVENT_KIND; @@ -68,3 +69,61 @@ pub fn order_to_tags(order: &Order) -> Vec<(String, String)> { tags } + +/// Transform mostro info fields to tags +/// +/// # Arguments +/// +/// +pub fn info_to_tags(mostro_pubkey: &PublicKey) -> Vec<(String, String)> { + let mostro_settings = Settings::get_mostro(); + let ln_settings = Settings::get_ln(); + + let tags = vec![ + // max_order_amount + ("mostro_pubkey".to_string(), mostro_pubkey.to_string()), + // max_order_amount + ( + "max_order_amount".to_string(), + mostro_settings.max_order_amount.to_string(), + ), + // min_order_amount + ( + "min_order_amount".to_string(), + mostro_settings.min_payment_amount.to_string(), + ), + // expiration_hours + ( + "expiration_hours".to_string(), + mostro_settings.expiration_hours.to_string(), + ), + // expiration_seconds + ( + "expiration_seconds".to_string(), + mostro_settings.expiration_seconds.to_string(), + ), + // fee + ("fee".to_string(), mostro_settings.fee.to_string()), + // hold_invoice_expiration_window + ( + "hold_invoice_expiration_window".to_string(), + ln_settings.hold_invoice_expiration_window.to_string(), + ), + // hold_invoice_cltv_delta + ( + "hold_invoice_cltv_delta".to_string(), + ln_settings.hold_invoice_cltv_delta.to_string(), + ), + // invoice_expiration_window + ( + "invoice_expiration_window".to_string(), + ln_settings.hold_invoice_cltv_delta.to_string(), + ), + // Label to identify this is a Mostro's infos + ("y".to_string(), "mostrop2p".to_string()), + // Table name + ("z".to_string(), "info".to_string()), + ]; + + tags +} diff --git a/src/util.rs b/src/util.rs index 1c3c85f..bb7d430 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,6 +6,7 @@ use crate::lightning; use crate::lightning::LndConnector; use crate::messages; use crate::models::Yadio; +use crate::nip33::info_to_tags; use crate::nip33::{new_event, order_to_tags}; use crate::NOSTR_CLIENT; @@ -567,3 +568,13 @@ pub async fn send_new_order_msg( let _ = send_dm(destination_key, message).await; } } + +pub async fn send_mostro_info(mostro_pubkey: &Keys) -> Result<()> { + let tags = info_to_tags(&mostro_pubkey.public_key()); + let id = format!("Mostro npub {} info event", mostro_pubkey.public_key()); + + let info_ev = new_event(mostro_pubkey, "", id, tags)?; + NOSTR_CLIENT.get().unwrap().send_event(info_ev).await?; + + Ok(()) +} From 8fe6e2a59d11f69af934d8b2fc1ae79dbab806a5 Mon Sep 17 00:00:00 2001 From: arkanoider Date: Thu, 2 May 2024 17:34:00 +0200 Subject: [PATCH 2/2] info message schedule every 5 minutes --- src/app.rs | 7 ------- src/scheduler.rs | 22 ++++++++++++++++++++++ src/util.rs | 11 ----------- 3 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/app.rs b/src/app.rs index 9837d01..570c635 100644 --- a/src/app.rs +++ b/src/app.rs @@ -175,13 +175,6 @@ pub async fn run( warning_msg(&action, e) } } - Action::Info => { - if let Err(e) = - crate::util::send_mostro_info(&my_keys).await - { - warning_msg(&action, e) - } - } _ => info!("Received message with action {:?}", action), } } diff --git a/src/scheduler.rs b/src/scheduler.rs index 3ababa6..3f17b83 100644 --- a/src/scheduler.rs +++ b/src/scheduler.rs @@ -21,10 +21,32 @@ pub async fn start_scheduler(rate_list: Arc>>) { job_update_rate_events(rate_list).await; job_cancel_orders().await; job_retry_failed_payments().await; + job_info_event_send().await; info!("Scheduler Started"); } +async fn job_info_event_send() { + let mostro_pubkey = match get_keys() { + Ok(keys) => keys, + Err(e) => return error!("{e}"), + }; + + tokio::spawn(async move { + loop { + info!("Sending info about mostro"); + + let tags = crate::nip33::info_to_tags(&mostro_pubkey.public_key()); + let id = format!("Mostro npub {} info event", mostro_pubkey.public_key()); + + let info_ev = crate::nip33::new_event(&mostro_pubkey, "", id, tags).unwrap(); + let _ = NOSTR_CLIENT.get().unwrap().send_event(info_ev).await; + + tokio::time::sleep(tokio::time::Duration::from_secs(300)).await; + } + }); +} + async fn job_retry_failed_payments() { let ln_settings = Settings::get_ln(); let retries_number = ln_settings.payment_attempts as i64; diff --git a/src/util.rs b/src/util.rs index bb7d430..1c3c85f 100644 --- a/src/util.rs +++ b/src/util.rs @@ -6,7 +6,6 @@ use crate::lightning; use crate::lightning::LndConnector; use crate::messages; use crate::models::Yadio; -use crate::nip33::info_to_tags; use crate::nip33::{new_event, order_to_tags}; use crate::NOSTR_CLIENT; @@ -568,13 +567,3 @@ pub async fn send_new_order_msg( let _ = send_dm(destination_key, message).await; } } - -pub async fn send_mostro_info(mostro_pubkey: &Keys) -> Result<()> { - let tags = info_to_tags(&mostro_pubkey.public_key()); - let id = format!("Mostro npub {} info event", mostro_pubkey.public_key()); - - let info_ev = new_event(mostro_pubkey, "", id, tags)?; - NOSTR_CLIENT.get().unwrap().send_event(info_ev).await?; - - Ok(()) -}