Skip to content

feat: add NIP-43 invite code foundation#650

Open
Anshumancanrock wants to merge 1 commit into
mainfrom
feat/nip43-invite-codes
Open

feat: add NIP-43 invite code foundation#650
Anshumancanrock wants to merge 1 commit into
mainfrom
feat/nip43-invite-codes

Conversation

@Anshumancanrock

Copy link
Copy Markdown
Collaborator

Description

Invite code infrastructure for NIP-43 relay membership.

claimCode() uses a single atomic UPDATE with WHERE guards (not expired, uses remaining) so concurrent claims on the same code are safe without transactions.

claimed_by has no FK to users so users don't exist yet at claim time.

Related Issue

NIP-43 implementation

Motivation and Context

Need a data layer for NIP-43 join/leave handlers.

How Has This Been Tested?

25 unit tests, full suite 1322 passing.

Types of changes

  • Non-functional change (docs, style, minor refactor)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my code changes.
  • I added a changeset, or this is docs-only and I added an empty changeset.
  • All new and existing tests passed.

@changeset-bot

changeset-bot Bot commented Jun 23, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 0119664

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
nostream Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@Anshumancanrock Anshumancanrock force-pushed the feat/nip43-invite-codes branch 3 times, most recently from afbfc55 to 1c4b970 Compare June 23, 2026 19:32
@coveralls

coveralls commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

Coverage Status

Coverage is 67.099%feat/nip43-invite-codes into main. No base build found for main.

- Add event kinds 8000, 8001, 13534, 28934, 28935, 28936 and tags (member, claim)
- Create InviteCode/DBInviteCode types and Nip43Settings interface
- Add IInviteCodeRepository interface with create, findByCode, claimCode, findActiveCodes, deleteExpiredCodes
- Implement InviteCodeRepository with atomic claimCode via single UPDATE
- Add generateInviteCode() using crypto.randomBytes (128-bit entropy)
- Create invite_codes migration with CHECK constraints and partial index
- Add revokeAdmission() and findAllAdmitted() to UserRepository
- Add 27 unit tests with full coverage

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces foundational invite-code infrastructure to support NIP-43 relay membership workflows, including a new repository for managing invite codes, schema/types to represent them, and constants/settings needed by upcoming join/leave handlers.

Changes:

  • Added InviteCodeRepository with code generation, creation, lookup, atomic claim, active-code listing, and expiry cleanup methods.
  • Added invite_codes database migration plus corresponding TypeScript types and repository interface.
  • Extended NIP-43-related event kind/tag constants and added nip43 settings shape.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
test/unit/repositories/invite-code-repository.spec.ts Adds unit tests covering the new repository behaviors and expected transformations.
src/repositories/invite-code-repository.ts Implements invite-code persistence and atomic claiming logic over invite_codes.
src/constants/base.ts Adds NIP-43 event kinds and tags used by relay access metadata/requests.
src/@types/settings.ts Introduces Nip43Settings and wires it into the global Settings type.
src/@types/repositories.ts Defines IInviteCodeRepository interface for the new repository.
src/@types/invite-code.ts Adds InviteCode and DBInviteCode types for app/DB representations.
migrations/20260624_120000_create_nip43_invite_codes_table.js Creates invite_codes table and adds check constraints + expiry index.
.knip.json Ignores the new repository file in knip analysis.
.changeset/nip43-invite-codes.md Adds a minor changeset describing the new NIP-43 invite-code foundation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

maxUses: number = 1,
client: DatabaseClient = this.dbClient,
): Promise<InviteCode> {
logger('create invite code: %s (expires: %s, maxUses: %d)', code, expiresAt ?? 'never', maxUses)
code: string,
client: DatabaseClient = this.dbClient,
): Promise<InviteCode | undefined> {
logger('find invite code: %s', code)
pubkey: Pubkey,
client: DatabaseClient = this.dbClient,
): Promise<boolean> {
logger('claim invite code %s for %s', code, pubkey)
})
.update({
use_count: client.raw('use_count + 1'),
claimed_by: toBuffer(pubkey),
Comment on lines +43 to +46
afterEach(() => {
dbClient.destroy()
sandbox.restore()
})
Comment on lines +7 to +8
table.integer('max_uses').notNullable().defaultTo(1)
table.integer('use_count').notNullable().defaultTo(0)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these two fields could just be one called remaining_uses, once it gets to <= 0 the invite can no longer be used.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants