diff --git a/.eslintrc.json b/.eslintrc.json index 32a0311..c506e28 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -103,7 +103,6 @@ "no-octal-escape": 2, "no-path-concat": 0, "no-proto": 2, - "no-redeclare": 2, "no-regex-spaces": 2, "no-return-assign": 0, "no-self-assign": 2, diff --git a/event.ts b/event.ts index bc2f211..1402459 100644 --- a/event.ts +++ b/event.ts @@ -6,7 +6,6 @@ import {getPublicKey} from './keys' /* eslint-disable no-unused-vars */ export enum Kind { - UndefinedKindNumber = 255, Metadata = 0, Text = 1, RecommendRelay = 2, @@ -20,6 +19,7 @@ export enum Kind { ChannelMessage = 42, ChannelHideMessage = 43, ChannelMuteUser = 44, + Blank = 255, Report = 1984, ZapRequest = 9734, Zap = 9735, @@ -46,24 +46,26 @@ export type Event = UnsignedEvent & { sig: string } -export function getBlankEvent(kind?: K): EventTemplate { +export function getBlankEvent(): EventTemplate +export function getBlankEvent(kind: K): EventTemplate +export function getBlankEvent(kind: K | Kind.Blank = Kind.Blank) { return { - kind: kind || ( Kind.UndefinedKindNumber as K), + kind, content: '', tags: [], created_at: 0 } } -export function finishEvent(t: EventTemplate, privateKey: string): Event { - let event = t as Event +export function finishEvent(t: EventTemplate, privateKey: string): Event { + let event = t as Event event.pubkey = getPublicKey(privateKey) event.id = getEventHash(event) event.sig = getSignature(event, privateKey) return event } -export function serializeEvent(evt: UnsignedEvent): string { +export function serializeEvent(evt: UnsignedEvent): string { if (!validateEvent(evt)) throw new Error("can't serialize event with wrong or missing properties") @@ -77,14 +79,14 @@ export function serializeEvent(evt: UnsignedEvent): string { ]) } -export function getEventHash(event: UnsignedEvent): string { +export function getEventHash(event: UnsignedEvent): string { let eventHash = sha256(utf8Encoder.encode(serializeEvent(event))) return secp256k1.utils.bytesToHex(eventHash) } const isRecord = (obj: unknown): obj is Record => obj instanceof Object -export function validateEvent(event: T): event is T & UnsignedEvent { +export function validateEvent(event: T): event is T & UnsignedEvent { if (!isRecord(event)) return false if (typeof event.kind !== 'number') return false if (typeof event.content !== 'string') return false @@ -104,7 +106,7 @@ export function validateEvent(event: T): event is T & UnsignedEvent { return true } -export function verifySignature(event: Event): boolean { +export function verifySignature(event: Event): boolean { return secp256k1.schnorr.verifySync( event.sig, getEventHash(event), @@ -113,7 +115,7 @@ export function verifySignature(event: Event): boolean { } /** @deprecated Use `getSignature` instead. */ -export function signEvent(event: UnsignedEvent, key: string): string { +export function signEvent(event: UnsignedEvent, key: string): string { console.warn( 'nostr-tools: `signEvent` is deprecated and will be removed or changed in the future. Please use `getSignature` instead.' ) @@ -121,7 +123,7 @@ export function signEvent(event: UnsignedEvent, key: string): string { } /** Calculate the signature for an event. */ -export function getSignature(event: UnsignedEvent, key: string): string { +export function getSignature(event: UnsignedEvent, key: string): string { return secp256k1.utils.bytesToHex( secp256k1.schnorr.signSync(getEventHash(event), key) ) diff --git a/nip04.ts b/nip04.ts index 3428a08..91d55fa 100644 --- a/nip04.ts +++ b/nip04.ts @@ -5,7 +5,7 @@ import {base64} from '@scure/base' import {utf8Decoder, utf8Encoder} from './utils' // @ts-ignore -if (typeof(crypto) !== 'undefined' && !crypto.subtle && crypto.webcrypto) { +if (typeof crypto !== 'undefined' && !crypto.subtle && crypto.webcrypto) { // @ts-ignore crypto.subtle = crypto.webcrypto.subtle } diff --git a/nip25.ts b/nip25.ts index 6d8fad8..d50061d 100644 --- a/nip25.ts +++ b/nip25.ts @@ -17,9 +17,9 @@ export type ReactionEventTemplate = { export function finishReactionEvent( t: ReactionEventTemplate, - reacted: Event, + reacted: Event, privateKey: string, -): Event { +): Event { const inheritedTags = reacted.tags.filter( (tag) => tag.length >= 2 && (tag[0] === 'e' || tag[0] === 'p'), ) @@ -37,7 +37,7 @@ export function finishReactionEvent( }, privateKey) } -export function getReactedEventPointer(event: Event): undefined | EventPointer { +export function getReactedEventPointer(event: Event): undefined | EventPointer { if (event.kind !== Kind.Reaction) { return undefined } diff --git a/nip26.ts b/nip26.ts index 150135a..d28f8f3 100644 --- a/nip26.ts +++ b/nip26.ts @@ -48,7 +48,7 @@ export function createDelegation( } } -export function getDelegator(event: Event): string | null { +export function getDelegator(event: Event): string | null { // find delegation tag let tag = event.tags.find(tag => tag[0] === 'delegation' && tag.length >= 4) if (!tag) return null diff --git a/nip42.ts b/nip42.ts index cd873a9..4fb85b6 100644 --- a/nip42.ts +++ b/nip42.ts @@ -17,7 +17,7 @@ export const authenticate = async ({ }: { challenge: string relay: Relay - sign: (e: EventTemplate) => Promise + sign: (e: EventTemplate) => Promise> }): Promise => { const e: EventTemplate = { kind: Kind.ClientAuth, diff --git a/nip57.ts b/nip57.ts index 83bf8fd..77f0c12 100644 --- a/nip57.ts +++ b/nip57.ts @@ -13,7 +13,7 @@ export function useFetchImplementation(fetchImplementation: any) { _fetch = fetchImplementation } -export async function getZapEndpoint(metadata: Event): Promise { +export async function getZapEndpoint(metadata: Event<0>): Promise { try { let lnurl: string = '' let {lud06, lud16} = JSON.parse(metadata.content) @@ -53,11 +53,11 @@ export function makeZapRequest({ amount: number comment: string relays: string[] -}): EventTemplate { +}): EventTemplate<9734> { if (!amount) throw new Error('amount not given') if (!profile) throw new Error('profile not given') - let zr = { + let zr: EventTemplate<9734> = { kind: 9734, created_at: Math.round(Date.now() / 1000), content: comment, @@ -113,13 +113,13 @@ export function makeZapReceipt({ preimage: string | null bolt11: string paidAt: Date -}): EventTemplate { - let zr: Event = JSON.parse(zapRequest) +}): EventTemplate<9735> { + let zr: Event<9734> = JSON.parse(zapRequest) let tagsFromZapRequest = zr.tags.filter( ([t]) => t === 'e' || t === 'p' || t === 'a' ) - let zap = { + let zap: EventTemplate<9735> = { kind: 9735, created_at: Math.round(paidAt.getTime() / 1000), content: '', diff --git a/utils.ts b/utils.ts index 28c26fa..904aca2 100644 --- a/utils.ts +++ b/utils.ts @@ -21,8 +21,8 @@ export function normalizeURL(url: string): string { // fast insert-into-sorted-array functions adapted from https://github.com/terrymorse58/fast-sorted-array // export function insertEventIntoDescendingList( - sortedArray: Event[], - event: Event + sortedArray: Event[], + event: Event ) { let start = 0 let end = sortedArray.length - 1 @@ -66,8 +66,8 @@ export function insertEventIntoDescendingList( } export function insertEventIntoAscendingList( - sortedArray: Event[], - event: Event + sortedArray: Event[], + event: Event ) { let start = 0 let end = sortedArray.length - 1