11import * as secp256k1 from '@noble/secp256k1'
22import { applySpec , converge , curry , mergeLeft , nth , omit , pipe , prop , reduceBy } from 'ramda'
33
4- import { CanonicalEvent , Event } from '../@types/event'
5- import { EventId , Pubkey } from '../@types/base'
4+ import { CanonicalEvent , DBEvent , Event } from '../@types/event'
5+ import { EventId , Pubkey , Tag } from '../@types/base'
66import { EventKinds , EventTags } from '../constants/base'
77import { fromBuffer } from './transform'
88import { getLeadingZeroBits } from './proof-of-work'
99import { isGenericTagQuery } from './filter'
1010import { RuneLike } from './runes/rune-like'
1111import { SubscriptionFilter } from '../@types/subscription'
1212
13- export const serializeEvent = ( event : Partial < Event > ) : CanonicalEvent => [
13+ export const serializeEvent = ( event : Event ) : CanonicalEvent => [
1414 0 ,
1515 event . pubkey ,
1616 event . created_at ,
@@ -19,18 +19,18 @@ export const serializeEvent = (event: Partial<Event>): CanonicalEvent => [
1919 event . content ,
2020]
2121
22- export const toNostrEvent = applySpec ( {
23- id : pipe ( prop ( 'event_id' ) , fromBuffer ) ,
24- kind : prop ( 'event_kind' ) ,
25- pubkey : pipe ( prop ( 'event_pubkey' ) , fromBuffer ) ,
26- created_at : prop ( 'event_created_at' ) ,
27- content : prop ( 'event_content' ) ,
28- tags : prop ( 'event_tags' ) ,
29- sig : pipe ( prop ( 'event_signature' ) , fromBuffer ) ,
22+ export const toNostrEvent : ( event : DBEvent ) => Event = applySpec ( {
23+ id : pipe ( prop ( 'event_id' ) as ( ) => Buffer , fromBuffer ) ,
24+ kind : prop ( 'event_kind' ) as ( ) => number ,
25+ pubkey : pipe ( prop ( 'event_pubkey' ) as ( ) => Buffer , fromBuffer ) ,
26+ created_at : prop ( 'event_created_at' ) as ( ) => number ,
27+ content : prop ( 'event_content' ) as ( ) => string ,
28+ tags : prop ( 'event_tags' ) as ( ) => Tag [ ] ,
29+ sig : pipe ( prop ( 'event_signature' ) as ( ) => Buffer , fromBuffer ) ,
3030} )
3131
3232export const isEventMatchingFilter = ( filter : SubscriptionFilter ) => ( event : Event ) : boolean => {
33- const startsWith = ( input : string ) => ( prefix ) => input . startsWith ( prefix )
33+ const startsWith = ( input : string ) => ( prefix : string ) => input . startsWith ( prefix )
3434
3535 // NIP-01: Basic protocol flow description
3636
@@ -58,6 +58,9 @@ export const isEventMatchingFilter = (filter: SubscriptionFilter) => (event: Eve
5858 ) {
5959 if ( isDelegatedEvent ( event ) ) {
6060 const delegation = event . tags . find ( ( tag ) => tag [ 0 ] === EventTags . Delegation )
61+ if ( typeof delegation === 'undefined' ) {
62+ return false
63+ }
6164
6265 if ( ! filter . authors . some ( startsWith ( delegation [ 1 ] ) ) ) {
6366 return false
@@ -119,10 +122,10 @@ export const isDelegatedEventValid = async (event: Event): Promise<boolean> => {
119122 omit ( [ 'tags' ] ) ,
120123 pipe (
121124 prop ( 'tags' ) as any ,
122- reduceBy (
125+ reduceBy < EventTags , string [ ] > (
123126 ( acc , tag ) => ( [ ...acc , tag [ 1 ] ] ) ,
124127 [ ] ,
125- nth ( 0 ) ,
128+ nth ( 0 ) as any ,
126129 ) ,
127130 ) ,
128131 ] ,
0 commit comments