mirror of
https://github.com/block/buzz.git
synced 2026-10-27 12:40:30 +00:00
fix(cli): preserve signatures in event reads (#6884)
## Summary
- preserve string Nostr `sig` values in normal `messages get`, `messages
thread`, `messages search`, and `feed get` output
- omit absent or non-string signatures instead of fabricating empty
values
- keep compact output at exactly `{id, content, created_at}` and update
CLI/managed-agent contract docs
## Validation
- `cargo test -p buzz-cli --lib` (367 passed at final head
`a2498faa0e07f90efc0ab0165606ba3b048327c7`)
- release candidate live-relay matrix: 49/49 normal events independently
recomputed to their NIP-01 IDs and passed BIP-340 verification; all four
compact commands preserved exact three-key output and ordering
- `git diff origin/main...HEAD --check`
- independent adversarial review: clear, no findings
## Compatibility
The normal JSON change is additive for valid relay events. In-repo
consumers use tolerant field lookup. Compact output is unchanged and
locked by regressions for both message and feed formatting.
Generated by Carl, an automated contributor working with Wes.
---------
Signed-off-by: Carl <9d00794d3df50972eb8b615511783cab12a77a8fd5dd5edd58073ec73b54bd8b@buzz.block.builderlab.xyz>
Co-authored-by: Carl <9d00794d3df50972eb8b615511783cab12a77a8fd5dd5edd58073ec73b54bd8b@buzz.block.builderlab.xyz>
This commit is contained in:
@@ -428,7 +428,8 @@ buzz workflows delete --workflow "$WF_ID" | jq .
|
||||
```bash
|
||||
buzz feed get | jq .
|
||||
buzz feed get --limit 5 | jq .
|
||||
# Expected: [{id,pubkey,kind,content,created_at,tags}] — sig-stripped, sorted newest-first
|
||||
# Expected: complete signed Nostr events with
|
||||
# {id,pubkey,kind,content,created_at,sig,tags}, sorted newest-first
|
||||
```
|
||||
|
||||
### 6.11 Forum & Voting
|
||||
|
||||
@@ -1302,20 +1302,24 @@ fn to_ws_url(http_url: &str) -> String {
|
||||
.replace("http://", "ws://")
|
||||
}
|
||||
|
||||
/// Normalize raw event JSON array into consistent shape.
|
||||
/// Each event becomes: {id, pubkey, kind, content, created_at, tags}
|
||||
/// Normalize raw event JSON array into the canonical Nostr event shape.
|
||||
/// String signatures are preserved; absent or non-string signatures remain absent.
|
||||
pub fn normalize_events(events: &[serde_json::Value]) -> String {
|
||||
let normalized: Vec<serde_json::Value> = events
|
||||
.iter()
|
||||
.map(|e| {
|
||||
serde_json::json!({
|
||||
let mut event = serde_json::json!({
|
||||
"id": e.get("id").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"pubkey": e.get("pubkey").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"kind": e.get("kind").and_then(|v| v.as_u64()).unwrap_or(0),
|
||||
"content": e.get("content").and_then(|v| v.as_str()).unwrap_or(""),
|
||||
"created_at": e.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0),
|
||||
"tags": e.get("tags").cloned().unwrap_or(serde_json::json!([])),
|
||||
})
|
||||
});
|
||||
if let Some(sig) = e.get("sig").and_then(|v| v.as_str()) {
|
||||
event["sig"] = serde_json::json!(sig);
|
||||
}
|
||||
event
|
||||
})
|
||||
.collect();
|
||||
serde_json::to_string(&normalized).unwrap_or_default()
|
||||
@@ -2304,10 +2308,42 @@ mod retry_policy_tests {
|
||||
mod tests {
|
||||
use super::{
|
||||
advance_query_cursor, create_response_with_id_if_accepted, extract_relay_response_field,
|
||||
BuzzClient,
|
||||
normalize_events, BuzzClient,
|
||||
};
|
||||
use nostr::{EventBuilder, Keys, Kind, Tag};
|
||||
|
||||
#[test]
|
||||
fn normalize_events_preserves_the_complete_signed_event_shape() {
|
||||
let signed_event = EventBuilder::new(Kind::TextNote, "signed content")
|
||||
.tags([Tag::parse(["h", "channel-id"]).unwrap()])
|
||||
.sign_with_keys(&Keys::generate())
|
||||
.unwrap();
|
||||
let mut event = serde_json::to_value(&signed_event).unwrap();
|
||||
event["relay_internal"] = serde_json::json!("excluded");
|
||||
|
||||
let output: Vec<serde_json::Value> =
|
||||
serde_json::from_str(&normalize_events(&[event])).unwrap();
|
||||
let normalized = &output[0];
|
||||
let round_tripped: nostr::Event = serde_json::from_value(normalized.clone()).unwrap();
|
||||
|
||||
assert_eq!(round_tripped, signed_event);
|
||||
round_tripped.verify().unwrap();
|
||||
assert!(normalized.get("sig").is_some());
|
||||
assert!(normalized.get("relay_internal").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn normalize_events_omits_missing_or_non_string_signatures() {
|
||||
let output: Vec<serde_json::Value> = serde_json::from_str(&normalize_events(&[
|
||||
serde_json::json!({}),
|
||||
serde_json::json!({"sig": 42}),
|
||||
]))
|
||||
.unwrap();
|
||||
|
||||
assert!(output[0].get("sig").is_none());
|
||||
assert!(output[1].get("sig").is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn query_cursor_uses_last_events_composite_sort_key() {
|
||||
let mut filter = serde_json::json!({"kinds": [39000], "limit": 500});
|
||||
|
||||
@@ -5,6 +5,27 @@ use crate::error::CliError;
|
||||
|
||||
const VALID_FEED_TYPES: &[&str] = &["mentions", "needs_action", "activity", "agent_activity"];
|
||||
|
||||
fn format_events(normalized: &str, format: &crate::OutputFormat) -> String {
|
||||
match format {
|
||||
crate::OutputFormat::Compact => {
|
||||
let events: Vec<serde_json::Value> =
|
||||
serde_json::from_str(normalized).unwrap_or_default();
|
||||
let compact: Vec<serde_json::Value> = events
|
||||
.iter()
|
||||
.map(|e| {
|
||||
serde_json::json!({
|
||||
"id": e.get("id").cloned().unwrap_or_default(),
|
||||
"content": e.get("content").cloned().unwrap_or_default(),
|
||||
"created_at": e.get("created_at").cloned().unwrap_or_default(),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
serde_json::to_string(&compact).unwrap_or_default()
|
||||
}
|
||||
crate::OutputFormat::Json => normalized.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get activity feed — query events mentioning our pubkey (via p-tag).
|
||||
pub async fn cmd_get_feed(
|
||||
client: &BuzzClient,
|
||||
@@ -42,25 +63,7 @@ pub async fn cmd_get_feed(
|
||||
let mut events: Vec<serde_json::Value> = serde_json::from_str(&resp).unwrap_or_default();
|
||||
events.sort_by_key(|e| Reverse(e.get("created_at").and_then(|v| v.as_u64()).unwrap_or(0)));
|
||||
let normalized = normalize_events(&events);
|
||||
let output = match format {
|
||||
crate::OutputFormat::Compact => {
|
||||
let evts: Vec<serde_json::Value> =
|
||||
serde_json::from_str(&normalized).unwrap_or_default();
|
||||
let compact: Vec<serde_json::Value> = evts
|
||||
.iter()
|
||||
.map(|e| {
|
||||
serde_json::json!({
|
||||
"id": e.get("id").cloned().unwrap_or_default(),
|
||||
"content": e.get("content").cloned().unwrap_or_default(),
|
||||
"created_at": e.get("created_at").cloned().unwrap_or_default(),
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
serde_json::to_string(&compact).unwrap_or_default()
|
||||
}
|
||||
crate::OutputFormat::Json => normalized,
|
||||
};
|
||||
println!("{output}");
|
||||
println!("{}", format_events(&normalized, format));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -78,3 +81,35 @@ pub async fn dispatch(
|
||||
} => cmd_get_feed(client, since, limit, types.as_deref(), format).await,
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::format_events;
|
||||
|
||||
#[test]
|
||||
fn compact_event_format_remains_the_three_key_contract() {
|
||||
let normalized = serde_json::json!([{
|
||||
"id": "a".repeat(64),
|
||||
"pubkey": "b".repeat(64),
|
||||
"kind": 9,
|
||||
"content": "compact content",
|
||||
"created_at": 1_787_754_972_u64,
|
||||
"tags": [["p", "c".repeat(64)]],
|
||||
"sig": "d".repeat(128),
|
||||
}])
|
||||
.to_string();
|
||||
|
||||
let output: Vec<serde_json::Value> =
|
||||
serde_json::from_str(&format_events(&normalized, &crate::OutputFormat::Compact))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
output[0],
|
||||
serde_json::json!({
|
||||
"id": "a".repeat(64),
|
||||
"content": "compact content",
|
||||
"created_at": 1_787_754_972_u64,
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1057,7 +1057,7 @@ pub async fn dispatch(
|
||||
mod tests {
|
||||
use super::{
|
||||
channel_id_from_event, cmd_get_thread, event_mention_pubkeys, find_root_from_tags,
|
||||
match_profiles_by_name, merge_message_mentions, missing_members,
|
||||
format_events, match_profiles_by_name, merge_message_mentions, missing_members,
|
||||
normalize_explicit_mentions, parse_member_pubkeys, resolve_names_to_pubkeys,
|
||||
resolve_thread_target, thread_ref_from_event, thread_ref_from_parent_tags, BuzzClient,
|
||||
CliError, Uuid,
|
||||
@@ -1078,6 +1078,33 @@ mod tests {
|
||||
const PK_VALID_B: &str = "c6237ef84fa537c78dcee78efd2d4e59f728859c7f194da42ac51ededfa0be05";
|
||||
const PK_VALID_C: &str = "f4a42a97e594b77bdbd8ee35191c8b28a94a4cb871d96f32921558275421fb68";
|
||||
|
||||
#[test]
|
||||
fn compact_event_format_remains_the_three_key_contract() {
|
||||
let normalized = serde_json::json!([{
|
||||
"id": ID_A,
|
||||
"pubkey": PUBKEY,
|
||||
"kind": 9,
|
||||
"content": "compact content",
|
||||
"created_at": 1_787_754_972_u64,
|
||||
"tags": [["h", "channel-id"]],
|
||||
"sig": "d".repeat(128),
|
||||
}])
|
||||
.to_string();
|
||||
|
||||
let output: Vec<serde_json::Value> =
|
||||
serde_json::from_str(&format_events(&normalized, &crate::OutputFormat::Compact))
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
output[0],
|
||||
serde_json::json!({
|
||||
"id": ID_A,
|
||||
"content": "compact content",
|
||||
"created_at": 1_787_754_972_u64,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn malformed_channel_is_rejected_before_thread_fetch() {
|
||||
let client =
|
||||
|
||||
Reference in New Issue
Block a user