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/bin/ngit/sub_commands/init.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src/bin/ngit/sub_commands/init.rs') diff --git a/src/bin/ngit/sub_commands/init.rs b/src/bin/ngit/sub_commands/init.rs index 6fc1ec4..c9c8873 100644 --- a/src/bin/ngit/sub_commands/init.rs +++ b/src/bin/ngit/sub_commands/init.rs @@ -1,4 +1,4 @@ -use std::{collections::HashMap, str::FromStr}; +use std::collections::HashMap; use anyhow::{Context, Result}; use console::Style; @@ -442,7 +442,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { .map(std::string::ToString::to_string) .collect::>(); - prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo)?; + prompt_to_set_nostr_url_as_origin(&repo_ref, &git_repo).await?; // TODO: if no state event exists and there is currently a remote called // "origin", automtically push rather than waiting for the next commit @@ -483,7 +483,7 @@ pub async fn launch(cli_args: &Cli, args: &SubCommandArgs) -> Result<()> { Ok(()) } -fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> { +async fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Result<()> { println!( "starting from your next commit, when you `git push` to a remote that uses your nostr url, it will store your repository state on nostr and update the state of the git server(s) you just listed." ); @@ -493,7 +493,9 @@ fn prompt_to_set_nostr_url_as_origin(repo_ref: &RepoRef, git_repo: &Repo) -> Res if let Ok(origin_remote) = git_repo.git_repo.find_remote("origin") { if let Some(origin_url) = origin_remote.url() { - if let Ok(nostr_url) = NostrUrlDecoded::from_str(origin_url) { + if let Ok(nostr_url) = + NostrUrlDecoded::parse_and_resolve(origin_url, &Some(git_repo)).await + { if nostr_url.coordinate.identifier == repo_ref.identifier { if nostr_url.coordinate.public_key == repo_ref.trusted_maintainer { return Ok(()); -- 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/bin/ngit/sub_commands/init.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