upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/lib/git/nostr_url.rs
diff options
context:
space:
mode:
authorDanConwayDev <DanConwayDev@protonmail.com>2024-12-10 11:45:24 +0000
committerDanConwayDev <DanConwayDev@protonmail.com>2024-12-10 11:55:46 +0000
commit8d8c21d05e4d8d7af30a96704a5919efc07010dc (patch)
tree34d80870dc29b56b54959c4a0f96f54f9085f194 /src/lib/git/nostr_url.rs
parent8bd0f64bfc1d8348aaac11c9882e6211599df320 (diff)
feat(nostr_url): improve nip05 git cache format
store all nip05s in a single git config item called "nostr.nip05" using the `<nip05>:<hex-public-key>` 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.
Diffstat (limited to 'src/lib/git/nostr_url.rs')
-rw-r--r--src/lib/git/nostr_url.rs53
1 files 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 @@
1use core::fmt; 1use core::fmt;
2use std::str::FromStr; 2use std::{collections::HashMap, str::FromStr};
3 3
4use anyhow::{anyhow, bail, Context, Error, Result}; 4use anyhow::{anyhow, bail, Context, Error, Result};
5use nostr::nips::{nip01::Coordinate, nip05}; 5use nostr::nips::{nip01::Coordinate, nip05};
@@ -230,13 +230,11 @@ impl NostrUrlDecoded {
230} 230}
231 231
232fn resolve_nip05_from_git_config_cache(nip05: &str, git_repo: &Option<&Repo>) -> Result<PublicKey> { 232fn resolve_nip05_from_git_config_cache(nip05: &str, git_repo: &Option<&Repo>) -> Result<PublicKey> {
233 let stored_value = get_git_config_item( 233 if let Some(public_key) = load_nip_cache(git_repo)?.get(nip05) {
234 git_repo, 234 Ok(*public_key)
235 &format!("nostr.nip05.{}", urlencoding::encode(nip05)), 235 } else {
236 )? 236 bail!("nip05 not stored in local git config cache")
237 .context("not in cache")?; 237 }
238 PublicKey::parse(stored_value)
239 .context("stored nip05 resolution value did not parse as public key")
240} 238}
241 239
242fn save_nip05_to_git_config_cache( 240fn save_nip05_to_git_config_cache(
@@ -244,21 +242,32 @@ fn save_nip05_to_git_config_cache(
244 public_key: &PublicKey, 242 public_key: &PublicKey,
245 git_repo: &Option<&Repo>, 243 git_repo: &Option<&Repo>,
246) -> Result<()> { 244) -> Result<()> {
247 if save_git_config_item( 245 let mut h = load_nip_cache(git_repo)?;
248 git_repo, 246 h.insert(nip05.to_string(), *public_key);
249 &format!("nostr.nip05.{}", urlencoding::encode(nip05)), 247
250 &public_key.to_bech32()?, 248 let s = h
251 ) 249 .into_iter()
252 .is_err() 250 .map(|(nip05, public_key)| format!("{nip05}:{}", public_key.to_hex()))
253 { 251 .collect::<Vec<String>>()
254 save_git_config_item( 252 .join(",");
255 &None, 253
256 &format!("nostr.nip05.{}", urlencoding::encode(nip05)), 254 save_git_config_item(git_repo, "nostr.nip05", s.as_str())
257 &public_key.to_bech32()?, 255 .context("could not save nip05 cache in git config")
258 ) 256}
259 } else { 257
260 Ok(()) 258fn load_nip_cache(git_repo: &Option<&Repo>) -> Result<HashMap<String, PublicKey>> {
259 let mut h = HashMap::new();
260 let stored_value = get_git_config_item(git_repo, "nostr.nip05")?
261 .context("no nip05s in local git config cache so retun empty cache")
262 .unwrap_or_default();
263 for pair in stored_value.split(',') {
264 if let Some((cached_nip05, pubkey)) = pair.split_once(':') {
265 if let Ok(public_key) = PublicKey::parse(pubkey) {
266 h.insert(cached_nip05.to_string(), public_key);
267 }
268 }
261 } 269 }
270 Ok(h)
262} 271}
263 272
264#[derive(Debug, PartialEq, Default)] 273#[derive(Debug, PartialEq, Default)]