-
Notifications
You must be signed in to change notification settings - Fork 18
Move relay admin panel to NIP-43 message path #494
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -43,3 +43,7 @@ typesense-data/ | |
| .hermit/ | ||
| doc/ | ||
| /repos/ | ||
|
|
||
| # Local identity files | ||
| identity.key | ||
| **/identity.key | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,154 @@ | ||
| import { relayClient } from "@/shared/api/relayClient"; | ||
| import { getIdentity, signRelayEvent } from "@/shared/api/tauri"; | ||
| import type { | ||
| RelayEvent, | ||
| RelayMember, | ||
| RelayMemberRole, | ||
| } from "@/shared/api/types"; | ||
|
|
||
| const KIND_NIP43_MEMBERSHIP_LIST = 13534; | ||
| const KIND_RELAY_ADMIN_ADD_MEMBER = 9030; | ||
| const KIND_RELAY_ADMIN_REMOVE_MEMBER = 9031; | ||
| const KIND_RELAY_ADMIN_CHANGE_ROLE = 9032; | ||
|
|
||
| function isRelayMemberRole( | ||
| value: string | undefined, | ||
| ): value is RelayMemberRole { | ||
| return value === "owner" || value === "admin" || value === "member"; | ||
| } | ||
|
|
||
| function normalizePubkey(pubkey: string): string { | ||
| return pubkey.trim().toLowerCase(); | ||
| } | ||
|
|
||
| function eventCreatedAtIso(event: RelayEvent): string { | ||
| return new Date(event.created_at * 1_000).toISOString(); | ||
| } | ||
|
|
||
| export type RelayMembershipLookup = { | ||
| /** | ||
| * True when the relay returned a NIP-43 membership snapshot. | ||
| * | ||
| * Open relays do not publish kind:13534, so absence of this snapshot must not | ||
| * be treated as a denial by onboarding. | ||
| */ | ||
| snapshotFound: boolean; | ||
| membership: RelayMember | null; | ||
| }; | ||
|
|
||
| export function relayMembersFromEvent(event: RelayEvent): RelayMember[] { | ||
| const seen = new Set<string>(); | ||
| const members: RelayMember[] = []; | ||
| const createdAt = eventCreatedAtIso(event); | ||
|
|
||
| for (const tag of event.tags) { | ||
| const [name, rawPubkey, maybeRoleOrRelay, maybePTagRole] = tag; | ||
| if (name !== "member" && name !== "p") continue; | ||
| if (!rawPubkey) continue; | ||
|
|
||
| const pubkey = normalizePubkey(rawPubkey); | ||
| if (!/^[0-9a-f]{64}$/.test(pubkey) || seen.has(pubkey)) continue; | ||
| seen.add(pubkey); | ||
|
|
||
| const rawRole = name === "member" ? maybeRoleOrRelay : maybePTagRole; | ||
| const role = isRelayMemberRole(rawRole) ? rawRole : "member"; | ||
|
|
||
| members.push({ | ||
| pubkey, | ||
| role, | ||
| addedBy: null, | ||
| createdAt, | ||
| }); | ||
| } | ||
|
|
||
| return members; | ||
| } | ||
|
|
||
| export function relayMembershipLookupFromEvent( | ||
| event: RelayEvent | null, | ||
| pubkey: string, | ||
| ): RelayMembershipLookup { | ||
| if (!event) { | ||
| return { snapshotFound: false, membership: null }; | ||
| } | ||
|
|
||
| const normalizedPubkey = normalizePubkey(pubkey); | ||
| return { | ||
| snapshotFound: true, | ||
| membership: | ||
| relayMembersFromEvent(event).find( | ||
| (member) => normalizePubkey(member.pubkey) === normalizedPubkey, | ||
| ) ?? null, | ||
| }; | ||
| } | ||
|
|
||
| async function fetchMembershipListEvent(): Promise<RelayEvent | null> { | ||
| const events = await relayClient.fetchEvents({ | ||
| kinds: [KIND_NIP43_MEMBERSHIP_LIST], | ||
| limit: 1, | ||
| }); | ||
|
|
||
| return events[events.length - 1] ?? null; | ||
| } | ||
|
|
||
| export async function listRelayMembers(): Promise<RelayMember[]> { | ||
| const event = await fetchMembershipListEvent(); | ||
| return event ? relayMembersFromEvent(event) : []; | ||
| } | ||
|
|
||
| export async function getMyRelayMembershipLookup(): Promise<RelayMembershipLookup> { | ||
| const [{ pubkey }, event] = await Promise.all([ | ||
| getIdentity(), | ||
| fetchMembershipListEvent(), | ||
| ]); | ||
| return relayMembershipLookupFromEvent(event, pubkey); | ||
| } | ||
|
|
||
| export async function getMyRelayMembership(): Promise<RelayMember | null> { | ||
| return (await getMyRelayMembershipLookup()).membership; | ||
| } | ||
|
|
||
| async function publishRelayAdminEvent( | ||
| kind: number, | ||
| targetPubkey: string, | ||
| role?: string, | ||
| ): Promise<void> { | ||
| const tags = [["p", normalizePubkey(targetPubkey)]]; | ||
| if (role) { | ||
| tags.push(["role", role]); | ||
| } | ||
|
|
||
| const event = await signRelayEvent({ | ||
| kind, | ||
| content: "", | ||
| tags, | ||
| }); | ||
|
|
||
| await relayClient.publishEvent( | ||
| event, | ||
| "Timed out while updating relay access.", | ||
| "Failed to update relay access.", | ||
| ); | ||
| } | ||
|
|
||
| export async function addRelayMember( | ||
| targetPubkey: string, | ||
| role: string, | ||
| ): Promise<void> { | ||
| await publishRelayAdminEvent(KIND_RELAY_ADMIN_ADD_MEMBER, targetPubkey, role); | ||
| } | ||
|
|
||
| export async function removeRelayMember(targetPubkey: string): Promise<void> { | ||
| await publishRelayAdminEvent(KIND_RELAY_ADMIN_REMOVE_MEMBER, targetPubkey); | ||
| } | ||
|
|
||
| export async function changeRelayMemberRole( | ||
| targetPubkey: string, | ||
| newRole: string, | ||
| ): Promise<void> { | ||
| await publishRelayAdminEvent( | ||
| KIND_RELAY_ADMIN_CHANGE_ROLE, | ||
| targetPubkey, | ||
| newRole, | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Returning an empty list when no kind
13534event exists makesgetMyRelayMembership()resolve tonull, which onboarding interprets as “access denied” (checkMembershipDeniedinOnboardingFlow.tsx). On relays with membership gating disabled, the server does not publish the startup13534snapshot, so this path incorrectly sends normal users to the membership-denied flow before profile save. Please treat “no snapshot available” as a non-denial state (or explicitly surface an unknown state) instead of collapsing it to “not a member.”Useful? React with 👍 / 👎.