Add wrap manager for tracking gift wraps
This commit is contained in:
@@ -1,70 +0,0 @@
|
|||||||
# Tracker
|
|
||||||
|
|
||||||
Event tracker for managing which events have been seen from which relays, used for deduplication across multiple relay connections.
|
|
||||||
|
|
||||||
## Classes
|
|
||||||
|
|
||||||
### Tracker
|
|
||||||
|
|
||||||
Tracks the relationship between event IDs and relay URLs to prevent duplicate processing.
|
|
||||||
|
|
||||||
**Properties:**
|
|
||||||
- `relaysById` - Map of event IDs to sets of relay URLs
|
|
||||||
- `idsByRelay` - Map of relay URLs to sets of event IDs
|
|
||||||
|
|
||||||
**Methods:**
|
|
||||||
- `getIds(relay)` - Gets all event IDs seen from a relay
|
|
||||||
- `getRelays(eventId)` - Gets all relays that have sent an event
|
|
||||||
- `hasRelay(eventId, relay)` - Checks if an event was seen from a relay
|
|
||||||
- `addRelay(eventId, relay)` - Records that an event was seen from a relay
|
|
||||||
- `removeRelay(eventId, relay)` - Removes the event-relay association
|
|
||||||
- `track(eventId, relay)` - Tracks an event and returns true if already seen
|
|
||||||
- `copy(eventId1, eventId2)` - Copies relay associations from one event to another
|
|
||||||
- `load(relaysById)` - Loads tracker state from a map
|
|
||||||
- `clear()` - Clears all tracked data
|
|
||||||
|
|
||||||
**Events:**
|
|
||||||
- `add` - Emitted when event-relay association is added
|
|
||||||
- `remove` - Emitted when event-relay association is removed
|
|
||||||
- `load` - Emitted when tracker state is loaded
|
|
||||||
- `clear` - Emitted when tracker is cleared
|
|
||||||
|
|
||||||
## Example
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import {Tracker} from "@welshman/net"
|
|
||||||
|
|
||||||
const tracker = new Tracker()
|
|
||||||
|
|
||||||
// Track events from different relays
|
|
||||||
const isDuplicate1 = tracker.track("event123", "wss://relay1.com") // false
|
|
||||||
const isDuplicate2 = tracker.track("event123", "wss://relay2.com") // false
|
|
||||||
const isDuplicate3 = tracker.track("event123", "wss://relay1.com") // true (duplicate)
|
|
||||||
|
|
||||||
// Check which relays have sent an event
|
|
||||||
const relays = tracker.getRelays("event123") // Set(["wss://relay1.com", "wss://relay2.com"])
|
|
||||||
```
|
|
||||||
|
|
||||||
If you're not using `@welshman/app`, you might want to track relays for all events that come through:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import {Pool, Tracker, SocketEvent, isRelayEvent} from "@welshman/net"
|
|
||||||
import {isEphemeralKind, isDVMKind, verifyEvent} from "@welshman/util"
|
|
||||||
import {Repository} from "@welshman/relay"
|
|
||||||
|
|
||||||
const tracker = new Tracker()
|
|
||||||
const repository = new Repository()
|
|
||||||
|
|
||||||
Pool.get().subscribe(socket => {
|
|
||||||
socket.on(SocketEvent.Receive, message => {
|
|
||||||
if (isRelayEvent(message)) {
|
|
||||||
const event = message[2]
|
|
||||||
|
|
||||||
if (!isEphemeralKind(event.kind) && !isDVMKind(event.kind) && verifyEvent(event)) {
|
|
||||||
tracker.track(event.id, socket.url)
|
|
||||||
repository.publish(event)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
})
|
|
||||||
```
|
|
||||||
@@ -8,6 +8,8 @@ A few utilites for storing nostr events in memory.
|
|||||||
|
|
||||||
- **Event Store** - A Repository class which stores events in memory
|
- **Event Store** - A Repository class which stores events in memory
|
||||||
- **Relay Adapter** - A LocalRelay class which adapts nostr messages to the repository
|
- **Relay Adapter** - A LocalRelay class which adapts nostr messages to the repository
|
||||||
|
- **Event Tracker** - A Tracker class for managing which events have been seen from which relays
|
||||||
|
- **Gift Wrap Manager** - A WrapManager class for tracking and unwrapping NIP-59 gift wrapped events
|
||||||
|
|
||||||
## Quick Example
|
## Quick Example
|
||||||
|
|
||||||
@@ -66,6 +68,74 @@ relay.send("EVENT", {
|
|||||||
relay.send("REQ", "tagged", {kinds: [1], "#t": ["welshman"]})
|
relay.send("REQ", "tagged", {kinds: [1], "#t": ["welshman"]})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Tracking Events Across Relays
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import {Tracker} from "@welshman/relay"
|
||||||
|
|
||||||
|
const tracker = new Tracker()
|
||||||
|
|
||||||
|
// Track events from different relays
|
||||||
|
const isDuplicate1 = tracker.track("event123", "wss://relay1.com") // false
|
||||||
|
const isDuplicate2 = tracker.track("event123", "wss://relay2.com") // false
|
||||||
|
const isDuplicate3 = tracker.track("event123", "wss://relay1.com") // true (duplicate)
|
||||||
|
|
||||||
|
// Check which relays have sent an event
|
||||||
|
const relays = tracker.getRelays("event123") // Set(["wss://relay1.com", "wss://relay2.com"])
|
||||||
|
|
||||||
|
// Copy relay tracking from one event to another (useful for wrapped events)
|
||||||
|
tracker.copy("wrap-event-id", "rumor-event-id")
|
||||||
|
```
|
||||||
|
|
||||||
|
### Managing Gift Wrapped Events
|
||||||
|
|
||||||
|
The WrapManager handles NIP-59 gift wrapped events, automatically unwrapping incoming wrapped events and tracking the relationship between wraps and their inner rumors.
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
import {Repository, LocalRelay, Tracker, WrapManager} from "@welshman/relay"
|
||||||
|
import {ISigner} from "@welshman/signer"
|
||||||
|
|
||||||
|
const repository = Repository.get()
|
||||||
|
const relay = new LocalRelay(repository)
|
||||||
|
const tracker = new Tracker()
|
||||||
|
|
||||||
|
// Create a wrap manager with a function to get signers for different pubkeys
|
||||||
|
const wrapManager = new WrapManager({
|
||||||
|
relay,
|
||||||
|
tracker,
|
||||||
|
getSigner: (pubkey: string) => {
|
||||||
|
// Return the appropriate signer for this pubkey
|
||||||
|
return mySignerMap.get(pubkey)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
// When you publish a wrapped event, track it
|
||||||
|
wrapManager.add({
|
||||||
|
recipient: recipientPubkey,
|
||||||
|
wrap: wrappedEvent,
|
||||||
|
rumor: innerEvent
|
||||||
|
})
|
||||||
|
|
||||||
|
// When you receive a wrapped event, unwrap it
|
||||||
|
await wrapManager.unwrap(receivedWrapEvent)
|
||||||
|
|
||||||
|
// The rumor will be automatically published to the repository
|
||||||
|
// and relay tracking will be copied from the wrap to the rumor
|
||||||
|
|
||||||
|
// Remove wraps by various criteria
|
||||||
|
wrapManager.remove(wrapId)
|
||||||
|
wrapManager.removeByRumorId(rumorId)
|
||||||
|
|
||||||
|
// Listen for wrap manager events
|
||||||
|
wrapManager.on("add", (wrapItem) => {
|
||||||
|
console.log("Wrap added:", wrapItem)
|
||||||
|
})
|
||||||
|
|
||||||
|
wrapManager.on("remove", (wrapItem) => {
|
||||||
|
console.log("Wrap removed:", wrapItem)
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
@@ -1,26 +1,18 @@
|
|||||||
import {PublishStatus} from "@welshman/net"
|
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 {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 {afterEach, beforeEach, describe, expect, it, vi} from "vitest"
|
||||||
import {repository, tracker} from "../src/core"
|
import {repository, tracker} from "../src/core"
|
||||||
import {addSession, dropSession, makeNip01Session} from "../src/session"
|
import {addSession, dropSession, makeNip01Session} from "../src/session"
|
||||||
import {
|
import {abortThunk, MergedThunk, publishThunk, thunkQueue, flattenThunks} from "../src/thunk"
|
||||||
abortThunk,
|
|
||||||
Thunk,
|
|
||||||
MergedThunk,
|
|
||||||
prepEvent,
|
|
||||||
publishThunk,
|
|
||||||
thunkQueue,
|
|
||||||
flattenThunks,
|
|
||||||
} from "../src/thunk"
|
|
||||||
|
|
||||||
const secret = makeSecret()
|
const secret = makeSecret()
|
||||||
|
|
||||||
const pubkey = getPubkey(secret)
|
const pubkey = getPubkey(secret)
|
||||||
|
|
||||||
const mockRequest = {
|
const mockRequest = {
|
||||||
event: prepEvent({...makeEvent(NOTE), pubkey}),
|
event: prep({...makeEvent(NOTE), pubkey}),
|
||||||
relays: [LOCAL_RELAY_URL],
|
relays: [LOCAL_RELAY_URL],
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,8 +34,8 @@ describe("thunk", () => {
|
|||||||
|
|
||||||
describe("MergedThunk", () => {
|
describe("MergedThunk", () => {
|
||||||
it("should abort all thunks when merged controller aborts", () => {
|
it("should abort all thunks when merged controller aborts", () => {
|
||||||
const thunk1 = new Thunk(mockRequest)
|
const thunk1 = publishThunk(mockRequest)
|
||||||
const thunk2 = new Thunk(mockRequest)
|
const thunk2 = publishThunk(mockRequest)
|
||||||
const merged = new MergedThunk([thunk1, thunk2])
|
const merged = new MergedThunk([thunk1, thunk2])
|
||||||
|
|
||||||
abortThunk(merged)
|
abortThunk(merged)
|
||||||
@@ -55,8 +47,8 @@ describe("thunk", () => {
|
|||||||
|
|
||||||
describe("flattenThunks", () => {
|
describe("flattenThunks", () => {
|
||||||
it("should iterate through nested thunks", () => {
|
it("should iterate through nested thunks", () => {
|
||||||
const thunk1 = new Thunk(mockRequest)
|
const thunk1 = publishThunk(mockRequest)
|
||||||
const thunk2 = new Thunk(mockRequest)
|
const thunk2 = publishThunk(mockRequest)
|
||||||
const merged = new MergedThunk([thunk1, thunk2])
|
const merged = new MergedThunk([thunk1, thunk2])
|
||||||
const thunks = Array.from(flattenThunks([merged, thunk1]))
|
const thunks = Array.from(flattenThunks([merged, thunk1]))
|
||||||
|
|
||||||
@@ -86,20 +78,18 @@ describe("thunk", () => {
|
|||||||
|
|
||||||
describe("abortThunk", () => {
|
describe("abortThunk", () => {
|
||||||
it("should abort a thunk and clean up", () => {
|
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)
|
abortThunk(thunk)
|
||||||
|
|
||||||
expect(repository.removeEvent).toHaveBeenCalledWith(thunk.event.id)
|
expect(removeEventSpy).toHaveBeenCalledWith(thunk.event.id)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should update status during publishing", async () => {
|
it("should update status during publishing", async () => {
|
||||||
const track = vi.spyOn(tracker, "track")
|
const track = vi.spyOn(tracker, "track")
|
||||||
const thunk = new Thunk(mockRequest)
|
const thunk = publishThunk(mockRequest)
|
||||||
|
|
||||||
// Start the publish process
|
|
||||||
thunkQueue.push(thunk)
|
|
||||||
|
|
||||||
// Wait for initial async operations
|
// Wait for initial async operations
|
||||||
await vi.runAllTimersAsync()
|
await vi.runAllTimersAsync()
|
||||||
@@ -114,4 +104,18 @@ describe("thunk", () => {
|
|||||||
|
|
||||||
expect(thunk.results[LOCAL_RELAY_URL].status).toEqual(PublishStatus.Success)
|
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)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -28,7 +28,6 @@ import {
|
|||||||
PINS,
|
PINS,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import type {RoomMeta, Profile} 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 {Router, addMaximalFallbacks} from "@welshman/router"
|
||||||
import {
|
import {
|
||||||
userRelaySelections,
|
userRelaySelections,
|
||||||
@@ -211,12 +210,11 @@ export type SendWrappedOptions = Omit<ThunkOptions, "event" | "relays"> & {
|
|||||||
|
|
||||||
export const sendWrapped = async ({event, recipients, ...options}: SendWrappedOptions) =>
|
export const sendWrapped = async ({event, recipients, ...options}: SendWrappedOptions) =>
|
||||||
new MergedThunk(
|
new MergedThunk(
|
||||||
uniq(recipients)
|
uniq(recipients).map(recipient => {
|
||||||
.map(recipient => {
|
const relays = Router.get().PubkeyInbox(recipient).getUrls()
|
||||||
const relays = Router.get().PubkeyInbox(recipient).getUrls()
|
|
||||||
|
|
||||||
return publishThunk({event, relays, recipient, ...options})
|
return publishThunk({event, relays, recipient, ...options})
|
||||||
})
|
}),
|
||||||
)
|
)
|
||||||
|
|
||||||
// NIP 86
|
// NIP 86
|
||||||
|
|||||||
@@ -1,14 +1,13 @@
|
|||||||
import {throttle} from "@welshman/lib"
|
import {throttle} from "@welshman/lib"
|
||||||
import {Repository, LocalRelay} from "@welshman/relay"
|
import {Repository, LocalRelay, Tracker} from "@welshman/relay"
|
||||||
import {custom} from "@welshman/store"
|
import {custom} from "@welshman/store"
|
||||||
import {Tracker} from "@welshman/net"
|
|
||||||
|
export const tracker = new Tracker()
|
||||||
|
|
||||||
export const repository = Repository.get()
|
export const repository = Repository.get()
|
||||||
|
|
||||||
export const relay = new LocalRelay(repository)
|
export const relay = new LocalRelay(repository)
|
||||||
|
|
||||||
export const tracker = new Tracker()
|
|
||||||
|
|
||||||
// Adapt objects to stores
|
// Adapt objects to stores
|
||||||
|
|
||||||
export const makeRepositoryStore = ({throttle: t = 300}: {throttle?: number} = {}) =>
|
export const makeRepositoryStore = ({throttle: t = 300}: {throttle?: number} = {}) =>
|
||||||
|
|||||||
@@ -24,10 +24,10 @@ export * from "./zappers.js"
|
|||||||
|
|
||||||
import {derived} from "svelte/store"
|
import {derived} from "svelte/store"
|
||||||
import {sortBy, throttleWithValue, tryCatch} from "@welshman/lib"
|
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 {routerContext} from "@welshman/router"
|
||||||
import {Pool, SocketEvent, isRelayEvent, netContext} from "@welshman/net"
|
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 {repository, tracker} from "./core.js"
|
||||||
import {Relay, relays, loadRelay, trackRelayStats, getRelayQuality} from "./relays.js"
|
import {Relay, relays, loadRelay, trackRelayStats, getRelayQuality} from "./relays.js"
|
||||||
import {relaySelectionsByPubkey} from "./relaySelections.js"
|
import {relaySelectionsByPubkey} from "./relaySelections.js"
|
||||||
@@ -44,12 +44,17 @@ Pool.get().subscribe(socket => {
|
|||||||
const event = message[2]
|
const event = message[2]
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!isEphemeralKind(event.kind) &&
|
|
||||||
!isDVMKind(event.kind) &&
|
!isDVMKind(event.kind) &&
|
||||||
|
!isEphemeralKind(event.kind) &&
|
||||||
netContext.isEventValid(event, socket.url)
|
netContext.isEventValid(event, socket.url)
|
||||||
) {
|
) {
|
||||||
tracker.track(event.id, socket.url)
|
tracker.track(event.id, socket.url)
|
||||||
repository.publish(event)
|
|
||||||
|
if (event.kind === WRAP) {
|
||||||
|
unwrapAndStore(event)
|
||||||
|
} else {
|
||||||
|
repository.publish(event)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,8 +1,16 @@
|
|||||||
import {derived, writable} from "svelte/store"
|
import {derived, writable} from "svelte/store"
|
||||||
import {cached, randomId, append, omit, equals, assoc} from "@welshman/lib"
|
import {cached, randomId, append, omit, equals, assoc} from "@welshman/lib"
|
||||||
import {withGetter} from "@welshman/store"
|
import {withGetter} from "@welshman/store"
|
||||||
import {Wallet} from "@welshman/util"
|
|
||||||
import {
|
import {
|
||||||
|
Wallet,
|
||||||
|
WRAP,
|
||||||
|
isHashedEvent,
|
||||||
|
getPubkeyTagValues,
|
||||||
|
HashedEvent,
|
||||||
|
SignedEvent,
|
||||||
|
} from "@welshman/util"
|
||||||
|
import {
|
||||||
|
Nip59,
|
||||||
WrappedSigner,
|
WrappedSigner,
|
||||||
Nip46Broker,
|
Nip46Broker,
|
||||||
Nip46Signer,
|
Nip46Signer,
|
||||||
@@ -12,6 +20,8 @@ import {
|
|||||||
getPubkey,
|
getPubkey,
|
||||||
ISigner,
|
ISigner,
|
||||||
} from "@welshman/signer"
|
} from "@welshman/signer"
|
||||||
|
import {WrapManager} from "@welshman/relay"
|
||||||
|
import {relay, tracker} from "./core.js"
|
||||||
|
|
||||||
export enum SessionMethod {
|
export enum SessionMethod {
|
||||||
Nip01 = "nip01",
|
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 signer = withGetter(derived(session, getSigner))
|
||||||
|
|
||||||
export const nip44EncryptToSelf = (payload: string) => {
|
export const nip44EncryptToSelf = (payload: string) => {
|
||||||
@@ -265,3 +283,40 @@ export const nip44EncryptToSelf = (payload: string) => {
|
|||||||
|
|
||||||
return $signer.nip44.encrypt($pubkey!, payload)
|
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
@@ -1,10 +1,8 @@
|
|||||||
import type {Subscriber} from "svelte/store"
|
import type {Subscriber} from "svelte/store"
|
||||||
import {writable, get} from "svelte/store"
|
import {writable} from "svelte/store"
|
||||||
import type {Override} from '@welshman/lib'
|
import type {Override} from "@welshman/lib"
|
||||||
import {
|
import {
|
||||||
append,
|
append,
|
||||||
reject,
|
|
||||||
spec,
|
|
||||||
TaskQueue,
|
TaskQueue,
|
||||||
ifLet,
|
ifLet,
|
||||||
ensurePlural,
|
ensurePlural,
|
||||||
@@ -14,20 +12,7 @@ import {
|
|||||||
nth,
|
nth,
|
||||||
without,
|
without,
|
||||||
} from "@welshman/lib"
|
} from "@welshman/lib"
|
||||||
import {
|
import {HashedEvent, EventTemplate, SignedEvent, isSignedEvent, WRAPPED_KINDS} from "@welshman/util"
|
||||||
TrustedEvent,
|
|
||||||
HashedEvent,
|
|
||||||
EventTemplate,
|
|
||||||
SignedEvent,
|
|
||||||
StampedEvent,
|
|
||||||
OwnedEvent,
|
|
||||||
isStampedEvent,
|
|
||||||
isOwnedEvent,
|
|
||||||
isHashedEvent,
|
|
||||||
isUnwrappedEvent,
|
|
||||||
isSignedEvent,
|
|
||||||
WRAPPED_KINDS,
|
|
||||||
} from "@welshman/util"
|
|
||||||
import {
|
import {
|
||||||
publish,
|
publish,
|
||||||
PublishStatus,
|
PublishStatus,
|
||||||
@@ -35,15 +20,18 @@ import {
|
|||||||
PublishOptions,
|
PublishOptions,
|
||||||
PublishResultsByRelay,
|
PublishResultsByRelay,
|
||||||
} from "@welshman/net"
|
} from "@welshman/net"
|
||||||
import {ISigner, Nip59, prep} from '@welshman/signer'
|
import {ISigner, Nip59, prep} from "@welshman/signer"
|
||||||
import {repository, tracker} from "./core.js"
|
import {repository, tracker} from "./core.js"
|
||||||
import {pubkey, signer} from "./session.js"
|
import {pubkey, signer, wrapManager} from "./session.js"
|
||||||
|
|
||||||
export type ThunkOptions = Override<PublishOptions, {
|
export type ThunkOptions = Override<
|
||||||
event: EventTemplate
|
PublishOptions,
|
||||||
recipient?: string
|
{
|
||||||
delay?: number
|
event: EventTemplate
|
||||||
}>
|
recipient?: string
|
||||||
|
delay?: number
|
||||||
|
}
|
||||||
|
>
|
||||||
|
|
||||||
export class Thunk {
|
export class Thunk {
|
||||||
_subs: Subscriber<Thunk>[] = []
|
_subs: Subscriber<Thunk>[] = []
|
||||||
@@ -54,6 +42,7 @@ export class Thunk {
|
|||||||
results: PublishResultsByRelay = {}
|
results: PublishResultsByRelay = {}
|
||||||
complete = defer<void>()
|
complete = defer<void>()
|
||||||
controller = new AbortController()
|
controller = new AbortController()
|
||||||
|
wrap?: SignedEvent
|
||||||
|
|
||||||
constructor(readonly options: ThunkOptions) {
|
constructor(readonly options: ThunkOptions) {
|
||||||
if (!options.recipient && WRAPPED_KINDS.includes(options.event.kind)) {
|
if (!options.recipient && WRAPPED_KINDS.includes(options.event.kind)) {
|
||||||
@@ -132,11 +121,6 @@ export class Thunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _publish(event: SignedEvent) {
|
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
|
// Wait if the thunk is to be delayed
|
||||||
if (this.options.delay) {
|
if (this.options.delay) {
|
||||||
await sleep(this.options.delay)
|
await sleep(this.options.delay)
|
||||||
@@ -179,17 +163,17 @@ export class Thunk {
|
|||||||
// Handle abort immediately if possible
|
// Handle abort immediately if possible
|
||||||
if (this.controller.signal.aborted) return
|
if (this.controller.signal.aborted) return
|
||||||
|
|
||||||
// If we were given an event with wraps, reject it (this used to be allowed)
|
const {recipient} = this.options
|
||||||
if (isUnwrappedEvent(this.event)) {
|
|
||||||
throw new Error("Attempted to publish an unwrapped event")
|
|
||||||
}
|
|
||||||
|
|
||||||
// If we're sending it privately, wrap the event using nip 59
|
// 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 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
|
// 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
|
// Allow for lazily signing events in order to decrease apparent latency in the UI
|
||||||
// that results from waiting for remote signers
|
// that results from waiting for remote signers
|
||||||
try {
|
try {
|
||||||
return this._publish(
|
const signedEvent = await this.signer.sign(this.event, {
|
||||||
await this.signer.sign(this.event, {
|
signal: AbortSignal.timeout(15_000),
|
||||||
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) {
|
} catch (e: any) {
|
||||||
return this._fail(String(e || "Failed to sign event"))
|
return this._fail(String(e || "Failed to sign event"))
|
||||||
}
|
}
|
||||||
@@ -214,6 +203,16 @@ export class Thunk {
|
|||||||
thunkQueue.push(this)
|
thunkQueue.push(this)
|
||||||
repository.publish(this.event)
|
repository.publish(this.event)
|
||||||
thunks.update($thunks => append(this, $thunks))
|
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>) {
|
subscribe(subscriber: Subscriber<Thunk>) {
|
||||||
@@ -389,8 +388,6 @@ export const publishThunk = (options: ThunkOptions) => {
|
|||||||
export const abortThunk = (thunk: AbstractThunk) => {
|
export const abortThunk = (thunk: AbstractThunk) => {
|
||||||
for (const child of flattenThunks([thunk])) {
|
for (const child of flattenThunks([thunk])) {
|
||||||
child.controller.abort()
|
child.controller.abort()
|
||||||
repository.removeEvent(child.event.id)
|
|
||||||
thunks.update($thunks => reject(spec({id: child.event.id}), $thunks))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import {
|
|||||||
now,
|
now,
|
||||||
} from "@welshman/lib"
|
} from "@welshman/lib"
|
||||||
import {EPOCH, trimFilters, guessFilterDelta, TrustedEvent, Filter} from "@welshman/util"
|
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 {Feed, FeedType, RequestItem} from "./core.js"
|
||||||
import {FeedCompiler, FeedCompilerOptions} from "./compiler.js"
|
import {FeedCompiler, FeedCompilerOptions} from "./compiler.js"
|
||||||
import {requestPage} from "./request.js"
|
import {requestPage} from "./request.js"
|
||||||
|
|||||||
@@ -10,9 +10,9 @@ import {
|
|||||||
RELAYS,
|
RELAYS,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import {Nip01Signer, ISigner} from "@welshman/signer"
|
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 {Router, getFilterSelections, addMinimalFallbacks} from "@welshman/router"
|
||||||
import {Tracker, AdapterContext, request, publish} from "@welshman/net"
|
import {AdapterContext, request, publish} from "@welshman/net"
|
||||||
|
|
||||||
export type RequestPageOptions = {
|
export type RequestPageOptions = {
|
||||||
filters: Filter[]
|
filters: Filter[]
|
||||||
|
|||||||
@@ -958,6 +958,18 @@ export const deepMergeRight = (a: Obj, b: Obj) => {
|
|||||||
export const switcher = <T>(k: string, m: Record<string, T>) =>
|
export const switcher = <T>(k: string, m: Record<string, T>) =>
|
||||||
m[k] === undefined ? m.default : m[k]
|
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
|
// Combinators
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|||||||
@@ -9,4 +9,3 @@ export * from "./pool.js"
|
|||||||
export * from "./publish.js"
|
export * from "./publish.js"
|
||||||
export * from "./socket.js"
|
export * from "./socket.js"
|
||||||
export * from "./request.js"
|
export * from "./request.js"
|
||||||
export * from "./tracker.js"
|
|
||||||
|
|||||||
@@ -18,11 +18,11 @@ import {
|
|||||||
deduplicateEvents,
|
deduplicateEvents,
|
||||||
getFilterResultCardinality,
|
getFilterResultCardinality,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
|
import {Tracker} from "@welshman/relay"
|
||||||
import {RelayMessage, ClientMessageType, isRelayEvent, isRelayEose} from "./message.js"
|
import {RelayMessage, ClientMessageType, isRelayEvent, isRelayEose} from "./message.js"
|
||||||
import {getAdapter, AdapterContext, AdapterEvent} from "./adapter.js"
|
import {getAdapter, AdapterContext, AdapterEvent} from "./adapter.js"
|
||||||
import {SocketEvent, SocketStatus} from "./socket.js"
|
import {SocketEvent, SocketStatus} from "./socket.js"
|
||||||
import {netContext} from "./context.js"
|
import {netContext} from "./context.js"
|
||||||
import {Tracker} from "./tracker.js"
|
|
||||||
|
|
||||||
export type BaseRequestOptions = {
|
export type BaseRequestOptions = {
|
||||||
signal?: AbortSignal
|
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", () => {
|
describe("event removal", () => {
|
||||||
let repo: Repository
|
let repo: Repository
|
||||||
|
|
||||||
@@ -313,16 +297,6 @@ describe("Repository", () => {
|
|||||||
expect(repo.getEvent(event.id)).toBeUndefined()
|
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", () => {
|
it("should emit update on removal", () => {
|
||||||
const event = createEvent(1)
|
const event = createEvent(1)
|
||||||
const updateHandler = vi.fn()
|
const updateHandler = vi.fn()
|
||||||
|
|||||||
@@ -21,7 +21,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@welshman/lib": "workspace:*",
|
"@welshman/lib": "workspace:*",
|
||||||
"@welshman/util": "workspace:*"
|
"@welshman/util": "workspace:*",
|
||||||
|
"@welshman/signer": "workspace:*"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"rimraf": "~6.0.0",
|
"rimraf": "~6.0.0",
|
||||||
|
|||||||
@@ -1,2 +1,4 @@
|
|||||||
export * from "./repository.js"
|
|
||||||
export * from "./relay.js"
|
export * from "./relay.js"
|
||||||
|
export * from "./repository.js"
|
||||||
|
export * from "./tracker.js"
|
||||||
|
export * from "./wrapManager.js"
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
import {Emitter, sleep} from "@welshman/lib"
|
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"
|
import {Repository} from "./repository.js"
|
||||||
|
|
||||||
export class LocalRelay<E extends HashedEvent = TrustedEvent> extends Emitter {
|
export class LocalRelay extends Emitter {
|
||||||
subs = new Map<string, Filter[]>()
|
subs = new Map<string, Filter[]>()
|
||||||
|
|
||||||
constructor(readonly repository: Repository<E>) {
|
constructor(readonly repository: Repository) {
|
||||||
super()
|
super()
|
||||||
}
|
}
|
||||||
|
|
||||||
send(type: string, ...message: any[]) {
|
send(type: string, ...message: any[]) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case "EVENT":
|
case "EVENT":
|
||||||
return this.handleEVENT(message as [E])
|
return this.handleEVENT(message as [TrustedEvent])
|
||||||
case "CLOSE":
|
case "CLOSE":
|
||||||
return this.handleCLOSE(message as [string])
|
return this.handleCLOSE(message as [string])
|
||||||
case "REQ":
|
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)
|
this.repository.publish(event)
|
||||||
|
|
||||||
// Callers generally expect async relays
|
// Callers generally expect async relays
|
||||||
|
|||||||
@@ -1,47 +1,32 @@
|
|||||||
import {
|
import {DAY, Emitter, flatten, pluck, sortBy, inc, uniq, omit, now, range} from "@welshman/lib"
|
||||||
DAY,
|
|
||||||
Emitter,
|
|
||||||
flatten,
|
|
||||||
pluck,
|
|
||||||
sortBy,
|
|
||||||
inc,
|
|
||||||
chunk,
|
|
||||||
uniq,
|
|
||||||
omit,
|
|
||||||
now,
|
|
||||||
range,
|
|
||||||
} from "@welshman/lib"
|
|
||||||
import {
|
import {
|
||||||
DELETE,
|
DELETE,
|
||||||
EPOCH,
|
EPOCH,
|
||||||
matchFilter,
|
matchFilter,
|
||||||
isReplaceable,
|
isReplaceable,
|
||||||
isUnwrappedEvent,
|
|
||||||
getAddress,
|
getAddress,
|
||||||
Filter,
|
Filter,
|
||||||
TrustedEvent,
|
TrustedEvent,
|
||||||
HashedEvent,
|
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
|
|
||||||
export const LOCAL_RELAY_URL = "local://welshman.relay/"
|
export const LOCAL_RELAY_URL = "local://welshman.relay/"
|
||||||
|
|
||||||
const getDay = (ts: number) => Math.floor(ts / DAY)
|
const getDay = (ts: number) => Math.floor(ts / DAY)
|
||||||
|
|
||||||
export let repositorySingleton: Repository<TrustedEvent>
|
export let repositorySingleton: Repository
|
||||||
|
|
||||||
export type RepositoryUpdate = {
|
export type RepositoryUpdate = {
|
||||||
added: TrustedEvent[]
|
added: TrustedEvent[]
|
||||||
removed: Set<string>
|
removed: Set<string>
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
export class Repository extends Emitter {
|
||||||
eventsById = new Map<string, E>()
|
eventsById = new Map<string, TrustedEvent>()
|
||||||
eventsByWrap = new Map<string, E>()
|
eventsByAddress = new Map<string, TrustedEvent>()
|
||||||
eventsByAddress = new Map<string, E>()
|
eventsByTag = new Map<string, TrustedEvent[]>()
|
||||||
eventsByTag = new Map<string, E[]>()
|
eventsByDay = new Map<number, TrustedEvent[]>()
|
||||||
eventsByDay = new Map<number, E[]>()
|
eventsByAuthor = new Map<string, TrustedEvent[]>()
|
||||||
eventsByAuthor = new Map<string, E[]>()
|
eventsByKind = new Map<number, TrustedEvent[]>()
|
||||||
eventsByKind = new Map<number, E[]>()
|
|
||||||
deletes = new Map<string, number>()
|
deletes = new Map<string, number>()
|
||||||
expired = 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())
|
return Array.from(this.eventsById.values())
|
||||||
}
|
}
|
||||||
|
|
||||||
load = (events: E[], chunkSize = 1000) => {
|
load = (events: TrustedEvent[]) => {
|
||||||
const stale = new Set(this.eventsById.keys())
|
const stale = new Set(this.eventsById.keys())
|
||||||
|
|
||||||
this.eventsById.clear()
|
this.eventsById.clear()
|
||||||
this.eventsByWrap.clear()
|
|
||||||
this.eventsByAddress.clear()
|
this.eventsByAddress.clear()
|
||||||
this.eventsByTag.clear()
|
this.eventsByTag.clear()
|
||||||
this.eventsByDay.clear()
|
this.eventsByDay.clear()
|
||||||
@@ -80,13 +64,11 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
|
|
||||||
const added = []
|
const added = []
|
||||||
|
|
||||||
for (const eventsChunk of chunk(chunkSize, events)) {
|
for (const event of events) {
|
||||||
for (const event of eventsChunk) {
|
if (this.publish(event, {shouldNotify: false})) {
|
||||||
if (this.publish(event, {shouldNotify: false})) {
|
// Don't send duplicate events to subscribers
|
||||||
// Don't send duplicate events to subscribers
|
if (!stale.has(event.id)) {
|
||||||
if (!stale.has(event.id)) {
|
added.push(event)
|
||||||
added.push(event)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,7 +103,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
: this.eventsById.get(idOrAddress)
|
: this.eventsById.get(idOrAddress)
|
||||||
}
|
}
|
||||||
|
|
||||||
hasEvent = (event: E) => {
|
hasEvent = (event: TrustedEvent) => {
|
||||||
const duplicate = this.eventsById.get(event.id) || this.eventsByAddress.get(getAddress(event))
|
const duplicate = this.eventsById.get(event.id) || this.eventsByAddress.get(getAddress(event))
|
||||||
|
|
||||||
return duplicate && duplicate.created_at >= event.created_at
|
return duplicate && duplicate.created_at >= event.created_at
|
||||||
@@ -132,11 +114,6 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
|
|
||||||
if (event) {
|
if (event) {
|
||||||
this.eventsById.delete(event.id)
|
this.eventsById.delete(event.id)
|
||||||
|
|
||||||
for (const wrap of event.wraps || []) {
|
|
||||||
this.eventsByWrap.delete(wrap.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
this.eventsByAddress.delete(getAddress(event))
|
this.eventsByAddress.delete(getAddress(event))
|
||||||
|
|
||||||
for (const [k, v] of event.tags) {
|
for (const [k, v] of event.tags) {
|
||||||
@@ -157,7 +134,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
{includeDeleted = false, includeExpired = false, shouldSort = true} = {},
|
{includeDeleted = false, includeExpired = false, shouldSort = true} = {},
|
||||||
) => {
|
) => {
|
||||||
const result: E[][] = []
|
const result: TrustedEvent[][] = []
|
||||||
for (const originalFilter of filters) {
|
for (const originalFilter of filters) {
|
||||||
if (originalFilter.limit !== undefined && !shouldSort) {
|
if (originalFilter.limit !== undefined && !shouldSort) {
|
||||||
throw new Error("Unable to skip sorting if limit is defined")
|
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 events = applied ? this._getEvents(applied!.ids) : this.dump()
|
||||||
const sorted = this._sortEvents(shouldSort && Boolean(filter.limit), events)
|
const sorted = this._sortEvents(shouldSort && Boolean(filter.limit), events)
|
||||||
|
|
||||||
const chunk: E[] = []
|
const chunk: TrustedEvent[] = []
|
||||||
for (const event of sorted) {
|
for (const event of sorted) {
|
||||||
if (filter.limit && chunk.length >= filter.limit) {
|
if (filter.limit && chunk.length >= filter.limit) {
|
||||||
break
|
break
|
||||||
@@ -197,7 +174,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
return this._sortEvents(shouldSortAll, uniq(flatten(result)))
|
return this._sortEvents(shouldSortAll, uniq(flatten(result)))
|
||||||
}
|
}
|
||||||
|
|
||||||
publish = (event: E, {shouldNotify = true} = {}): boolean => {
|
publish = (event: TrustedEvent, {shouldNotify = true} = {}): boolean => {
|
||||||
if (!event?.id) {
|
if (!event?.id) {
|
||||||
console.warn("Attempted to publish invalid event to repository", event)
|
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)
|
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
|
// Update our timestamp and author indexes
|
||||||
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate)
|
this._updateIndex(this.eventsByDay, getDay(event.created_at), event, duplicate)
|
||||||
this._updateIndex(this.eventsByAuthor, event.pubkey, 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
|
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)
|
const ts = this.expired.get(event.id)
|
||||||
|
|
||||||
return Boolean(ts && ts < now())
|
return Boolean(ts && ts < now())
|
||||||
@@ -293,14 +266,19 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
|
|
||||||
// Utilities
|
// Utilities
|
||||||
|
|
||||||
_sortEvents = (shouldSort: boolean, events: E[]) =>
|
_sortEvents = (shouldSort: boolean, events: TrustedEvent[]) =>
|
||||||
shouldSort ? sortBy(e => -e.created_at, events) : events
|
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) || []
|
let a = m.get(k) || []
|
||||||
|
|
||||||
if (remove) {
|
if (remove) {
|
||||||
a = a.filter((x: E) => x !== remove)
|
a = a.filter((x: TrustedEvent) => x !== remove)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (add) {
|
if (add) {
|
||||||
@@ -311,7 +289,7 @@ export class Repository<E extends TrustedEvent = TrustedEvent> extends Emitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_getEvents = (ids: Iterable<string>) => {
|
_getEvents = (ids: Iterable<string>) => {
|
||||||
const events: E[] = []
|
const events: TrustedEvent[] = []
|
||||||
|
|
||||||
for (const id of ids) {
|
for (const id of ids) {
|
||||||
const event = this.eventsById.get(id)
|
const event = this.eventsById.get(id)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,7 +4,16 @@ import * as nt04 from "nostr-tools/nip04"
|
|||||||
import * as nt44 from "nostr-tools/nip44"
|
import * as nt44 from "nostr-tools/nip44"
|
||||||
import {generateSecretKey, getPublicKey, getEventHash} from "nostr-tools/pure"
|
import {generateSecretKey, getPublicKey, getEventHash} from "nostr-tools/pure"
|
||||||
import {Emitter, cached, now} from "@welshman/lib"
|
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())
|
export const makeSecret = () => bytesToHex(generateSecretKey())
|
||||||
|
|
||||||
|
|||||||
@@ -101,25 +101,6 @@ describe("Events", () => {
|
|||||||
expect(Events.isSignedEvent(createSignedEvent())).toBe(true)
|
expect(Events.isSignedEvent(createSignedEvent())).toBe(true)
|
||||||
expect(Events.isSignedEvent(createHashedEvent())).toBe(false)
|
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", () => {
|
describe("event conversion", () => {
|
||||||
@@ -152,34 +133,12 @@ describe("Events", () => {
|
|||||||
const trustedEvent = {
|
const trustedEvent = {
|
||||||
...createHashedEvent(),
|
...createHashedEvent(),
|
||||||
sig: sig,
|
sig: sig,
|
||||||
wraps: [createSignedEvent()],
|
nonsense: 1,
|
||||||
}
|
}
|
||||||
const result = Events.asSignedEvent(trustedEvent)
|
const result = Events.asSignedEvent(trustedEvent)
|
||||||
expect(result).not.toHaveProperty("wraps")
|
expect(result).not.toHaveProperty("nonsense")
|
||||||
expect(result).toHaveProperty("sig")
|
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", () => {
|
describe("signature validation", () => {
|
||||||
|
|||||||
@@ -182,14 +182,6 @@ describe("Filters", () => {
|
|||||||
const result = getReplyFilters([event])
|
const result = getReplyFilters([event])
|
||||||
expect((result[0] as any)["#a"]).toBeDefined()
|
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", () => {
|
describe("addRepostFilters", () => {
|
||||||
|
|||||||
@@ -40,13 +40,8 @@ export type SignedEvent = HashedEvent & {
|
|||||||
[verifiedSymbol]?: boolean
|
[verifiedSymbol]?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export type UnwrappedEvent = HashedEvent & {
|
|
||||||
wraps: SignedEvent[]
|
|
||||||
}
|
|
||||||
|
|
||||||
export type TrustedEvent = HashedEvent & {
|
export type TrustedEvent = HashedEvent & {
|
||||||
sig?: string
|
sig?: string
|
||||||
wraps?: SignedEvent[]
|
|
||||||
[verifiedSymbol]?: boolean
|
[verifiedSymbol]?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -110,12 +105,6 @@ export const isHashedEvent = (e: HashedEvent): e is HashedEvent =>
|
|||||||
export const isSignedEvent = (e: TrustedEvent): e is SignedEvent =>
|
export const isSignedEvent = (e: TrustedEvent): e is SignedEvent =>
|
||||||
Boolean(isHashedEvent(e) && typeof e.sig === "string" && e.sig.length > 0)
|
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
|
// Type coercion and attribute stripping
|
||||||
|
|
||||||
export const asEventTemplate = (e: EventTemplate): EventTemplate =>
|
export const asEventTemplate = (e: EventTemplate): EventTemplate =>
|
||||||
@@ -133,12 +122,6 @@ export const asHashedEvent = (e: HashedEvent): HashedEvent =>
|
|||||||
export const asSignedEvent = (e: SignedEvent): SignedEvent =>
|
export const asSignedEvent = (e: SignedEvent): SignedEvent =>
|
||||||
pick(["kind", "tags", "content", "created_at", "pubkey", "id", "sig"], e)
|
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
|
// Utilities for working with events
|
||||||
|
|
||||||
export const getIdentifier = (e: EventTemplate) => e.tags.find(t => t[0] === "d")?.[1]
|
export const getIdentifier = (e: EventTemplate) => e.tags.find(t => t[0] === "d")?.[1]
|
||||||
|
|||||||
@@ -178,8 +178,6 @@ export const getReplyFilters = (events: TrustedEvent[], filter: Filter = {}) =>
|
|||||||
if (isReplaceableKind(event.kind)) {
|
if (isReplaceableKind(event.kind)) {
|
||||||
a.push(getAddress(event))
|
a.push(getAddress(event))
|
||||||
}
|
}
|
||||||
|
|
||||||
event.wraps?.forEach(wrap => e.push(wrap.id))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const filters = []
|
const filters = []
|
||||||
|
|||||||
@@ -198,7 +198,4 @@ export const DEPRECATED_RELAY_RECOMMENDATION = 2
|
|||||||
export const DEPRECATED_DIRECT_MESSAGE = 4
|
export const DEPRECATED_DIRECT_MESSAGE = 4
|
||||||
export const DEPRECATED_NAMED_GENERIC = 30001
|
export const DEPRECATED_NAMED_GENERIC = 30001
|
||||||
|
|
||||||
export const WRAPPED_KINDS = [
|
export const WRAPPED_KINDS = [DIRECT_MESSAGE, DIRECT_MESSAGE_FILE]
|
||||||
DIRECT_MESSAGE,
|
|
||||||
DIRECT_MESSAGE_FILE,
|
|
||||||
]
|
|
||||||
|
|||||||
Generated
+3
@@ -266,6 +266,9 @@ importers:
|
|||||||
'@welshman/lib':
|
'@welshman/lib':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../lib
|
version: link:../lib
|
||||||
|
'@welshman/signer':
|
||||||
|
specifier: workspace:*
|
||||||
|
version: link:../signer
|
||||||
'@welshman/util':
|
'@welshman/util':
|
||||||
specifier: workspace:*
|
specifier: workspace:*
|
||||||
version: link:../util
|
version: link:../util
|
||||||
|
|||||||
Reference in New Issue
Block a user