From 6d3a4eb870cd344b11ccda13e1339584ed4e4d17 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Mon, 9 Dec 2024 09:34:08 +0000 Subject: feat(NostrUrlDecoded) add nip05 support replace `NostrUrlDecoded::from_str` with `NostrUrlDecoded::parse_and_resolve` store nip05 pubkey mapping in git cache --- src/lib/repo_ref.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'src/lib/repo_ref.rs') diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 1b25ccf..089befc 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -41,6 +41,7 @@ impl TryFrom<(nostr::Event, Option)> for RepoRef { type Error = anyhow::Error; fn try_from((event, trusted_maintainer): (nostr::Event, Option)) -> Result { + // TODO: turn trusted maintainer into NostrUrlDecoded if !event.kind.eq(&Kind::GitRepoAnnouncement) { bail!("incorrect kind"); } @@ -239,6 +240,7 @@ impl RepoRef { coordinate: self.coordinate_with_hint(), protocol: None, user: None, + nip05: None, // TODO: if nip05 for pubkey saved in local git config use it. } ) } @@ -259,7 +261,7 @@ pub async fn get_repo_coordinates_when_remote_unknown( pub async fn try_and_get_repo_coordinates_when_remote_unknown( git_repo: &Repo, ) -> Result { - let remote_coordinates = get_repo_coordinates_from_nostr_remotes(git_repo)?; + let remote_coordinates = get_repo_coordinates_from_nostr_remotes(git_repo).await?; if remote_coordinates.is_empty() { if let Ok(c) = get_repo_coordinates_from_git_config(git_repo) { Ok(c) @@ -327,11 +329,15 @@ fn get_repo_coordinates_from_git_config(git_repo: &Repo) -> Result { .context("git config item \"nostr.repo\" is not an naddr") } -fn get_repo_coordinates_from_nostr_remotes(git_repo: &Repo) -> Result> { +async fn get_repo_coordinates_from_nostr_remotes( + git_repo: &Repo, +) -> Result> { let mut repo_coordinates = HashMap::new(); for remote_name in git_repo.git_repo.remotes()?.iter().flatten() { if let Some(remote_url) = git_repo.git_repo.find_remote(remote_name)?.url() { - if let Ok(nostr_url_decoded) = NostrUrlDecoded::from_str(remote_url) { + if let Ok(nostr_url_decoded) = + NostrUrlDecoded::parse_and_resolve(remote_url, &Some(git_repo)).await + { repo_coordinates.insert(remote_name.to_string(), nostr_url_decoded.coordinate); } } @@ -383,7 +389,9 @@ async fn get_repo_coordinate_from_user_prompt( .input(PromptInputParms::default().with_prompt("nostr repository"))?; let coordinate = if let Ok(c) = Coordinate::parse(&input) { c - } else if let Ok(nostr_url) = NostrUrlDecoded::from_str(&input) { + } else if let Ok(nostr_url) = + NostrUrlDecoded::parse_and_resolve(&input, &Some(git_repo)).await + { nostr_url.coordinate } else { eprintln!("not a valid naddr or git nostr remote URL starting nostr://"); -- cgit v1.2.3 From 36eb9395567dc0022b1700ec9cda1389baf22476 Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 10 Dec 2024 12:12:14 +0000 Subject: fix(nostr_url): use nip05 in nostr url if cached prioritise using nip05 nostr url format when the nip05, public key mapping is stored in the (usually local) git config. --- src/bin/ngit/sub_commands/init.rs | 6 +++--- src/lib/git/nostr_url.rs | 10 ++++++++++ src/lib/repo_ref.rs | 20 ++++++++++++++------ 3 files changed, 27 insertions(+), 9 deletions(-) (limited to 'src/lib/repo_ref.rs') diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index c9c8873..86b681c 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -522,7 +522,7 @@ async fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) } } println!("contributors can clone your repository by installing ngit and using this clone url:"); - println!("{}", repo_ref.to_nostr_git_url()); + println!("{}", repo_ref.to_nostr_git_url(&Some(git_repo))); Ok(()) } @@ -535,7 +535,7 @@ fn ask_to_set_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> { )? { git_repo .git_repo - .remote_set_url("origin", &repo_ref.to_nostr_git_url())?; + .remote_set_url("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?; } Ok(()) } @@ -548,7 +548,7 @@ fn ask_to_create_new_origin_remote(repo_ref: &RepoRef, git_repo: &Repo) -> Resul )? { git_repo .git_repo - .remote("origin", &repo_ref.to_nostr_git_url())?; + .remote("origin", &repo_ref.to_nostr_git_url(&Some(git_repo)))?; } Ok(()) } diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index 8bf458b..bc56e1a 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs @@ -237,6 +237,16 @@ fn resolve_nip05_from_git_config_cache(nip05: &str, git_repo: &Option<&Repo>) -> } } +pub fn use_nip05_git_config_cache_to_find_nip05_from_public_key( + public_key: &PublicKey, + git_repo: &Option<&Repo>, +) -> Result> { + let h = load_nip_cache(git_repo)?; + Ok(h.iter() + .find_map(|(k, v)| if *v == *public_key { Some(k) } else { None }) + .cloned()) +} + fn save_nip05_to_git_config_cache( nip05: &str, public_key: &PublicKey, diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs index 089befc..9a3c8f1 100644 --- a/src/lib/repo_ref.rs +++ b/src/lib/repo_ref.rs @@ -19,7 +19,10 @@ use crate::{ Interactor, InteractorPrompt, PromptChoiceParms, PromptConfirmParms, PromptInputParms, }, client::{consolidate_fetch_reports, get_repo_ref_from_cache, sign_event, Connect}, - git::{nostr_url::NostrUrlDecoded, Repo, RepoActions}, + git::{ + nostr_url::{use_nip05_git_config_cache_to_find_nip05_from_public_key, NostrUrlDecoded}, + Repo, RepoActions, + }, login::user::get_user_details, }; @@ -232,15 +235,20 @@ impl RepoRef { .collect::)>>() } - pub fn to_nostr_git_url(&self) -> String { + pub fn to_nostr_git_url(&self, git_repo: &Option<&Repo>) -> String { + let c = self.coordinate_with_hint(); format!( "{}", NostrUrlDecoded { original_string: String::new(), - coordinate: self.coordinate_with_hint(), + nip05: use_nip05_git_config_cache_to_find_nip05_from_public_key( + &c.public_key, + git_repo, + ) + .unwrap_or_default(), + coordinate: c, protocol: None, user: None, - nip05: None, // TODO: if nip05 for pubkey saved in local git config use it. } ) } @@ -446,10 +454,10 @@ fn set_or_create_git_remote_with_nostr_url( repo_ref: &RepoRef, git_repo: &Repo, ) -> Result<()> { - let url = repo_ref.to_nostr_git_url(); + let url = repo_ref.to_nostr_git_url(&Some(git_repo)); if git_repo .git_repo - .remote_set_url(name, &repo_ref.to_nostr_git_url()) + .remote_set_url(name, &repo_ref.to_nostr_git_url(&Some(git_repo))) .is_err() { git_repo.git_repo.remote(name, &url)?; -- cgit v1.2.3