From 94f3c005cf4f549d438aa760d8b88e406b9c4ccd Mon Sep 17 00:00:00 2001 From: 8go <17750857+8go@users.noreply.github.com> Date: Tue, 1 Oct 2024 00:26:56 +0200 Subject: [PATCH] added new feature: --nsec Now one can run a command like this to fire-and-forget, i.e. send a message without a config file and forget everything after message has been sent: nostr-commander-rs --nsec nsec1SomeStrangeString --add-relay "wss://some.relay.net/" --publish "some text" or nostr-commander-rs --nsec $NSEC --add-relay "wss://some.relay.net/" --publish - < SOMETEXTFILE.txt --- Cargo.lock | 125 ++++++++++++++++++------------------------------ Cargo.toml | 4 +- README.md | 27 ++++++++--- VERSION | 2 +- help.help.txt | 10 ++-- help.manual.txt | 27 ++++++++--- help.usage.txt | 1 + src/main.rs | 113 +++++++++++++++++++++++++++++++++---------- 8 files changed, 186 insertions(+), 123 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 76f6a66..9a85afe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -143,9 +143,9 @@ checksum = "7c02d123df017efcdfbd739ef81735b36c5ba83ec3c59c80a9d7ecc718f92e50" [[package]] name = "async-trait" -version = "0.1.82" +version = "0.1.83" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" +checksum = "721cae7de5c34fbb2acd27e21e6d2cf7b886dce0c27388d46c4e6c47ea4318dd" dependencies = [ "proc-macro2", "quote", @@ -206,9 +206,9 @@ dependencies = [ [[package]] name = "autocfg" -version = "1.3.0" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "backtrace" @@ -272,9 +272,9 @@ dependencies = [ [[package]] name = "bitcoin" -version = "0.32.2" +version = "0.32.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea507acc1cd80fc084ace38544bbcf7ced7c2aa65b653b102de0ce718df668f6" +checksum = "0032b0e8ead7074cda7fc4f034409607e3f03a6f71d66ade8a307f79b4d99e73" dependencies = [ "base58ck", "bech32", @@ -389,9 +389,9 @@ dependencies = [ [[package]] name = "cc" -version = "1.1.21" +version = "1.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "07b1695e2c7e8fc85310cde85aeaab7e3097f593c91d209d3f9df76c928100f0" +checksum = "3bbb537bb4a30b90362caddba8f360c0a56bc13d3a5570028e7197204cb54a17" dependencies = [ "shlex", ] @@ -622,9 +622,9 @@ checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6" [[package]] name = "flate2" -version = "1.0.33" +version = "1.0.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "324a1be68054ef05ad64b861cc9eaf1d623d2d8cb25b4bf2cb9cdd902b4bf253" +checksum = "a1b589b4dc103969ad3cf85c950899926ec64300a1a46d76c03a6072957036f0" dependencies = [ "crc32fast", "miniz_oxide", @@ -998,9 +998,9 @@ dependencies = [ [[package]] name = "hyper-util" -version = "0.1.8" +version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da62f120a8a37763efb0cf8fdf264b884c7b8b9ac8660b900c8661030c00e6ba" +checksum = "41296eb09f183ac68eec06e03cdbea2e759633d4067b2f6552fc2e009bcad08b" dependencies = [ "bytes", "futures-channel", @@ -1011,7 +1011,6 @@ dependencies = [ "pin-project-lite", "socket2", "tokio", - "tower", "tower-service", "tracing", ] @@ -1122,9 +1121,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libc" -version = "0.2.158" +version = "0.2.159" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" +checksum = "561d97a539a36e26a9a5fad1ea11a3039a67714694aaa379433e580854bc3dc5" [[package]] name = "libredox" @@ -1274,7 +1273,7 @@ dependencies = [ [[package]] name = "nostr-commander" -version = "0.2.5" +version = "0.3.0" dependencies = [ "anyhow", "atty", @@ -1417,9 +1416,12 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.19.0" +version = "1.20.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" +checksum = "82881c4be219ab5faaf2ad5e5e5ecdff8c66bd7402ca3160975c93b24961afd1" +dependencies = [ + "portable-atomic", +] [[package]] name = "opaque-debug" @@ -1533,26 +1535,6 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" -[[package]] -name = "pin-project" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6bf43b791c5b9e34c3d182969b4abb522f9343702850a2e57f460d00d09b4b3" -dependencies = [ - "pin-project-internal", -] - -[[package]] -name = "pin-project-internal" -version = "1.1.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "pin-project-lite" version = "0.2.14" @@ -1567,9 +1549,9 @@ checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" [[package]] name = "pkg-config" -version = "0.3.30" +version = "0.3.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +checksum = "953ec861398dccce10c670dfeaf3ec4911ca479e9c02154b3a215178c5f566f2" [[package]] name = "poly1305" @@ -1582,6 +1564,12 @@ dependencies = [ "universal-hash", ] +[[package]] +name = "portable-atomic" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc9c68a3f6da06753e9335d63e27f6b9754dd1920d941135b7ea8224f141adb2" + [[package]] name = "ppv-lite86" version = "0.2.20" @@ -1689,9 +1677,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.4" +version = "0.5.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" +checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" dependencies = [ "bitflags 2.6.0", ] @@ -1709,9 +1697,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.6" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +checksum = "38200e5ee88914975b69f657f0801b6f6dccafd44fd9326302a4aaeecfacb1d8" dependencies = [ "aho-corasick", "memchr", @@ -1721,9 +1709,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +checksum = "368758f23274712b504848e9d5a6f010445cc8b87a7cdb4d7cbee666c1288da3" dependencies = [ "aho-corasick", "memchr", @@ -1732,9 +1720,9 @@ dependencies = [ [[package]] name = "regex-syntax" -version = "0.8.4" +version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" +checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" @@ -1911,9 +1899,9 @@ dependencies = [ [[package]] name = "rustls-pki-types" -version = "1.8.0" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fc0a2ce646f8655401bb81e7927b812614bd5d91dbc968696be50603510fcaf0" +checksum = "0e696e35370c65c9c541198af4543ccd580cf17fc25d8e05c5a242b202488c55" [[package]] name = "rustls-webpki" @@ -2174,9 +2162,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.77" +version = "2.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" +checksum = "89132cd0bf050864e1d38dc3bbc07a0eb8e7530af26344d3d2bbbef83499f590" dependencies = [ "proc-macro2", "quote", @@ -2221,9 +2209,9 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.12.0" +version = "3.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbcdd0c794ebb0d4cf35e88edd2f7d2c4c3e9a5a6dab322839b321c6a87a64" +checksum = "f0f2c9fc62d0beef6951ccffd757e241266a2c833136efbe35af6cd2567dca5b" dependencies = [ "cfg-if", "fastrand", @@ -2244,18 +2232,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0342370b38b6a11b6cc11d6a805569958d54cfa061a29969c3b5ce2ea405724" +checksum = "d50af8abc119fb8bb6dbabcfa89656f46f84aa0ac7688088608076ad2b459a84" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.63" +version = "1.0.64" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261" +checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", @@ -2399,27 +2387,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tower" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c" -dependencies = [ - "futures-core", - "futures-util", - "pin-project", - "pin-project-lite", - "tokio", - "tower-layer", - "tower-service", -] - -[[package]] -name = "tower-layer" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" - [[package]] name = "tower-service" version = "0.3.3" diff --git a/Cargo.toml b/Cargo.toml index ccdd325..b1a716b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,7 +2,7 @@ [package] name = "nostr-commander" -version = "0.2.5" +version = "0.3.0" edition = "2021" description = "simple but convenient CLI-based Nostr client app for publishing,sending and subscribing" documentation = "https://docs.rs/nostr-commander" @@ -39,7 +39,7 @@ chrono = "0.4" json = "0.12" atty = "0.2" reqwest = "0.11" -regex = "1.9" +regex = "1.11" [dev-dependencies] diff --git a/README.md b/README.md index b550f66..19271f5 100644 --- a/README.md +++ b/README.md @@ -148,9 +148,11 @@ $ nostr-commander-rs --subscribe-channel 25e5c82273a271cb1a840d0060391a0bf4965ca ``` Welcome to "nostr-commander-rs", a Nostr CLI client. ─── On the first run use --create-user to create a user. On further runs you can publish notes, send -private DM messages, etc. ─── Have a look at the repo -"https://github.com/8go/nostr-commander-rs/" and see if you can contribute code -to improve this tool. Safe! +private DM messages, etc. Alternatively, you can skip creating a user and do a +fire-and-forget publish like this: nostr-commander-rs --nsec +nsec1SomeStrangeString --add-relay "wss://some.relay.net/" --publish "test". +─── Have a look at the repo "https://github.com/8go/nostr-commander-rs/" and +see if you can contribute code to improve this tool. Safe! Usage: nostr-commander-rs [OPTIONS] @@ -236,9 +238,10 @@ Options: Create a new user, i.e. a new key pair. Details:: This is usually done only once at the beginning. If you ever want to wipe this user, use '--delete-user' which deletes the key pair. Use this option in - combination with --name, --display_name, --about, --picture, and - --nip05. Also highly recommended that you use this option together - with --add-relay + combination with --name, --display_name, --about, --picture, --nip05, + and --nsec. Also highly recommended that you use this option together + with --add-relay. Add --nsec as option to import your existing nsec + private key, otherwise a new private key will be generated for you --delete-user Delete the current user, i.e. delete the current key pair. Details:: @@ -278,6 +281,18 @@ Options: be queried via the keyboard. If you want to set it to empty and not be queried, provide an empty string '' + --nsec + Provide one private key. Details:: It has the form + 'nsec1SomeStrangeString'. Alternatively you can use the Hex form of + the private key. Since this is your private key, you must protect it. + Don't put this key into a script, into Github, etc. If --nsec is + provided it will be used instead of the private key in the + credentials file. This argument is used e.g. in combination with + argument --publish. If you are using --nsec without a credentials + file most likely you want to also use -ad-relay argument. If --nsec + is used without --create-user then the credentials file will not be + modified or will not be created + -p, --publish [...] Publish one or multiple notes. Details:: Notes data must not be binary data, it must be text. Input piped via stdin can additionally diff --git a/VERSION b/VERSION index 3a4036f..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.5 +0.3.0 diff --git a/help.help.txt b/help.help.txt index d6d408f..b1b127c 100644 --- a/help.help.txt +++ b/help.help.txt @@ -1,8 +1,10 @@ Welcome to "nostr-commander-rs", a Nostr CLI client. ─── On the first run use --create-user to create a user. On further runs you can publish notes, send -private DM messages, etc. ─── Have a look at the repo -"https://github.com/8go/nostr-commander-rs/" and see if you can contribute code -to improve this tool. Safe! +private DM messages, etc. Alternatively, you can skip creating a user and do a +fire-and-forget publish like this: nostr-commander-rs --nsec +nsec1SomeStrangeString --add-relay "wss://some.relay.net/" --publish "test". +─── Have a look at the repo "https://github.com/8go/nostr-commander-rs/" and +see if you can contribute code to improve this tool. Safe! Usage: nostr-commander-rs [OPTIONS] Options: --contribute @@ -39,6 +41,8 @@ Options: Specify an optional picture or avatar. --nip05 Specify an optional nip05 name. + --nsec + Provide one private key. -p, --publish [...] Publish one or multiple notes. --publish-pow [...] diff --git a/help.manual.txt b/help.manual.txt index bc952a1..62b0f8a 100644 --- a/help.manual.txt +++ b/help.manual.txt @@ -1,8 +1,10 @@ Welcome to "nostr-commander-rs", a Nostr CLI client. ─── On the first run use --create-user to create a user. On further runs you can publish notes, send -private DM messages, etc. ─── Have a look at the repo -"https://github.com/8go/nostr-commander-rs/" and see if you can contribute code -to improve this tool. Safe! +private DM messages, etc. Alternatively, you can skip creating a user and do a +fire-and-forget publish like this: nostr-commander-rs --nsec +nsec1SomeStrangeString --add-relay "wss://some.relay.net/" --publish "test". +─── Have a look at the repo "https://github.com/8go/nostr-commander-rs/" and +see if you can contribute code to improve this tool. Safe! Usage: nostr-commander-rs [OPTIONS] @@ -88,9 +90,10 @@ Options: Create a new user, i.e. a new key pair. Details:: This is usually done only once at the beginning. If you ever want to wipe this user, use '--delete-user' which deletes the key pair. Use this option in - combination with --name, --display_name, --about, --picture, and - --nip05. Also highly recommended that you use this option together - with --add-relay + combination with --name, --display_name, --about, --picture, --nip05, + and --nsec. Also highly recommended that you use this option together + with --add-relay. Add --nsec as option to import your existing nsec + private key, otherwise a new private key will be generated for you --delete-user Delete the current user, i.e. delete the current key pair. Details:: @@ -130,6 +133,18 @@ Options: be queried via the keyboard. If you want to set it to empty and not be queried, provide an empty string '' + --nsec + Provide one private key. Details:: It has the form + 'nsec1SomeStrangeString'. Alternatively you can use the Hex form of + the private key. Since this is your private key, you must protect it. + Don't put this key into a script, into Github, etc. If --nsec is + provided it will be used instead of the private key in the + credentials file. This argument is used e.g. in combination with + argument --publish. If you are using --nsec without a credentials + file most likely you want to also use -ad-relay argument. If --nsec + is used without --create-user then the credentials file will not be + modified or will not be created + -p, --publish [...] Publish one or multiple notes. Details:: Notes data must not be binary data, it must be text. Input piped via stdin can additionally diff --git a/help.usage.txt b/help.usage.txt index e5241fb..235306e 100644 --- a/help.usage.txt +++ b/help.usage.txt @@ -17,6 +17,7 @@ Options: --about --picture --nip05 + --nsec -p, --publish [...] --publish-pow [...] --dm [...] diff --git a/src/main.rs b/src/main.rs index 225555b..a2301e5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -324,7 +324,11 @@ impl fmt::Display for Output { /// Welcome to "nostr-commander-rs", a Nostr CLI client. ─── /// On the first run use --create-user to create a user. /// On further runs you can publish notes, send private DM messages, -/// etc. ─── +/// etc. +/// Alternatively, you can skip creating a user and do a fire-and-forget +/// publish like this: nostr-commander-rs --nsec nsec1SomeStrangeString --add-relay +/// "wss://some.relay.net/" --publish "test". +/// ─── /// Have a look at the repo "https://github.com/8go/nostr-commander-rs/" /// and see if you can contribute code to improve this tool. /// Safe! @@ -451,9 +455,13 @@ pub struct Args { /// done only once at the beginning. If you ever want to wipe /// this user, use '--delete-user' which deletes the key /// pair. Use this option in combination with --name, - /// --display_name, --about, --picture, and --nip05. + /// --display_name, --about, --picture, --nip05, and + /// --nsec. /// Also highly recommended that you use this option /// together with --add-relay. + /// Add --nsec as option to import your existing nsec + /// private key, otherwise a new private key will be + /// generated for you. #[arg(long, alias = "create-key", default_value_t = false)] create_user: bool, @@ -515,6 +523,22 @@ pub struct Args { #[arg(long, value_name = "NIP05_ID")] nip05: Option, + /// Provide one private key. + /// Details:: It has the form 'nsec1SomeStrangeString'. + /// Alternatively you can use the Hex form of the private key. + /// Since this is your private key, you must protect it. Don't put + /// this key into a script, into Github, etc. + /// If --nsec is provided it will be used instead of the private key in + /// the credentials file. + /// This argument is used e.g. in combination with argument + /// --publish. + /// If you are using --nsec without a credentials file most likely + /// you want to also use -ad-relay argument. + /// If --nsec is used without --create-user then the credentials + /// file will not be modified or will not be created. + #[arg(long, value_name = "PRIVATE_KEY")] + nsec: Option, + /// Publish one or multiple notes. /// Details:: /// Notes data must not be binary data, it @@ -884,6 +908,7 @@ impl Args { about: None, picture: None, nip05: None, + nsec: None, publish: Vec::new(), publish_pow: Vec::new(), dm: Vec::new(), @@ -1525,11 +1550,15 @@ pub(crate) fn read_credentials(ap: &mut Args) -> Result<(), Error> { return Ok(()); } Err(ref e) => { - error!( - "Error: failed to read credentials file {:?}. Aborting. Correct path? Error reported: {:?}.", - get_credentials_actual_path(&ap), - e, - ); + if ap.nsec.is_some() { + debug!("no credentials file found or read") + } else { + error!( + "Error: failed to read credentials file {:?}. Correct path? Error reported: {:?}.", + get_credentials_actual_path(&ap), + e, + ); + }; return Err(Error::StorageFailure); } } @@ -1661,8 +1690,16 @@ pub(crate) fn cli_create_user(ap: &mut Args) -> Result<(), Error> { } ap.creds.relays.dedup_by(|a, b| a.url == b.url); - // Generate new keys - let my_keys: Keys = Keys::generate(); + let my_keys: Keys = if ap.nsec.is_some() { + // nsec key provided as argument, import it + // parses from bech32 as well as from hex + debug!("Importing private key from --nsec argument"); + Keys::new(SecretKey::parse(ap.nsec.clone().unwrap())?) + } else { + // Generate new keys + debug!("A new private key is being generated for you"); + Keys::generate() + }; debug!("Generated private key is: {:?}", my_keys.secret_key()); debug!("Generated public key is: {:?}", my_keys.public_key()); match my_keys.public_key().to_bech32() { @@ -1778,20 +1815,25 @@ pub(crate) async fn cli_add_relay(client: &mut Client, ap: &mut Args) -> Result< } } - match ap.creds.save(get_credentials_actual_path(ap)) { - Ok(()) => { - debug!( - "writing new relays {:?} to credentials file successful.", - ap.creds.relays - ); - } - Err(ref e) => { - error!( - "Error: writing new relays {:?} to credentials file failed. Reported error {:?}.", - ap.creds.relays, e - ); - err_count += 1; + if ap.nsec.is_none() || ap.create_user { + debug!("Creating or updating credentials file to add new relays."); + match ap.creds.save(get_credentials_actual_path(ap)) { + Ok(()) => { + debug!( + "writing new relays {:?} to credentials file successful.", + ap.creds.relays + ); + } + Err(ref e) => { + error!( + "Error: writing new relays {:?} to credentials file failed. Reported error {:?}.", + ap.creds.relays, e + ); + err_count += 1; + } } + } else { + debug!("Not creating or not updating credentials file with new relays.") } match err_count { @@ -2904,14 +2946,25 @@ async fn main() -> Result<(), Error> { } else { match crate::read_credentials(&mut ap) { Ok(()) => { - info!("User created successfully."); + info!("User credentials read successfully."); } Err(ref e) => { - error!("Credentials file does not exists or cannot be read. Try creating a user first with --create-user. Check your arguments and try again. Worst case if file is corrupted or lost, consider doing a '--delete-user' to clean up, then perform a new '--create-user'. {:?}.", e); - return Err(Error::ReadingCredentialsFailed); + if ap.nsec.is_some() { + debug!("User id will be taken from --nsec argument."); + } else { + error!("Credentials file does not exists or cannot be read. Try creating a user first with --create-user. Check your arguments and try again. Worst case if file is corrupted or lost, consider doing a '--delete-user' to clean up, then perform a new '--create-user'. {:?}.", e); + return Err(Error::ReadingCredentialsFailed); + } } } } + if ap.nsec.is_some() { + debug!("We use private and public key from --nsec argument."); + // parses from bech32 as well as from hex + let my_keys = Keys::parse(&ap.nsec.clone().unwrap())?; + ap.creds.public_key_bech32 = my_keys.public_key().to_bech32().unwrap(); + ap.creds.secret_key_bech32 = my_keys.secret_key().to_bech32().unwrap(); + } // credentials are filled now debug!("Welcome to nostr-commander-rs"); @@ -3029,6 +3082,9 @@ async fn main() -> Result<(), Error> { info!("initiating connect now."); client.connect().await; info!("connect successful."); + if client.relays().await.is_empty() { + error!("Client has no relay. Certain operations will fail. Consider using --add-relay argument.") + } is_connected = true; } @@ -3193,7 +3249,12 @@ async fn main() -> Result<(), Error> { let filter = Filter::new().pubkeys(ap.creds.subscribed_channels.clone()); subscribe_to_filter(&client, &ap, filter, "channels").await; } - ap.creds.save(get_credentials_actual_path(&ap))?; + if ap.nsec.is_none() || ap.create_user { + debug!("Creating or updating credentials file."); + ap.creds.save(get_credentials_actual_path(&ap))?; + } else { + debug!("Not creating or not updating credentials file.") + } // notices will be published even if we do not go into handle_notification event loop // Design choice: Do not automatically listen when subscriptions exist, only listen to subscriptions if --listen is set.