Class NDKEvent

NDKEvent is the basic building block of NDK; most things you do with NDK will revolve around writing or consuming NDKEvents.

Hierarchy

Constructors

Properties

content: string = ""
created_at?: number
decrypt: ((...args: [sender?: NDKUser, signer?: NDKSigner]) => Promise<void>) = ...

Type declaration

encode: ((...args: []) => string) = ...

Type declaration

    • (...args: []): string
    • Encodes a bech32 id.

      Parameters

      • Rest ...args: []

      Returns string

      • Encoded naddr, note or nevent.
encrypt: ((...args: [recipient?: NDKUser, signer?: NDKSigner]) => Promise<void>) = ...

Type declaration

fetchReplyEvent: ((...args: [subOpts?: NDKSubscriptionOptions]) => Promise<undefined | null | NDKEvent>) = ...

Type declaration

fetchRootEvent: ((...args: [subOpts?: NDKSubscriptionOptions]) => Promise<undefined | null | NDKEvent>) = ...

Type declaration

    • (...args: [subOpts?: NDKSubscriptionOptions]): Promise<undefined | null | NDKEvent>
    • Fetch the root event of the current event.

      Parameters

      Returns Promise<undefined | null | NDKEvent>

      The fetched root event or null if no event was found

      Example

      const replyEvent = await ndk.fetchEvent("nevent1qqs8x8vnycyha73grv380gmvlury4wtmx0nr9a5ds2dngqwgu87wn6gpzemhxue69uhhyetvv9ujuurjd9kkzmpwdejhgq3ql2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqz4cwjd")
      const rootEvent = await replyEvent.fetchRootEvent();
      console.log(replyEvent.encode() + " is a reply in the thread " + rootEvent?.encode());
fetchTaggedEvent: ((...args: [tag: string, marker?: string]) => Promise<undefined | null | NDKEvent>) = ...

Type declaration

    • (...args: [tag: string, marker?: string]): Promise<undefined | null | NDKEvent>
    • Fetch an event tagged with the given tag following relay hints if provided.

      Parameters

      • Rest ...args: [tag: string, marker?: string]

      Returns Promise<undefined | null | NDKEvent>

      The fetched event or null if no event was found, undefined if no matching tag was found in the event *

      Example

      const replyEvent = await ndk.fetchEvent("nevent1qqs8x8vnycyha73grv380gmvlury4wtmx0nr9a5ds2dngqwgu87wn6gpzemhxue69uhhyetvv9ujuurjd9kkzmpwdejhgq3ql2vyh47mk2p0qlsku7hg0vn29faehy9hy34ygaclpn66ukqp3afqz4cwjd")
      const originalEvent = await replyEvent.fetchTaggedEvent("e", "reply");
      console.log(replyEvent.encode() + " is a reply to event " + originalEvent?.encode());
getEventHash: ((...args: []) => string) = ...

Type declaration

    • (...args: []): string
    • Parameters

      • Rest ...args: []

      Returns string

id: string = ""
isEphemeral: ((...args: []) => boolean) = ...

Type declaration

    • (...args: []): boolean
    • Parameters

      • Rest ...args: []

      Returns boolean

isParamReplaceable: ((...args: []) => boolean) = ...

Type declaration

    • (...args: []): boolean
    • Parameters

      • Rest ...args: []

      Returns boolean

isReplaceable: ((...args: []) => boolean) = ...

Type declaration

    • (...args: []): boolean
    • Parameters

      • Rest ...args: []

      Returns boolean

kind?: number
ndk?: default
onRelays: NDKRelay[] = []

The relays that this event was received from and/or successfully published to.

pubkey: string = ""
publishError?: Error
publishStatus?: "pending" | "success" | "error" = "success"

The status of the publish operation.

relay: undefined | NDKRelay

The relay that this event was first received from.

repost: ((...args: [publish: boolean, signer?: NDKSigner]) => Promise<NDKEvent>) = ...

