Replying to Avatar Lez

From 3fc1a99b85fb20f78b1ea28e5daa75eef4688da5 Mon Sep 17 00:00:00 2001

From: Laszlo Megyer

Date: Mon, 2 Dec 2024 13:20:13 +0100

Subject: [PATCH 1/3] Parse NIP-05 address from a git remote URL

The nip05 address is stored as it is, because we can't run async code in

from_str().

We need to add an async resolve_nip05() to NostrUrlDecoded.

---

src/lib/git/nostr_url.rs | 27 +++++++++++++++++++--------

src/lib/repo_ref.rs | 1 +

2 files changed, 20 insertions(+), 8 deletions(-)

diff --git a/src/lib/git/nostr_url.rs b/src/lib/git/nostr_url.rs

index a501765..be4a825 100644

--- a/src/lib/git/nostr_url.rs

+++ b/src/lib/git/nostr_url.rs

@@ -59,6 +59,7 @@ pub struct NostrUrlDecoded {

pub coordinates: HashSet,

pub protocol: Option,

pub user: Option,

+ pub nip05: Option,

}

impl fmt::Display for NostrUrlDecoded {

@@ -98,6 +99,7 @@ impl std::str::FromStr for NostrUrlDecoded {

let mut protocol = None;

let mut user = None;

let mut relays = vec![];

+ let mut nip05 = None;

if !url.starts_with("nostr://") {

bail!("nostr git url must start with nostr://");

@@ -134,16 +136,24 @@ impl std::str::FromStr for NostrUrlDecoded {

.split('/')

.collect();

+ // split first part into user if present and hostname

+ let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?;

+ let hostname = if let Some(at_index) = part.find('@') {

+ // TODO throw error if user already set

+ user = Some(part[..at_index].to_string());

+ &part[at_index + 1..]

+ } else {

+ part

+ };

+

+ // check if it uses a NIP-05 address

+ if hostname.contains('.') {

+ nip05 = Some(part.to_string())

+ }

+

// extract optional protocol

if protocol.is_none() {

- let part = parts.first().context(INCORRECT_NOSTR_URL_FORMAT_ERROR)?;

- let protocol_str = if let Some(at_index) = part.find('@') {

- user = Some(part[..at_index].to_string());

- &part[at_index + 1..]

- } else {

- part

- };

- protocol = match protocol_str {

+ protocol = match hostname {

"ssh" => Some(ServerProtocol::Ssh),

"https" => Some(ServerProtocol::Https),

"http" => Some(ServerProtocol::Http),

@@ -196,6 +206,7 @@ impl std::str::FromStr for NostrUrlDecoded {

coordinates,

protocol,

user,

+ nip05,

})

}

}

diff --git a/src/lib/repo_ref.rs b/src/lib/repo_ref.rs

index 69fbe64..877d1ee 100644

--- a/src/lib/repo_ref.rs

+++ b/src/lib/repo_ref.rs

@@ -229,6 +229,7 @@ impl RepoRef {

coordinates: HashSet::from_iter(vec![self.coordinate_with_hint()]),

protocol: None,

user: None,

+ nip05: None,

}

)

}

--

libgit2 1.8.1

A couple of ideas:

1) we could potentially store mapping of nip05 address to PublicKey in `git_repo.get_git_config_item("nostr.", Some(false))` so we only need to fetch it via the API the first time rather than every interaction. We wouldn't need to retain the nip05 relay hint because the repository announcement event is already in cache.

2) we could store NostrUrlDecoded in RepoRef and so that `to_nostr_git_url` could return the url with the nip05 address if it was originally used.

Reply to this note

Please Login to reply.

Discussion

No replies yet.