Skip to content

Commit faf55f1

Browse files
feat: add NIP-70 protected event detection util (#643)
1 parent 1295272 commit faf55f1

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"nostream": patch
3+
---
4+
5+
feat: add NIP-70 protected event detection utility

src/constants/base.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ export enum EventTags {
7979
AuthRelay = 'relay',
8080
// Marmot Protocol MIP-03: group ID for filtering kind:445 Group Events
8181
Group = 'h',
82+
// NIP-70: Protected Events
83+
Protected = '-',
8284
}
8385

8486
export const ALL_RELAYS = 'ALL_RELAYS'

src/utils/event.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,3 +289,9 @@ export const isWelcomeRumorEvent = (event: Event): boolean => {
289289
export const isMarmotGroupEvent = (event: Event): boolean => {
290290
return event.kind === EventKinds.MARMOT_GROUP_EVENT
291291
}
292+
293+
// NIP-70: Protected Events
294+
295+
export const isProtectedEvent = (event: Event): boolean => {
296+
return event.tags.some((tag) => tag[0] === EventTags.Protected)
297+
}

test/unit/utils/event.spec.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isGiftWrapEvent,
1414
isMarmotGroupEvent,
1515
isParameterizedReplaceableEvent,
16+
isProtectedEvent,
1617
isReplaceableEvent,
1718
isRequestToVanishEvent,
1819
isSealEvent,
@@ -655,3 +656,56 @@ describe('NIP-40', () => {
655656
})
656657
})
657658
})
659+
660+
describe('NIP-70', () => {
661+
describe('isProtectedEvent', () => {
662+
it('returns true if event has a ["-"] tag', () => {
663+
const event: Event = {
664+
tags: [['-']],
665+
} as any
666+
expect(isProtectedEvent(event)).to.be.true
667+
})
668+
669+
it('returns true if protected tag has extra values', () => {
670+
const event: Event = {
671+
tags: [['-', 'some-reason']],
672+
} as any
673+
expect(isProtectedEvent(event)).to.be.true
674+
})
675+
676+
it('returns false if event has no tags', () => {
677+
const event: Event = {
678+
tags: [],
679+
} as any
680+
expect(isProtectedEvent(event)).to.be.false
681+
})
682+
683+
it('returns false if event has unrelated tags', () => {
684+
const event: Event = {
685+
tags: [
686+
['e', '7377fa81fc6c7ae7f7f4ef8938d4a603f7bf98183b35ab128235cc92d4bebf96'],
687+
['p', '22e804d26ed16b68db5259e78449e96dab5d464c8f470bda3eb1a70467f2c793'],
688+
],
689+
} as any
690+
expect(isProtectedEvent(event)).to.be.false
691+
})
692+
693+
it('returns false if "-" appears as a tag value, not a tag name', () => {
694+
const event: Event = {
695+
tags: [['e', '-']],
696+
} as any
697+
expect(isProtectedEvent(event)).to.be.false
698+
})
699+
700+
it('returns true when protected tag is among other tags', () => {
701+
const event: Event = {
702+
tags: [
703+
['e', '7377fa81fc6c7ae7f7f4ef8938d4a603f7bf98183b35ab128235cc92d4bebf96'],
704+
['-'],
705+
['p', '22e804d26ed16b68db5259e78449e96dab5d464c8f470bda3eb1a70467f2c793'],
706+
],
707+
} as any
708+
expect(isProtectedEvent(event)).to.be.true
709+
})
710+
})
711+
})

0 commit comments

Comments
 (0)