Re-work publishing of wrapped events

This commit is contained in:
Jon Staab
2025-10-17 11:33:22 -07:00
parent 543dbda64f
commit ca38cbe20b
11 changed files with 141 additions and 116 deletions
+8 -9
View File
@@ -1,15 +1,12 @@
import {UnwrappedEvent, SignedEvent, HashedEvent, StampedEvent, WRAP, SEAL} from "@welshman/util"
import {own, hash, decrypt, ISigner} from "./util.js"
import {isHashedEvent, SignedEvent, HashedEvent, StampedEvent, WRAP, SEAL} from "@welshman/util"
import {prep, hash, decrypt, ISigner} from "./util.js"
import {Nip01Signer} from "./signers/nip01.js"
export const seen = new Map<string, UnwrappedEvent | Error>()
export const seen = new Map<string, HashedEvent | Error>()
export const now = (drift = 0) =>
Math.round(Date.now() / 1000 - Math.random() * Math.pow(10, drift))
export const getRumor = async (signer: ISigner, template: StampedEvent) =>
hash(own(template, await signer.getPubkey()))
export const getSeal = async (signer: ISigner, pubkey: string, rumor: HashedEvent) =>
signer.sign(
hash({
@@ -44,11 +41,12 @@ export const wrap = async (
template: StampedEvent,
tags: string[][] = [],
) => {
const rumor = await getRumor(signer, template)
const author = await signer.getPubkey()
const rumor = await prep(template, author)
const seal = await getSeal(signer, pubkey, rumor)
const wrap = await getWrap(wrapper, pubkey, seal, tags)
return Object.assign(rumor, {wrap}) as UnwrappedEvent
return wrap
}
export const unwrap = async (signer: ISigner, wrap: SignedEvent) => {
@@ -68,10 +66,11 @@ export const unwrap = async (signer: ISigner, wrap: SignedEvent) => {
const rumor = JSON.parse(await decrypt(signer, seal.pubkey, seal.content))
if (seal.pubkey !== rumor.pubkey) throw new Error("Seal pubkey does not match rumor pubkey")
if (!isHashedEvent(rumor)) throw new Error("Unwrapped object was not a hashed event")
seen.set(wrap.id, rumor)
return Object.assign(rumor, {wrap}) as UnwrappedEvent
return rumor
} catch (error) {
seen.set(wrap.id, error as Error)
+17 -1
View File
@@ -4,7 +4,7 @@ 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} from "@welshman/util"
import {SignedEvent, HashedEvent, EventTemplate, StampedEvent, OwnedEvent, isStampedEvent, isOwnedEvent, isHashedEvent} from "@welshman/util"
export const makeSecret = () => bytesToHex(generateSecretKey())
@@ -21,6 +21,22 @@ export const own = (event: StampedEvent, pubkey: string) => ({...event, pubkey})
export const hash = (event: OwnedEvent) => ({...event, id: getHash(event)})
export const prep = (event: EventTemplate, pubkey: string, created_at = now()) => {
if (!isStampedEvent(event as StampedEvent)) {
event = stamp(event, created_at)
}
if (!isOwnedEvent(event as OwnedEvent)) {
event = own(event as StampedEvent, pubkey)
}
if (!isHashedEvent(event as HashedEvent)) {
event = hash(event as OwnedEvent)
}
return event as HashedEvent
}
export const sign = (event: HashedEvent, secret: string) => ({...event, sig: getSig(event, secret)})
export const nip04 = {