Skip to content

Commit 741ca0c

Browse files
authored
feat(cli): add agents archive/unarchive/archived subcommands (#2173)
1 parent 458b034 commit 741ca0c

4 files changed

Lines changed: 1169 additions & 52 deletions

File tree

‎crates/buzz-cli/src/client.rs‎

Lines changed: 158 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,39 @@ impl BuzzClient {
499499
self.query_pages(filter, None).await
500500
}
501501

502+
/// Sign an event builder verbatim: no NIP-OA auth-tag injection, and none
503+
/// of [`sign_event`]'s "callers must not add auth tags" enforcement.
504+
///
505+
/// Used only for NIP-IA identity archive/unarchive requests (kind
506+
/// 9035/9036), whose optional `auth` tag is a *content-level*
507+
/// owner-of-agent attestation about the *target* identity — unrelated to
508+
/// this client's own NIP-OA membership delegation (`self.auth_tag`,
509+
/// which [`sign_event`] injects into every other event and which
510+
/// `submit_event` separately attaches via the `x-auth-tag` HTTP header).
511+
/// Routing an identity archive request through `sign_event` would either
512+
/// silently drop the caller's owner attestation or double up an
513+
/// unrelated tag.
514+
pub fn sign_event_unchecked(&self, builder: EventBuilder) -> Result<nostr::Event, CliError> {
515+
builder
516+
.sign_with_keys(&self.keys)
517+
.map_err(|e| CliError::Other(format!("signing failed: {e}")))
518+
}
519+
520+
/// GET a public, unauthenticated relay endpoint (e.g. the NIP-11 `/info`
521+
/// document), returning the raw JSON body. No NIP-98 Authorization and no
522+
/// `x-auth-tag` header — the endpoint is public relay metadata, not a
523+
/// membership-scoped resource.
524+
pub async fn get_public(&self, path: &str) -> Result<String, CliError> {
525+
let url = format!("{}{path}", self.relay_url);
526+
let resp = self
527+
.http
528+
.get(&url)
529+
.header("Accept", "application/nostr+json")
530+
.send()
531+
.await?;
532+
self.handle_response(resp).await
533+
}
534+
502535
/// Execute a one-shot query via the HTTP bridge.
503536
/// `filter` is a Nostr filter object (will be wrapped in an array).
504537
/// Returns the raw JSON response (array of events).
@@ -920,7 +953,10 @@ pub fn normalize_write_response(raw: &str) -> String {
920953

921954
#[cfg(test)]
922955
mod tests {
923-
use super::{advance_query_cursor, create_response_with_id, extract_relay_response_field};
956+
use super::{
957+
advance_query_cursor, create_response_with_id, extract_relay_response_field, BuzzClient,
958+
};
959+
use nostr::{EventBuilder, Keys, Kind, Tag};
924960

925961
#[test]
926962
fn query_cursor_uses_last_events_composite_sort_key() {
@@ -974,4 +1010,125 @@ mod tests {
9741010
assert_eq!(v["event_id"].as_str(), Some("abc"));
9751011
assert_eq!(v["accepted"].as_bool(), Some(true));
9761012
}
1013+
1014+
// --- (a) auth-suppression regression pair ---
1015+
1016+
fn make_auth_tag() -> (Tag, String) {
1017+
let owner_hex = "a".repeat(64);
1018+
let sig_hex = "b".repeat(128);
1019+
let tag_vec = vec![
1020+
"auth".to_string(),
1021+
owner_hex,
1022+
"conditions".to_string(),
1023+
sig_hex,
1024+
];
1025+
let json = serde_json::to_string(&tag_vec).unwrap();
1026+
let tag = Tag::parse(tag_vec).unwrap();
1027+
(tag, json)
1028+
}
1029+
1030+
#[test]
1031+
fn sign_event_unchecked_does_not_inject_ambient_auth_tag() {
1032+
let keys = Keys::generate();
1033+
let (auth_tag, auth_json) = make_auth_tag();
1034+
let client = BuzzClient::new(
1035+
"https://test.relay".into(),
1036+
keys,
1037+
Some(auth_tag),
1038+
Some(auth_json),
1039+
)
1040+
.unwrap();
1041+
1042+
let builder =
1043+
EventBuilder::new(Kind::Custom(9035), "archive").tags([Tag::parse(["-"]).unwrap()]);
1044+
let event = client.sign_event_unchecked(builder).unwrap();
1045+
1046+
let auth_tags: Vec<_> = event
1047+
.tags
1048+
.iter()
1049+
.filter(|t| t.as_slice().first().map(|s| s.as_str()) == Some("auth"))
1050+
.collect();
1051+
assert!(
1052+
auth_tags.is_empty(),
1053+
"sign_event_unchecked must not inject the ambient NIP-OA auth tag \
1054+
into identity archive events; found {auth_tags:?}"
1055+
);
1056+
}
1057+
1058+
#[test]
1059+
fn sign_event_unchecked_preserves_callers_content_auth_tag() {
1060+
let keys = Keys::generate();
1061+
let (auth_tag, auth_json) = make_auth_tag();
1062+
let client = BuzzClient::new(
1063+
"https://test.relay".into(),
1064+
keys,
1065+
Some(auth_tag),
1066+
Some(auth_json),
1067+
)
1068+
.unwrap();
1069+
1070+
let content_auth = Tag::parse([
1071+
"auth",
1072+
&"c".repeat(64),
1073+
"owner-attestation",
1074+
&"d".repeat(128),
1075+
])
1076+
.unwrap();
1077+
1078+
let builder = EventBuilder::new(Kind::Custom(9035), "archive")
1079+
.tags([Tag::parse(["-"]).unwrap(), content_auth]);
1080+
let event = client.sign_event_unchecked(builder).unwrap();
1081+
1082+
let auth_tags: Vec<_> = event
1083+
.tags
1084+
.iter()
1085+
.filter(|t| t.as_slice().first().map(|s| s.as_str()) == Some("auth"))
1086+
.collect();
1087+
assert_eq!(
1088+
auth_tags.len(),
1089+
1,
1090+
"content-level auth tag must survive sign_event_unchecked; found {auth_tags:?}"
1091+
);
1092+
assert_eq!(auth_tags[0].as_slice()[1], "c".repeat(64));
1093+
}
1094+
1095+
#[test]
1096+
fn with_auth_tag_sets_header_when_configured() {
1097+
let keys = Keys::generate();
1098+
let (auth_tag, auth_json) = make_auth_tag();
1099+
let client = BuzzClient::new(
1100+
"https://test.relay".into(),
1101+
keys,
1102+
Some(auth_tag),
1103+
Some(auth_json.clone()),
1104+
)
1105+
.unwrap();
1106+
1107+
let req = client.http.post("https://test.relay/events");
1108+
let req = client.with_auth_tag(req);
1109+
let built = req.build().unwrap();
1110+
let header = built
1111+
.headers()
1112+
.get("x-auth-tag")
1113+
.expect("x-auth-tag header must be present");
1114+
assert_eq!(
1115+
header.to_str().unwrap(),
1116+
&auth_json,
1117+
"x-auth-tag header must carry the raw auth tag JSON"
1118+
);
1119+
}
1120+
1121+
#[test]
1122+
fn with_auth_tag_omits_header_when_not_configured() {
1123+
let keys = Keys::generate();
1124+
let client = BuzzClient::new("https://test.relay".into(), keys, None, None).unwrap();
1125+
1126+
let req = client.http.post("https://test.relay/events");
1127+
let req = client.with_auth_tag(req);
1128+
let built = req.build().unwrap();
1129+
assert!(
1130+
built.headers().get("x-auth-tag").is_none(),
1131+
"x-auth-tag header must not be present when no auth tag is configured"
1132+
);
1133+
}
9771134
}

0 commit comments

Comments
 (0)