Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add support for nip-26 delegations
  • Loading branch information
mariano-perez-rodriguez committed Dec 7, 2023
commit 4fcea6c1d20d42d16d237b4ffa21123ad0f9fd13
20 changes: 20 additions & 0 deletions filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ describe('Filter', () => {

expect(result).toEqual(true)
})

it('should return true when the event delegator is in the filter', () => {
const filter = { author: ['abc'] }

const event = buildEvent({ pubkey: 'def', tags: [['delegation', 'abc', '', '']] })

const result = matchFilter(filter, event)

expect(result).toEqual(true)
})

it('should return true when the event delegator prefix is in the filter', () => {
const filter = { author: ['a'] }

const event = buildEvent({ pubkey: 'def', tags: [['delegation', 'abc', '', '']] })

const result = matchFilter(filter, event)

expect(result).toEqual(true)
})
})

describe('matchFilters', () => {
Expand Down
12 changes: 10 additions & 2 deletions filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ export function matchFilter(filter: Filter<number>, event: Event<number>): boole
return false
}
}
const d_pubkey: string = event.tags.find(tag => tag[0] === 'delegation')?.at(1) ?? ''
if (filter.kinds && filter.kinds.indexOf(event.kind) === -1) return false
if (filter.authors && filter.authors.indexOf(event.pubkey) === -1) {
if (!filter.authors.some(prefix => event.pubkey.startsWith(prefix))) {
if (
filter.authors &&
filter.authors.indexOf(event.pubkey) === -1 &&
(d_pubkey === '' || filter.authors.indexOf(d_pubkey) === -1)
) {
if (
!filter.authors.some(prefix => event.pubkey.startsWith(prefix)) &&
(d_pubkey === '' || !filter.authors.some(prefix => d_pubkey.startsWith(prefix)))
) {
return false
}
}
Expand Down