From 8d8c21d05e4d8d7af30a96704a5919efc07010dc Mon Sep 17 00:00:00 2001 From: DanConwayDev Date: Tue, 10 Dec 2024 11:45:24 +0000 Subject: feat(nostr_url): improve nip05 git cache format store all nip05s in a single git config item called "nostr.nip05" using the `:` format with multiple values comma seperated. git config is suposed to be human readable so it consideration was given to storing the npub instead of hex but nip05 generally uses hex and its rarely, if at all, going to be read directly by humans. in the future it might be better to use git syntax for storing multiple items but library git2 doesn't support this. it would be trivial to do though. multiple nip05 items is an edge case so this format is ok for now. --- src/lib/git/nostr_url.rs | 53 ++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-) diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs index 6f418d5..8bf458b 100644 --- a/src/lib/git/nostr_url.rs +++ b/src/lib/git/nostr_url.rs @@ -1,5 +1,5 @@ use core::fmt; -use std::str::FromStr; +use std::{collections::HashMap, str::FromStr}; use anyhow::{anyhow, bail, Context, Error, Result}; use nostr::nips::{nip01::Coordinate, nip05}; @@ -230,13 +230,11 @@ impl NostrUrlDecoded { } 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") + if let Some(public_key) = load_nip_cache(git_repo)?.get(nip05) { + Ok(*public_key) + } else { + bail!("nip05 not stored in local git config cache") + } } fn save_nip05_to_git_config_cache( @@ -244,21 +242,32 @@ fn save_nip05_to_git_config_cache( 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(()) + let mut h = load_nip_cache(git_repo)?; + h.insert(nip05.to_string(), *public_key); + + let s = h + .into_iter() + .map(|(nip05, public_key)| format!("{nip05}:{}", public_key.to_hex())) + .collect::>() + .join(","); + + save_git_config_item(git_repo, "nostr.nip05", s.as_str()) + .context("could not save nip05 cache in git config") +} + +fn load_nip_cache(git_repo: &Option<&Repo>) -> Result> { + let mut h = HashMap::new(); + let stored_value = get_git_config_item(git_repo, "nostr.nip05")? + .context("no nip05s in local git config cache so retun empty cache") + .unwrap_or_default(); + for pair in stored_value.split(',') { + if let Some((cached_nip05, pubkey)) = pair.split_once(':') { + if let Ok(public_key) = PublicKey::parse(pubkey) { + h.insert(cached_nip05.to_string(), public_key); + } + } } + Ok(h) } #[derive(Debug, PartialEq, Default)] -- cgit v1.2.3