buzzkit 0.2.1__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- buzzkit-0.3.1/CHANGELOG.md +195 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/Cargo.lock +8 -8
- {buzzkit-0.2.1 → buzzkit-0.3.1}/Cargo.toml +3 -3
- {buzzkit-0.2.1 → buzzkit-0.3.1}/PKG-INFO +27 -4
- {buzzkit-0.2.1 → buzzkit-0.3.1}/README.md +26 -3
- {buzzkit-0.2.1 → buzzkit-0.3.1}/pyproject.toml +1 -1
- {buzzkit-0.2.1 → buzzkit-0.3.1}/python/buzzkit/__init__.py +3 -1
- {buzzkit-0.2.1 → buzzkit-0.3.1}/python/buzzkit/agents.py +37 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/python/buzzkit/client.py +81 -12
- {buzzkit-0.2.1 → buzzkit-0.3.1}/src/huddle.rs +4 -2
- {buzzkit-0.2.1 → buzzkit-0.3.1}/src/lib.rs +10 -3
- {buzzkit-0.2.1 → buzzkit-0.3.1}/tests/test_agents.py +84 -0
- buzzkit-0.3.1/tests/test_client.py +119 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/tests/test_signing.py +19 -0
- buzzkit-0.2.1/tests/test_client.py +0 -27
- {buzzkit-0.2.1 → buzzkit-0.3.1}/.cargo/config.toml +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/.github/workflows/CI.yml +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/.github/workflows/release.yml +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/.gitignore +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/LICENSE +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/LICENSE-APACHE +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/NOTICE +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/build-support/vendored-libopus.cmake +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/_shared.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/analyze_audio_tap.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/claim_invite.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/huddle_echo.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/huddle_wire_recorder.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/list_channels.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/offline_sign.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/send_message.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/set_profile.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/examples/subscribe.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/python/buzzkit/_native.pyi +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/python/buzzkit/huddle.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/python/buzzkit/py.typed +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/tests/test_huddle.py +0 -0
- {buzzkit-0.2.1 → buzzkit-0.3.1}/tests/test_live_relay.py +0 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to buzzkit are documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.3.1] — 2026-10-01
|
|
11
|
+
|
|
12
|
+
### Security
|
|
13
|
+
|
|
14
|
+
- `nostr` 0.44.6 → 0.44.7, fixing RUSTSEC-2026-0225 through -0230 — among
|
|
15
|
+
them resource exhaustion in NIP-98 authorization parsing and in NIP-44 v2
|
|
16
|
+
decryption, both features buzzkit enables. `chacha20` 0.10.1 (yanked) →
|
|
17
|
+
0.10.2.
|
|
18
|
+
|
|
19
|
+
### Changed
|
|
20
|
+
|
|
21
|
+
- Upstream `buzz-core`/`buzz-sdk` pin moves from `209536ade` (post-v0.5.3) to
|
|
22
|
+
`5fdb2e536` (post-v0.5.26). Two visible effects:
|
|
23
|
+
- `build_message_event` keeps a `p` tag mentioning the signer itself
|
|
24
|
+
(block/buzz#4975); it was silently dropped before.
|
|
25
|
+
- `verify_auth_tag` rejects uppercase-hex owner keys and signatures, as
|
|
26
|
+
relays do.
|
|
27
|
+
- `start_huddle(ttl=...)` is deprecated and ignored (`DeprecationWarning` for
|
|
28
|
+
any value but 3600). Relays now reject a huddle announcement unless its
|
|
29
|
+
backing channel carries the relay's huddle TTL (block/buzz#6056) — 3600 s,
|
|
30
|
+
or an operator override that replaces the client's value — so any other
|
|
31
|
+
`ttl` failed after creating the channel.
|
|
32
|
+
|
|
33
|
+
### Fixed
|
|
34
|
+
|
|
35
|
+
- `connect()` raises `RuntimeError` as soon as the relay rejects the NIP-42
|
|
36
|
+
auth (e.g. `restricted: not a relay member`) or closes the socket before
|
|
37
|
+
accepting it, and closes the connection. It used to log the rejection and
|
|
38
|
+
wait out the 20 s timeout. A relay that never answers still raises
|
|
39
|
+
`TimeoutError`.
|
|
40
|
+
|
|
41
|
+
### Documented
|
|
42
|
+
|
|
43
|
+
- Owner commands: the README's check now verifies the event itself
|
|
44
|
+
(`verify_event`) before trusting its content and author, as upstream
|
|
45
|
+
`buzz-acp` does for every inbound event (block/buzz#7010).
|
|
46
|
+
- Close code **1008** means the community was deleted: do not reconnect
|
|
47
|
+
(block/buzz#4425). 1012 still means a graceful restart.
|
|
48
|
+
- The 1 MiB WebSocket frame cap is buzzkit's receive limit; the relay's own
|
|
49
|
+
inbound limit is 512 KiB by default.
|
|
50
|
+
|
|
51
|
+
## [0.3.0] — 2026-08-02
|
|
52
|
+
|
|
53
|
+
### Added
|
|
54
|
+
|
|
55
|
+
- **Owner control commands**: `parse_owner_command(event, agent_pubkey_hex)`
|
|
56
|
+
matches the platform's owner commands (`!shutdown` / `!cancel` / `!rotate`)
|
|
57
|
+
on the wire — kind 9, exact trimmed content, `p`-tag mention of the agent —
|
|
58
|
+
mirroring upstream `buzz-acp`'s `is_owner_control_command` (`!shutdown
|
|
59
|
+
please` is a regular message, ` !rotate ` is a command). Pure function, no
|
|
60
|
+
I/O. `OWNER_COMMANDS` exported.
|
|
61
|
+
- **`BuzzClient.verified_owner_hex`**: the NIP-OA auth tag's attester,
|
|
62
|
+
Schnorr-verified against the client's own pubkey at construction; `None`
|
|
63
|
+
when the tag is absent or does not verify. This is the value that must gate
|
|
64
|
+
privileged actions (e.g. honoring `!shutdown`).
|
|
65
|
+
|
|
66
|
+
### Documented
|
|
67
|
+
|
|
68
|
+
- `owner_pubkey_hex` is **declared, not proven** — it reads the tag's owner
|
|
69
|
+
field without verifying the signature and must never gate privileged
|
|
70
|
+
actions. The README's NIP-OA section shows both halves of the
|
|
71
|
+
owner-command check.
|
|
72
|
+
|
|
73
|
+
## [0.2.1] — 2026-07-31
|
|
74
|
+
|
|
75
|
+
### Fixed
|
|
76
|
+
|
|
77
|
+
- **HTTP bridge errors are now readable**: a rejected bridge call previously
|
|
78
|
+
raised a bare `400 Bad Request`; the relay's actual reason (e.g.
|
|
79
|
+
`invalid: edit target event not found`) sat unread in the response body.
|
|
80
|
+
`post_event`, `query`, and `claim_invite` now raise a `RuntimeError`
|
|
81
|
+
carrying the status and body.
|
|
82
|
+
|
|
83
|
+
### Documented (relay contracts, verified live)
|
|
84
|
+
|
|
85
|
+
- `delete_message(reason=...)` is **moderation-only**: a kind-9005 tombstone
|
|
86
|
+
carrying `public_reason` requires channel owner/admin rights and is
|
|
87
|
+
rejected even for the message's own author. Omit `reason` to delete your
|
|
88
|
+
own message.
|
|
89
|
+
- **The bridge acks before indexing**: an edit/delete sent immediately after
|
|
90
|
+
its target can transiently fail with `target event not found`; retrying is
|
|
91
|
+
the caller's call.
|
|
92
|
+
|
|
93
|
+
## [0.2.0] — 2026-07-31
|
|
94
|
+
|
|
95
|
+
### Added
|
|
96
|
+
|
|
97
|
+
- **Message lifecycle**: threaded replies
|
|
98
|
+
(`send_message(..., reply_to=, reply_root=)`), `react` / `remove_reaction`
|
|
99
|
+
(kinds 7/5), `edit_message` (kind 40003), `delete_message` (kind 9005
|
|
100
|
+
tombstone, optional room-facing reason), `set_topic` (kind 9002),
|
|
101
|
+
`leave_channel` (kind 9022).
|
|
102
|
+
- **Agent ownership (NIP-OA)**: `verify_auth_tag`, `verify_agent_profile`,
|
|
103
|
+
and `BuzzClient.resolve_agent(name, owner)` — agent-by-name lookup scoped
|
|
104
|
+
to the owner's kind-30177 managed-agent records, verified
|
|
105
|
+
cryptographically (mirrors block/buzz#3178).
|
|
106
|
+
- **NIP-38 user status**: `build_user_status_event` / `BuzzClient.set_status`
|
|
107
|
+
(kind 30315; blank text clears it).
|
|
108
|
+
- **Reconnect signals**: `BuzzClient.close_code` exposes the WebSocket close
|
|
109
|
+
code; 1012 means the relay is restarting gracefully — back off gently and
|
|
110
|
+
dedupe replayed events by id.
|
|
111
|
+
|
|
112
|
+
### Changed
|
|
113
|
+
|
|
114
|
+
- Upstream `buzz-core`/`buzz-sdk` pin moves from v0.4.23 to post-v0.5.3
|
|
115
|
+
(`209536ade`); crate API delta is purely additive and picks up the IPv6
|
|
116
|
+
SSRF hardening (block/buzz#2801) and kinds 30177/30178.
|
|
117
|
+
- `__version__` derives from package metadata instead of a hardcoded string.
|
|
118
|
+
- Behavior notes when upgrading: channel names are canonicalized upstream
|
|
119
|
+
(leading `#` stripped; a name canonicalizing to empty raises `ValueError`);
|
|
120
|
+
presence contract on buzz v0.5.x relays is a 60 s heartbeat against a
|
|
121
|
+
180 s TTL (30 s stays safe on both relay generations); relay REQ pages cap
|
|
122
|
+
at 1000 events and the advertised limit is now honest.
|
|
123
|
+
|
|
124
|
+
### Security
|
|
125
|
+
|
|
126
|
+
- `nostr` 0.44.4 → 0.44.6, fixing RUSTSEC-2026-0216 (remote DoS via a
|
|
127
|
+
malformed NIP-44 payload; buzzkit enables `nip44`).
|
|
128
|
+
|
|
129
|
+
## [0.1.4] — 2026-07-23
|
|
130
|
+
|
|
131
|
+
### Changed
|
|
132
|
+
|
|
133
|
+
- **Huddle audio robustness**: all WebSocket I/O runs on a dedicated thread
|
|
134
|
+
(caller event-loop stalls can no longer distort pacing); the paced sender
|
|
135
|
+
never bursts (late frames are dropped and the clock realigned); silence is
|
|
136
|
+
streamed between utterances so receiver jitter buffers see a continuous
|
|
137
|
+
real-time timeline (Opus DTX keeps those packets tiny); Opus encode/decode
|
|
138
|
+
release the GIL.
|
|
139
|
+
|
|
140
|
+
### Added
|
|
141
|
+
|
|
142
|
+
- `paced=False` mode: an external pacer (e.g. RoomKit's
|
|
143
|
+
`OutboundAudioPacer`) owns the clock and the client just relays frames.
|
|
144
|
+
- Diagnostics examples: `huddle_wire_recorder.py` (join as a silent peer and
|
|
145
|
+
record exactly what receivers get) and `analyze_audio_tap.py` (stage-by-
|
|
146
|
+
stage timing analysis). These traced choppy huddle audio to a Buzz desktop
|
|
147
|
+
playout bug (block/buzz#2652).
|
|
148
|
+
- CI enforces `ruff format` and `ty` type checking.
|
|
149
|
+
|
|
150
|
+
## [0.1.3] — 2026-07-23
|
|
151
|
+
|
|
152
|
+
### Added
|
|
153
|
+
|
|
154
|
+
- **Huddle voice**: `HuddleClient` (challenge → NIP-42 auth → joined
|
|
155
|
+
handshake, real-time paced Opus send, roster tracking, barge-in), Rust
|
|
156
|
+
`HuddleEncoder`/`HuddleDecoder` for the huddle v2 wire protocol (Opus
|
|
157
|
+
48 kHz mono + 8-byte header), `BuzzClient.start_huddle()`, kind 9007/48100
|
|
158
|
+
builders, and `set_profile(auth_tag=)` defaulting to the client's NIP-OA
|
|
159
|
+
attestation.
|
|
160
|
+
- Self-contained wheels (static libopus) for Linux x86_64/aarch64
|
|
161
|
+
(manylinux_2_28), macOS x86_64/arm64, and Windows x64.
|
|
162
|
+
|
|
163
|
+
## [0.1.2] — 2026-07-22
|
|
164
|
+
|
|
165
|
+
### Added
|
|
166
|
+
|
|
167
|
+
- **NIP-OA owner attestation**: `compute_auth_tag(owner_secret,
|
|
168
|
+
agent_pubkey_hex, conditions)` (the owner attests the agent;
|
|
169
|
+
self-attestation rejected), `build_auth_event(..., auth_tag=)`, and
|
|
170
|
+
`BuzzClient(auth_tag=)` injecting the tag into AUTH and the profile.
|
|
171
|
+
- **Presence**: `build_presence_event` / `BuzzClient.publish_presence`
|
|
172
|
+
(kind 20001, ephemeral, WebSocket-published).
|
|
173
|
+
|
|
174
|
+
## [0.1.1] — 2026-07-22
|
|
175
|
+
|
|
176
|
+
### Added
|
|
177
|
+
|
|
178
|
+
- **Channel self-join**: `join_channel` / `build_join_channel_event`
|
|
179
|
+
(NIP-29 kind 9000, role `bot`). Builders allow self-tagging — `nostr`'s
|
|
180
|
+
`EventBuilder` otherwise strips a `p` tag naming the event author, which
|
|
181
|
+
made relays reject the self-join for a missing `p` tag.
|
|
182
|
+
|
|
183
|
+
## [0.1.0] — 2026-07-22
|
|
184
|
+
|
|
185
|
+
### Added
|
|
186
|
+
|
|
187
|
+
- Initial release. PyO3 bindings over Block's zero-I/O `buzz-core` /
|
|
188
|
+
`buzz-sdk` crates: keypair generation, event building/signing/verification,
|
|
189
|
+
NIP-42 auth events, NIP-98 HTTP auth — all synchronous, no tokio.
|
|
190
|
+
- Async `BuzzClient` (all I/O in Python): HTTP bridge (`query`,
|
|
191
|
+
`list_channels`, `send_message`, `set_profile`, `claim_invite`,
|
|
192
|
+
`post_event`) and authenticated WebSocket (`connect`, `subscribe`,
|
|
193
|
+
`subscribe_channel`, `publish`, `close`).
|
|
194
|
+
- One abi3 (CPython ≥ 3.12) mixed wheel; MIT-licensed with Apache-2.0
|
|
195
|
+
attribution (NOTICE) for the statically linked buzz crates.
|
|
@@ -146,7 +146,7 @@ checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649"
|
|
|
146
146
|
[[package]]
|
|
147
147
|
name = "buzz-core"
|
|
148
148
|
version = "0.1.0"
|
|
149
|
-
source = "git+https://github.com/block/buzz?rev=
|
|
149
|
+
source = "git+https://github.com/block/buzz?rev=5fdb2e53659ee29002545f1022c138fe0b8282f9#5fdb2e53659ee29002545f1022c138fe0b8282f9"
|
|
150
150
|
dependencies = [
|
|
151
151
|
"base64",
|
|
152
152
|
"chrono",
|
|
@@ -168,7 +168,7 @@ dependencies = [
|
|
|
168
168
|
[[package]]
|
|
169
169
|
name = "buzz-sdk"
|
|
170
170
|
version = "0.1.0"
|
|
171
|
-
source = "git+https://github.com/block/buzz?rev=
|
|
171
|
+
source = "git+https://github.com/block/buzz?rev=5fdb2e53659ee29002545f1022c138fe0b8282f9#5fdb2e53659ee29002545f1022c138fe0b8282f9"
|
|
172
172
|
dependencies = [
|
|
173
173
|
"buzz-core",
|
|
174
174
|
"nostr",
|
|
@@ -180,7 +180,7 @@ dependencies = [
|
|
|
180
180
|
|
|
181
181
|
[[package]]
|
|
182
182
|
name = "buzzkit"
|
|
183
|
-
version = "0.
|
|
183
|
+
version = "0.3.1"
|
|
184
184
|
dependencies = [
|
|
185
185
|
"base64",
|
|
186
186
|
"buzz-core",
|
|
@@ -231,9 +231,9 @@ dependencies = [
|
|
|
231
231
|
|
|
232
232
|
[[package]]
|
|
233
233
|
name = "chacha20"
|
|
234
|
-
version = "0.10.
|
|
234
|
+
version = "0.10.2"
|
|
235
235
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
236
|
-
checksum = "
|
|
236
|
+
checksum = "65c35e4b699c7e15ccbe7ee35c005e4fc0a278d22238a2857e6ce2dadeda1b06"
|
|
237
237
|
dependencies = [
|
|
238
238
|
"cfg-if",
|
|
239
239
|
"cpufeatures 0.3.0",
|
|
@@ -727,9 +727,9 @@ dependencies = [
|
|
|
727
727
|
|
|
728
728
|
[[package]]
|
|
729
729
|
name = "nostr"
|
|
730
|
-
version = "0.44.
|
|
730
|
+
version = "0.44.7"
|
|
731
731
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
732
|
-
checksum = "
|
|
732
|
+
checksum = "c7d3d987ea7078dc36947cde532637c472a229426702e4331dd7667325378bd9"
|
|
733
733
|
dependencies = [
|
|
734
734
|
"base64",
|
|
735
735
|
"bech32",
|
|
@@ -957,7 +957,7 @@ version = "0.10.2"
|
|
|
957
957
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
958
958
|
checksum = "c7f5fa3a058cd35567ef9bfa5e75732bee0f9e4c55fa90477bef2dfcdbc4be80"
|
|
959
959
|
dependencies = [
|
|
960
|
-
"chacha20 0.10.
|
|
960
|
+
"chacha20 0.10.2",
|
|
961
961
|
"getrandom 0.4.3",
|
|
962
962
|
"rand_core 0.10.1",
|
|
963
963
|
]
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "buzzkit"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.1"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
publish = false
|
|
6
6
|
description = "PyO3 bindings over Block's Buzz zero-I/O crates (buzz-core / buzz-sdk)"
|
|
@@ -20,8 +20,8 @@ pyo3 = { version = "0.24", features = ["abi3-py312"] }
|
|
|
20
20
|
# Event/Keys/EventBuilder types unify across crate boundaries.
|
|
21
21
|
nostr = { version = "0.44", features = ["nip44", "nip98"] }
|
|
22
22
|
# Pinned to a specific Buzz commit; bump deliberately to track their kind churn.
|
|
23
|
-
buzz-core = { git = "https://github.com/block/buzz", rev = "
|
|
24
|
-
buzz-sdk = { git = "https://github.com/block/buzz", rev = "
|
|
23
|
+
buzz-core = { git = "https://github.com/block/buzz", rev = "5fdb2e53659ee29002545f1022c138fe0b8282f9", package = "buzz-core" }
|
|
24
|
+
buzz-sdk = { git = "https://github.com/block/buzz", rev = "5fdb2e53659ee29002545f1022c138fe0b8282f9", package = "buzz-sdk" }
|
|
25
25
|
uuid = { version = "1", features = ["v4"] }
|
|
26
26
|
sha2 = "0.10"
|
|
27
27
|
hex = "0.4"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: buzzkit
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Classifier: Development Status :: 3 - Alpha
|
|
5
5
|
Classifier: Intended Audience :: Developers
|
|
6
6
|
Classifier: License :: OSI Approved :: MIT License
|
|
@@ -140,6 +140,25 @@ agents = await bz.resolve_agent("Honey", owner_pubkey_hex)
|
|
|
140
140
|
verified = [a for a in agents if a["verification"] == "verified"]
|
|
141
141
|
```
|
|
142
142
|
|
|
143
|
+
The owner controls a running agent over the relay with `!shutdown` /
|
|
144
|
+
`!cancel` / `!rotate` (a kind-9 message mentioning the agent — the same wire
|
|
145
|
+
shape Buzz's own agent harness obeys). buzzkit gives you every part of the
|
|
146
|
+
check — the command, the event's signature, and its author:
|
|
147
|
+
|
|
148
|
+
```python
|
|
149
|
+
cmd = buzzkit.parse_owner_command(event, bz.pubkey_hex) # "shutdown" | "cancel" | "rotate" | None
|
|
150
|
+
if (
|
|
151
|
+
cmd == "shutdown"
|
|
152
|
+
and buzzkit.verify_event(json.dumps(event)) # authentic: id + signature
|
|
153
|
+
and event["pubkey"] == bz.verified_owner_hex # sent by the proven owner
|
|
154
|
+
):
|
|
155
|
+
... # proven owner intent — exit gracefully (publish_presence("offline"), close())
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
`verified_owner_hex` is the auth tag's attester, Schnorr-verified against the
|
|
159
|
+
client's own pubkey at construction (`None` when absent or invalid); the
|
|
160
|
+
unverified `owner_pubkey_hex` must never gate privileged actions.
|
|
161
|
+
|
|
143
162
|
## Joining a community (relay onboarding)
|
|
144
163
|
|
|
145
164
|
Hosted Buzz communities are **closed relays**: an identity must be a relay member
|
|
@@ -165,6 +184,7 @@ claiming. After joining, `set_profile(...)` gives the agent a display name.
|
|
|
165
184
|
| `pubkey_from_secret(secret)` | derive `(npub, hex)` |
|
|
166
185
|
| `build_*_event` (message/reply, reaction, edit, delete, profile, user status, channel, presence…) | build + sign events |
|
|
167
186
|
| `compute_auth_tag` / `verify_auth_tag` / `verify_agent_profile` | NIP-OA owner attestation |
|
|
187
|
+
| `parse_owner_command` / `BuzzClient.verified_owner_hex` / `OWNER_COMMANDS` | owner control commands (`!shutdown`…) |
|
|
168
188
|
| `sign_nip98(secret, method, url, body)` | HTTP bridge auth header |
|
|
169
189
|
| `verify_event(json)` | check id + Schnorr signature |
|
|
170
190
|
| `BuzzClient.send_message / react / remove_reaction / edit_message / set_profile / set_status / resolve_agent / query / list_channels / claim_invite` | HTTP bridge |
|
|
@@ -173,9 +193,12 @@ claiming. After joining, `set_profile(...)` gives the agent a display name.
|
|
|
173
193
|
| `HuddleEncoder` / `HuddleDecoder` | raw huddle wire frames ↔ PCM |
|
|
174
194
|
|
|
175
195
|
Threaded replies: `send_message(..., reply_to=<event-id>)` (add
|
|
176
|
-
`reply_root=` for nested replies). Reconnect note:
|
|
177
|
-
|
|
178
|
-
|
|
196
|
+
`reply_root=` for nested replies). Reconnect note: check
|
|
197
|
+
`BuzzClient.close_code` in your reconnect loop. The relay closes with code
|
|
198
|
+
**1012** on graceful restart (back off, then dedupe replayed events by id) and
|
|
199
|
+
**1008** when the community was deleted (stop: there is nothing to come back
|
|
200
|
+
to). `connect()` raises `RuntimeError` at once when the relay rejects the
|
|
201
|
+
NIP-42 auth (e.g. `restricted: not a relay member`).
|
|
179
202
|
|
|
180
203
|
## Build from source
|
|
181
204
|
|
|
@@ -106,6 +106,25 @@ agents = await bz.resolve_agent("Honey", owner_pubkey_hex)
|
|
|
106
106
|
verified = [a for a in agents if a["verification"] == "verified"]
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
The owner controls a running agent over the relay with `!shutdown` /
|
|
110
|
+
`!cancel` / `!rotate` (a kind-9 message mentioning the agent — the same wire
|
|
111
|
+
shape Buzz's own agent harness obeys). buzzkit gives you every part of the
|
|
112
|
+
check — the command, the event's signature, and its author:
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
cmd = buzzkit.parse_owner_command(event, bz.pubkey_hex) # "shutdown" | "cancel" | "rotate" | None
|
|
116
|
+
if (
|
|
117
|
+
cmd == "shutdown"
|
|
118
|
+
and buzzkit.verify_event(json.dumps(event)) # authentic: id + signature
|
|
119
|
+
and event["pubkey"] == bz.verified_owner_hex # sent by the proven owner
|
|
120
|
+
):
|
|
121
|
+
... # proven owner intent — exit gracefully (publish_presence("offline"), close())
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
`verified_owner_hex` is the auth tag's attester, Schnorr-verified against the
|
|
125
|
+
client's own pubkey at construction (`None` when absent or invalid); the
|
|
126
|
+
unverified `owner_pubkey_hex` must never gate privileged actions.
|
|
127
|
+
|
|
109
128
|
## Joining a community (relay onboarding)
|
|
110
129
|
|
|
111
130
|
Hosted Buzz communities are **closed relays**: an identity must be a relay member
|
|
@@ -131,6 +150,7 @@ claiming. After joining, `set_profile(...)` gives the agent a display name.
|
|
|
131
150
|
| `pubkey_from_secret(secret)` | derive `(npub, hex)` |
|
|
132
151
|
| `build_*_event` (message/reply, reaction, edit, delete, profile, user status, channel, presence…) | build + sign events |
|
|
133
152
|
| `compute_auth_tag` / `verify_auth_tag` / `verify_agent_profile` | NIP-OA owner attestation |
|
|
153
|
+
| `parse_owner_command` / `BuzzClient.verified_owner_hex` / `OWNER_COMMANDS` | owner control commands (`!shutdown`…) |
|
|
134
154
|
| `sign_nip98(secret, method, url, body)` | HTTP bridge auth header |
|
|
135
155
|
| `verify_event(json)` | check id + Schnorr signature |
|
|
136
156
|
| `BuzzClient.send_message / react / remove_reaction / edit_message / set_profile / set_status / resolve_agent / query / list_channels / claim_invite` | HTTP bridge |
|
|
@@ -139,9 +159,12 @@ claiming. After joining, `set_profile(...)` gives the agent a display name.
|
|
|
139
159
|
| `HuddleEncoder` / `HuddleDecoder` | raw huddle wire frames ↔ PCM |
|
|
140
160
|
|
|
141
161
|
Threaded replies: `send_message(..., reply_to=<event-id>)` (add
|
|
142
|
-
`reply_root=` for nested replies). Reconnect note:
|
|
143
|
-
|
|
144
|
-
|
|
162
|
+
`reply_root=` for nested replies). Reconnect note: check
|
|
163
|
+
`BuzzClient.close_code` in your reconnect loop. The relay closes with code
|
|
164
|
+
**1012** on graceful restart (back off, then dedupe replayed events by id) and
|
|
165
|
+
**1008** when the community was deleted (stop: there is nothing to come back
|
|
166
|
+
to). `connect()` raises `RuntimeError` at once when the relay rejects the
|
|
167
|
+
NIP-42 auth (e.g. `restricted: not a relay member`).
|
|
145
168
|
|
|
146
169
|
## Build from source
|
|
147
170
|
|
|
@@ -4,7 +4,7 @@ build-backend = "maturin"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "buzzkit"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.3.1"
|
|
8
8
|
description = "Python bindings + async client for Block's Buzz (Nostr) protocol, backed by Rust buzz-core/buzz-sdk"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.12"
|
|
@@ -57,7 +57,7 @@ from ._native import (
|
|
|
57
57
|
verify_auth_tag,
|
|
58
58
|
verify_event,
|
|
59
59
|
)
|
|
60
|
-
from .agents import verify_agent_profile
|
|
60
|
+
from .agents import OWNER_COMMANDS, parse_owner_command, verify_agent_profile
|
|
61
61
|
from .client import BuzzClient
|
|
62
62
|
from .huddle import (
|
|
63
63
|
HuddleAudio,
|
|
@@ -93,6 +93,7 @@ __all__ = [
|
|
|
93
93
|
"KIND_STREAM_MESSAGE",
|
|
94
94
|
"KIND_STREAM_MESSAGE_V2",
|
|
95
95
|
"KIND_USER_STATUS",
|
|
96
|
+
"OWNER_COMMANDS",
|
|
96
97
|
"BuzzClient",
|
|
97
98
|
"HuddleAudio",
|
|
98
99
|
"HuddleClient",
|
|
@@ -120,6 +121,7 @@ __all__ = [
|
|
|
120
121
|
"generate_keypair",
|
|
121
122
|
"pubkey_from_secret",
|
|
122
123
|
"sign_nip98",
|
|
124
|
+
"parse_owner_command",
|
|
123
125
|
"verify_agent_profile",
|
|
124
126
|
"verify_auth_tag",
|
|
125
127
|
"verify_event",
|
|
@@ -89,3 +89,40 @@ def verify_agent_profile(profile_event: dict[str, Any], owner_pubkey_hex: str) -
|
|
|
89
89
|
if not _conditions_apply(tag[2], profile_event):
|
|
90
90
|
return "condition_mismatch"
|
|
91
91
|
return "verified"
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
#: Owner control commands the Buzz platform defines. The message content,
|
|
95
|
+
#: trimmed, must equal ``!<command>`` exactly — ``!shutdown please`` is a
|
|
96
|
+
#: regular message, not a command (mirrors upstream ``buzz-acp``).
|
|
97
|
+
OWNER_COMMANDS = ("shutdown", "cancel", "rotate")
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def parse_owner_command(event: dict[str, Any], agent_pubkey_hex: str) -> str | None:
|
|
101
|
+
"""Match a Buzz owner control command addressed to ``agent_pubkey_hex``.
|
|
102
|
+
|
|
103
|
+
Mirrors upstream ``buzz-acp``'s ``is_owner_control_command``: a control
|
|
104
|
+
command is a chat message (kind 9) whose trimmed content is exactly one
|
|
105
|
+
of :data:`OWNER_COMMANDS` prefixed with ``!``, and which mentions the
|
|
106
|
+
agent through a ``["p", <agent_pubkey>]`` tag. Returns the bare command
|
|
107
|
+
name (``"shutdown"``, ``"cancel"``, ``"rotate"``) or ``None``.
|
|
108
|
+
|
|
109
|
+
Wire matching only. Before acting, the caller must also check that the
|
|
110
|
+
event is authentic (:func:`verify_event` on its JSON — upstream verifies
|
|
111
|
+
every inbound event's id and signature, so a relay cannot forge one) and
|
|
112
|
+
that its *author* is the agent's owner (see
|
|
113
|
+
:attr:`BuzzClient.verified_owner_hex`): upstream treats a non-owner
|
|
114
|
+
``!shutdown`` as a regular message, and so must callers.
|
|
115
|
+
"""
|
|
116
|
+
if event.get("kind") != _native.KIND_STREAM_MESSAGE:
|
|
117
|
+
return None
|
|
118
|
+
content = str(event.get("content", "") or "").strip()
|
|
119
|
+
if not content.startswith("!") or content[1:] not in OWNER_COMMANDS:
|
|
120
|
+
return None
|
|
121
|
+
tags = event.get("tags")
|
|
122
|
+
if not isinstance(tags, list):
|
|
123
|
+
return None
|
|
124
|
+
mentioned = any(
|
|
125
|
+
isinstance(t, list) and len(t) >= 2 and t[0] == "p" and t[1] == agent_pubkey_hex
|
|
126
|
+
for t in tags
|
|
127
|
+
)
|
|
128
|
+
return content[1:] if mentioned else None
|
|
@@ -21,6 +21,7 @@ import contextlib
|
|
|
21
21
|
import json
|
|
22
22
|
import logging
|
|
23
23
|
import uuid
|
|
24
|
+
import warnings
|
|
24
25
|
from collections.abc import AsyncIterator
|
|
25
26
|
from typing import Any
|
|
26
27
|
|
|
@@ -34,7 +35,13 @@ logger = logging.getLogger("buzzkit.client")
|
|
|
34
35
|
|
|
35
36
|
_AUTH_TIMEOUT = 20.0
|
|
36
37
|
_OK_TIMEOUT = 20.0
|
|
37
|
-
|
|
38
|
+
# Inbound cap for frames *from* the relay. The relay's own inbound limit is
|
|
39
|
+
# 512 KiB by default (BUZZ_MAX_FRAME_BYTES); 1 MiB is its HTTP body limit.
|
|
40
|
+
_MAX_FRAME = 1 << 20
|
|
41
|
+
# Relays reject a huddle announcement unless its backing channel carries the
|
|
42
|
+
# relay's huddle TTL: 3600 s, or BUZZ_EPHEMERAL_TTL_OVERRIDE, which replaces
|
|
43
|
+
# whatever the client sends (block/buzz#6056).
|
|
44
|
+
_HUDDLE_TTL = 3600
|
|
38
45
|
|
|
39
46
|
|
|
40
47
|
def _checked(r: httpx.Response) -> httpx.Response:
|
|
@@ -76,14 +83,16 @@ class BuzzClient:
|
|
|
76
83
|
self._secret = secret
|
|
77
84
|
self._auth_tag = auth_tag # NIP-OA owner attestation (AUTH + profile)
|
|
78
85
|
self.npub, self.pubkey_hex = _native.pubkey_from_secret(secret)
|
|
86
|
+
self._verified_owner = self._verify_owner() # attestations are immutable
|
|
79
87
|
#: WebSocket close code from the last disconnect (``None`` while
|
|
80
88
|
#: connected or never connected). 1012 means the relay is restarting
|
|
81
89
|
#: (graceful drain): reconnect with backoff and dedupe replayed
|
|
82
|
-
#: events by id after resubscribing.
|
|
90
|
+
#: events by id after resubscribing. 1008 means the community was
|
|
91
|
+
#: deleted: do not reconnect.
|
|
83
92
|
self.close_code: int | None = None
|
|
84
93
|
self._ws: Any = None
|
|
85
94
|
self._reader: asyncio.Task | None = None
|
|
86
|
-
self.
|
|
95
|
+
self._auth_result: asyncio.Future | None = None
|
|
87
96
|
self._auth_event_id: str | None = None
|
|
88
97
|
self._subs: dict[str, asyncio.Queue] = {}
|
|
89
98
|
self._pending_ok: dict[str, asyncio.Future] = {}
|
|
@@ -202,7 +211,7 @@ class BuzzClient:
|
|
|
202
211
|
return await self.publish(ev)
|
|
203
212
|
|
|
204
213
|
async def start_huddle(
|
|
205
|
-
self, parent_channel_id: str, *, name: str | None = None, ttl: int =
|
|
214
|
+
self, parent_channel_id: str, *, name: str | None = None, ttl: int | None = None
|
|
206
215
|
) -> str:
|
|
207
216
|
"""Start a huddle in a channel; returns the ephemeral huddle channel id.
|
|
208
217
|
|
|
@@ -210,7 +219,19 @@ class BuzzClient:
|
|
|
210
219
|
:meth:`connect` first) and posts the kind-48100 announcement to the
|
|
211
220
|
parent channel. Join the audio with
|
|
212
221
|
``HuddleClient(..., huddle_id, parent_channel_id=parent_channel_id)``.
|
|
222
|
+
|
|
223
|
+
``ttl`` is deprecated and ignored: relays only accept a huddle whose
|
|
224
|
+
backing channel carries their huddle TTL (3600 s, or an operator
|
|
225
|
+
override that replaces the client's value), so buzzkit always sends
|
|
226
|
+
3600.
|
|
213
227
|
"""
|
|
228
|
+
if ttl is not None and ttl != _HUDDLE_TTL:
|
|
229
|
+
warnings.warn(
|
|
230
|
+
f"start_huddle(ttl={ttl}) is ignored: relays require a {_HUDDLE_TTL} s "
|
|
231
|
+
"huddle channel; the parameter will be removed",
|
|
232
|
+
DeprecationWarning,
|
|
233
|
+
stacklevel=2,
|
|
234
|
+
)
|
|
214
235
|
huddle_id = str(uuid.uuid4())
|
|
215
236
|
create_ev = _native.build_create_channel_event(
|
|
216
237
|
self._secret,
|
|
@@ -218,7 +239,7 @@ class BuzzClient:
|
|
|
218
239
|
name or f"huddle-{huddle_id[:8]}",
|
|
219
240
|
visibility="private",
|
|
220
241
|
channel_type="stream",
|
|
221
|
-
ttl=
|
|
242
|
+
ttl=_HUDDLE_TTL,
|
|
222
243
|
)
|
|
223
244
|
result = await self.publish(create_ev)
|
|
224
245
|
if not result["accepted"]:
|
|
@@ -317,11 +338,38 @@ class BuzzClient:
|
|
|
317
338
|
|
|
318
339
|
@property
|
|
319
340
|
def owner_pubkey_hex(self) -> str:
|
|
320
|
-
"""This identity's effective owner: its auth-tag attester, else itself.
|
|
341
|
+
"""This identity's effective owner: its auth-tag attester, else itself.
|
|
342
|
+
|
|
343
|
+
Declared, not proven — this reads the tag's owner field without
|
|
344
|
+
verifying its signature. Anything that grants the owner privileges
|
|
345
|
+
(e.g. honoring ``!shutdown``) must use :attr:`verified_owner_hex`.
|
|
346
|
+
"""
|
|
321
347
|
if self._auth_tag is not None:
|
|
322
348
|
return json.loads(self._auth_tag)[1]
|
|
323
349
|
return self.pubkey_hex
|
|
324
350
|
|
|
351
|
+
def _verify_owner(self) -> str | None:
|
|
352
|
+
if self._auth_tag is None:
|
|
353
|
+
return None
|
|
354
|
+
try:
|
|
355
|
+
return _native.verify_auth_tag(self._auth_tag, self.pubkey_hex)
|
|
356
|
+
except ValueError as e:
|
|
357
|
+
logger.warning("auth tag does not verify for this identity: %s", e)
|
|
358
|
+
return None
|
|
359
|
+
|
|
360
|
+
@property
|
|
361
|
+
def verified_owner_hex(self) -> str | None:
|
|
362
|
+
"""The owner pubkey hex *proven* by this client's NIP-OA auth tag.
|
|
363
|
+
|
|
364
|
+
The tag's Schnorr signature was verified against this client's own
|
|
365
|
+
pubkey (:func:`verify_auth_tag`) at construction. ``None`` when the
|
|
366
|
+
client has no auth tag or the tag does not verify — callers gating
|
|
367
|
+
privileged actions (e.g. honoring ``!shutdown``) must treat ``None``
|
|
368
|
+
as "no owner", never fall back to the unverified
|
|
369
|
+
:attr:`owner_pubkey_hex`.
|
|
370
|
+
"""
|
|
371
|
+
return self._verified_owner
|
|
372
|
+
|
|
325
373
|
async def claim_invite(self, code_or_url: str) -> dict:
|
|
326
374
|
"""Redeem a relay invite: accept the join-policy (if any), then claim.
|
|
327
375
|
|
|
@@ -363,12 +411,31 @@ class BuzzClient:
|
|
|
363
411
|
await self.close()
|
|
364
412
|
|
|
365
413
|
async def connect(self) -> None:
|
|
366
|
-
"""Open the WebSocket and complete the NIP-42 auth handshake.
|
|
367
|
-
|
|
414
|
+
"""Open the WebSocket and complete the NIP-42 auth handshake.
|
|
415
|
+
|
|
416
|
+
Raises ``RuntimeError`` as soon as the relay rejects the AUTH (e.g.
|
|
417
|
+
``restricted: not a relay member``) or closes the socket before
|
|
418
|
+
accepting it, and ``TimeoutError`` when it never answers.
|
|
419
|
+
"""
|
|
368
420
|
self.close_code = None
|
|
421
|
+
self._auth_result = asyncio.get_running_loop().create_future()
|
|
369
422
|
self._ws = await websockets.connect(self._ws_url, max_size=_MAX_FRAME)
|
|
370
423
|
self._reader = asyncio.create_task(self._read_loop())
|
|
371
|
-
|
|
424
|
+
try:
|
|
425
|
+
await asyncio.wait_for(self._auth_result, timeout=_AUTH_TIMEOUT)
|
|
426
|
+
except BaseException:
|
|
427
|
+
await self.close()
|
|
428
|
+
raise
|
|
429
|
+
|
|
430
|
+
def _settle_auth(self, error: str | None) -> None:
|
|
431
|
+
"""Resolve the pending :meth:`connect` (no-op once settled)."""
|
|
432
|
+
fut = self._auth_result
|
|
433
|
+
if fut is None or fut.done():
|
|
434
|
+
return
|
|
435
|
+
if error is None:
|
|
436
|
+
fut.set_result(None)
|
|
437
|
+
else:
|
|
438
|
+
fut.set_exception(RuntimeError(error))
|
|
372
439
|
|
|
373
440
|
async def _read_loop(self) -> None:
|
|
374
441
|
try:
|
|
@@ -403,6 +470,9 @@ class BuzzClient:
|
|
|
403
470
|
self.close_code = close.code if close else None
|
|
404
471
|
logger.info("buzz websocket closed (code %s)", self.close_code)
|
|
405
472
|
finally:
|
|
473
|
+
self._settle_auth(
|
|
474
|
+
f"connection closed before NIP-42 auth completed (code {self.close_code})"
|
|
475
|
+
)
|
|
406
476
|
for q in self._subs.values():
|
|
407
477
|
q.put_nowait(("closed", "connection lost"))
|
|
408
478
|
|
|
@@ -418,10 +488,9 @@ class BuzzClient:
|
|
|
418
488
|
accepted = bool(msg[2]) if len(msg) > 2 else False
|
|
419
489
|
message = msg[3] if len(msg) > 3 else ""
|
|
420
490
|
if event_id == self._auth_event_id:
|
|
421
|
-
if accepted:
|
|
422
|
-
self._authed.set()
|
|
423
|
-
else:
|
|
491
|
+
if not accepted:
|
|
424
492
|
logger.error("NIP-42 auth rejected: %s", message)
|
|
493
|
+
self._settle_auth(None if accepted else f"NIP-42 auth rejected: {message}")
|
|
425
494
|
return
|
|
426
495
|
fut = self._pending_ok.pop(event_id, None)
|
|
427
496
|
if fut is not None and not fut.done():
|
|
@@ -130,8 +130,10 @@ fn pcm_bytes_to_i16(pcm: &[u8]) -> Result<Vec<i16>, String> {
|
|
|
130
130
|
));
|
|
131
131
|
}
|
|
132
132
|
Ok(pcm
|
|
133
|
-
.
|
|
134
|
-
.
|
|
133
|
+
.as_chunks::<2>()
|
|
134
|
+
.0
|
|
135
|
+
.iter()
|
|
136
|
+
.map(|&b| i16::from_le_bytes(b))
|
|
135
137
|
.collect())
|
|
136
138
|
}
|
|
137
139
|
|
|
@@ -91,9 +91,16 @@ fn build_message_event(
|
|
|
91
91
|
parent_event_id: event_id(parent, "reply_to")?,
|
|
92
92
|
}),
|
|
93
93
|
};
|
|
94
|
-
let builder =
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
let builder = buzz_sdk::build_message(
|
|
95
|
+
cid,
|
|
96
|
+
content,
|
|
97
|
+
thread_ref.as_ref(),
|
|
98
|
+
&mention_refs,
|
|
99
|
+
false,
|
|
100
|
+
&[],
|
|
101
|
+
&[],
|
|
102
|
+
)
|
|
103
|
+
.map_err(|e| PyValueError::new_err(format!("build_message: {e}")))?;
|
|
97
104
|
sign(builder, &keys)
|
|
98
105
|
}
|
|
99
106
|
|
|
@@ -96,3 +96,87 @@ async def test_resolve_agent_scopes_to_owner_records(monkeypatch):
|
|
|
96
96
|
assert by_pk[agent_pk]["profile"]["display_name"] == "Honey"
|
|
97
97
|
assert by_pk[missing_pk]["verification"] == "missing_profile"
|
|
98
98
|
assert "owner_pubkey" not in by_pk[missing_pk]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# ── owner control commands ───────────────────────────────────────────────────
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _command_event(content: str, agent_pk: str, *, kind: int = 9) -> dict:
|
|
105
|
+
return {
|
|
106
|
+
"kind": kind,
|
|
107
|
+
"pubkey": "f" * 64,
|
|
108
|
+
"content": content,
|
|
109
|
+
"tags": [["h", "chan-uuid"], ["p", agent_pk]],
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def test_parse_owner_command_matches_the_wire_shape():
|
|
114
|
+
agent = "a" * 64
|
|
115
|
+
for command in buzzkit.OWNER_COMMANDS:
|
|
116
|
+
assert buzzkit.parse_owner_command(_command_event(f"!{command}", agent), agent) == command
|
|
117
|
+
# upstream trims: " !rotate " is a command (buzz-acp has this exact case)
|
|
118
|
+
assert buzzkit.parse_owner_command(_command_event(" !rotate ", agent), agent) == "rotate"
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
def test_parse_owner_command_rejects_non_commands():
|
|
122
|
+
agent = "a" * 64
|
|
123
|
+
cases = [
|
|
124
|
+
_command_event("!shutdown please", agent), # exact match only
|
|
125
|
+
_command_event("shutdown", agent), # no bang
|
|
126
|
+
_command_event("!reboot", agent), # unknown command
|
|
127
|
+
_command_event("", agent), # empty
|
|
128
|
+
_command_event("!shutdown", agent, kind=40002), # wrong kind (V2 is not matched)
|
|
129
|
+
_command_event("!shutdown", "b" * 64), # mentions another agent
|
|
130
|
+
]
|
|
131
|
+
for event in cases:
|
|
132
|
+
assert buzzkit.parse_owner_command(event, agent) is None
|
|
133
|
+
unmentioned = _command_event("!shutdown", agent)
|
|
134
|
+
unmentioned["tags"] = [["h", "chan-uuid"]]
|
|
135
|
+
assert buzzkit.parse_owner_command(unmentioned, agent) is None
|
|
136
|
+
malformed = _command_event("!shutdown", agent)
|
|
137
|
+
malformed["tags"] = "not-a-list"
|
|
138
|
+
assert buzzkit.parse_owner_command(malformed, agent) is None
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_owner_command_check_rejects_forged_events():
|
|
142
|
+
owner_sk, owner_pk = _keys()
|
|
143
|
+
agent_sk, agent_pk = _keys()
|
|
144
|
+
bz = BuzzClient("wss://r", agent_sk, auth_tag=buzzkit.compute_auth_tag(owner_sk, agent_pk))
|
|
145
|
+
channel = "6b1f4b1e-1b8a-4a3e-9c51-0d7f5f2c9a10"
|
|
146
|
+
|
|
147
|
+
def message(secret: str, content: str) -> dict:
|
|
148
|
+
return json.loads(buzzkit.build_message_event(secret, channel, content, [agent_pk]))
|
|
149
|
+
|
|
150
|
+
def is_owner_shutdown(event: dict) -> bool: # the README's documented check
|
|
151
|
+
return (
|
|
152
|
+
buzzkit.parse_owner_command(event, bz.pubkey_hex) == "shutdown"
|
|
153
|
+
and buzzkit.verify_event(json.dumps(event))
|
|
154
|
+
and event["pubkey"] == bz.verified_owner_hex
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
assert is_owner_shutdown(message(owner_sk, "!shutdown"))
|
|
158
|
+
# A relay rewriting an owner's message into a command matches the wire
|
|
159
|
+
# shape, but no longer verifies.
|
|
160
|
+
forged = {**message(owner_sk, "hi"), "content": "!shutdown"}
|
|
161
|
+
assert buzzkit.parse_owner_command(forged, bz.pubkey_hex) == "shutdown"
|
|
162
|
+
assert not is_owner_shutdown(forged)
|
|
163
|
+
# Nor does a stranger's command relabelled with the owner's pubkey.
|
|
164
|
+
stranger_sk, _ = _keys()
|
|
165
|
+
spoofed = {**message(stranger_sk, "!shutdown"), "pubkey": owner_pk}
|
|
166
|
+
assert not is_owner_shutdown(spoofed)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def test_verified_owner_hex_proves_the_attestation():
|
|
170
|
+
owner_sk, owner_pk = _keys()
|
|
171
|
+
agent_sk, agent_pk = _keys()
|
|
172
|
+
tag = buzzkit.compute_auth_tag(owner_sk, agent_pk)
|
|
173
|
+
assert BuzzClient("wss://r", agent_sk, auth_tag=tag).verified_owner_hex == owner_pk
|
|
174
|
+
# no tag → no owner
|
|
175
|
+
assert BuzzClient("wss://r", agent_sk).verified_owner_hex is None
|
|
176
|
+
# a tag attesting a DIFFERENT agent does not verify for this client...
|
|
177
|
+
other_sk, other_pk = _keys()
|
|
178
|
+
stolen = buzzkit.compute_auth_tag(owner_sk, other_pk)
|
|
179
|
+
client = BuzzClient("wss://r", agent_sk, auth_tag=stolen)
|
|
180
|
+
assert client.verified_owner_hex is None
|
|
181
|
+
# ...even though the unverified reading still names the claimed owner
|
|
182
|
+
assert client.owner_pubkey_hex == owner_pk
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""Offline tests for BuzzClient: HTTP bridge errors, auth handshake, huddle start."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import contextlib
|
|
7
|
+
import json
|
|
8
|
+
import uuid
|
|
9
|
+
import warnings
|
|
10
|
+
|
|
11
|
+
import buzzkit
|
|
12
|
+
import httpx
|
|
13
|
+
import pytest
|
|
14
|
+
import websockets
|
|
15
|
+
from buzzkit.client import BuzzClient
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
async def test_post_event_surfaces_relay_error_body(monkeypatch):
|
|
19
|
+
def handler(request: httpx.Request) -> httpx.Response:
|
|
20
|
+
return httpx.Response(400, json={"error": "invalid: edit target event not found"})
|
|
21
|
+
|
|
22
|
+
real = httpx.AsyncClient
|
|
23
|
+
monkeypatch.setattr(
|
|
24
|
+
httpx,
|
|
25
|
+
"AsyncClient",
|
|
26
|
+
lambda **kw: real(transport=httpx.MockTransport(handler), timeout=1),
|
|
27
|
+
)
|
|
28
|
+
nsec, _, _ = buzzkit.generate_keypair()
|
|
29
|
+
bz = BuzzClient("wss://relay.example", nsec)
|
|
30
|
+
ev = buzzkit.build_message_event(nsec, str(uuid.uuid4()), "x")
|
|
31
|
+
with pytest.raises(RuntimeError, match="edit target event not found"):
|
|
32
|
+
await bz.post_event(ev)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
# ── WebSocket auth handshake (fake relay) ────────────────────────────────────
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@contextlib.asynccontextmanager
|
|
39
|
+
async def serve(handler):
|
|
40
|
+
server = await websockets.serve(handler, "127.0.0.1", 0)
|
|
41
|
+
port = server.sockets[0].getsockname()[1]
|
|
42
|
+
try:
|
|
43
|
+
yield f"ws://127.0.0.1:{port}"
|
|
44
|
+
finally:
|
|
45
|
+
server.close()
|
|
46
|
+
await server.wait_closed()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
async def _challenge_and_read_auth(ws) -> dict:
|
|
50
|
+
await ws.send(json.dumps(["AUTH", "challenge-1"]))
|
|
51
|
+
return json.loads(await ws.recv())[1]
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
async def test_connect_completes_on_accepted_auth():
|
|
55
|
+
async def handler(ws):
|
|
56
|
+
auth = await _challenge_and_read_auth(ws)
|
|
57
|
+
await ws.send(json.dumps(["OK", auth["id"], True, ""]))
|
|
58
|
+
await ws.wait_closed()
|
|
59
|
+
|
|
60
|
+
nsec, _, _ = buzzkit.generate_keypair()
|
|
61
|
+
async with serve(handler) as url:
|
|
62
|
+
bz = BuzzClient(url, nsec)
|
|
63
|
+
await asyncio.wait_for(bz.connect(), 2)
|
|
64
|
+
await bz.close()
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def test_connect_fails_fast_on_rejected_auth():
|
|
68
|
+
async def handler(ws):
|
|
69
|
+
auth = await _challenge_and_read_auth(ws)
|
|
70
|
+
await ws.send(json.dumps(["OK", auth["id"], False, "restricted: not a relay member"]))
|
|
71
|
+
await ws.wait_closed()
|
|
72
|
+
|
|
73
|
+
nsec, _, _ = buzzkit.generate_keypair()
|
|
74
|
+
async with serve(handler) as url:
|
|
75
|
+
bz = BuzzClient(url, nsec)
|
|
76
|
+
# Well under the 20 s auth timeout: the rejection itself ends connect().
|
|
77
|
+
with pytest.raises(RuntimeError, match="not a relay member"):
|
|
78
|
+
await asyncio.wait_for(bz.connect(), 2)
|
|
79
|
+
assert bz._ws is None
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
async def test_connect_fails_fast_when_closed_before_auth():
|
|
83
|
+
async def handler(ws):
|
|
84
|
+
await _challenge_and_read_auth(ws)
|
|
85
|
+
await ws.close(code=1008, reason="community deleted")
|
|
86
|
+
|
|
87
|
+
nsec, _, _ = buzzkit.generate_keypair()
|
|
88
|
+
async with serve(handler) as url:
|
|
89
|
+
bz = BuzzClient(url, nsec)
|
|
90
|
+
with pytest.raises(RuntimeError, match="closed before NIP-42 auth"):
|
|
91
|
+
await asyncio.wait_for(bz.connect(), 2)
|
|
92
|
+
assert bz.close_code == 1008
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
# ── start_huddle ─────────────────────────────────────────────────────────────
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
async def test_start_huddle_always_sends_the_relay_huddle_ttl(monkeypatch):
|
|
99
|
+
nsec, _, _ = buzzkit.generate_keypair()
|
|
100
|
+
bz = BuzzClient("wss://relay.example", nsec)
|
|
101
|
+
published: list[dict] = []
|
|
102
|
+
|
|
103
|
+
async def fake_publish(event_json: str) -> dict:
|
|
104
|
+
published.append(json.loads(event_json))
|
|
105
|
+
return {"accepted": True, "event_id": published[-1]["id"], "message": ""}
|
|
106
|
+
|
|
107
|
+
monkeypatch.setattr(bz, "publish", fake_publish)
|
|
108
|
+
parent = str(uuid.uuid4())
|
|
109
|
+
with pytest.warns(DeprecationWarning, match="ttl=7200"):
|
|
110
|
+
await bz.start_huddle(parent, ttl=7200)
|
|
111
|
+
with warnings.catch_warnings():
|
|
112
|
+
warnings.simplefilter("error") # the default and an explicit 3600 stay silent
|
|
113
|
+
await bz.start_huddle(parent)
|
|
114
|
+
await bz.start_huddle(parent, ttl=3600)
|
|
115
|
+
|
|
116
|
+
created = [ev for ev in published if ev["kind"] == buzzkit.KIND_CREATE_CHANNEL]
|
|
117
|
+
assert len(created) == 3
|
|
118
|
+
for ev in created:
|
|
119
|
+
assert ["ttl", "3600"] in ev["tags"]
|
|
@@ -145,6 +145,13 @@ def test_join_channel_event_keeps_self_p_tag():
|
|
|
145
145
|
assert any(t[0] == "h" for t in event["tags"])
|
|
146
146
|
|
|
147
147
|
|
|
148
|
+
def test_message_event_keeps_self_mention():
|
|
149
|
+
nsec, _, pk_hex = buzzkit.generate_keypair()
|
|
150
|
+
event = json.loads(buzzkit.build_message_event(nsec, str(uuid.uuid4()), "note", [pk_hex]))
|
|
151
|
+
# Self-mentions survive nostr's self-tag stripping (upstream block/buzz#4975).
|
|
152
|
+
assert ["p", pk_hex] in event["tags"]
|
|
153
|
+
|
|
154
|
+
|
|
148
155
|
def test_compute_auth_tag():
|
|
149
156
|
owner_nsec, _, owner_pk = buzzkit.generate_keypair()
|
|
150
157
|
_, _, agent_pk = buzzkit.generate_keypair()
|
|
@@ -222,6 +229,18 @@ def test_verify_auth_tag_roundtrip():
|
|
|
222
229
|
buzzkit.verify_auth_tag("not json", agent_pk)
|
|
223
230
|
|
|
224
231
|
|
|
232
|
+
def test_verify_auth_tag_rejects_noncanonical_hex():
|
|
233
|
+
owner_nsec, _, _ = buzzkit.generate_keypair()
|
|
234
|
+
_, _, agent_pk = buzzkit.generate_keypair()
|
|
235
|
+
tag = json.loads(buzzkit.compute_auth_tag(owner_nsec, agent_pk))
|
|
236
|
+
# Relays reject uppercase hex; verification must not accept what they refuse.
|
|
237
|
+
for i in (1, 3):
|
|
238
|
+
upper = list(tag)
|
|
239
|
+
upper[i] = upper[i].upper()
|
|
240
|
+
with pytest.raises(ValueError):
|
|
241
|
+
buzzkit.verify_auth_tag(json.dumps(upper), agent_pk)
|
|
242
|
+
|
|
243
|
+
|
|
225
244
|
def test_huddle_started_event():
|
|
226
245
|
nsec, _, _ = buzzkit.generate_keypair()
|
|
227
246
|
parent, ephemeral = str(uuid.uuid4()), str(uuid.uuid4())
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"""Offline tests for BuzzClient's HTTP bridge error surfacing."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import uuid
|
|
6
|
-
|
|
7
|
-
import buzzkit
|
|
8
|
-
import httpx
|
|
9
|
-
import pytest
|
|
10
|
-
from buzzkit.client import BuzzClient
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
async def test_post_event_surfaces_relay_error_body(monkeypatch):
|
|
14
|
-
def handler(request: httpx.Request) -> httpx.Response:
|
|
15
|
-
return httpx.Response(400, json={"error": "invalid: edit target event not found"})
|
|
16
|
-
|
|
17
|
-
real = httpx.AsyncClient
|
|
18
|
-
monkeypatch.setattr(
|
|
19
|
-
httpx,
|
|
20
|
-
"AsyncClient",
|
|
21
|
-
lambda **kw: real(transport=httpx.MockTransport(handler), timeout=1),
|
|
22
|
-
)
|
|
23
|
-
nsec, _, _ = buzzkit.generate_keypair()
|
|
24
|
-
bz = BuzzClient("wss://relay.example", nsec)
|
|
25
|
-
ev = buzzkit.build_message_event(nsec, str(uuid.uuid4()), "x")
|
|
26
|
-
with pytest.raises(RuntimeError, match="edit target event not found"):
|
|
27
|
-
await bz.post_event(ev)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|