MCPcopy Create free account
hub / github.com/cameri/nostream / canAcceptEvent

Method canAcceptEvent

src/handlers/event-message-handler.ts:136–225  ·  view source on GitHub ↗
(event: Event)

Source from the content-addressed store, hash-verified

134 }
135
136 protected canAcceptEvent(event: Event): string | undefined {
137 if (this.getRelayPublicKey() === event.pubkey) {
138 return
139 }
140 const now = Math.floor(Date.now() / 1000)
141
142 const limits = this.settings().limits?.event ?? {}
143
144 if (Array.isArray(limits.content)) {
145 for (const limit of limits.content) {
146 if (
147 typeof limit.maxLength !== 'undefined' &&
148 limit.maxLength > 0 &&
149 event.content.length > limit.maxLength &&
150 (!Array.isArray(limit.kinds) || limit.kinds.some(isEventKindOrRangeMatch(event)))
151 ) {
152 return `rejected: content is longer than ${limit.maxLength} bytes`
153 }
154 }
155 } else if (
156 typeof limits.content?.maxLength !== 'undefined' &&
157 limits.content?.maxLength > 0 &&
158 event.content.length > limits.content.maxLength &&
159 (!Array.isArray(limits.content.kinds) || limits.content.kinds.some(isEventKindOrRangeMatch(event)))
160 ) {
161 return `rejected: content is longer than ${limits.content.maxLength} bytes`
162 }
163
164 if (
165 typeof limits.createdAt?.maxPositiveDelta !== 'undefined' &&
166 limits.createdAt.maxPositiveDelta > 0 &&
167 event.created_at > now + limits.createdAt.maxPositiveDelta
168 ) {
169 return `rejected: created_at is more than ${limits.createdAt.maxPositiveDelta} seconds in the future`
170 }
171
172 if (
173 typeof limits.createdAt?.maxNegativeDelta !== 'undefined' &&
174 limits.createdAt.maxNegativeDelta > 0 &&
175 event.created_at < now - limits.createdAt.maxNegativeDelta
176 ) {
177 return `rejected: created_at is more than ${limits.createdAt.maxNegativeDelta} seconds in the past`
178 }
179
180 if (typeof limits.eventId?.minLeadingZeroBits !== 'undefined' && limits.eventId.minLeadingZeroBits > 0) {
181 const pow = getEventProofOfWork(event.id)
182 if (pow < limits.eventId.minLeadingZeroBits) {
183 return `pow: difficulty ${pow}<${limits.eventId.minLeadingZeroBits}`
184 }
185 }
186
187 if (typeof limits.pubkey?.minLeadingZeroBits !== 'undefined' && limits.pubkey.minLeadingZeroBits > 0) {
188 const pow = getPubkeyProofOfWork(event.pubkey)
189 if (pow < limits.pubkey.minLeadingZeroBits) {
190 return `pow: pubkey difficulty ${pow}<${limits.pubkey.minLeadingZeroBits}`
191 }
192 }
193

Callers 1

handleMessageMethod · 0.95

Calls 4

getRelayPublicKeyMethod · 0.95
isEventKindOrRangeMatchFunction · 0.90
getEventProofOfWorkFunction · 0.90
getPubkeyProofOfWorkFunction · 0.90

Tested by

no test coverage detected