Type declaration

    • (...args: [publish: boolean, signer?: NDKSigner]): Promise<NDKEvent>
    • NIP-18 reposting event.

      Parameters

      • Rest ...args: [publish: boolean, signer?: NDKSigner]

      Returns Promise<NDKEvent>

      The reposted event

      Default

      true
      

      Function

serialize: ((...args: [includeSig: boolean, includeId: boolean]) => string) = ...

Type declaration

    • (...args: [includeSig: boolean, includeId: boolean]): string
    • Parameters

      • Rest ...args: [includeSig: boolean, includeId: boolean]

      Returns string

sig?: string
signatureVerified?: boolean
tags: NDKTag[] = []
validate: ((...args: []) => boolean) = ...

Type declaration

    • (...args: []): boolean
    • Parameters

      • Rest ...args: []

      Returns boolean

verifySignature: ((...args: [persist: boolean]) => undefined | boolean) = ...

Type declaration

    • (...args: [persist: boolean]): undefined | boolean
    • Parameters

      • Rest ...args: [persist: boolean]

      Returns undefined | boolean

Accessors

  • get alt(): undefined | string
  • Gets the NIP-31 "alt" tag of the event.

    Returns undefined | string

  • set alt(alt: undefined | string): void
  • Sets the NIP-31 "alt" tag of the event. Use this to set an alt tag so clients that don't handle a particular event kind can display something useful for users.

    Parameters

    • alt: undefined | string

    Returns void

  • get dTag(): undefined | string
  • Gets the NIP-33 "d" tag of the event.

    Returns undefined | string

  • set dTag(value: undefined | string): void
  • Sets the NIP-33 "d" tag of the event.

    Parameters

    • value: undefined | string

    Returns void

  • get isValid(): boolean
  • Checks whether the event is valid per underlying NIPs.

    This method is meant to be overridden by subclasses that implement specific NIPs to allow the enforcement of NIP-specific validation rules.

    Otherwise, it will only check for basic event properties.

    Returns boolean

