Skip to content

Event Factory

The EventFactory is a class is used to provide a Signer and relay hints to the blueprints

Creating a factory

When creating a new event factory you can pass a context object in that is used by all blueprints

ts
const signer = new SimpleSigner();

const factory = new EventFactory({
  // optionally pass a signer in (required for encryption)
  signer: signer,
  // optionally set a NIP-89 client
  client: {
    name: "My Awesome Client",
    address: {
      pubkey: "3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d",
      identifier: "awesome-client",
    },
  },
});

Relay hints

Relay hints can ge added to all event tags that support them by passing in getRelayHint and getPubkeyRelayHint methods into the context

ts
const factory = new EventFactory({
  getRelayHint: async (event) => {
    // an async process to find the best relay hint for this event
    try {
      return await calculateRelayHint(event)
    }
    catch(){
      return undefined
    }
  },
  getPubkeyRelayHint: async (pubkey) => {
    // an async process to find a relay hint for this pubkey
    return loadPubkeyMailboxes(pubkey).then((mailboxes) => getOutboxes(mailboxes)[0]);
  },
});

Using a blueprint

The factory.create method can be used to create an event from a blueprint

ts
await factory.create(NoteBlueprint, "hello world");

Quick helper methods

Manually creating an event

The factory.process method can be used to create an event from an EventTemplate and Operations

ts
import { includeSingletonTag, setContent, includeAltTag } from "applesauce-factory/operations";

await factory.process(
  { kind: 1063 },
  setContent("the bitcoin whitepaper"),
  includeAltTag("File metadata"),
  includeSingletonTag(["x", "b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553"]),
  includeSingletonTag(["size", "184292"]),
  includeSingletonTag(["m", "application/pdf"]),
  includeSingletonTag([
    "url",
    "https://cdn.example.com/b1674191a88ec5cdd733e4240a81803105dc412d6c6708d53ab94fc248f4f553.pdf",
  ]),
);

Prebuilt blueprints

The NoteBlueprint can be used to create top level text notes (kind 1) and supports, quotes, emojis, and hashtags

The NoteReplyBlueprint can be used to create note replies (kind 1) to top level text note (kind 1)

IMPORTANT

The NoteReplyBlueprint only supports replying to kind 1 notes. if you need replies to other kinds use CommentBlueprint

The CommentBlueprint can be used to create NIP-22 comments on any event kind

The ReactionBlueprint can be used to create NIP-25 reactions and supports the common + and - reactions along with NIP-30 emojis

The ShareBlueprint can be used to create NIP-18 repost / share event

The DeleteBlueprint can be used to create NIP-09 delete event