feat(nwcp): add NIP-42 client AUTH support#38
Conversation
When a NIP-42-enabled relay sends [AUTH, challenge], nwcprovider now responds with a signed kind-22242 event (relay URL + challenge tags). Previously the AUTH message type hit the catch-all raise Exception branch, causing an error log and no response. The relay then closed subscriptions because the challenge was never answered. Fix: add _on_auth_message handler and route msg[0]=AUTH to it in _on_message. Closes: (to be linked after PR filed) Ref: https://github.com/nostr-protocol/nips/blob/master/42.md
There was a problem hiding this comment.
Pull request overview
Adds NIP-42 client-side AUTH challenge handling to NWCServiceProvider so the provider can interoperate with relays that require ["AUTH", "<challenge>"] responses instead of failing with an “Unknown message type” exception.
Changes:
- Route incoming relay
"AUTH"messages to a dedicated handler. - Implement
_on_auth_messageto build/sign a kind22242auth event and send it back via["AUTH", event].
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| except Exception as e: | ||
| logger.error("Error parsing event: " + str(e)) |
| auth_event: dict = { | ||
| "kind": 22242, | ||
| "content": "", | ||
| "created_at": int(time.time()), | ||
| "tags": [ | ||
| ["relay", self.relay], | ||
| ["challenge", challenge], | ||
| ], | ||
| } | ||
| self._sign_event(auth_event) |
|
Dependency note — this PR is a blocker for newly-exposed NIPs on our Umbrel relay fork.
NIP-11 on Thank you for your kinds consideration. |
|
@satwise Thanks for this- clean fix, and it lines up nicely with the existing resubscribe logic so the auth-then-resubscribe flow should just work. Verified it against NIP-42 too: kind Couple of small things:
Neither is a blocker on the logic, happy to merge either way, just nice-to-haves. 🙏 |
Implement stakeholder-requested relay auth/publish diagnostics. Changes: - Route incoming OK frames to _on_ok_message instead of ignoring them. - Log rejected OK responses at warning level with relay, event id, and relay reason. - Log accepted OK responses and malformed OK payloads at debug level. Validation summary (no package unit test added, per request): - Ad-hoc runtime validation confirmed AUTH response emits kind 22242 with relay/challenge tags and valid signature. - Log checks confirmed expected rejected/accepted/malformed OK diagnostics are emitted.
@DoktorShift thank you for your review and suggestions. I am happy to accommodate your requests. I implemented relay OK response handling so rejected publish/auth outcomes are now surfaced as warning logs with relay, event id, and reason, while accepted and malformed OK responses are captured at debug level for diagnostics. Included are the results of the unit-test validation you requested for the AUTH path: Pertinent log results:
|
Problem
When a NIP-42-enabled relay sends
["AUTH", "<challenge>"], nwcprovider hits the catch-allraise Exception("Unknown message type")branch. The error is logged and no response is sent. The relay closes subscriptions because the challenge is never answered.This makes nwcprovider completely incompatible with any relay that has
nip42_auth = true.