-
Notifications
You must be signed in to change notification settings - Fork 231
feat: implement NIP-17 & NIP-44 v2 for Modern Direct Messages #458
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
+1,178
−374
Merged
Changes from 1 commit
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -11,11 +11,13 @@ | |
| 12, | ||
| 15, | ||
| 16, | ||
| 17, | ||
| 20, | ||
| 22, | ||
| 28, | ||
| 33, | ||
| 40 | ||
| 40, | ||
| 44 | ||
| ], | ||
| "supportedNipExtensions": [ | ||
| "11a" | ||
|
|
||
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
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,64 @@ | ||
| import { createCommandResult } from '../../utils/messages' | ||
| import { createLogger } from '../../factories/logger-factory' | ||
| import { Event } from '../../@types/event' | ||
| import { EventTags } from '../../constants/base' | ||
| import { IEventRepository } from '../../@types/repositories' | ||
| import { IEventStrategy } from '../../@types/message-handlers' | ||
| import { IWebSocketAdapter } from '../../@types/adapters' | ||
| import { validateNip44Payload } from '../../utils/nip44' | ||
| import { WebSocketAdapterEvent } from '../../constants/adapter' | ||
|
|
||
| const debug = createLogger('gift-wrap-event-strategy') | ||
|
|
||
| export class GiftWrapEventStrategy implements IEventStrategy<Event, Promise<void>> { | ||
| public constructor( | ||
| private readonly webSocket: IWebSocketAdapter, | ||
| private readonly eventRepository: IEventRepository, | ||
| ) {} | ||
|
|
||
| public async execute(event: Event): Promise<void> { | ||
| debug('received gift wrap event: %o', event) | ||
|
|
||
| const reason = this.validateGiftWrap(event) | ||
| if (reason) { | ||
| this.webSocket.emit( | ||
| WebSocketAdapterEvent.Message, | ||
| createCommandResult(event.id, false, `invalid: ${reason}`), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| const count = await this.eventRepository.create(event) | ||
| this.webSocket.emit( | ||
| WebSocketAdapterEvent.Message, | ||
| createCommandResult(event.id, true, count ? '' : 'duplicate:'), | ||
| ) | ||
|
|
||
| if (count) { | ||
| this.webSocket.emit(WebSocketAdapterEvent.Broadcast, event) | ||
| } | ||
| } | ||
|
|
||
| private validateGiftWrap(event: Event): string | undefined { | ||
| // NIP-17: gift wrap MUST have exactly one p tag (one recipient per wrap) | ||
| const recipientTags = event.tags.filter( | ||
| (tag) => tag.length >= 2 && tag[0] === EventTags.Pubkey, | ||
| ) | ||
|
|
||
| if (recipientTags.length === 0) { | ||
| return 'gift wrap event (kind 1059) must have a p tag identifying the recipient' | ||
| } | ||
|
|
||
| if (recipientTags.length > 1) { | ||
| return 'gift wrap event (kind 1059) must have exactly one p tag per recipient' | ||
| } | ||
|
|
||
|
CKodidela marked this conversation as resolved.
Outdated
|
||
| // Validate that the content is a structurally valid NIP-44 v2 payload | ||
| const payloadError = validateNip44Payload(event.content) | ||
| if (payloadError) { | ||
| return `gift wrap content must be a valid NIP-44 v2 payload: ${payloadError}` | ||
| } | ||
|
|
||
| return undefined | ||
| } | ||
| } | ||
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.
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.
Uh oh!
There was an error while loading. Please reload this page.