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/git_remote_nostr/main.rs | 12 +++--- src/bin/ngit/sub_commands/init.rs | 10 +++-- src/lib/git/nostr_url.rs | 81 ++++++++++++++++++++++++++++++++++----- src/lib/repo_ref.rs | 16 ++++++-- 4 files changed, 96 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/bin/git_remote_nostr/main.rs b/src/bin/git_remote_nostr/main.rs index 8e12d68..84327d7 100644 --- a/src/bin/git_remote_nostr/main.rs +++ b/src/bin/git_remote_nostr/main.rs @@ -9,7 +9,6 @@ use std::{ collections::HashSet, env, io, path::{Path, PathBuf}, - str::FromStr, }; use anyhow::{bail, Context, Result}; @@ -28,7 +27,7 @@ mod utils; #[tokio::main] async fn main() -> Result<()> { - let Some((decoded_nostr_url, git_repo)) = process_args()? else { + let Some((decoded_nostr_url, git_repo)) = process_args().await? else { return Ok(()); }; @@ -109,7 +108,7 @@ async fn main() -> Result<()> { } } -fn process_args() -> Result> { +async fn process_args() -> Result> { let args = env::args(); let args = args.skip(1).take(2).collect::>(); @@ -135,13 +134,14 @@ fn process_args() -> Result> { return Ok(None); }; - let decoded_nostr_url = - NostrUrlDecoded::from_str(nostr_remote_url).context("invalid nostr url")?; - let git_repo = Repo::from_path(&PathBuf::from( std::env::var("GIT_DIR").context("git should set GIT_DIR when remote helper is called")?, ))?; + let decoded_nostr_url = NostrUrlDecoded::parse_and_resolve(nostr_remote_url, &Some(&git_repo)) + .await + .context("invalid nostr url")?; + Ok(Some((decoded_nostr_url, git_repo))) } 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(()); diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index c26bb2e..ac57538 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs @@ -2,9 +2,11 @@ use core::fmt; use std::str::FromStr; use anyhow::{anyhow, bail, Context, Error, Result}; -use nostr::nips::nip01::Coordinate; +use nostr::nips::{nip01::Coordinate, nip05}; use nostr_sdk::{PublicKey, RelayUrl, ToBech32, Url}; +use super::{get_git_config_item, save_git_config_item, Repo}; + #[derive(Debug, PartialEq, Default, Clone)] pub enum ServerProtocol { Ssh, @@ -59,6 +61,7 @@ pub struct NostrUrlDecoded { pub coordinate: Coordinate, pub protocol: Option, pub user: Option, + pub nip05: Option, } impl fmt::Display for NostrUrlDecoded { @@ -89,10 +92,8 @@ impl fmt::Display for NostrUrlDecoded { static INCORRECT_NOSTR_URL_FORMAT_ERROR: &str = "incorrect nostr git url format. try nostr://naddr123 or nostr://npub123/my-repo or nostr://ssh/npub123/relay.damus.io/my-repo"; -impl std::str::FromStr for NostrUrlDecoded { - type Err = anyhow::Error; - - fn from_str(url: &str) -> Result { +impl NostrUrlDecoded { + pub async fn parse_and_resolve(url: &str, git_repo: &Option<&Repo>) -> Result { let mut protocol = None; let mut user = None; let mut relays = vec![]; @@ -154,6 +155,7 @@ impl std::str::FromStr for NostrUrlDecoded { } // extract naddr npub//identifer let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?; + let mut nip05 = None; // naddr used let coordinate = if let Ok(coordinate) = Coordinate::parse(part) { if coordinate.kind.eq(&nostr_sdk::Kind::GitRepoAnnouncement) { @@ -161,8 +163,9 @@ impl std::str::FromStr for NostrUrlDecoded { } else { bail!("naddr doesnt point to a git repository announcement"); } - // npub//identifer used - } else if let Ok(public_key) = PublicKey::parse(part) { + // //identifer used + } else { + let npub_or_nip05 = part.to_owned(); parts.remove(0); let identifier = parts .pop() @@ -179,14 +182,41 @@ impl std::str::FromStr for NostrUrlDecoded { RelayUrl::parse(&decoded).context("could not parse relays in nostr git url")?; relays.push(url); } + let public_key = match PublicKey::parse(npub_or_nip05) { + Ok(public_key) => public_key, + Err(_) => { + nip05 = Some(npub_or_nip05.to_string()); + if let Ok(public_key) = + resolve_nip05_from_git_config_cache(npub_or_nip05, git_repo) + { + public_key + } else { + // TODO eprint loading message + let res = nip05::profile(npub_or_nip05, None) + .await + .context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?; + // TODO clear loading message + nip05 = Some(npub_or_nip05.to_string()); + let _ = save_nip05_to_git_config_cache( + npub_or_nip05, + &res.public_key, + git_repo, + ); + if relays.is_empty() { + for r in res.relays { + relays.push(r); + } + } + res.public_key + } + } + }; Coordinate { identifier, public_key, kind: nostr_sdk::Kind::GitRepoAnnouncement, relays, } - } else { - bail!(INCORRECT_NOSTR_URL_FORMAT_ERROR); }; Ok(Self { @@ -194,10 +224,43 @@ impl std::str::FromStr for NostrUrlDecoded { coordinate, protocol, user, + nip05, }) } } +fn resolve_nip05_from_git_config_cache(nip05: &str, git_repo: &Option<&Repo>) -> Result { + let stored_value = get_git_config_item( + git_repo, + &format!("nostr.nip05.{}", urlencoding::encode(nip05)), + )? + .context("not in cache")?; + PublicKey::parse(stored_value) + .context("stored nip05 resolution value did not parse as public key") +} + +fn save_nip05_to_git_config_cache( + nip05: &str, + public_key: &PublicKey, + git_repo: &Option<&Repo>, +) -> Result<()> { + if save_git_config_item( + git_repo, + &format!("nostr.nip05.{}", urlencoding::encode(nip05)), + &public_key.to_bech32()?, + ) + .is_err() + { + save_git_config_item( + &None, + &format!("nostr.nip05.{}", urlencoding::encode(nip05)), + &public_key.to_bech32()?, + ) + } else { + Ok(()) + } +} + #[derive(Debug, PartialEq, Default)] pub struct CloneUrl { original_string: String, 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