libp2r2p 0.12.13 → 0.12.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +39 -8
- package/package.json +1 -1
- package/private-messenger/index.js +218 -35
package/README.md
CHANGED
|
@@ -370,14 +370,29 @@ where `key` is a lowercase 64-character SHA-256 digest, `keep` is `latest`
|
|
|
370
370
|
Sequence units are resolver-owned and are compared only within records sharing
|
|
371
371
|
the final scoped coalescing key.
|
|
372
372
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
373
|
+
Every inbox type first attempts exact deduplication by channel, type, event ID
|
|
374
|
+
and provenance, then a valid resolver key, then the automatic `D` fallback for
|
|
375
|
+
ask/reply/tell/yell. When none supplies an identity, ask, reply, tell, yell,
|
|
376
|
+
message and nym fall back to Nostr identity within the same channel and type:
|
|
377
|
+
the first usable `d` tag selects an address, replaceable/addressable events
|
|
378
|
+
without a usable `d` use `""`, and regular or ephemeral events use `event.id`.
|
|
379
|
+
Ephemeral events never use an address. An explicit `d` is not merged with a
|
|
380
|
+
regular event that has no address.
|
|
381
|
+
|
|
382
|
+
Resolver and `D` keys retain channel, sender, type, code and provenance scope.
|
|
383
|
+
Automatic keys omit sender, provenance and code. For copies with the same event
|
|
384
|
+
ID, provenance orders `signed > direct > hearsay`. Different versions of an
|
|
385
|
+
address follow NIP-01: higher inner `created_at`, then lower event ID on a tie;
|
|
386
|
+
`signed` and `direct` compare as legitimate versions, while hearsay never
|
|
387
|
+
displaces either. Address ordering ignores outer time and the `ms` tag.
|
|
388
|
+
|
|
389
|
+
Resolver/idempotency `latest` replaces the pending item in place. Its order is:
|
|
390
|
+
explicit sequence; `eventCreatedAtMs`, read from the inner `created_at` plus the
|
|
391
|
+
`ms` tag; outer `created_at`; event ID. `oldest` preserves the pending item.
|
|
392
|
+
While an item is reserved, all policies allow at most one successor. Ordering
|
|
393
|
+
metadata persists internally, is removed before app delivery, and older pending
|
|
394
|
+
records remain valid until drained. Older recovered events do not replace newer
|
|
395
|
+
requests.
|
|
381
396
|
|
|
382
397
|
New high-level ask, reply, tell and yell rumors include exactly one
|
|
383
398
|
`["ms", "0".."999"]` tag and use the same instant to derive inner `created_at`.
|
|
@@ -400,6 +415,22 @@ Coalescing does not provide an ingress rate limit or per-peer quota. Exact
|
|
|
400
415
|
event deduplication, authorization, quarantine, global inbox capacity,
|
|
401
416
|
pause/ACK/NACK ownership and existing storage policy remain independent.
|
|
402
417
|
|
|
418
|
+
### Inbound peer admission
|
|
419
|
+
|
|
420
|
+
`PrivateMessenger` accepts an optional synchronous
|
|
421
|
+
`admitInboundPeer(message, context)` callback. It runs after decryption and
|
|
422
|
+
lifecycle gates, but before typed handlers, automatic replies, seed ingestion
|
|
423
|
+
and inbox admission. The context contains `type`, `channelPubkey`,
|
|
424
|
+
`senderPubkey`, `code`, `bytes` and `now`.
|
|
425
|
+
|
|
426
|
+
Returning `false` drops that delivery locally without an operational error or
|
|
427
|
+
NACK. A thrown error, Promise or non-boolean result fails open: the message is
|
|
428
|
+
admitted and the error is reported. The hook is disabled by default and does
|
|
429
|
+
not coalesce, authorize or persist limits itself.
|
|
430
|
+
|
|
431
|
+
Direct ask/reply/tell/yell events invoke their typed callback once and do not
|
|
432
|
+
invoke `onMessage` again for admission purposes.
|
|
433
|
+
|
|
403
434
|
### Deleting Private Broadcasts
|
|
404
435
|
|
|
405
436
|
By default, each high-level private-message send creates one fresh deletion
|
package/package.json
CHANGED
|
@@ -70,6 +70,7 @@ import { getRelaysByPubkey, pickRelaysForPubkeys, subscribeRelayListUpdates, rel
|
|
|
70
70
|
import * as privateChannel from '../private-channel/index.js'
|
|
71
71
|
import { DEFAULT_RECEIVED_CHUNK_TTL_MS } from '../private-channel/services/received-chunks.js'
|
|
72
72
|
import { createQueue } from '../idb-queue/index.js'
|
|
73
|
+
import { classifyEvent, isEphemeralEvent } from '../event/index.js'
|
|
73
74
|
import { createChannelStateStore } from './services/channel-state.js'
|
|
74
75
|
import { normalizeIdempotencyKey, readIdempotencyKey } from '../private-message/helpers/idempotency.js'
|
|
75
76
|
import { eventCreatedAtMs, readMilliseconds } from '../private-message/helpers/timestamp.js'
|
|
@@ -131,6 +132,11 @@ const COALESCING_INDEX = 'byCoalescingKey'
|
|
|
131
132
|
const COALESCING_DIGEST = /^[0-9a-f]{64}$/
|
|
132
133
|
const COALESCING_KEEP = new Set(['oldest', 'latest'])
|
|
133
134
|
const DEDUPE_TYPES = new Set(['ask', 'reply', 'tell', 'yell'])
|
|
135
|
+
const PROVENANCE_PRIORITY = {
|
|
136
|
+
signed: 2,
|
|
137
|
+
direct: 1,
|
|
138
|
+
hearsay: 0
|
|
139
|
+
}
|
|
134
140
|
const TIMESTAMPED_PRIVATE_MESSAGE_KINDS = new Set([
|
|
135
141
|
privateMessage.ASK_KIND,
|
|
136
142
|
privateMessage.REPLY_KIND,
|
|
@@ -318,6 +324,7 @@ export class PrivateMessenger {
|
|
|
318
324
|
onDebug,
|
|
319
325
|
onError = defaultOnError,
|
|
320
326
|
resolveInboundCoalescing,
|
|
327
|
+
admitInboundPeer,
|
|
321
328
|
_privateMessage = privateMessage,
|
|
322
329
|
_privateChannel = privateChannel,
|
|
323
330
|
_getRelaysByPubkey = getRelaysByPubkey,
|
|
@@ -342,6 +349,9 @@ export class PrivateMessenger {
|
|
|
342
349
|
if (resolveInboundCoalescing != null && typeof resolveInboundCoalescing !== 'function') {
|
|
343
350
|
throw new ValidationError('INVALID_RESOLVE_INBOUND_COALESCING')
|
|
344
351
|
}
|
|
352
|
+
if (admitInboundPeer != null && typeof admitInboundPeer !== 'function') {
|
|
353
|
+
throw new ValidationError('INVALID_ADMIT_INBOUND_PEER')
|
|
354
|
+
}
|
|
345
355
|
this.fallbackRelays = normalizeFallbackRelays(fallbackRelays)
|
|
346
356
|
this.fallbackDelayMs = normalizeFallbackDelay(fallbackDelayMs)
|
|
347
357
|
this.offlineRecoverySeconds = normalizeOfflineRecoverySeconds(offlineRecoverySeconds)
|
|
@@ -387,6 +397,7 @@ export class PrivateMessenger {
|
|
|
387
397
|
this.onMessageQueued = onMessageQueued
|
|
388
398
|
this.onDebug = onDebug
|
|
389
399
|
this.resolveInboundCoalescing = resolveInboundCoalescing
|
|
400
|
+
this.admitInboundPeer = admitInboundPeer
|
|
390
401
|
this._privateMessage = _privateMessage.createPrivateMessageSession?.() ?? _privateMessage
|
|
391
402
|
this._privateChannel = _privateChannel
|
|
392
403
|
this._getRelaysByPubkey = _getRelaysByPubkey
|
|
@@ -1332,13 +1343,18 @@ export class PrivateMessenger {
|
|
|
1332
1343
|
privateChannelReaderSigner: channel.readerSigner,
|
|
1333
1344
|
privateChannelReaderPubkey: channel.readerPubkey,
|
|
1334
1345
|
mode: channel.mode,
|
|
1335
|
-
onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message),
|
|
1336
|
-
onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message),
|
|
1337
|
-
onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message),
|
|
1338
|
-
onYell: message => this.receive(pubkey, () => this.handleYell(pubkey, message), message),
|
|
1339
|
-
onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message),
|
|
1340
|
-
onMessage: message =>
|
|
1341
|
-
|
|
1346
|
+
onAsk: message => this.receive(pubkey, () => this.handleAsk(pubkey, message), message, { inboundType: 'ask' }),
|
|
1347
|
+
onReply: message => this.receive(pubkey, () => this.handleReply(pubkey, message), message, { inboundType: 'reply' }),
|
|
1348
|
+
onTell: message => this.receive(pubkey, () => this.handleTell(pubkey, message), message, { inboundType: 'tell' }),
|
|
1349
|
+
onYell: message => this.receive(pubkey, () => this.handleYell(pubkey, message), message, { inboundType: 'yell' }),
|
|
1350
|
+
onNym: message => this.receive(pubkey, () => this.handleNym(pubkey, message), message, { inboundType: 'nym' }),
|
|
1351
|
+
onMessage: message => {
|
|
1352
|
+
const type = eventType(message.event)
|
|
1353
|
+
if (type !== 'message' && message.provenance !== 'hearsay' &&
|
|
1354
|
+
(!message.senderPubkey || message.senderPubkey === message.event.pubkey)) return Promise.resolve()
|
|
1355
|
+
return this.receive(pubkey, () => this.handleMessage(pubkey, message), message, { inboundType: 'message' })
|
|
1356
|
+
},
|
|
1357
|
+
onSeed: seed => this.receive(pubkey, () => this.enqueueSeed(pubkey, seed), seed, { inboundType: 'seed' }),
|
|
1342
1358
|
onContentKeyUsage: usage => this.handleContentKeyUsage(pubkey, usage),
|
|
1343
1359
|
onDecryptionFailure: failure => this.deferDecryption(failure),
|
|
1344
1360
|
_onProcessing: pubkey => this.decryptionScheduler.activity(pubkey),
|
|
@@ -2102,7 +2118,44 @@ export class PrivateMessenger {
|
|
|
2102
2118
|
return jobs
|
|
2103
2119
|
}
|
|
2104
2120
|
|
|
2105
|
-
|
|
2121
|
+
#admitInboundPeer (pubkey, inboundType, message) {
|
|
2122
|
+
if (!this.admitInboundPeer) return true
|
|
2123
|
+
const type = inboundType || 'message'
|
|
2124
|
+
const context = {
|
|
2125
|
+
type,
|
|
2126
|
+
channelPubkey: pubkey,
|
|
2127
|
+
senderPubkey: inboundSenderPubkey(message),
|
|
2128
|
+
code: messageCode(message) || message?.recordType || '',
|
|
2129
|
+
bytes: inboundMessageBytes(message),
|
|
2130
|
+
now: this._now()
|
|
2131
|
+
}
|
|
2132
|
+
let admitted
|
|
2133
|
+
try {
|
|
2134
|
+
admitted = this.admitInboundPeer(
|
|
2135
|
+
{ ...message, type, channelPubkey: pubkey },
|
|
2136
|
+
context
|
|
2137
|
+
)
|
|
2138
|
+
if (admitted != null && typeof admitted.then === 'function') {
|
|
2139
|
+
throw new Error('INBOUND_PEER_ADMISSION_MUST_BE_SYNCHRONOUS')
|
|
2140
|
+
}
|
|
2141
|
+
} catch (cause) {
|
|
2142
|
+
this.onError?.(inboundPeerAdmissionError(cause))
|
|
2143
|
+
return true
|
|
2144
|
+
}
|
|
2145
|
+
if (admitted === false) {
|
|
2146
|
+
this.debug('inbound-peer-dropped', {
|
|
2147
|
+
type,
|
|
2148
|
+
code: context.code
|
|
2149
|
+
})
|
|
2150
|
+
return false
|
|
2151
|
+
}
|
|
2152
|
+
if (admitted !== true) {
|
|
2153
|
+
this.onError?.(inboundPeerAdmissionError(new ValidationError('INVALID_INBOUND_PEER_ADMISSION')))
|
|
2154
|
+
}
|
|
2155
|
+
return true
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
receive (pubkey, operation, message = {}, { trackActivity = true, inboundType = 'message' } = {}) {
|
|
2106
2159
|
if (this.closePromise) {
|
|
2107
2160
|
if (this.storageActive) this.recordInterruption([pubkey], messageTime(message))
|
|
2108
2161
|
return Promise.reject(new Error('PRIVATE_MESSENGER_CLOSED'))
|
|
@@ -2113,6 +2166,7 @@ export class PrivateMessenger {
|
|
|
2113
2166
|
this.recordInterruption([pubkey], messageTime(message))
|
|
2114
2167
|
throw this.pausedError()
|
|
2115
2168
|
}
|
|
2169
|
+
if (!this.#admitInboundPeer(pubkey, inboundType, message)) return
|
|
2116
2170
|
return operation()
|
|
2117
2171
|
}).finally(releaseActivity)
|
|
2118
2172
|
}
|
|
@@ -2261,6 +2315,7 @@ export class PrivateMessenger {
|
|
|
2261
2315
|
let digest = null
|
|
2262
2316
|
let keep = 'latest'
|
|
2263
2317
|
let sequence = null
|
|
2318
|
+
let policy = 'semantic'
|
|
2264
2319
|
if (resolved != null) {
|
|
2265
2320
|
if (isPlainObject(resolved) && typeof resolved.key === 'string' &&
|
|
2266
2321
|
COALESCING_DIGEST.test(resolved.key) &&
|
|
@@ -2274,27 +2329,37 @@ export class PrivateMessenger {
|
|
|
2274
2329
|
this.onError?.(coalescingError(new ValidationError('INVALID_INBOUND_COALESCING_RESULT')))
|
|
2275
2330
|
}
|
|
2276
2331
|
}
|
|
2277
|
-
if (!digest && tagged.key)
|
|
2278
|
-
|
|
2332
|
+
if (!digest && tagged.key) {
|
|
2333
|
+
digest = idempotencyDigest(tagged.key)
|
|
2334
|
+
policy = 'idempotency'
|
|
2335
|
+
}
|
|
2279
2336
|
|
|
2280
2337
|
const senderPubkey = info.senderPubkey || message.event?.pubkey || ''
|
|
2281
2338
|
const code = messageCode(message)
|
|
2282
|
-
if (!channelPubkey || !senderPubkey || typeof code !== 'string' || !info.provenance) {
|
|
2339
|
+
if (digest && (!channelPubkey || !senderPubkey || typeof code !== 'string' || !info.provenance)) {
|
|
2283
2340
|
this.onError?.(coalescingError(new ValidationError('INVALID_INBOUND_COALESCING_SCOPE')))
|
|
2284
|
-
|
|
2341
|
+
digest = null
|
|
2285
2342
|
}
|
|
2286
|
-
|
|
2287
|
-
|
|
2288
|
-
|
|
2289
|
-
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
|
|
2293
|
-
|
|
2294
|
-
|
|
2295
|
-
|
|
2296
|
-
|
|
2343
|
+
if (digest) {
|
|
2344
|
+
return {
|
|
2345
|
+
coalescingKey: JSON.stringify([
|
|
2346
|
+
channelPubkey,
|
|
2347
|
+
senderPubkey,
|
|
2348
|
+
type,
|
|
2349
|
+
code,
|
|
2350
|
+
info.provenance,
|
|
2351
|
+
digest
|
|
2352
|
+
]),
|
|
2353
|
+
coalescingKeep: keep,
|
|
2354
|
+
coalescingOrder: coalescingOrder(message, {
|
|
2355
|
+
policy,
|
|
2356
|
+
sequence,
|
|
2357
|
+
eventCreatedAtMs: messageCreatedAtMs
|
|
2358
|
+
})
|
|
2359
|
+
}
|
|
2297
2360
|
}
|
|
2361
|
+
|
|
2362
|
+
return automaticCoalescingMetadata(type, channelPubkey, message, info)
|
|
2298
2363
|
}
|
|
2299
2364
|
|
|
2300
2365
|
async enqueueRumor (type, channelPubkey, message) {
|
|
@@ -2302,7 +2367,7 @@ export class PrivateMessenger {
|
|
|
2302
2367
|
if (channel?.mode === 'watchtower' && type !== 'ask') return
|
|
2303
2368
|
const info = deliveryInfo(message.event, message.senderPubkey ?? message.meta?.senderPubkey ?? message.meta?.router?.tags?.find(tag => tag[0] === 'f')?.[1])
|
|
2304
2369
|
const eventId = message.event?.id || ''
|
|
2305
|
-
//
|
|
2370
|
+
// Exact copies are dropped; stronger provenance participates in coalescing below.
|
|
2306
2371
|
const dedupeKey = eventId ? [channelPubkey, type, eventId, info.provenance] : null
|
|
2307
2372
|
if (dedupeKey && await this.queue.someBy('byChannelTypeEventId', dedupeKey)) {
|
|
2308
2373
|
this.markSeen(channelPubkey, message.outer?.created_at || message.event?.created_at || nowSeconds())
|
|
@@ -2327,8 +2392,9 @@ export class PrivateMessenger {
|
|
|
2327
2392
|
meta: message.meta || null
|
|
2328
2393
|
}
|
|
2329
2394
|
Object.assign(record, this.#coalescingMetadata(type, channelPubkey, message, info) || {})
|
|
2395
|
+
let coalesced = null
|
|
2330
2396
|
try {
|
|
2331
|
-
|
|
2397
|
+
coalesced = record.coalescingKey
|
|
2332
2398
|
? await this.queue.coalesceBy(COALESCING_INDEX, record, {
|
|
2333
2399
|
compare: compareCoalescingItems,
|
|
2334
2400
|
keep: record.coalescingKeep
|
|
@@ -2339,6 +2405,7 @@ export class PrivateMessenger {
|
|
|
2339
2405
|
this.markSeen(channelPubkey, message.outer?.created_at || message.event?.created_at || nowSeconds())
|
|
2340
2406
|
this.debug('message-coalescing-ignored', {
|
|
2341
2407
|
code: messageCode(message) || '',
|
|
2408
|
+
policy: record.coalescingOrder?.policy || 'legacy',
|
|
2342
2409
|
keep: record.coalescingKeep,
|
|
2343
2410
|
payloadBytes: encoder.encode(JSON.stringify(record)).byteLength
|
|
2344
2411
|
})
|
|
@@ -2347,6 +2414,7 @@ export class PrivateMessenger {
|
|
|
2347
2414
|
if (coalesced) {
|
|
2348
2415
|
this.debug('message-coalesced', {
|
|
2349
2416
|
code: messageCode(message) || '',
|
|
2417
|
+
policy: record.coalescingOrder?.policy || 'legacy',
|
|
2350
2418
|
keep: record.coalescingKeep,
|
|
2351
2419
|
status: coalesced.status,
|
|
2352
2420
|
removedUnreserved: coalesced.removed,
|
|
@@ -2386,7 +2454,9 @@ export class PrivateMessenger {
|
|
|
2386
2454
|
}
|
|
2387
2455
|
this.markSeen(channelPubkey, message.outer?.created_at || message.event?.created_at || nowSeconds())
|
|
2388
2456
|
this.wakeDeliveries()
|
|
2389
|
-
if (!record.coalescingKey
|
|
2457
|
+
if (!record.coalescingKey || coalesced?.status === 'inserted') {
|
|
2458
|
+
this.debug('enqueue', debugMessageInfo(type, channelPubkey, message))
|
|
2459
|
+
}
|
|
2390
2460
|
this.onMessageQueued?.()
|
|
2391
2461
|
}
|
|
2392
2462
|
|
|
@@ -3373,9 +3443,9 @@ export class PrivateMessenger {
|
|
|
3373
3443
|
modeByPubkey: { [channelPubkey]: channel.mode },
|
|
3374
3444
|
receivedChunkTtlMs: this.receivedChunkTtlMsFor(channel),
|
|
3375
3445
|
receivedChunkIndexedDB: this._indexedDB,
|
|
3376
|
-
onEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }),
|
|
3377
|
-
onNymEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }),
|
|
3378
|
-
onSeedEvent: seed => this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed),
|
|
3446
|
+
onEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { inboundType: eventType(event) }),
|
|
3447
|
+
onNymEvent: (event, outer, meta) => this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { inboundType: 'nym' }),
|
|
3448
|
+
onSeedEvent: seed => this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed, { inboundType: 'seed' }),
|
|
3379
3449
|
onContentKeyUsage: usage => this.handleContentKeyUsage(channelPubkey, usage),
|
|
3380
3450
|
onDecryptionFailure: failure => this.deferDecryption(failure),
|
|
3381
3451
|
_onProcessing: pubkey => this.decryptionScheduler.activity(pubkey),
|
|
@@ -3731,13 +3801,13 @@ export class PrivateMessenger {
|
|
|
3731
3801
|
},
|
|
3732
3802
|
onEvent: (event, outer, meta) => {
|
|
3733
3803
|
current()
|
|
3734
|
-
return this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false })
|
|
3804
|
+
return this.receive(channelPubkey, () => this.enqueueRumor(eventType(event), channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false, inboundType: eventType(event) })
|
|
3735
3805
|
},
|
|
3736
3806
|
onNymEvent: (event, outer, meta) => {
|
|
3737
3807
|
current()
|
|
3738
|
-
return this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false })
|
|
3808
|
+
return this.receive(channelPubkey, () => this.enqueueRumor('nym', channelPubkey, { event, outer, meta, payload: parseEventContent(event) }), { event, outer }, { trackActivity: false, inboundType: 'nym' })
|
|
3739
3809
|
},
|
|
3740
|
-
onSeedEvent: seed => { current(); return this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed, { trackActivity: false }) },
|
|
3810
|
+
onSeedEvent: seed => { current(); return this.receive(channelPubkey, () => this.enqueueSeed(channelPubkey, seed), seed, { trackActivity: false, inboundType: 'seed' }) },
|
|
3741
3811
|
onRecovered: async record => {
|
|
3742
3812
|
current()
|
|
3743
3813
|
await callbacks.onRecovered?.(record)
|
|
@@ -3965,9 +4035,65 @@ function messageCode (message) {
|
|
|
3965
4035
|
: null
|
|
3966
4036
|
}
|
|
3967
4037
|
|
|
3968
|
-
function
|
|
4038
|
+
function automaticCoalescingMetadata (type, channelPubkey, message, info) {
|
|
4039
|
+
const identity = automaticEventIdentity(message.event)
|
|
4040
|
+
if (!identity || !channelPubkey || !type || !info.provenance) return null
|
|
4041
|
+
const eventIdPriority = PROVENANCE_PRIORITY[info.provenance] ?? 0
|
|
4042
|
+
return {
|
|
4043
|
+
coalescingKey: JSON.stringify([
|
|
4044
|
+
channelPubkey,
|
|
4045
|
+
type,
|
|
4046
|
+
'nostr-identity',
|
|
4047
|
+
identity.kind,
|
|
4048
|
+
identity.digest
|
|
4049
|
+
]),
|
|
4050
|
+
coalescingKeep: 'latest',
|
|
4051
|
+
coalescingOrder: identity.kind === 'address'
|
|
4052
|
+
? {
|
|
4053
|
+
policy: 'event-address',
|
|
4054
|
+
eventIdPriority,
|
|
4055
|
+
inner: Number.isSafeInteger(message.event?.created_at) ? message.event.created_at : 0,
|
|
4056
|
+
eventId: message.event.id
|
|
4057
|
+
}
|
|
4058
|
+
: {
|
|
4059
|
+
policy: 'event-id',
|
|
4060
|
+
eventIdPriority
|
|
4061
|
+
}
|
|
4062
|
+
}
|
|
4063
|
+
}
|
|
4064
|
+
|
|
4065
|
+
function automaticEventIdentity (event) {
|
|
4066
|
+
if (!event || typeof event.id !== 'string' || !event.id) return null
|
|
4067
|
+
if (!isEphemeralEvent(event)) {
|
|
4068
|
+
const firstD = event.tags?.find(tag => Array.isArray(tag) && tag[0] === 'd')
|
|
4069
|
+
let coordinate = null
|
|
4070
|
+
if (typeof firstD?.[1] === 'string') {
|
|
4071
|
+
coordinate = firstD[1]
|
|
4072
|
+
} else {
|
|
4073
|
+
const classes = classifyEvent(event)
|
|
4074
|
+
if (classes.includes('replaceable') || classes.includes('addressable')) coordinate = ''
|
|
4075
|
+
}
|
|
4076
|
+
if (coordinate !== null && Number.isSafeInteger(event.kind) && event.kind >= 0 && typeof event.pubkey === 'string') {
|
|
4077
|
+
return {
|
|
4078
|
+
kind: 'address',
|
|
4079
|
+
digest: coalescingDigest(['nostr-address:v1', event.kind, event.pubkey, coordinate])
|
|
4080
|
+
}
|
|
4081
|
+
}
|
|
4082
|
+
}
|
|
4083
|
+
return {
|
|
4084
|
+
kind: 'event-id',
|
|
4085
|
+
digest: coalescingDigest(['nostr-event-id:v1', event.id])
|
|
4086
|
+
}
|
|
4087
|
+
}
|
|
4088
|
+
|
|
4089
|
+
function coalescingOrder (message, {
|
|
4090
|
+
policy = 'semantic',
|
|
4091
|
+
sequence = null,
|
|
4092
|
+
eventCreatedAtMs = 0
|
|
4093
|
+
} = {}) {
|
|
3969
4094
|
const order = {
|
|
3970
|
-
|
|
4095
|
+
policy,
|
|
4096
|
+
eventTimeMs: eventCreatedAtMs,
|
|
3971
4097
|
outer: Number.isSafeInteger(message?.outer?.created_at) ? message.outer.created_at : 0,
|
|
3972
4098
|
inner: Number.isSafeInteger(message?.event?.created_at) ? message.event.created_at : 0,
|
|
3973
4099
|
eventId: message?.event?.id || ''
|
|
@@ -3977,6 +4103,34 @@ function coalescingOrder (message, sequence = null, messageCreatedAtMs = 0) {
|
|
|
3977
4103
|
}
|
|
3978
4104
|
|
|
3979
4105
|
function compareCoalescingOrder (left, right) {
|
|
4106
|
+
if (left?.policy === 'event-id' || right?.policy === 'event-id') {
|
|
4107
|
+
return compareEventIdCoalescingOrder(left, right)
|
|
4108
|
+
}
|
|
4109
|
+
if (left?.policy === 'event-address' || right?.policy === 'event-address') {
|
|
4110
|
+
return compareAddressCoalescingOrder(left, right)
|
|
4111
|
+
}
|
|
4112
|
+
return compareLegacyCoalescingOrder(left, right)
|
|
4113
|
+
}
|
|
4114
|
+
|
|
4115
|
+
function compareEventIdCoalescingOrder (left, right) {
|
|
4116
|
+
return (left?.eventIdPriority || 0) - (right?.eventIdPriority || 0)
|
|
4117
|
+
}
|
|
4118
|
+
|
|
4119
|
+
function compareAddressCoalescingOrder (left, right) {
|
|
4120
|
+
const leftId = left?.eventId || ''
|
|
4121
|
+
const rightId = right?.eventId || ''
|
|
4122
|
+
if (leftId === rightId) {
|
|
4123
|
+
return (left?.eventIdPriority || 0) - (right?.eventIdPriority || 0)
|
|
4124
|
+
}
|
|
4125
|
+
const trust = ((left?.eventIdPriority || 0) > 0 ? 1 : 0) - ((right?.eventIdPriority || 0) > 0 ? 1 : 0)
|
|
4126
|
+
if (trust) return trust
|
|
4127
|
+
const leftTime = Number.isSafeInteger(left?.inner) ? left.inner : 0
|
|
4128
|
+
const rightTime = Number.isSafeInteger(right?.inner) ? right.inner : 0
|
|
4129
|
+
if (leftTime !== rightTime) return leftTime < rightTime ? -1 : 1
|
|
4130
|
+
return leftId < rightId ? 1 : -1
|
|
4131
|
+
}
|
|
4132
|
+
|
|
4133
|
+
function compareLegacyCoalescingOrder (left, right) {
|
|
3980
4134
|
if (Number.isSafeInteger(left?.sequence) && Number.isSafeInteger(right?.sequence) &&
|
|
3981
4135
|
left.sequence !== right.sequence) {
|
|
3982
4136
|
return left.sequence < right.sequence ? -1 : 1
|
|
@@ -4000,7 +4154,11 @@ function compareCoalescingItems (left, right) {
|
|
|
4000
4154
|
}
|
|
4001
4155
|
|
|
4002
4156
|
function idempotencyDigest (idempotencyKey) {
|
|
4003
|
-
return
|
|
4157
|
+
return coalescingDigest(['idempotency', idempotencyKey])
|
|
4158
|
+
}
|
|
4159
|
+
|
|
4160
|
+
function coalescingDigest (value) {
|
|
4161
|
+
return bytesToHex(sha256(encoder.encode(JSON.stringify(value))))
|
|
4004
4162
|
}
|
|
4005
4163
|
|
|
4006
4164
|
function coalescingError (cause) {
|
|
@@ -4058,6 +4216,31 @@ function messageTime (message) {
|
|
|
4058
4216
|
return message.outer?.created_at || message.event?.created_at || nowSeconds()
|
|
4059
4217
|
}
|
|
4060
4218
|
|
|
4219
|
+
function inboundSenderPubkey (message) {
|
|
4220
|
+
return message?.senderPubkey ||
|
|
4221
|
+
message?.event?.pubkey ||
|
|
4222
|
+
message?.meta?.senderPubkey ||
|
|
4223
|
+
message?.router?.tags?.find(tag => tag[0] === 'f')?.[1] ||
|
|
4224
|
+
''
|
|
4225
|
+
}
|
|
4226
|
+
|
|
4227
|
+
function inboundMessageBytes (message) {
|
|
4228
|
+
const value = message?.event
|
|
4229
|
+
? message.event
|
|
4230
|
+
: message?.recordType ? message : message?.router || message
|
|
4231
|
+
try {
|
|
4232
|
+
return encoder.encode(JSON.stringify(value)).byteLength
|
|
4233
|
+
} catch {
|
|
4234
|
+
return 0
|
|
4235
|
+
}
|
|
4236
|
+
}
|
|
4237
|
+
|
|
4238
|
+
function inboundPeerAdmissionError (cause) {
|
|
4239
|
+
return Object.assign(new Error(cause?.message || 'INVALID_INBOUND_PEER_ADMISSION', { cause }), {
|
|
4240
|
+
code: 'INVALID_INBOUND_PEER_ADMISSION'
|
|
4241
|
+
})
|
|
4242
|
+
}
|
|
4243
|
+
|
|
4061
4244
|
function oldestCreatedAt (events) {
|
|
4062
4245
|
let oldest = null
|
|
4063
4246
|
for (const event of events || []) {
|