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/git/nostr_url.rs | 81 ++++++++++++++++++++++++++++++++++++++++++------ src/lib/repo_ref.rs | 16 +++++++--- 2 files changed, 84 insertions(+), 13 deletions(-) (limited to 'src/lib') 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