diff options
| author | DanConwayDev <DanConwayDev@protonmail.com> | 2024-03-28 12:35:37 +0000 |
|---|---|---|
| committer | DanConwayDev <DanConwayDev@protonmail.com> | 2024-03-28 12:35:37 +0000 |
| commit | 4b06b2bd70d37656d727c8fffc0656c1aa3c7b97 (patch) | |
| tree | 1d885fdef21a7530c1a91f848788a665d6fc9948 /src/key_handling | |
| parent | 91a68de459b7d22a8dfb8a324e43740fca3e0a35 (diff) | |
| parent | 11dc9fa2d8b82594d3803f12d2c5a49e57026cfb (diff) | |
Merge upgrade rust-nostr v0.29
refactor to address breaking changes in rust-nostr
fix a number of in dependancy which have been upstreamed
Diffstat (limited to 'src/key_handling')
| -rw-r--r-- | src/key_handling/encryption.rs | 12 | ||||
| -rw-r--r-- | src/key_handling/users.rs | 24 |
2 files changed, 22 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 @@ | |||
| 1 | use std::str::FromStr; | ||
| 2 | |||
| 1 | use anyhow::{anyhow, bail, ensure, Context, Result}; | 3 | use anyhow::{anyhow, bail, ensure, Context, Result}; |
| 2 | use chacha20poly1305::{ | 4 | use 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)] |
| 7 | use mockall::*; | 9 | use mockall::*; |
| 8 | use nostr::{prelude::*, Keys}; | 10 | use nostr::{prelude::*, Keys}; |
| 11 | use nostr_sdk::bech32::{self, FromBase32, ToBase32}; | ||
| 9 | use rand::{distributions::Alphanumeric, thread_rng, Rng}; | 12 | use rand::{distributions::Alphanumeric, thread_rng, Rng}; |
| 10 | use zeroize::Zeroize; | 13 | use 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..1dd75a8 100644 --- a/src/key_handling/users.rs +++ b/src/key_handling/users.rs | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | use std::time::SystemTime; | 1 | use std::{str::FromStr, time::SystemTime}; |
| 2 | 2 | ||
| 3 | use anyhow::{Context, Result}; | 3 | use anyhow::{Context, Result}; |
| 4 | use async_trait::async_trait; | 4 | use async_trait::async_trait; |
| @@ -32,13 +32,13 @@ pub trait UserManagement { | |||
| 32 | &self, | 32 | &self, |
| 33 | #[cfg(test)] client: &MockConnect, | 33 | #[cfg(test)] client: &MockConnect, |
| 34 | #[cfg(not(test))] client: &Client, | 34 | #[cfg(not(test))] client: &Client, |
| 35 | public_key: &XOnlyPublicKey, | 35 | public_key: &PublicKey, |
| 36 | after: u64, | 36 | after: u64, |
| 37 | ) -> Result<UserRef>; | 37 | ) -> Result<UserRef>; |
| 38 | fn get_user_from_cache(&self, public_key: &XOnlyPublicKey) -> Result<UserRef>; | 38 | fn get_user_from_cache(&self, public_key: &PublicKey) -> Result<UserRef>; |
| 39 | fn add_user_to_config( | 39 | fn add_user_to_config( |
| 40 | &self, | 40 | &self, |
| 41 | public_key: XOnlyPublicKey, | 41 | public_key: PublicKey, |
| 42 | encrypted_secret_key: Option<String>, | 42 | encrypted_secret_key: Option<String>, |
| 43 | overwrite: bool, | 43 | overwrite: bool, |
| 44 | ) -> Result<()>; | 44 | ) -> Result<()>; |
| @@ -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 | } |
| @@ -99,7 +99,7 @@ impl UserManagement for UserManager { | |||
| 99 | 99 | ||
| 100 | fn add_user_to_config( | 100 | fn add_user_to_config( |
| 101 | &self, | 101 | &self, |
| 102 | public_key: XOnlyPublicKey, | 102 | public_key: PublicKey, |
| 103 | encrypted_secret_key: Option<String>, | 103 | encrypted_secret_key: Option<String>, |
| 104 | overwrite: bool, | 104 | overwrite: bool, |
| 105 | ) -> Result<()> { | 105 | ) -> Result<()> { |
| @@ -129,7 +129,7 @@ impl UserManagement for UserManager { | |||
| 129 | .context("failed to save application configuration with new user details in") | 129 | .context("failed to save application configuration with new user details in") |
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | fn get_user_from_cache(&self, public_key: &XOnlyPublicKey) -> Result<UserRef> { | 132 | fn get_user_from_cache(&self, public_key: &PublicKey) -> Result<UserRef> { |
| 133 | let cfg = self | 133 | let cfg = self |
| 134 | .config_manager | 134 | .config_manager |
| 135 | .load() | 135 | .load() |
| @@ -148,7 +148,7 @@ impl UserManagement for UserManager { | |||
| 148 | &self, | 148 | &self, |
| 149 | #[cfg(test)] client: &MockConnect, | 149 | #[cfg(test)] client: &MockConnect, |
| 150 | #[cfg(not(test))] client: &Client, | 150 | #[cfg(not(test))] client: &Client, |
| 151 | public_key: &XOnlyPublicKey, | 151 | public_key: &PublicKey, |
| 152 | use_cache_unless_checked_more_than_x_secs_ago: u64, | 152 | use_cache_unless_checked_more_than_x_secs_ago: u64, |
| 153 | ) -> Result<UserRef> { | 153 | ) -> Result<UserRef> { |
| 154 | let cfg = self | 154 | let cfg = self |
| @@ -249,7 +249,11 @@ impl UserManagement for UserManager { | |||
| 249 | relays: new_relays_event | 249 | relays: new_relays_event |
| 250 | .tags | 250 | .tags |
| 251 | .iter() | 251 | .iter() |
| 252 | .filter(|t| t.kind().eq(&nostr::TagKind::R)) | 252 | .filter(|t| { |
| 253 | t.kind().eq(&nostr::TagKind::SingleLetter( | ||
| 254 | SingleLetterTag::lowercase(Alphabet::R), | ||
| 255 | )) | ||
| 256 | }) | ||
| 253 | .map(|t| UserRelayRef { | 257 | .map(|t| UserRelayRef { |
| 254 | url: t.as_vec()[1].clone(), | 258 | url: t.as_vec()[1].clone(), |
| 255 | read: t.as_vec().len() == 2 || t.as_vec()[2].eq("read"), | 259 | read: t.as_vec().len() == 2 || t.as_vec()[2].eq("read"), |
| @@ -471,7 +475,7 @@ mod tests { | |||
| 471 | .expect_encrypt_key() | 475 | .expect_encrypt_key() |
| 472 | .once() | 476 | .once() |
| 473 | .withf(|k, p| { | 477 | .withf(|k, p| { |
| 474 | k.eq(&Keys::from_sk_str(TEST_KEY_1_NSEC).unwrap()) && p.eq(TEST_PASSWORD) | 478 | k.eq(&Keys::from_str(TEST_KEY_1_NSEC).unwrap()) && p.eq(TEST_PASSWORD) |
| 475 | }) | 479 | }) |
| 476 | .returning(|_, _| Ok(TEST_KEY_1_ENCRYPTED.into())); | 480 | .returning(|_, _| Ok(TEST_KEY_1_ENCRYPTED.into())); |
| 477 | 481 | ||