Add wrap manager for tracking gift wraps

This commit is contained in:
Jon Staab
2025-10-17 12:42:29 -07:00
parent ca38cbe20b
commit 247c7bafeb
27 changed files with 406 additions and 319 deletions
+26 -22
View File
@@ -1,26 +1,18 @@
import {PublishStatus} from "@welshman/net"
import {NOTE, makeEvent} from "@welshman/util"
import {NOTE, DIRECT_MESSAGE, WRAP, makeEvent} from "@welshman/util"
import {LOCAL_RELAY_URL} from "@welshman/relay"
import {getPubkey, makeSecret} from "@welshman/signer"
import {getPubkey, makeSecret, prep} from "@welshman/signer"
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"
import {repository, tracker} from "../src/core"
import {addSession, dropSession, makeNip01Session} from "../src/session"
import {
abortThunk,
Thunk,
MergedThunk,
prepEvent,
publishThunk,
thunkQueue,
flattenThunks,
} from "../src/thunk"
import {abortThunk, MergedThunk, publishThunk, thunkQueue, flattenThunks} from "../src/thunk"
const secret = makeSecret()
const pubkey = getPubkey(secret)
const mockRequest = {
event: prepEvent({...makeEvent(NOTE), pubkey}),
event: prep({...makeEvent(NOTE), pubkey}),
relays: [LOCAL_RELAY_URL],
}
@@ -42,8 +34,8 @@ describe("thunk", () => {
describe("MergedThunk", () => {
it("should abort all thunks when merged controller aborts", () => {
const thunk1 = new Thunk(mockRequest)
const thunk2 = new Thunk(mockRequest)
const thunk1 = publishThunk(mockRequest)
const thunk2 = publishThunk(mockRequest)
const merged = new MergedThunk([thunk1, thunk2])
abortThunk(merged)
@@ -55,8 +47,8 @@ describe("thunk", () => {
describe("flattenThunks", () => {
it("should iterate through nested thunks", () => {
const thunk1 = new Thunk(mockRequest)
const thunk2 = new Thunk(mockRequest)
const thunk1 = publishThunk(mockRequest)
const thunk2 = publishThunk(mockRequest)
const merged = new MergedThunk([thunk1, thunk2])
const thunks = Array.from(flattenThunks([merged, thunk1]))
@@ -86,20 +78,18 @@ describe("thunk", () => {
describe("abortThunk", () => {
it("should abort a thunk and clean up", () => {
const thunk = new Thunk(mockRequest)
const removeEventSpy = vi.spyOn(repository, "removeEvent")
const thunk = publishThunk(mockRequest)
abortThunk(thunk)
expect(repository.removeEvent).toHaveBeenCalledWith(thunk.event.id)
expect(removeEventSpy).toHaveBeenCalledWith(thunk.event.id)
})
})
it("should update status during publishing", async () => {
const track = vi.spyOn(tracker, "track")
const thunk = new Thunk(mockRequest)
// Start the publish process
thunkQueue.push(thunk)
const thunk = publishThunk(mockRequest)
// Wait for initial async operations
await vi.runAllTimersAsync()
@@ -114,4 +104,18 @@ describe("thunk", () => {
expect(thunk.results[LOCAL_RELAY_URL].status).toEqual(PublishStatus.Success)
})
describe("wrapped events", () => {
it("if recipient is included, the event should be wrapped", async () => {
const recipient = getPubkey(makeSecret())
const event = prep({...makeEvent(DIRECT_MESSAGE), pubkey})
const thunk = publishThunk({event, relays: [], recipient})
const publishSpy = vi.spyOn(thunk, "_publish")
await vi.runAllTimersAsync()
expect(publishSpy.mock.calls[0][0].kind).toBe(WRAP)
expect(publishSpy.mock.calls[0][0].id).not.toBe(thunk.event.id)
})
})
})
+4 -6
View File
@@ -28,7 +28,6 @@ import {
PINS,
} from "@welshman/util"
import type {RoomMeta, Profile} from "@welshman/util"
import {Nip59, stamp, hash, own} from "@welshman/signer"
import {Router, addMaximalFallbacks} from "@welshman/router"
import {
userRelaySelections,
@@ -211,12 +210,11 @@ export type SendWrappedOptions = Omit<ThunkOptions, "event" | "relays"> & {
export const sendWrapped = async ({event, recipients, ...options}: SendWrappedOptions) =>
new MergedThunk(
uniq(recipients)
.map(recipient => {
const relays = Router.get().PubkeyInbox(recipient).getUrls()
uniq(recipients).map(recipient => {
const relays = Router.get().PubkeyInbox(recipient).getUrls()
return publishThunk({event, relays, recipient, ...options})
})
return publishThunk({event, relays, recipient, ...options})
}),
)
// NIP 86
+3 -4
View File
@@ -1,14 +1,13 @@
import {throttle} from "@welshman/lib"
import {Repository, LocalRelay} from "@welshman/relay"
import {Repository, LocalRelay, Tracker} from "@welshman/relay"
import {custom} from "@welshman/store"
import {Tracker} from "@welshman/net"
export const tracker = new Tracker()
export const repository = Repository.get()
export const relay = new LocalRelay(repository)
export const tracker = new Tracker()
// Adapt objects to stores
export const makeRepositoryStore = ({throttle: t = 300}: {throttle?: number} = {}) =>
+9 -4
View File
@@ -24,10 +24,10 @@ export * from "./zappers.js"
import {derived} from "svelte/store"
import {sortBy, throttleWithValue, tryCatch} from "@welshman/lib"
import {isEphemeralKind, isDVMKind, RelayMode, getRelaysFromList} from "@welshman/util"
import {isEphemeralKind, isDVMKind, WRAP, RelayMode, getRelaysFromList} from "@welshman/util"
import {routerContext} from "@welshman/router"
import {Pool, SocketEvent, isRelayEvent, netContext} from "@welshman/net"
import {pubkey} from "./session.js"
import {pubkey, unwrapAndStore} from "./session.js"
import {repository, tracker} from "./core.js"
import {Relay, relays, loadRelay, trackRelayStats, getRelayQuality} from "./relays.js"
import {relaySelectionsByPubkey} from "./relaySelections.js"
@@ -44,12 +44,17 @@ Pool.get().subscribe(socket => {
const event = message[2]
if (
!isEphemeralKind(event.kind) &&
!isDVMKind(event.kind) &&
!isEphemeralKind(event.kind) &&
netContext.isEventValid(event, socket.url)
) {
tracker.track(event.id, socket.url)
repository.publish(event)
if (event.kind === WRAP) {
unwrapAndStore(event)
} else {
repository.publish(event)
}
}
}
})
+56 -1
View File
@@ -1,8 +1,16 @@
import {derived, writable} from "svelte/store"
import {cached, randomId, append, omit, equals, assoc} from "@welshman/lib"
import {withGetter} from "@welshman/store"
import {Wallet} from "@welshman/util"
import {
Wallet,
WRAP,
isHashedEvent,
getPubkeyTagValues,
HashedEvent,
SignedEvent,
} from "@welshman/util"
import {
Nip59,
WrappedSigner,
Nip46Broker,
Nip46Signer,
@@ -12,6 +20,8 @@ import {
getPubkey,
ISigner,
} from "@welshman/signer"
import {WrapManager} from "@welshman/relay"
import {relay, tracker} from "./core.js"
export enum SessionMethod {
Nip01 = "nip01",
@@ -253,6 +263,14 @@ export const getSigner = cached({
},
})
export const getSignerFromPubkey = (pubkey: string) => {
const session = getSession(pubkey)
if (session) {
return getSigner(session)
}
}
export const signer = withGetter(derived(session, getSigner))
export const nip44EncryptToSelf = (payload: string) => {
@@ -265,3 +283,40 @@ export const nip44EncryptToSelf = (payload: string) => {
return $signer.nip44.encrypt($pubkey!, payload)
}
export const wrapManager = new WrapManager({relay, tracker})
export const unwrapAndStore = async (wrap: SignedEvent) => {
if (wrap.kind !== WRAP) throw new Error("Tried to unwrap an invalid event")
// First, check index and repository
const cached = wrapManager.getRumor(wrap.id)
if (cached) {
return cached
}
let rumor: HashedEvent | undefined
let recipient: string | undefined
// Next, try to decrypt as the recipient
for (const pubkey of getPubkeyTagValues(wrap.tags)) {
const signer = getSignerFromPubkey(pubkey)
if (signer) {
try {
rumor = await Nip59.fromSigner(signer).unwrap(wrap)
recipient = pubkey
break
} catch (e) {
// pass
}
}
}
if (rumor && recipient && isHashedEvent(rumor)) {
wrapManager.add({wrap, rumor, recipient})
}
return rumor
}
+41 -44
View File
@@ -1,10 +1,8 @@
import type {Subscriber} from "svelte/store"
import {writable, get} from "svelte/store"
import type {Override} from '@welshman/lib'
import {writable} from "svelte/store"
import type {Override} from "@welshman/lib"
import {
append,
reject,
spec,
TaskQueue,
ifLet,
ensurePlural,
@@ -14,20 +12,7 @@ import {
nth,
without,
} from "@welshman/lib"
import {
TrustedEvent,
HashedEvent,
EventTemplate,
SignedEvent,
StampedEvent,
OwnedEvent,
isStampedEvent,
isOwnedEvent,
isHashedEvent,
isUnwrappedEvent,
isSignedEvent,
WRAPPED_KINDS,
} from "@welshman/util"
import {HashedEvent, EventTemplate, SignedEvent, isSignedEvent, WRAPPED_KINDS} from "@welshman/util"
import {
publish,
PublishStatus,
@@ -35,15 +20,18 @@ import {
PublishOptions,
PublishResultsByRelay,
} from "@welshman/net"
import {ISigner, Nip59, prep} from '@welshman/signer'
import {ISigner, Nip59, prep} from "@welshman/signer"
import {repository, tracker} from "./core.js"
import {pubkey, signer} from "./session.js"
import {pubkey, signer, wrapManager} from "./session.js"
export type ThunkOptions = Override<PublishOptions, {
event: EventTemplate
recipient?: string
delay?: number
}>
export type ThunkOptions = Override<
PublishOptions,
{
event: EventTemplate
recipient?: string
delay?: number
}
>
export class Thunk {
_subs: Subscriber<Thunk>[] = []
@@ -54,6 +42,7 @@ export class Thunk {
results: PublishResultsByRelay = {}
complete = defer<void>()
controller = new AbortController()
wrap?: SignedEvent
constructor(readonly options: ThunkOptions) {
if (!options.recipient && WRAPPED_KINDS.includes(options.event.kind)) {
@@ -132,11 +121,6 @@ export class Thunk {
}
async _publish(event: SignedEvent) {
// Copy the signature over since we may have deferred signing
ifLet(repository.getEvent(event.id), savedEvent => {
savedEvent.sig = event.sig
})
// Wait if the thunk is to be delayed
if (this.options.delay) {
await sleep(this.options.delay)
@@ -179,17 +163,17 @@ export class Thunk {
// Handle abort immediately if possible
if (this.controller.signal.aborted) return
// If we were given an event with wraps, reject it (this used to be allowed)
if (isUnwrappedEvent(this.event)) {
throw new Error("Attempted to publish an unwrapped event")
}
const {recipient} = this.options
// If we're sending it privately, wrap the event using nip 59
if (this.options.recipient) {
if (recipient) {
const nip59 = Nip59.fromSigner(this.signer)
const event = await nip59.wrap(this.options.recipient, this.event)
return this._publish(event)
this.wrap = await nip59.wrap(recipient, this.event)
wrapManager.add({recipient, wrap: this.wrap, rumor: this.event})
return this._publish(this.wrap)
}
// If the event has been signed, we're good to go
@@ -200,11 +184,16 @@ export class Thunk {
// Allow for lazily signing events in order to decrease apparent latency in the UI
// that results from waiting for remote signers
try {
return this._publish(
await this.signer.sign(this.event, {
signal: AbortSignal.timeout(15_000),
})
)
const signedEvent = await this.signer.sign(this.event, {
signal: AbortSignal.timeout(15_000),
})
// Copy the signature over since we deferred signing
ifLet(repository.getEvent(signedEvent.id), savedEvent => {
savedEvent.sig = signedEvent.sig
})
return this._publish(signedEvent)
} catch (e: any) {
return this._fail(String(e || "Failed to sign event"))
}
@@ -214,6 +203,16 @@ export class Thunk {
thunkQueue.push(this)
repository.publish(this.event)
thunks.update($thunks => append(this, $thunks))
this.controller.signal.addEventListener("abort", () => {
if (this.wrap) {
wrapManager.remove(this.wrap.id)
} else {
repository.removeEvent(this.event.id)
}
thunks.update($thunks => remove(this, $thunks))
})
}
subscribe(subscriber: Subscriber<Thunk>) {
@@ -389,8 +388,6 @@ export const publishThunk = (options: ThunkOptions) => {
export const abortThunk = (thunk: AbstractThunk) => {
for (const child of flattenThunks([thunk])) {
child.controller.abort()
repository.removeEvent(child.event.id)
thunks.update($thunks => reject(spec({id: child.event.id}), $thunks))
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ import {
now,
} from "@welshman/lib"
import {EPOCH, trimFilters, guessFilterDelta, TrustedEvent, Filter} from "@welshman/util"
import {Tracker} from "@welshman/net"
import {Tracker} from "@welshman/relay"
import {Feed, FeedType, RequestItem} from "./core.js"
import {FeedCompiler, FeedCompilerOptions} from "./compiler.js"
import {requestPage} from "./request.js"
+2 -2
View File
@@ -10,9 +10,9 @@ import {
RELAYS,
} from "@welshman/util"
import {Nip01Signer, ISigner} from "@welshman/signer"
import {LOCAL_RELAY_URL} from "@welshman/relay"
import {LOCAL_RELAY_URL, Tracker} from "@welshman/relay"
import {Router, getFilterSelections, addMinimalFallbacks} from "@welshman/router"
import {Tracker, AdapterContext, request, publish} from "@welshman/net"
import {AdapterContext, request, publish} from "@welshman/net"
export type RequestPageOptions = {
filters: Filter[]
+12
View File
@@ -958,6 +958,18 @@ export const deepMergeRight = (a: Obj, b: Obj) => {
export const switcher = <T>(k: string, m: Record<string, T>) =>
m[k] === undefined ? m.default : m[k]
// ----------------------------------------------------------------------------
// Maps
// ----------------------------------------------------------------------------
export const mapPop = <K, T>(k: K, m: Map<K, T>) => {
const v = m.get(k)
m.delete(k)
return v
}
// ----------------------------------------------------------------------------
// Combinators
// ----------------------------------------------------------------------------
-1
View File
@@ -9,4 +9,3 @@ export * from "./pool.js"
export * from "./publish.js"
export * from "./socket.js"
export * from "./request.js"
export * from "./tracker.js"
+1 -1
View File
@@ -18,11 +18,11 @@ import {
deduplicateEvents,
getFilterResultCardinality,
} from "@welshman/util"
import {Tracker} from "@welshman/relay"
import {RelayMessage, ClientMessageType, isRelayEvent, isRelayEose} from "./message.js"
import {getAdapter, AdapterContext, AdapterEvent} from "./adapter.js"
import {SocketEvent, SocketStatus} from "./socket.js"
import {netContext} from "./context.js"
import {Tracker} from "./tracker.js"
export type BaseRequestOptions = {
signal?: AbortSignal
@@ -282,22 +282,6 @@ describe("Repository", () => {
})
})
describe("wrapped events", () => {
let repo: Repository
beforeEach(() => {
repo = new Repository()
})
it("should handle wrapped events", () => {
const event: TrustedEvent = createEvent(1, {wraps: [createEvent(1)]})
repo.publish(event)
expect(repo.eventsByWrap.get(event.wraps!.[0]!.id)).toEqual(event)
})
})
describe("event removal", () => {
let repo: Repository
@@ -313,16 +297,6 @@ describe("Repository", () => {
expect(repo.getEvent(event.id)).toBeUndefined()
})
it("should remove wrapped events", () => {
const wrapped = createEvent(1)
const event = createEvent(1, {wraps: [wrapped]})
repo.publish(event)
repo.removeEvent(event.id)
expect(repo.eventsByWrap.get(wrapped.id)).toBeUndefined()
})
it("should emit update on removal", () => {
const event = createEvent(1)
const updateHandler = vi.fn()
+2 -1
View File
@@ -21,7 +21,8 @@
},
"dependencies": {
"@welshman/lib": "workspace:*",
"@welshman/util": "workspace:*"
"@welshman/util": "workspace:*",
"@welshman/signer": "workspace:*"
},
"devDependencies": {
"rimraf": "~6.0.0",
+3 -1
View File
@@ -1,2 +1,4 @@
export * from "./repository.js"
export * from "./relay.js"
export * from "./repository.js"
export * from "./tracker.js"
export * from "./wrapManager.js"
+5 -5
View File
@@ -1,18 +1,18 @@
import {Emitter, sleep} from "@welshman/lib"
import {Filter, TrustedEvent, HashedEvent, matchFilters} from "@welshman/util"
import {Filter, TrustedEvent, matchFilters} from "@welshman/util"
import {Repository} from "./repository.js"
export class LocalRelay<E extends HashedEvent = TrustedEvent> extends Emitter {
export class LocalRelay extends Emitter {
subs = new Map<string, Filter[]>()
constructor(readonly repository: Repository<E>) {
constructor(readonly repository: Repository) {
super()
}
send(type: string, ...message: any[]) {
switch (type) {
case "EVENT":
return this.handleEVENT(message as [E])
return this.handleEVENT(message as [TrustedEvent])
case "CLOSE":
return this.handleCLOSE(message as [string])
case "REQ":
@@ -20,7 +20,7 @@ export class LocalRelay<E extends HashedEvent = TrustedEvent> extends Emitter {
}
}
handleEVENT([event]: [E]) {
handleEVENT([event]: [TrustedEvent]) {
this.repository.publish(event)
// Callers generally expect async relays
+33 -55
View File
@@ -1,47 +1,32 @@
import {
DAY,
Emitter,
flatten,
pluck,
sortBy,
inc,
chunk,
uniq,
omit,
now,
range,
} from "@welshman/lib"
import {DAY, Emitter, flatten, pluck, sortBy, inc, uniq, omit, now, range} from "@welshman/lib"
import {
DELETE,
EPOCH,
matchFilter,
isReplaceable,
isUnwrappedEvent,
getAddress,
Filter,
TrustedEvent,
HashedEvent,
} from "@welshman/util"
export const LOCAL_RELAY_URL = "local://welshman.relay/"
const getDay = (ts: number) => Math.floor(ts / DAY)
export let repositorySingleton: Repository<TrustedEvent>
export let repositorySingleton: Repository
export type RepositoryUpdate = {
added: TrustedEvent[]
removed: Set<string>
}
export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
eventsById = new Map<string, E>()
eventsByWrap = new Map<string, E>()
eventsByAddress = new Map<string, E>()
eventsByTag = new Map<string, E[]>()
eventsByDay = new Map<number, E[]>()
eventsByAuthor = new Map<string, E[]>()
eventsByKind = new Map<number, E[]>()
export class Repository extends Emitter {
eventsById = new Map<string, TrustedEvent>()
eventsByAddress = new Map<string, TrustedEvent>()
eventsByTag = new Map<string, TrustedEvent[]>()
eventsByDay = new Map<number, TrustedEvent[]>()
eventsByAuthor = new Map<string, TrustedEvent[]>()
eventsByKind = new Map<number, TrustedEvent[]>()
deletes = new Map<string, number>()
expired = new Map<string, number>()
@@ -65,11 +50,10 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
return Array.from(this.eventsById.values())
}
load = (events: E[], chunkSize = 1000) => {
load = (events: TrustedEvent[]) => {
const stale = new Set(this.eventsById.keys())
this.eventsById.clear()
this.eventsByWrap.clear()
this.eventsByAddress.clear()
this.eventsByTag.clear()
this.eventsByDay.clear()
@@ -80,13 +64,11 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
const added = []
for (const eventsChunk of chunk(chunkSize, events)) {
for (const event of eventsChunk) {
if (this.publish(event, {shouldNotify: false})) {
// Don't send duplicate events to subscribers
if (!stale.has(event.id)) {
added.push(event)
}
for (const event of events) {
if (this.publish(event, {shouldNotify: false})) {
// Don't send duplicate events to subscribers
if (!stale.has(event.id)) {
added.push(event)
}
}
}
@@ -121,7 +103,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
: this.eventsById.get(idOrAddress)
}
hasEvent = (event: E) => {
hasEvent = (event: TrustedEvent) => {
const duplicate = this.eventsById.get(event.id) || this.eventsByAddress.get(getAddress(event))
return duplicate && duplicate.created_at >= event.created_at
@@ -132,11 +114,6 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
if (event) {
this.eventsById.delete(event.id)
for (const wrap of event.wraps || []) {
this.eventsByWrap.delete(wrap.id)
}
this.eventsByAddress.delete(getAddress(event))
for (const [k, v] of event.tags) {
@@ -157,7 +134,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
filters: Filter[],
{includeDeleted = false, includeExpired = false, shouldSort = true} = {},
) => {
const result: E[][] = []
const result: TrustedEvent[][] = []
for (const originalFilter of filters) {
if (originalFilter.limit !== undefined && !shouldSort) {
throw new Error("Unable to skip sorting if limit is defined")
@@ -169,7 +146,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
const events = applied ? this._getEvents(applied!.ids) : this.dump()
const sorted = this._sortEvents(shouldSort && Boolean(filter.limit), events)
const chunk: E[] = []
const chunk: TrustedEvent[] = []
for (const event of sorted) {
if (filter.limit && chunk.length >= filter.limit) {
break
@@ -197,7 +174,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
return this._sortEvents(shouldSortAll, uniq(flatten(result)))
}
publish = (event: E, {shouldNotify = true} = {}): boolean => {
publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
if (!event?.id) {
console.warn("Attempted to publish invalid event to repository", event)
@@ -234,11 +211,6 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
this.eventsByAddress.set(address, event)
}
// Save wrapper index
for (const wrap of event.wraps || []) {
this.eventsByWrap.set(wrap.id, event)
}
// Update our timestamp and author indexes
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate)
this._updateIndex(this.eventsByAuthor, event.pubkey, event, duplicate)
@@ -279,13 +251,14 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
return true
}
isDeletedByAddress = (event: E) => (this.deletes.get(getAddress(event)) || 0) > event.created_at
isDeletedByAddress = (event: TrustedEvent) =>
(this.deletes.get(getAddress(event)) || 0) > event.created_at
isDeletedById = (event: E) => (this.deletes.get(event.id) || 0) > event.created_at
isDeletedById = (event: TrustedEvent) => (this.deletes.get(event.id) || 0) > event.created_at
isDeleted = (event: E) => this.isDeletedByAddress(event) || this.isDeletedById(event)
isDeleted = (event: TrustedEvent) => this.isDeletedByAddress(event) || this.isDeletedById(event)
isExpired = (event: E) => {
isExpired = (event: TrustedEvent) => {
const ts = this.expired.get(event.id)
return Boolean(ts && ts < now())
@@ -293,14 +266,19 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
// Utilities
_sortEvents = (shouldSort: boolean, events: E[]) =>
_sortEvents = (shouldSort: boolean, events: TrustedEvent[]) =>
shouldSort ? sortBy(e => -e.created_at, events) : events
_updateIndex = <K>(m: Map<K, E[]>, k: K, add?: E, remove?: E) => {
_updateIndex = <K>(
m: Map<K, TrustedEvent[]>,
k: K,
add?: TrustedEvent,
remove?: TrustedEvent,
) => {
let a = m.get(k) || []
if (remove) {
a = a.filter((x: E) => x !== remove)
a = a.filter((x: TrustedEvent) => x !== remove)
}
if (add) {
@@ -311,7 +289,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
}
_getEvents = (ids: Iterable<string>) => {
const events: E[] = []
const events: TrustedEvent[] = []
for (const id of ids) {
const event = this.eventsById.get(id)
+122
View File
@@ -0,0 +1,122 @@
import {Emitter, remove, omit} from "@welshman/lib"
import {HashedEvent, SignedEvent} from "@welshman/util"
import {Tracker} from "./tracker.js"
import {LocalRelay} from "./relay.js"
export type WrapItem = Omit<HashedEvent, "content"> & {
rumorId: string
recipient: string
}
export type WrapReference = string[]
export type WrapManagerOptions = {
relay: LocalRelay
tracker: Tracker
}
export class WrapManager extends Emitter {
_wrapIndex = new Map<string, WrapItem>()
_rumorIndex = new Map<string, WrapReference>()
_recipientIndex = new Map<string, WrapReference>()
constructor(readonly options: WrapManagerOptions) {
super()
}
getRumor = (id: string) => {
const wrapItem = this._wrapIndex.get(id)
if (wrapItem) {
return this.options.relay.repository.getEvent(wrapItem.rumorId)
}
}
// Adding/importing
load = (wrapItems: WrapItem[]) => {
this._wrapIndex.clear()
this._rumorIndex.clear()
this._recipientIndex.clear()
for (const wrapItem of wrapItems) {
this._add(wrapItem)
}
this.emit("load")
}
add = ({recipient, rumor, wrap}: {recipient: string; rumor: HashedEvent; wrap: SignedEvent}) => {
const wrapItem = {
...omit(["content"], wrap),
rumorId: rumor.id,
recipient,
}
this._add(wrapItem)
// Send via our relay so that listeners get notified
this.options.relay.send("EVENT", rumor)
// Mark the rumor as having come from the wrap's urls
this.options.tracker.copy(wrap.id, rumor.id)
this.emit("add", wrapItem)
}
// Removing
remove = (id: string) => {
const wrapItem = this._wrapIndex.get(id)
if (wrapItem) {
this._remove(wrapItem)
this.options.relay.repository.removeEvent(wrapItem.rumorId)
this.emit("remove", wrapItem)
}
}
removeByRumorId = (rumorId: string) => {
for (const id of this._rumorIndex.get(rumorId) || []) {
this.remove(id)
}
}
// Utils
_add = (wrapItem: WrapItem) => {
this._wrapIndex.set(wrapItem.id, wrapItem)
this._addReference(this._rumorIndex, wrapItem.rumorId, wrapItem.id)
this._addReference(this._recipientIndex, wrapItem.recipient, wrapItem.id)
}
_addReference = (index: Map<string, WrapReference>, key: string, wrapId: string) => {
const reference = index.get(key)
if (reference) {
reference.push(wrapId)
} else {
index.set(key, [wrapId])
}
}
_remove = (wrapItem: WrapItem) => {
this._wrapIndex.delete(wrapItem.id)
this._removeReference(this._rumorIndex, wrapItem.rumorId, wrapItem.id)
this._removeReference(this._recipientIndex, wrapItem.recipient, wrapItem.id)
}
_removeReference = (index: Map<string, WrapReference>, key: string, wrapId: string) => {
const reference = index.get(key)
if (reference) {
const wraps = remove(wrapId, reference)
if (wraps.length > 0) {
index.set(key, wraps)
} else {
index.delete(key)
}
}
}
}
+10 -1
View File
@@ -4,7 +4,16 @@ import * as nt04 from "nostr-tools/nip04"
import * as nt44 from "nostr-tools/nip44"
import {generateSecretKey, getPublicKey, getEventHash} from "nostr-tools/pure"
import {Emitter, cached, now} from "@welshman/lib"
import {SignedEvent, HashedEvent, EventTemplate, StampedEvent, OwnedEvent, isStampedEvent, isOwnedEvent, isHashedEvent} from "@welshman/util"
import {
SignedEvent,
HashedEvent,
EventTemplate,
StampedEvent,
OwnedEvent,
isStampedEvent,
isOwnedEvent,
isHashedEvent,
} from "@welshman/util"
export const makeSecret = () => bytesToHex(generateSecretKey())
+2 -43
View File
@@ -101,25 +101,6 @@ describe("Events", () => {
expect(Events.isSignedEvent(createSignedEvent())).toBe(true)
expect(Events.isSignedEvent(createHashedEvent())).toBe(false)
})
it("should validate TrustedEvent", () => {
const unwrapped = {
...createHashedEvent(),
wraps: [createSignedEvent()],
}
expect(Events.isTrustedEvent(createHashedEvent())).toBe(false)
expect(Events.isTrustedEvent(createSignedEvent())).toBe(true)
expect(Events.isTrustedEvent(unwrapped)).toBe(true)
})
it("should validate UnwrappedEvent", () => {
const unwrapped = {
...createHashedEvent(),
wraps: [createSignedEvent()],
}
expect(Events.isUnwrappedEvent(unwrapped)).toBe(true)
expect(Events.isUnwrappedEvent(createHashedEvent())).toBe(false)
})
})
describe("event conversion", () => {
@@ -152,34 +133,12 @@ describe("Events", () => {
const trustedEvent = {
...createHashedEvent(),
sig: sig,
wraps: [createSignedEvent()],
nonsense: 1,
}
const result = Events.asSignedEvent(trustedEvent)
expect(result).not.toHaveProperty("wraps")
expect(result).not.toHaveProperty("nonsense")
expect(result).toHaveProperty("sig")
})
it("should convert to UnwrappedEvent", () => {
const trustedEvent = {
...createHashedEvent(),
sig: sig,
wraps: [createSignedEvent()],
}
const result = Events.asUnwrappedEvent(trustedEvent)
expect(result).toHaveProperty("wraps")
expect(result).not.toHaveProperty("sig")
})
it("should convert to TrustedEvent", () => {
const trustedEvent = {
...createHashedEvent(),
sig: sig,
wraps: [createSignedEvent()],
}
const result = Events.asTrustedEvent(trustedEvent)
expect(result).toHaveProperty("sig")
expect(result).toHaveProperty("wraps")
})
})
describe("signature validation", () => {
-8
View File
@@ -182,14 +182,6 @@ describe("Filters", () => {
const result = getReplyFilters([event])
expect((result[0] as any)["#a"]).toBeDefined()
})
it("should handle wrapped events", () => {
const event = createEvent({
wraps: [createEvent()],
})
const result = getReplyFilters([event])
expect((result[0] as any)["#e"]).toHaveLength(2)
})
})
describe("addRepostFilters", () => {
-17
View File
@@ -40,13 +40,8 @@ export type SignedEvent = HashedEvent & {
[verifiedSymbol]?: boolean
}
export type UnwrappedEvent = HashedEvent & {
wraps: SignedEvent[]
}
export type TrustedEvent = HashedEvent & {
sig?: string
wraps?: SignedEvent[]
[verifiedSymbol]?: boolean
}
@@ -110,12 +105,6 @@ export const isHashedEvent = (e: HashedEvent): e is HashedEvent =>
export const isSignedEvent = (e: TrustedEvent): e is SignedEvent =>
Boolean(isHashedEvent(e) && typeof e.sig === "string" && e.sig.length > 0)
export const isUnwrappedEvent = (e: TrustedEvent): e is UnwrappedEvent =>
Boolean(isHashedEvent(e) && e.wraps?.every(isSignedEvent))
export const isTrustedEvent = (e: TrustedEvent): e is TrustedEvent =>
isSignedEvent(e) || isUnwrappedEvent(e)
// Type coercion and attribute stripping
export const asEventTemplate = (e: EventTemplate): EventTemplate =>
@@ -133,12 +122,6 @@ export const asHashedEvent = (e: HashedEvent): HashedEvent =>
export const asSignedEvent = (e: SignedEvent): SignedEvent =>
pick(["kind", "tags", "content", "created_at", "pubkey", "id", "sig"], e)
export const asUnwrappedEvent = (e: UnwrappedEvent): UnwrappedEvent =>
pick(["kind", "tags", "content", "created_at", "pubkey", "id", "wraps"], e)
export const asTrustedEvent = (e: TrustedEvent): TrustedEvent =>
pick(["kind", "tags", "content", "created_at", "pubkey", "id", "sig", "wraps"], e)
// Utilities for working with events
export const getIdentifier = (e: EventTemplate) => e.tags.find(t => t[0] === "d")?.[1]
-2
View File
@@ -178,8 +178,6 @@ export const getReplyFilters = (events: TrustedEvent[], filter: Filter = {}) =>
if (isReplaceableKind(event.kind)) {
a.push(getAddress(event))
}
event.wraps?.forEach(wrap => e.push(wrap.id))
}
const filters = []
+1 -4
View File
@@ -198,7 +198,4 @@ export const DEPRECATED_RELAY_RECOMMENDATION = 2
export const DEPRECATED_DIRECT_MESSAGE = 4
export const DEPRECATED_NAMED_GENERIC = 30001
export const WRAPPED_KINDS = [
DIRECT_MESSAGE,
DIRECT_MESSAGE_FILE,
]
export const WRAPPED_KINDS = [DIRECT_MESSAGE, DIRECT_MESSAGE_FILE]