From 0d7a8383d7d54e64bb0e1d5f4e06110c1e6a818b Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 22 Mar 2024 15:51:34 +0000 Subject: rename from_sk_str -> from_str to reflect new name in rust-nostr --- src/key_handling/encryption.rs | 12 ++++++++---- src/key_handling/users.rs | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) (limited to 'src/key_handling') 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 @@ +use std::str::FromStr; + use anyhow::{anyhow, bail, ensure, Context, Result}; use chacha20poly1305::{ aead::{rand_core::RngCore, Aead, AeadCore, KeyInit, OsRng, Payload}, @@ -6,6 +8,7 @@ use chacha20poly1305::{ #[cfg(test)] use mockall::*; use nostr::{prelude::*, Keys}; +use nostr_sdk::bech32::{self, FromBase32, ToBase32}; use rand::{distributions::Alphanumeric, thread_rng, Rng}; use zeroize::Zeroize; @@ -120,10 +123,11 @@ impl EncryptDecrypt for Encryptor { bail!("invalid encrypted key"); } - let key = Keys::from_sk_str( - std::str::from_utf8(&inner_secret).context("inner secret is not [u8]")?, - ) - .context("incorrect password. Key decrypted with password did not produce a valid nsec.")?; + let key = + Keys::from_str(std::str::from_utf8(&inner_secret).context("inner secret is not [u8]")?) + .context( + "incorrect password. Key decrypted with password did not produce a valid nsec.", + )?; inner_secret.zeroize(); 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 @@ -use std::time::SystemTime; +use std::{str::FromStr, time::SystemTime}; use anyhow::{Context, Result}; use async_trait::async_trait; @@ -59,7 +59,7 @@ impl UserManagement for UserManager { .input(PromptInputParms::default().with_prompt(prompt)) .context("failed to get nsec input from interactor")?, }; - match Keys::from_sk_str(&pk) { + match Keys::from_str(&pk) { Ok(key) => { break key; } @@ -471,7 +471,7 @@ mod tests { .expect_encrypt_key() .once() .withf(|k, p| { - k.eq(&Keys::from_sk_str(TEST_KEY_1_NSEC).unwrap()) && p.eq(TEST_PASSWORD) + k.eq(&Keys::from_str(TEST_KEY_1_NSEC).unwrap()) && p.eq(TEST_PASSWORD) }) .returning(|_, _| Ok(TEST_KEY_1_ENCRYPTED.into())); -- cgit v1.2.3 From 9a450f2ce740da08843a541d230c43194b934f5f Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 22 Mar 2024 15:51:36 +0000 Subject: replace XOnlyPublicKey with wrapper PublicKey to reflect new name in rust-nostr --- src/config.rs | 6 +++--- src/key_handling/users.rs | 12 ++++++------ src/repo_ref.rs | 14 +++++++------- src/sub_commands/init.rs | 10 +++++----- 4 files changed, 21 insertions(+), 21 deletions(-) (limited to 'src/key_handling') diff --git a/src/config.rs b/src/config.rs index 2370e34..7fca446 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,7 +4,7 @@ use anyhow::{anyhow, Context, Result}; use directories::ProjectDirs; #[cfg(test)] use mockall::*; -use nostr::{secp256k1::XOnlyPublicKey, ToBech32}; +use nostr::{PublicKey, ToBech32}; use serde::{self, Deserialize, Serialize}; #[derive(Default)] @@ -69,7 +69,7 @@ pub struct MyConfig { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] pub struct UserRef { - pub public_key: XOnlyPublicKey, + pub public_key: PublicKey, pub encrypted_key: String, pub metadata: UserMetadata, pub relays: UserRelays, @@ -77,7 +77,7 @@ pub struct UserRef { } impl UserRef { - pub fn new(public_key: XOnlyPublicKey, encrypted_key: String) -> Self { + pub fn new(public_key: PublicKey, encrypted_key: String) -> Self { Self { public_key, encrypted_key, diff --git a/src/key_handling/users.rs b/src/key_handling/users.rs index c061cd3..751c577 100644 --- a/src/key_handling/users.rs +++ b/src/key_handling/users.rs @@ -32,13 +32,13 @@ pub trait UserManagement { &self, #[cfg(test)] client: &MockConnect, #[cfg(not(test))] client: &Client, - public_key: &XOnlyPublicKey, + public_key: &PublicKey, after: u64, ) -> Result; - fn get_user_from_cache(&self, public_key: &XOnlyPublicKey) -> Result; + fn get_user_from_cache(&self, public_key: &PublicKey) -> Result; fn add_user_to_config( &self, - public_key: XOnlyPublicKey, + public_key: PublicKey, encrypted_secret_key: Option, overwrite: bool, ) -> Result<()>; @@ -99,7 +99,7 @@ impl UserManagement for UserManager { fn add_user_to_config( &self, - public_key: XOnlyPublicKey, + public_key: PublicKey, encrypted_secret_key: Option, overwrite: bool, ) -> Result<()> { @@ -129,7 +129,7 @@ impl UserManagement for UserManager { .context("failed to save application configuration with new user details in") } - fn get_user_from_cache(&self, public_key: &XOnlyPublicKey) -> Result { + fn get_user_from_cache(&self, public_key: &PublicKey) -> Result { let cfg = self .config_manager .load() @@ -148,7 +148,7 @@ impl UserManagement for UserManager { &self, #[cfg(test)] client: &MockConnect, #[cfg(not(test))] client: &Client, - public_key: &XOnlyPublicKey, + public_key: &PublicKey, use_cache_unless_checked_more_than_x_secs_ago: u64, ) -> Result { let cfg = self diff --git a/src/repo_ref.rs b/src/repo_ref.rs index c7b42fa..0a14005 100644 --- a/src/repo_ref.rs +++ b/src/repo_ref.rs @@ -1,7 +1,7 @@ use std::{fs::File, io::BufReader, str::FromStr}; use anyhow::{bail, Context, Result}; -use nostr::{nips::nip19::Nip19, secp256k1::XOnlyPublicKey, FromBech32, Tag, ToBech32}; +use nostr::{nips::nip19::Nip19, FromBech32, PublicKey, Tag, ToBech32}; use serde::{Deserialize, Serialize}; #[cfg(not(test))] @@ -23,7 +23,7 @@ pub struct RepoRef { pub git_server: String, pub web: Vec, pub relays: Vec, - pub maintainers: Vec, + pub maintainers: Vec, // code languages and hashtags } @@ -78,7 +78,7 @@ impl TryFrom for RepoRef { } for pk in maintainers { r.maintainers.push( - nostr_sdk::prelude::XOnlyPublicKey::from_str(&pk) + nostr_sdk::prelude::PublicKey::from_str(&pk) .context(format!("cannot convert entry from maintainers tag {pk} into a valid nostr public key. it should be in hex format")) .context("invalid repository event")?, ); @@ -246,11 +246,11 @@ pub fn get_repo_config_from_yaml(git_repo: &Repo) -> Result { Ok(repo_config_yaml) } -pub fn extract_pks(pk_strings: Vec) -> Result> { - let mut pks: Vec = vec![]; +pub fn extract_pks(pk_strings: Vec) -> Result> { + let mut pks: Vec = vec![]; for s in pk_strings { pks.push( - nostr_sdk::prelude::XOnlyPublicKey::from_bech32(s.clone()) + nostr_sdk::prelude::PublicKey::from_bech32(s.clone()) .context(format!("cannot convert {s} into a valid nostr public key"))?, ); } @@ -259,7 +259,7 @@ pub fn extract_pks(pk_strings: Vec) -> Result> { pub fn save_repo_config_to_yaml( git_repo: &Repo, - maintainers: Vec, + maintainers: Vec, relays: Vec, ) -> Result<()> { let path = git_repo.get_path()?.join("maintainers.yaml"); diff --git a/src/sub_commands/init.rs b/src/sub_commands/init.rs index 4f098c0..56129a6 100644 --- a/src/sub_commands/init.rs +++ b/src/sub_commands/init.rs @@ -1,5 +1,5 @@ use anyhow::{Context, Result}; -use nostr::{secp256k1::XOnlyPublicKey, FromBech32, ToBech32}; +use nostr::{FromBech32, PublicKey, ToBech32}; use super::send::send_events; #[cfg(not(test))] @@ -163,7 +163,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { args.web.clone() }; - let maintainers: Vec = { + let maintainers: Vec = { let mut dont_ask = !args.other_maintainers.is_empty(); let mut maintainers_string = if !args.other_maintainers.is_empty() { [args.other_maintainers.clone()].concat().join(" ") @@ -185,7 +185,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { }; // add current user if not present if maintainers.iter().any(|m| { - if let Ok(m_pubkey) = XOnlyPublicKey::from_bech32(m) { + if let Ok(m_pubkey) = PublicKey::from_bech32(m) { user_ref.public_key.eq(&m_pubkey) } else { false @@ -210,9 +210,9 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { .map(std::string::ToString::to_string) .collect(); } - let mut maintainers: Vec = vec![]; + let mut maintainers: Vec = vec![]; for m in maintainers_string.split(' ') { - if let Ok(m_pubkey) = XOnlyPublicKey::from_bech32(m) { + if let Ok(m_pubkey) = PublicKey::from_bech32(m) { maintainers.push(m_pubkey); } else { println!("not a valid set of npubs seperated by a space"); -- cgit v1.2.3 From 49bc478fffc63a3cad0be194c6c45a868e432691 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Fri, 22 Mar 2024 15:51:40 +0000 Subject: update tag generation to reflect changes in rust-nostr --- src/key_handling/users.rs | 6 +++++- src/repo_ref.rs | 5 +++-- src/sub_commands/list.rs | 12 +++++++++--- src/sub_commands/send.rs | 31 ++++++++++++++++++++----------- tests/list.rs | 12 +++++++++--- tests/pull.rs | 10 ++++++++-- 6 files changed, 54 insertions(+), 22 deletions(-) (limited to 'src/key_handling') diff --git a/src/key_handling/users.rs b/src/key_handling/users.rs index 751c577..1dd75a8 100644 --- a/src/key_handling/users.rs +++ b/src/key_handling/users.rs @@ -249,7 +249,11 @@ impl UserManagement for UserManager { relays: new_relays_event .tags .iter() - .filter(|t| t.kind().eq(&nostr::TagKind::R)) + .filter(|t| { + t.kind().eq(&nostr::TagKind::SingleLetter( + SingleLetterTag::lowercase(Alphabet::R), + )) + }) .map(|t| UserRelayRef { url: t.as_vec()[1].clone(), read: t.as_vec().len() == 2 || t.as_vec()[2].eq("read"), diff --git a/src/repo_ref.rs b/src/repo_ref.rs index 0a14005..2dab79c 100644 --- a/src/repo_ref.rs +++ b/src/repo_ref.rs @@ -200,8 +200,9 @@ pub async fn fetch( nostr::Filter::default().kind(nostr::Kind::Custom(REPO_REF_KIND)); match nip19 { Nip19::Coordinate(c) => { - repo_event_filter = - repo_event_filter.identifier(c.identifier).author(c.pubkey); + repo_event_filter = repo_event_filter + .identifier(c.identifier) + .author(c.public_key); for r in c.relays { relays.push(r); } diff --git a/src/sub_commands/list.rs b/src/sub_commands/list.rs index 2d81164..1a30b9b 100644 --- a/src/sub_commands/list.rs +++ b/src/sub_commands/list.rs @@ -730,9 +730,12 @@ pub async fn find_proposal_events( vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]) .custom_tag( - nostr::Alphabet::A, + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ) + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::A), repo_ref .maintainers .iter() @@ -742,7 +745,10 @@ pub async fn find_proposal_events( // events nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]) + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ) .reference(root_commit), ], ) diff --git a/src/sub_commands/send.rs b/src/sub_commands/send.rs index cc182f1..91654d2 100644 --- a/src/sub_commands/send.rs +++ b/src/sub_commands/send.rs @@ -4,7 +4,10 @@ use anyhow::{bail, Context, Result}; use console::Style; use futures::future::join_all; use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; -use nostr::{nips::nip19::Nip19, EventBuilder, FromBech32, Marker, Tag, TagKind, UncheckedUrl}; +use nostr::{ + nips::{nip01::Coordinate, nip19::Nip19}, + EventBuilder, FromBech32, Marker, Tag, TagKind, UncheckedUrl, +}; use nostr_sdk::hashes::sha1::Hash as Sha1Hash; use super::list::tag_value; @@ -539,11 +542,14 @@ pub fn generate_cover_letter_and_patch_events( vec![ // TODO: why not tag all maintainer identifiers? Tag::A { - kind: nostr::Kind::Custom(REPO_REF_KIND), - public_key: *repo_ref.maintainers.first() - .context("repo reference should always have at least one maintainer - the issuer of the repo event") - ?, - identifier: repo_ref.identifier.to_string(), + coordinate: Coordinate { + kind: nostr::Kind::Custom(REPO_REF_KIND), + public_key: *repo_ref.maintainers.first() + .context("repo reference should always have at least one maintainer - the issuer of the repo event") + ?, + identifier: repo_ref.identifier.to_string(), + relays: repo_ref.relays.clone(), + }, relay_url: repo_ref.relays.first().map(nostr::UncheckedUrl::from).clone(), }, Tag::Reference(format!("{root_commit}")), @@ -795,11 +801,14 @@ pub fn generate_patch_event( [ vec![ Tag::A { - kind: nostr::Kind::Custom(REPO_REF_KIND), - public_key: *repo_ref.maintainers.first() - .context("repo reference should always have at least one maintainer - the issuer of the repo event") - ?, - identifier: repo_ref.identifier.to_string(), + coordinate: Coordinate { + kind: nostr::Kind::Custom(REPO_REF_KIND), + public_key: *repo_ref.maintainers.first() + .context("repo reference should always have at least one maintainer - the issuer of the repo event") + ?, + identifier: repo_ref.identifier.to_string(), + relays: repo_ref.relays.clone(), + }, relay_url: relay_hint.clone(), }, Tag::Reference(format!("{root_commit}")), diff --git a/tests/list.rs b/tests/list.rs index 4f55645..ee5e1dc 100644 --- a/tests/list.rs +++ b/tests/list.rs @@ -190,7 +190,7 @@ mod cannot_find_repo_event { input.succeeds_with( &Coordinate { kind: nostr::Kind::Custom(REPOSITORY_KIND), - pubkey: TEST_KEY_1_KEYS.public_key(), + public_key: TEST_KEY_1_KEYS.public_key(), identifier: repo_event.identifier().unwrap().to_string(), relays: vec!["ws://localhost:8056".to_string()], } @@ -1621,7 +1621,10 @@ mod when_main_branch_is_uptodate { vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]), + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), ], Some(Duration::from_millis(500)), )?; @@ -1748,7 +1751,10 @@ mod when_main_branch_is_uptodate { vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]), + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), ], Some(Duration::from_millis(500)), )?; diff --git a/tests/pull.rs b/tests/pull.rs index 50dddbf..853f518 100644 --- a/tests/pull.rs +++ b/tests/pull.rs @@ -746,7 +746,10 @@ mod when_branch_is_checked_out { vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]), + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), ], Some(Duration::from_millis(500)), )?; @@ -843,7 +846,10 @@ mod when_branch_is_checked_out { vec![ nostr::Filter::default() .kind(nostr::Kind::Custom(PATCH_KIND)) - .custom_tag(nostr::Alphabet::T, vec!["root"]), + .custom_tag( + nostr::SingleLetterTag::lowercase(nostr::Alphabet::T), + vec!["root"], + ), ], Some(Duration::from_millis(500)), )?; -- cgit v1.2.3