Methods

  • Provides a deduplication key for the event.

    For kinds 0, 3, 10k-20k this will be the event : For kinds 30k-40k this will be the event :: For all other kinds this will be the event id

    Returns string

  • Generates a deletion event of the current event

    Parameters

    • Optional reason: string

      The reason for the deletion

    • publish: boolean = true

      Whether to publish the deletion event automatically

    Returns Promise<NDKEvent>

    The deletion event

  • Provides the filter that will return matching events for this event.

    Returns NDKFilter

    The filter that will return matching events for this event

    Example

    event = new NDKEvent(ndk, { kind: 30000, pubkey: 'pubkey', tags: [ ["d", "d-code"] ] });
    event.filter(); // { "#a": ["30000:pubkey:d-code"] }

    Example

    event = new NDKEvent(ndk, { kind: 1, pubkey: 'pubkey', id: "eventid" });
    event.filter(); // { "#e": ["eventid"] }
  • Generates tags for users, notes, and other events tagged in content. Will also generate random "d" tag for parameterized replaceable events where needed.

    Returns Promise<ContentTag>

    The tags and content of the event.

  • Get all tags with the given name

    Parameters

    • tagName: string

      {string} The name of the tag to search for

    • Optional marker: string

    Returns NDKTag[]

    An array of the matching tags

  • Attempt to sign and then publish an NDKEvent to a given relaySet. If no relaySet is provided, the relaySet will be calculated by NDK.

    Parameters

    • Optional relaySet: NDKRelaySet

      {NDKRelaySet} The relaySet to publish the even to.

    • Optional timeoutMs: number
    • Optional requiredRelayCount: number

      The number of relays that must receive the event for the publish to be considered successful.

    Returns Promise<Set<NDKRelay>>

    A promise that resolves to the relays the event was published to.

  • React to an existing event

    Parameters

    • content: string

      The content of the reaction

    • publish: boolean = true

    Returns Promise<NDKEvent>

  • Get the tags that can be used to reference this event from another event

    Parameters

    • Optional marker: string

      The marker to use in the tag

    • Optional skipAuthorTag: boolean

      Whether to explicitly skip adding the author tag of the event

    • Optional forceTag: string

      Force a specific tag to be used instead of the default "e" or "a" tag

    Returns NDKTag[]

    The NDKTag object referencing this event

    Example

    event = new NDKEvent(ndk, { kind: 30000, pubkey: 'pubkey', tags: [ ["d", "d-code"] ] });
    event.referenceTags(); // [["a", "30000:pubkey:d-code"], ["e", "parent-id"]]

    event = new NDKEvent(ndk, { kind: 1, pubkey: 'pubkey', id: "eventid" });
    event.referenceTags(); // [["e", "parent-id"]]
  • Remove all tags with the given name (e.g. "d", "a", "p")

    Parameters

    • tagName: string

      Tag name to search for and remove

    Returns void

  • Returns the "d" tag of a parameterized replaceable event or throws an error if the event isn't a parameterized replaceable event.

    Returns string

    the "d" tag of the event.

  • Sign the event if a signer is present.

    It will generate tags. Repleacable events will have their created_at field set to the current time.

    Parameters

    • Optional signer: NDKSigner

      {NDKSigner} The NDKSigner to use to sign the event

    Returns Promise<string>

    A Promise that resolves to the signature of the signed event.

  • Tag a user with an optional marker.

    Parameters

    • user: NDKUser

      The user to tag.

    • Optional marker: string

      The marker to use in the tag.

    Returns void

  • Tag a user with an optional marker.

    Parameters

    • user: NDKUser

      The user to tag.

    • Optional marker: string

      The marker to use in the tag.

    Returns void

  • Tag a user with an optional marker.

    Parameters

    • event: NDKEvent

      The event to tag.

    • Optional marker: string

      The marker to use in the tag.

    • Optional skipAuthorTag: boolean

      Whether to explicitly skip adding the author tag of the event.

    • Optional forceTag: string

      Force a specific tag to be used instead of the default "e" or "a" tag.

    Returns void

    Example

    reply.tag(opEvent, "reply");
    // reply.tags => [["e", <id>, <relay>, "reply"]]
  • Returns the "reference" value ("::") for this replaceable event.

    Returns string

    The id

  • Returns the id of the event or, if it's a parameterized event, the generated id of the event using "d" tag, pubkey, and kind.

    Returns string

    The id

  • Get the tag that can be used to reference this event from another event.

    Consider using referenceTags() instead (unless you have a good reason to use this)

    Parameters

    • Optional marker: string

    Returns NDKTag

    The NDKTag object referencing this event

    Example

    event = new NDKEvent(ndk, { kind: 30000, pubkey: 'pubkey', tags: [ ["d", "d-code"] ] });
    event.tagReference(); // ["a", "30000:pubkey:d-code"]

    event = new NDKEvent(ndk, { kind: 1, pubkey: 'pubkey', id: "eventid" });
    event.tagReference(); // ["e", "eventid"]
  • Determines the type of tag that can be used to reference this event from another event.

    Returns "a" | "e"

    The tag type

    Example

    event = new NDKEvent(ndk, { kind: 30000, pubkey: 'pubkey', tags: [ ["d", "d-code"] ] });
    event.tagType(); // "a"
  • Get the first tag with the given name

    Parameters

    • tagName: string

      Tag name to search for

    Returns undefined | string

    The value of the first tag with the given name, or undefined if no such tag exists

  • Return a NostrEvent object, trying to fill in missing fields when possible, adding tags when necessary.

    Parameters

    • Optional pubkey: string

      {string} The pubkey of the user who the event belongs to.

    Returns Promise<NostrEvent>

    A promise that resolves to a NostrEvent.

Generated using TypeDoc