upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/key_handling/encryption.rs12
-rw-r--r--src/key_handling/users.rs6
-rw-r--r--src/login.rs8
-rw-r--r--test_utils/src/lib.rs8
4 files changed, 20 insertions, 14 deletions
diff --git a/src/key_handling/encryption.rs b/src/key_handling/encryption.rs
index 0ef7f69..54002fa 100644
--- a/src/key_handling/encryption.rs
+++ b/src/key_handling/encryption.rs
@@ -1,3 +1,5 @@
1use std::str::FromStr;
2
1use anyhow::{anyhow, bail, ensure, Context, Result}; 3use anyhow::{anyhow, bail, ensure, Context, Result};
2use chacha20poly1305::{ 4use chacha20poly1305::{
3 aead::{rand_core::RngCore, Aead, AeadCore, KeyInit, OsRng, Payload}, 5 aead::{rand_core::RngCore, Aead, AeadCore, KeyInit, OsRng, Payload},
@@ -6,6 +8,7 @@ use chacha20poly1305::{
6#[cfg(test)] 8#[cfg(test)]
7use mockall::*; 9use mockall::*;
8use nostr::{prelude::*, Keys}; 10use nostr::{prelude::*, Keys};
11use nostr_sdk::bech32::{self, FromBase32, ToBase32};
9use rand::{distributions::Alphanumeric, thread_rng, Rng}; 12use rand::{distributions::Alphanumeric, thread_rng, Rng};
10use zeroize::Zeroize; 13use zeroize::Zeroize;
11 14
@@ -120,10 +123,11 @@ impl EncryptDecrypt for Encryptor {
120 bail!("invalid encrypted key"); 123 bail!("invalid encrypted key");
121 } 124 }
122 125
123 let key = Keys::from_sk_str( 126 let key =
124 std::str::from_utf8(&inner_secret).context("inner secret is not [u8]")?, 127 Keys::from_str(std::str::from_utf8(&inner_secret).context("inner secret is not [u8]")?)
125 ) 128 .context(
126 .context("incorrect password. Key decrypted with password did not produce a valid nsec.")?; 129 "incorrect password. Key decrypted with password did not produce a valid nsec.",
130 )?;
127 131
128 inner_secret.zeroize(); 132 inner_secret.zeroize();
129 133
diff --git a/src/key_handling/users.rs b/src/key_handling/users.rs
index 2e88fba..c061cd3 100644
--- a/src/key_handling/users.rs
+++ b/src/key_handling/users.rs
@@ -1,4 +1,4 @@
1use std::time::SystemTime; 1use std::{str::FromStr, time::SystemTime};
2 2
3use anyhow::{Context, Result}; 3use anyhow::{Context, Result};
4use async_trait::async_trait; 4use async_trait::async_trait;
@@ -59,7 +59,7 @@ impl UserManagement for UserManager {
59 .input(PromptInputParms::default().with_prompt(prompt)) 59 .input(PromptInputParms::default().with_prompt(prompt))
60 .context("failed to get nsec input from interactor")?, 60 .context("failed to get nsec input from interactor")?,
61 }; 61 };
62 match Keys::from_sk_str(&pk) { 62 match Keys::from_str(&pk) {
63 Ok(key) => { 63 Ok(key) => {
64 break key; 64 break key;
65 } 65 }
@@ -471,7 +471,7 @@ mod tests {
471 .expect_encrypt_key() 471 .expect_encrypt_key()
472 .once() 472 .once()
473 .withf(|k, p| { 473 .withf(|k, p| {
474 k.eq(&Keys::from_sk_str(TEST_KEY_1_NSEC).unwrap()) && p.eq(TEST_PASSWORD) 474 k.eq(&Keys::from_str(TEST_KEY_1_NSEC).unwrap()) && p.eq(TEST_PASSWORD)
475 }) 475 })
476 .returning(|_, _| Ok(TEST_KEY_1_ENCRYPTED.into())); 476 .returning(|_, _| Ok(TEST_KEY_1_ENCRYPTED.into()));
477 477
diff --git a/src/login.rs b/src/login.rs
index b0fe230..4cdf3c1 100644
--- a/src/login.rs
+++ b/src/login.rs
@@ -1,5 +1,7 @@
1use std::str::FromStr;
2
1use anyhow::{bail, Context, Result}; 3use anyhow::{bail, Context, Result};
2use nostr::{prelude::FromSkStr, secp256k1::XOnlyPublicKey}; 4use nostr::PublicKey;
3use zeroize::Zeroize; 5use zeroize::Zeroize;
4 6
5#[cfg(not(test))] 7#[cfg(not(test))]
@@ -25,7 +27,7 @@ pub async fn launch(
25 // if nsec parameter 27 // if nsec parameter
26 let key = if let Some(nsec_unwrapped) = nsec { 28 let key = if let Some(nsec_unwrapped) = nsec {
27 // get key or fail without prompts 29 // get key or fail without prompts
28 let key = nostr::Keys::from_sk_str(nsec_unwrapped).context("invalid nsec parameter")?; 30 let key = nostr::Keys::from_str(nsec_unwrapped).context("invalid nsec parameter")?;
29 31
30 // if password, add user to enable password login in future 32 // if password, add user to enable password login in future
31 if password.is_some() { 33 if password.is_some() {
@@ -91,7 +93,7 @@ pub async fn launch(
91} 93}
92 94
93async fn get_user_details( 95async fn get_user_details(
94 public_key: &XOnlyPublicKey, 96 public_key: &PublicKey,
95 #[cfg(test)] client: &crate::client::MockConnect, 97 #[cfg(test)] client: &crate::client::MockConnect,
96 #[cfg(not(test))] client: &Client, 98 #[cfg(not(test))] client: &Client,
97) -> Result<UserRef> { 99) -> Result<UserRef> {
diff --git a/test_utils/src/lib.rs b/test_utils/src/lib.rs
index a0c67df..ad187be 100644
--- a/test_utils/src/lib.rs
+++ b/test_utils/src/lib.rs
@@ -1,9 +1,9 @@
1use std::{ffi::OsStr, path::PathBuf}; 1use std::{ffi::OsStr, path::PathBuf, str::FromStr};
2 2
3use anyhow::{bail, ensure, Context, Result}; 3use anyhow::{bail, ensure, Context, Result};
4use dialoguer::theme::{ColorfulTheme, Theme}; 4use dialoguer::theme::{ColorfulTheme, Theme};
5use directories::ProjectDirs; 5use directories::ProjectDirs;
6use nostr::{self, prelude::FromSkStr, Kind, Tag}; 6use nostr::{self, Kind, Tag};
7use once_cell::sync::Lazy; 7use once_cell::sync::Lazy;
8use rexpect::session::{Options, PtySession}; 8use rexpect::session::{Options, PtySession};
9use strip_ansi_escapes::strip_str; 9use strip_ansi_escapes::strip_str;
@@ -26,7 +26,7 @@ pub static TEST_KEY_1_DISPLAY_NAME: &str = "bob";
26pub static TEST_KEY_1_ENCRYPTED: &str = "ncryptsec1qyq607h3cykxc3f2a44u89cdk336fptccn3fm5pf3nmf93d3c86qpunc7r6klwcn6lyszjy72wxwqq9aljg4pm6atvjrds9e248yhv76xfnt464265kgnjsvg8rlg06wg4sp9uljzfpu8zuaztcvfn2j8ggdrg8mldh850cy75efsyqqansert9wqmn4e6khpgvfz7h5le9"; 26pub static TEST_KEY_1_ENCRYPTED: &str = "ncryptsec1qyq607h3cykxc3f2a44u89cdk336fptccn3fm5pf3nmf93d3c86qpunc7r6klwcn6lyszjy72wxwqq9aljg4pm6atvjrds9e248yhv76xfnt464265kgnjsvg8rlg06wg4sp9uljzfpu8zuaztcvfn2j8ggdrg8mldh850cy75efsyqqansert9wqmn4e6khpgvfz7h5le9";
27pub static TEST_KEY_1_ENCRYPTED_WEAK: &str = "ncryptsec1qy8ke0tjqnn8wt3w6lnc86c27ry3qrptxctjfcgruryxy0at238kwyjwsswd7z88thysruzw3awlrsxjvw5uptcd7vt70ft9rtkx00m8cgy3khm4hxa5d2gfnc6athnfruy2eyl6pkas8k34jg85z7xjqqadzfzh9rp0fzxqtw0tvxksac3n8yc98uksvuf93e0lcvqy8j6"; 27pub static TEST_KEY_1_ENCRYPTED_WEAK: &str = "ncryptsec1qy8ke0tjqnn8wt3w6lnc86c27ry3qrptxctjfcgruryxy0at238kwyjwsswd7z88thysruzw3awlrsxjvw5uptcd7vt70ft9rtkx00m8cgy3khm4hxa5d2gfnc6athnfruy2eyl6pkas8k34jg85z7xjqqadzfzh9rp0fzxqtw0tvxksac3n8yc98uksvuf93e0lcvqy8j6";
28pub static TEST_KEY_1_KEYS: Lazy<nostr::Keys> = 28pub static TEST_KEY_1_KEYS: Lazy<nostr::Keys> =
29 Lazy::new(|| nostr::Keys::from_sk_str(TEST_KEY_1_NSEC).unwrap()); 29 Lazy::new(|| nostr::Keys::from_str(TEST_KEY_1_NSEC).unwrap());
30 30
31pub fn generate_test_key_1_metadata_event(name: &str) -> nostr::Event { 31pub fn generate_test_key_1_metadata_event(name: &str) -> nostr::Event {
32 nostr::event::EventBuilder::metadata(&nostr::Metadata::new().name(name)) 32 nostr::event::EventBuilder::metadata(&nostr::Metadata::new().name(name))
@@ -95,7 +95,7 @@ pub static TEST_KEY_2_NPUB: &str =
95pub static TEST_KEY_2_DISPLAY_NAME: &str = "carole"; 95pub static TEST_KEY_2_DISPLAY_NAME: &str = "carole";
96pub static TEST_KEY_2_ENCRYPTED: &str = "...2"; 96pub static TEST_KEY_2_ENCRYPTED: &str = "...2";
97pub static TEST_KEY_2_KEYS: Lazy<nostr::Keys> = 97pub static TEST_KEY_2_KEYS: Lazy<nostr::Keys> =
98 Lazy::new(|| nostr::Keys::from_sk_str(TEST_KEY_2_NSEC).unwrap()); 98 Lazy::new(|| nostr::Keys::from_str(TEST_KEY_2_NSEC).unwrap());
99 99
100pub fn generate_test_key_2_metadata_event(name: &str) -> nostr::Event { 100pub fn generate_test_key_2_metadata_event(name: &str) -> nostr::Event {
101 nostr::event::EventBuilder::metadata(&nostr::Metadata::new().name(name)) 101 nostr::event::EventBuilder::metadata(&nostr::Metadata::new().name(name))