Remove CustomEvent

This commit is contained in:
Jon Staab
2024-08-19 08:59:39 -07:00
parent 975d51cafa
commit 8d3ca2ef6a
25 changed files with 124 additions and 120 deletions
+5 -5
View File
@@ -1,4 +1,4 @@
import {UnwrappedEvent, SignedEvent, HashedEvent, EventTemplate, WRAP, SEAL} from '@welshman/util'
import {UnwrappedEvent, SignedEvent, HashedEvent, StampedEvent, WRAP, SEAL} from '@welshman/util'
import {own, hash, decrypt, ISigner} from './util'
import {Nip01Signer} from './signers/nip01'
@@ -7,8 +7,8 @@ export const seen = new Map<string, UnwrappedEvent | Error>()
export const now = (drift = 0) =>
Math.round(Date.now() / 1000 - Math.random() * Math.pow(10, drift))
export const getRumor = async (signer: ISigner, template: EventTemplate) =>
hash(own(await signer.getPubkey(), template))
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({
@@ -28,7 +28,7 @@ export const getWrap = async (wrapper: ISigner, pubkey: string, seal: SignedEven
tags: [...tags, ["p", pubkey]],
}))
export const wrap = async (signer: ISigner, wrapper: ISigner, pubkey: string, template: EventTemplate, tags: string[][] = []) => {
export const wrap = async (signer: ISigner, wrapper: ISigner, pubkey: string, template: StampedEvent, tags: string[][] = []) => {
const rumor = await getRumor(signer, template)
const seal = await getSeal(signer, pubkey, rumor)
const wrap = await getWrap(wrapper, pubkey, seal, tags)
@@ -77,7 +77,7 @@ export class Nip59 {
withWrapper = (wrapper: ISigner) => new Nip59(this.signer, wrapper)
wrap = (pubkey: string, template: EventTemplate, tags: string[][] = []) =>
wrap = (pubkey: string, template: StampedEvent, tags: string[][] = []) =>
wrap(this.signer, this.wrapper || Nip01Signer.ephemeral(), pubkey, template, tags)
unwrap = (event: SignedEvent) =>
+2 -2
View File
@@ -1,4 +1,4 @@
import {EventTemplate} from '@welshman/util'
import {StampedEvent} from '@welshman/util'
import {nip04, nip44, own, hash, sign, getPubkey, ISigner, makeSecret} from "../util"
export class Nip01Signer implements ISigner {
@@ -14,7 +14,7 @@ export class Nip01Signer implements ISigner {
getPubkey = async () => this.pubkey
sign = async (event: EventTemplate) => sign(hash(own(this.pubkey, event)), this.secret)
sign = async (event: StampedEvent) => sign(hash(own(event, this.pubkey)), this.secret)
nip04 = {
encrypt: async (pubkey: string, message: string) =>
+3 -3
View File
@@ -1,4 +1,4 @@
import {EventTemplate} from '@welshman/util'
import {StampedEvent} from '@welshman/util'
import {hash, own, Sign, ISigner, EncryptionImplementation} from '../util'
export type Nip07 = {
@@ -30,8 +30,8 @@ export class Nip07Signer implements ISigner {
getPubkey = async () => getNip07()!.getPublicKey()!
sign = async (template: EventTemplate) => {
const event = hash(own(await this.getPubkey(), template))
sign = async (template: StampedEvent) => {
const event = hash(own(template, await this.getPubkey()))
return this.#then(ext => ext.signEvent(event))
}
+4 -4
View File
@@ -1,7 +1,7 @@
import {finalizeEvent, getPublicKey} from "nostr-tools"
import {hexToBytes} from '@noble/hashes/utils'
import {Emitter, tryCatch, randomId, sleep, equals, now} from "@welshman/lib"
import {createEvent, TrustedEvent, EventTemplate, NOSTR_CONNECT} from "@welshman/util"
import {createEvent, TrustedEvent, StampedEvent, NOSTR_CONNECT} from "@welshman/util"
import {subscribe, publish, Subscription} from "@welshman/net"
import {nip04, nip44, ISigner, decrypt, hash, own} from '../util'
import {Nip01Signer} from './nip01'
@@ -138,7 +138,7 @@ export class Nip46Broker extends Emitter {
return this.#connectResult === "ack"
}
signEvent = async (event: EventTemplate) => {
signEvent = async (event: StampedEvent) => {
return JSON.parse(await this.request("sign_event", [JSON.stringify(event)]) as string)
}
@@ -169,8 +169,8 @@ export class Nip46Signer implements ISigner {
getPubkey = async () => this.broker.pubkey
sign = (template: EventTemplate) =>
this.broker.signEvent(hash(own(this.broker.pubkey, template)))
sign = (template: StampedEvent) =>
this.broker.signEvent(hash(own(template, this.broker.pubkey)))
nip04 = {
encrypt: this.broker.nip04Encrypt,
+7 -5
View File
@@ -1,8 +1,8 @@
import {schnorr} from '@noble/curves/secp256k1'
import {bytesToHex, hexToBytes} from '@noble/hashes/utils'
import {nip04 as nt04, nip44 as nt44, generateSecretKey, getPublicKey, getEventHash} from "nostr-tools"
import {cached} from '@welshman/lib'
import {SignedEvent, HashedEvent, EventTemplate, OwnedEvent} from '@welshman/util'
import {cached, now} from '@welshman/lib'
import {SignedEvent, HashedEvent, EventTemplate, StampedEvent, OwnedEvent} from '@welshman/util'
export const makeSecret = () => bytesToHex(generateSecretKey())
@@ -13,9 +13,11 @@ export const getHash = (event: OwnedEvent) => getEventHash(event)
export const getSig = (event: HashedEvent, secret: string) =>
bytesToHex(schnorr.sign(event.id, secret))
export const own = (pubkey: string, event: EventTemplate) => ({...event, pubkey})
export const stamp = (event: EventTemplate, created_at = now()) => ({...event, created_at})
export const hash = (event: OwnedEvent): HashedEvent => ({...event, id: getHash(event)})
export const own = (event: StampedEvent, pubkey: string) => ({...event, pubkey})
export const hash = (event: OwnedEvent) => ({...event, id: getHash(event)})
export const sign = (event: HashedEvent, secret: string) => ({...event, sig: getSig(event, secret)})
@@ -35,7 +37,7 @@ export const nip44 = {
decrypt: (pubkey: string, secret: string, m: string) => nt44.v2.decrypt(m, nip44.getSharedSecret(secret, pubkey)),
}
export type Sign = (event: EventTemplate) => Promise<SignedEvent>
export type Sign = (event: StampedEvent) => Promise<SignedEvent>
export type Encrypt = (pubkey: string, message: string) => Promise<string>