Add wrap manager for tracking gift wraps
This commit is contained in:
@@ -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", () => {
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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]
|
||||
|
||||
@@ -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 = []
|
||||
|
||||
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user