-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathevent.ts
More file actions
306 lines (242 loc) · 9.03 KB
/
Copy pathevent.ts
File metadata and controls
306 lines (242 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
import * as secp256k1 from '@noble/secp256k1'
import { ALL_RELAYS, EventKinds, EventTags } from '../constants/base'
import { applySpec, pipe, prop } from 'ramda'
import { CanonicalEvent, DBEvent, Event, UnidentifiedEvent, UnsignedEvent } from '../@types/event'
import { EventId, Pubkey, Tag } from '../@types/base'
import cluster from 'cluster'
import { deriveFromSecret } from './secret'
import { EventKindsRange } from '../@types/settings'
import { fromBuffer } from './transform'
import { getLeadingZeroBits } from './proof-of-work'
import { isGenericTagQuery, isGeohashPrefixCriterion, stripGeohashPrefixWildcard } from './filter'
import { SubscriptionFilter } from '../@types/subscription'
import { WebSocketServerAdapterEvent } from '../constants/adapter'
export const serializeEvent = (event: UnidentifiedEvent): CanonicalEvent => [
0,
event.pubkey,
event.created_at,
event.kind,
event.tags,
event.content,
]
export const toNostrEvent: (event: DBEvent) => Event = applySpec({
id: pipe(prop('event_id') as () => Buffer, fromBuffer),
kind: prop('event_kind') as () => number,
pubkey: pipe(prop('event_pubkey') as () => Buffer, fromBuffer),
created_at: prop('event_created_at') as () => number,
content: prop('event_content') as () => string,
tags: prop('event_tags') as () => Tag[],
sig: pipe(prop('event_signature') as () => Buffer, fromBuffer),
})
export const isEventKindOrRangeMatch =
({ kind }: Event) =>
(item: EventKinds | EventKindsRange) =>
typeof item === 'number' ? item === kind : kind >= item[0] && kind <= item[1]
export const isEventMatchingFilter =
(filter: SubscriptionFilter) =>
(event: Event): boolean => {
const startsWith = (input: string) => (prefix: string) => input.startsWith(prefix)
const isMatchingGenericTagCriterion = (key: string, criterion: string) => (tag: Tag): boolean => {
const [, tagName] = key
if (tag[0] !== tagName) {
return false
}
if (isGeohashPrefixCriterion(key, criterion)) {
return tag[1].startsWith(stripGeohashPrefixWildcard(criterion))
}
return tag[1] === criterion
}
// NIP-01: Basic protocol flow description
if (Array.isArray(filter.ids) && !filter.ids.some(startsWith(event.id))) {
return false
}
if (Array.isArray(filter.kinds) && !filter.kinds.includes(event.kind)) {
return false
}
if (typeof filter.since === 'number' && event.created_at < filter.since) {
return false
}
if (typeof filter.until === 'number' && event.created_at > filter.until) {
return false
}
if (Array.isArray(filter.authors)) {
if (!filter.authors.some(startsWith(event.pubkey))) {
return false
}
}
// NIP-27: Multicast
// const targetMulticastGroups: string[] = event.tags.reduce(
// (acc, tag) => (tag[0] === EventTags.Multicast)
// ? [...acc, tag[1]]
// : acc,
// [] as string[]
// )
// if (targetMulticastGroups.length && !Array.isArray(filter['#m'])) {
// return false
// }
// NIP-01: Support #e and #p tags
// NIP-12: Support generic tag queries
if (
Object.entries(filter)
.filter(([key, criteria]) => isGenericTagQuery(key) && Array.isArray(criteria))
.some(([key, criteria]) => {
return !event.tags.some((tag) => criteria.some((criterion) => isMatchingGenericTagCriterion(key, criterion)(tag)))
})
) {
return false
}
// NIP-50
if (typeof filter.search === 'string' && filter.search.length > 0) {
const contentLower = event.content.toLowerCase()
const terms = filter.search.toLowerCase().split(/\s+/).filter(Boolean)
if (terms.length === 0 || !terms.every((term) => contentLower.includes(term))) {
return false
}
}
return true
}
export const getEventHash = async (event: Event | UnidentifiedEvent | UnsignedEvent): Promise<string> => {
const id = await secp256k1.utils.sha256(Buffer.from(JSON.stringify(serializeEvent(event))))
return Buffer.from(id).toString('hex')
}
export const isEventIdValid = async (event: Event): Promise<boolean> => {
return event.id === (await getEventHash(event))
}
export const isEventSignatureValid = async (event: Event): Promise<boolean> => {
return secp256k1.schnorr.verify(event.sig, event.id, event.pubkey)
}
export const identifyEvent = async (event: UnidentifiedEvent): Promise<UnsignedEvent> => {
const id = await getEventHash(event)
return { ...event, id }
}
let privateKeyCache: string | undefined
export function getRelayPrivateKey(secret?: string): string {
if (privateKeyCache) {
return privateKeyCache
}
if (process.env.RELAY_PRIVATE_KEY) {
privateKeyCache = process.env.RELAY_PRIVATE_KEY
return privateKeyCache
}
privateKeyCache = deriveFromSecret(secret).toString('hex')
return privateKeyCache
}
const publicKeyCache: Record<string, string> = {}
export const getPublicKey = (privkey: string) => {
if (privkey in publicKeyCache) {
return publicKeyCache[privkey]
}
publicKeyCache[privkey] = secp256k1.utils.bytesToHex(secp256k1.getPublicKey(privkey, true).subarray(1))
return publicKeyCache[privkey]
}
export const signEvent =
(privkey: string | Buffer | undefined) =>
async (event: UnsignedEvent): Promise<Event> => {
const sig = await secp256k1.schnorr.sign(event.id, privkey as any)
return { ...event, sig: Buffer.from(sig).toString('hex') }
}
export const broadcastEvent = async (event: Event): Promise<Event> => {
return new Promise((resolve, reject) => {
if (!cluster.isWorker || typeof process.send === 'undefined') {
return resolve(event)
}
process.send(
{
eventName: WebSocketServerAdapterEvent.Broadcast,
event,
},
undefined,
undefined,
(error: Error | null) => {
if (error) {
return reject(error)
}
resolve(event)
},
)
})
}
export const isReplaceableEvent = (event: Event): boolean => {
return (
event.kind === EventKinds.SET_METADATA ||
event.kind === EventKinds.CONTACT_LIST ||
event.kind === EventKinds.CHANNEL_METADATA ||
(event.kind >= EventKinds.REPLACEABLE_FIRST && event.kind <= EventKinds.REPLACEABLE_LAST)
)
}
export const isEphemeralEvent = (event: Event): boolean => {
return event.kind >= EventKinds.EPHEMERAL_FIRST && event.kind <= EventKinds.EPHEMERAL_LAST
}
export const isParameterizedReplaceableEvent = (event: Event): boolean => {
return (
event.kind >= EventKinds.PARAMETERIZED_REPLACEABLE_FIRST && event.kind <= EventKinds.PARAMETERIZED_REPLACEABLE_LAST
)
}
export const isDeleteEvent = (event: Event): boolean => {
return event.kind === EventKinds.DELETE
}
export const isRequestToVanishEvent = (event: Event, relayUrl?: string): boolean => {
if (event.kind !== EventKinds.REQUEST_TO_VANISH) {
return false
}
if (typeof relayUrl === 'undefined') {
return true
}
const relayTags = event.tags.filter((tag) => tag.length >= 2 && tag[0] === EventTags.Relay).map((tag) => tag[1])
return relayTags.length > 0 && relayTags.every((relay) => relay === relayUrl || relay === ALL_RELAYS)
}
export const isExpiredEvent = (event: Event): boolean => {
if (!event.tags.length) {
return false
}
const expirationTime = getEventExpiration(event)
if (!expirationTime) {
return false
}
const now = Math.floor(new Date().getTime() / 1000)
return expirationTime <= now
}
export const getEventExpiration = (event: Event): number | undefined => {
const [, rawExpirationTime] = event.tags.find((tag) => tag.length >= 2 && tag[0] === EventTags.Expiration) ?? []
if (!rawExpirationTime) {
return
}
const expirationTime = Number(rawExpirationTime)
if (Number.isSafeInteger(expirationTime) && Math.log10(expirationTime) < 10) {
return expirationTime
}
}
export const getEventProofOfWork = (eventId: EventId): number => {
return getLeadingZeroBits(Buffer.from(eventId, 'hex'))
}
export const getPubkeyProofOfWork = (pubkey: Pubkey): number => {
return getLeadingZeroBits(Buffer.from(pubkey, 'hex'))
}
// NIP-17: Private Direct Messages helpers
export const isGiftWrapEvent = (event: Event): boolean => {
return event.kind === EventKinds.GIFT_WRAP
}
export const isSealEvent = (event: Event): boolean => {
return event.kind === EventKinds.SEAL
}
export const isDirectMessageEvent = (event: Event): boolean => {
return event.kind === EventKinds.DIRECT_MESSAGE
}
export const isFileMessageEvent = (event: Event): boolean => {
return event.kind === EventKinds.FILE_MESSAGE
}
// NIP-03: OpenTimestamps attestation
export const isOpenTimestampsEvent = (event: Event): boolean => {
return event.kind === EventKinds.OPEN_TIMESTAMPS
}
// Marmot Protocol helpers
export const isWelcomeRumorEvent = (event: Event): boolean => {
return event.kind === EventKinds.MARMOT_WELCOME_RUMOR
}
export const isMarmotGroupEvent = (event: Event): boolean => {
return event.kind === EventKinds.MARMOT_GROUP_EVENT
}
// NIP-70: Protected Events
export const isProtectedEvent = (event: Event): boolean => {
return event.tags.some((tag) => tag[0] === EventTags.Protected)
}