applesauce-core 5.2.0 → 6.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/dist/casts/cast.d.ts +31 -0
  2. package/dist/casts/cast.js +67 -0
  3. package/dist/casts/index.d.ts +3 -0
  4. package/dist/casts/index.js +3 -0
  5. package/dist/casts/pubkey.d.ts +27 -0
  6. package/dist/casts/pubkey.js +49 -0
  7. package/dist/casts/user.d.ts +19 -0
  8. package/dist/casts/user.js +25 -0
  9. package/dist/factories/delete.d.ts +18 -0
  10. package/dist/factories/delete.js +23 -0
  11. package/dist/factories/event.d.ts +61 -0
  12. package/dist/factories/event.js +164 -0
  13. package/dist/factories/index.d.ts +5 -0
  14. package/dist/factories/index.js +5 -0
  15. package/dist/factories/mailboxes.d.ts +33 -0
  16. package/dist/factories/mailboxes.js +57 -0
  17. package/dist/factories/profile.d.ts +46 -0
  18. package/dist/factories/profile.js +80 -0
  19. package/dist/factories/types.d.ts +56 -0
  20. package/dist/helpers/event.d.ts +11 -2
  21. package/dist/helpers/event.js +2 -1
  22. package/dist/helpers/mailboxes.d.ts +5 -1
  23. package/dist/helpers/mailboxes.js +5 -0
  24. package/dist/helpers/pipeline.d.ts +14 -1
  25. package/dist/helpers/pipeline.js +17 -2
  26. package/dist/helpers/pointers.js +2 -2
  27. package/dist/helpers/profile.d.ts +2 -2
  28. package/dist/helpers/regexp.d.ts +2 -0
  29. package/dist/helpers/regexp.js +8 -2
  30. package/dist/helpers/relays.d.ts +3 -1
  31. package/dist/helpers/relays.js +8 -10
  32. package/dist/helpers/url.d.ts +1 -4
  33. package/dist/helpers/url.js +1 -4
  34. package/dist/index.d.ts +3 -1
  35. package/dist/index.js +4 -1
  36. package/dist/observable/catch-error-inline.d.ts +3 -0
  37. package/dist/observable/catch-error-inline.js +5 -0
  38. package/dist/observable/chainable.d.ts +36 -0
  39. package/dist/observable/chainable.js +72 -0
  40. package/dist/observable/combine-latest-by-index.d.ts +10 -0
  41. package/dist/observable/combine-latest-by-index.js +121 -0
  42. package/dist/observable/combine-latest-by-key.d.ts +10 -0
  43. package/dist/observable/combine-latest-by-key.js +136 -0
  44. package/dist/observable/combine-latest-by-value.d.ts +10 -0
  45. package/dist/observable/combine-latest-by-value.js +117 -0
  46. package/dist/observable/combine-latest-by.d.ts +16 -0
  47. package/dist/observable/combine-latest-by.js +12 -0
  48. package/dist/observable/index.d.ts +10 -4
  49. package/dist/observable/index.js +10 -4
  50. package/dist/observable/timeout-with-ignore.d.ts +24 -0
  51. package/dist/observable/timeout-with-ignore.js +33 -0
  52. package/dist/operations/client.d.ts +1 -1
  53. package/dist/operations/client.js +2 -2
  54. package/dist/operations/content.d.ts +5 -2
  55. package/dist/operations/content.js +9 -7
  56. package/dist/operations/delete.d.ts +1 -1
  57. package/dist/operations/encrypted-content.d.ts +9 -3
  58. package/dist/operations/encrypted-content.js +9 -3
  59. package/dist/operations/event.d.ts +6 -4
  60. package/dist/operations/event.js +20 -12
  61. package/dist/operations/hidden-content.d.ts +8 -3
  62. package/dist/operations/hidden-content.js +11 -6
  63. package/dist/operations/mailboxes.d.ts +5 -1
  64. package/dist/operations/mailboxes.js +76 -0
  65. package/dist/operations/profile.d.ts +1 -1
  66. package/dist/operations/tag/common.d.ts +22 -7
  67. package/dist/operations/tag/common.js +33 -18
  68. package/dist/operations/tag/relay.d.ts +1 -1
  69. package/dist/operations/tags.d.ts +10 -4
  70. package/dist/operations/tags.js +19 -13
  71. package/package.json +20 -5
  72. package/dist/event-factory/event-factory.d.ts +0 -57
  73. package/dist/event-factory/event-factory.js +0 -94
  74. package/dist/event-factory/index.d.ts +0 -3
  75. package/dist/event-factory/index.js +0 -3
  76. package/dist/event-factory/methods.d.ts +0 -17
  77. package/dist/event-factory/methods.js +0 -44
  78. package/dist/event-factory/types.d.ts +0 -78
  79. /package/dist/{event-factory → factories}/types.js +0 -0
@@ -1,5 +1,5 @@
1
- import { EventOperation } from "../event-factory/types.js";
2
- import { EventTemplate, NostrEvent, UnsignedEvent } from "../helpers/event.js";
1
+ import type { EventOperation } from "../factories/types.js";
2
+ import { EventTemplate, KnownEvent, KnownEventTemplate, KnownUnsignedEvent, NostrEvent, UnsignedEvent } from "../helpers/event.js";
3
3
  /** An operation that removes the signature from the event template */
4
4
  export declare function stripSignature<Input extends NostrEvent | UnsignedEvent | EventTemplate>(): EventOperation<Input, Omit<Input, "sig">>;
5
5
  /** An operation that removes the id and pubkey from the event template */
@@ -29,11 +29,13 @@ export type MetaTagOptions = {
29
29
  export declare function setMetaTags(options?: MetaTagOptions): EventOperation;
30
30
  /**
31
31
  * An operation that adds the signers pubkey to the event
32
+ * @param signer - Optional EventSigner (throws if not provided)
32
33
  * @throws {Error} if no signer is provided
33
34
  */
34
- export declare function stamp(): EventOperation<EventTemplate, UnsignedEvent>;
35
+ export declare function stamp<K extends number = number>(signer?: import("../factories/types.js").EventSigner): EventOperation<KnownEventTemplate<K>, KnownUnsignedEvent<K>>;
35
36
  /**
36
37
  * An operation that signs the event
38
+ * @param signer - Optional EventSigner (throws if not provided)
37
39
  * @throws {Error} if no signer is provided
38
40
  */
39
- export declare function sign(): EventOperation<EventTemplate | UnsignedEvent, NostrEvent>;
41
+ export declare function sign<K extends number = number, T extends KnownEventTemplate<K> = KnownEventTemplate<K>>(signer?: import("../factories/types.js").EventSigner): EventOperation<T, KnownEvent<K>>;
@@ -1,12 +1,12 @@
1
1
  import { nanoid } from "nanoid";
2
- import { getTagValue } from "../helpers/event.js";
3
- import { isAddressableKind } from "../helpers/event.js";
2
+ import { isKind } from "nostr-tools/kinds";
3
+ import { EncryptedContentSymbol } from "../helpers/encrypted-content.js";
4
+ import { getTagValue, isAddressableKind, } from "../helpers/event.js";
4
5
  import { eventPipe, skip } from "../helpers/pipeline.js";
5
6
  import { ensureSingletonTag } from "../helpers/tags.js";
6
7
  import { unixNow } from "../helpers/time.js";
7
8
  import { removeSingletonTag, setSingletonTag } from "./tag/common.js";
8
9
  import { includeSingletonTag, modifyPublicTags } from "./tags.js";
9
- import { EncryptedContentSymbol } from "../helpers/encrypted-content.js";
10
10
  /** An operation that removes the signature from the event template */
11
11
  export function stripSignature() {
12
12
  return (draft) => {
@@ -72,16 +72,17 @@ export function setMetaTags(options) {
72
72
  }
73
73
  /**
74
74
  * An operation that adds the signers pubkey to the event
75
+ * @param signer - Optional EventSigner (throws if not provided)
75
76
  * @throws {Error} if no signer is provided
76
77
  */
77
- export function stamp() {
78
- return async (draft, ctx) => {
79
- if (!ctx.signer)
78
+ export function stamp(signer) {
79
+ return async (draft) => {
80
+ if (!signer)
80
81
  throw new Error("Missing signer");
81
82
  // Remove old fields from signed nostr event
82
83
  Reflect.deleteProperty(draft, "id");
83
84
  Reflect.deleteProperty(draft, "sig");
84
- const pubkey = await ctx.signer.getPublicKey();
85
+ const pubkey = await signer.getPublicKey();
85
86
  const newDraft = { ...draft, pubkey };
86
87
  // copy the plaintext hidden content if its on the draft
87
88
  if (Reflect.has(draft, EncryptedContentSymbol))
@@ -91,14 +92,21 @@ export function stamp() {
91
92
  }
92
93
  /**
93
94
  * An operation that signs the event
95
+ * @param signer - Optional EventSigner (throws if not provided)
94
96
  * @throws {Error} if no signer is provided
95
97
  */
96
- export function sign() {
97
- return async (draft, ctx) => {
98
- if (!ctx.signer)
98
+ export function sign(signer) {
99
+ return async (draft) => {
100
+ if (!signer)
99
101
  throw new Error("Missing signer");
100
- draft = await stamp()(draft, ctx);
101
- const signed = await ctx.signer.signEvent(draft);
102
+ const unsigned = await stamp(signer)(draft);
103
+ const signed = await signer.signEvent(unsigned);
104
+ // Verify the pubkey has not changed
105
+ if (Reflect.has(draft, "pubkey") && Reflect.get(draft, "pubkey") !== signed.pubkey)
106
+ throw new Error("Signer modified pubkey");
107
+ // If its the same kind, return the signed event
108
+ if (!isKind(signed, draft.kind))
109
+ throw new Error("Signer modified event kind");
102
110
  // copy the plaintext hidden content if its on the draft
103
111
  if (Reflect.has(draft, EncryptedContentSymbol))
104
112
  Reflect.set(signed, EncryptedContentSymbol, Reflect.get(draft, EncryptedContentSymbol));
@@ -1,4 +1,9 @@
1
- import { EventOperation } from "../event-factory/types.js";
1
+ import type { EventOperation } from "../factories/types.js";
2
2
  import { EncryptionMethod } from "../helpers/encrypted-content.js";
3
- /** Sets the hidden content on an event */
4
- export declare function setHiddenContent(content: string, override?: EncryptionMethod): EventOperation;
3
+ /**
4
+ * Sets the hidden content on an event
5
+ * @param content - Plaintext content to encrypt
6
+ * @param signer - EventSigner for encryption
7
+ * @param override - Optional encryption method override
8
+ */
9
+ export declare function setHiddenContent(content: string, signer?: import("../factories/types.js").EventSigner, override?: EncryptionMethod): EventOperation;
@@ -1,10 +1,15 @@
1
1
  import { setEncryptedContent } from "./encrypted-content.js";
2
- /** Sets the hidden content on an event */
3
- export function setHiddenContent(content, override) {
4
- return async (draft, ctx) => {
5
- if (!ctx.signer)
2
+ /**
3
+ * Sets the hidden content on an event
4
+ * @param content - Plaintext content to encrypt
5
+ * @param signer - EventSigner for encryption
6
+ * @param override - Optional encryption method override
7
+ */
8
+ export function setHiddenContent(content, signer, override) {
9
+ return async (draft) => {
10
+ if (!signer)
6
11
  throw new Error("Signer required for encrypted content");
7
- const pubkey = await ctx.signer.getPublicKey();
8
- return setEncryptedContent(pubkey, content, override)(draft, ctx);
12
+ const pubkey = await signer.getPublicKey();
13
+ return setEncryptedContent(pubkey, content, signer, override)(draft);
9
14
  };
10
15
  }
@@ -1,4 +1,4 @@
1
- import { EventOperation } from "../event-factory/types.js";
1
+ import type { EventOperation } from "../factories/types.js";
2
2
  /** Add an outbox relay in NIP-65 mailboxes */
3
3
  export declare function addOutboxRelay(url: string | URL): EventOperation;
4
4
  /** Remove an outbox relay in NIP-65 mailboxes */
@@ -11,3 +11,7 @@ export declare function removeInboxRelay(url: string | URL): EventOperation;
11
11
  export declare function addMailboxRelay(url: string | URL): EventOperation;
12
12
  /** Completely removes a mailbox relay from NIP-65 */
13
13
  export declare function removeMailboxRelay(url: string | URL): EventOperation;
14
+ /** Sets all inbox relays, replacing existing ones while preserving outbox dimension */
15
+ export declare function setInboxRelays(urls: (string | URL)[]): EventOperation;
16
+ /** Sets all outbox relays, replacing existing ones while preserving inbox dimension */
17
+ export declare function setOutboxRelays(urls: (string | URL)[]): EventOperation;
@@ -78,3 +78,79 @@ export function addMailboxRelay(url) {
78
78
  export function removeMailboxRelay(url) {
79
79
  return modifyPublicTags(removeRelayTag(url, "r"));
80
80
  }
81
+ /** Sets all inbox relays, replacing existing ones while preserving outbox dimension */
82
+ export function setInboxRelays(urls) {
83
+ const normalizedUrls = urls.map((url) => normalizeURL(url).toString());
84
+ return modifyPublicTags((tags) => {
85
+ // Step 1: Remove all "read" markers from existing relays
86
+ const withoutInboxes = tags
87
+ .map((tag) => {
88
+ if (!isRTag(tag))
89
+ return tag;
90
+ // Remove inbox-only relays (read marker)
91
+ if (tag[2] === "read")
92
+ return null;
93
+ // Convert both (no marker) to outbox-only (write marker)
94
+ if (tag[2] === undefined)
95
+ return ["r", tag[1], "write"];
96
+ // Keep outbox-only relays (write marker) as is
97
+ return tag;
98
+ })
99
+ .filter((t) => t !== null);
100
+ // Step 2: Add "read" markers to the specified URLs
101
+ const result = [...withoutInboxes];
102
+ for (const url of normalizedUrls) {
103
+ const existingIndex = result.findIndex((t) => isRTag(t) && isSameURL(t[1], url));
104
+ if (existingIndex >= 0) {
105
+ const existing = result[existingIndex];
106
+ // Convert outbox-only (write) to both (no marker)
107
+ if (existing[2] === "write") {
108
+ result[existingIndex] = ["r", url];
109
+ }
110
+ }
111
+ else {
112
+ // Add as inbox-only (read marker)
113
+ result.push(["r", url, "read"]);
114
+ }
115
+ }
116
+ return result;
117
+ });
118
+ }
119
+ /** Sets all outbox relays, replacing existing ones while preserving inbox dimension */
120
+ export function setOutboxRelays(urls) {
121
+ const normalizedUrls = urls.map((url) => normalizeURL(url).toString());
122
+ return modifyPublicTags((tags) => {
123
+ // Step 1: Remove all "write" markers from existing relays
124
+ const withoutOutboxes = tags
125
+ .map((tag) => {
126
+ if (!isRTag(tag))
127
+ return tag;
128
+ // Remove outbox-only relays (write marker)
129
+ if (tag[2] === "write")
130
+ return null;
131
+ // Convert both (no marker) to inbox-only (read marker)
132
+ if (tag[2] === undefined)
133
+ return ["r", tag[1], "read"];
134
+ // Keep inbox-only relays (read marker) as is
135
+ return tag;
136
+ })
137
+ .filter((t) => t !== null);
138
+ // Step 2: Add "write" markers to the specified URLs
139
+ const result = [...withoutOutboxes];
140
+ for (const url of normalizedUrls) {
141
+ const existingIndex = result.findIndex((t) => isRTag(t) && isSameURL(t[1], url));
142
+ if (existingIndex >= 0) {
143
+ const existing = result[existingIndex];
144
+ // Convert inbox-only (read) to both (no marker)
145
+ if (existing[2] === "read") {
146
+ result[existingIndex] = ["r", url];
147
+ }
148
+ }
149
+ else {
150
+ // Add as outbox-only (write marker)
151
+ result.push(["r", url, "write"]);
152
+ }
153
+ }
154
+ return result;
155
+ });
156
+ }
@@ -1,4 +1,4 @@
1
- import { EventOperation } from "../event-factory/types.js";
1
+ import type { EventOperation } from "../factories/types.js";
2
2
  import { ProfileContent } from "../helpers/index.js";
3
3
  /** Sets the content of a kind 0 metadata event */
4
4
  export declare function setProfile(content: ProfileContent): EventOperation;
@@ -1,16 +1,31 @@
1
1
  import { NostrEvent } from "nostr-tools";
2
- import { TagOperation } from "../../event-factory/types.js";
2
+ import type { TagOperation } from "../../factories/types.js";
3
3
  import { AddressPointer, EventPointer, ProfilePointer } from "../../helpers/pointers.js";
4
- /** Adds a single "p" tag for a ProfilePointer */
5
- export declare function addProfilePointerTag(pubkey: string | ProfilePointer, replace?: boolean): TagOperation;
4
+ /**
5
+ * Adds a single "p" tag for a ProfilePointer
6
+ * @param pubkey - Pubkey string or ProfilePointer object
7
+ * @param relayHint - Optional relay hint (string) or function to get relay hint
8
+ * @param replace - If true, removes existing "p" tags for this pubkey
9
+ */
10
+ export declare function addProfilePointerTag(pubkey: string | ProfilePointer, relayHint?: string | ((pubkey: string) => Promise<string | undefined>), replace?: boolean): TagOperation;
6
11
  /** Removes all "p" tags matching a pubkey */
7
12
  export declare function removeProfilePointerTag(pubkey: string | ProfilePointer): TagOperation;
8
- /** Adds a single "e" tag for an EventPointer */
9
- export declare function addEventPointerTag(id: string | EventPointer | NostrEvent, replace?: boolean): TagOperation;
13
+ /**
14
+ * Adds a single "e" tag for an EventPointer
15
+ * @param id - Event ID string, EventPointer object, or NostrEvent
16
+ * @param relayHint - Optional relay hint (string) or function to get relay hint
17
+ * @param replace - If true, removes existing "e" tags for this event ID
18
+ */
19
+ export declare function addEventPointerTag(id: string | EventPointer | NostrEvent, relayHint?: string | ((eventId: string) => Promise<string | undefined>), replace?: boolean): TagOperation;
10
20
  /** Removes all "e" tags matching EventPointer or id */
11
21
  export declare function removeEventPointerTag(id: string | EventPointer): TagOperation;
12
- /** Adds a single "a" tag based on an AddressPointer */
13
- export declare function addAddressPointerTag(address: string | AddressPointer | NostrEvent, replace?: boolean): TagOperation;
22
+ /**
23
+ * Adds a single "a" tag based on an AddressPointer
24
+ * @param address - Address string, AddressPointer object, or NostrEvent
25
+ * @param relayHint - Optional relay hint (string) or function to get relay hint
26
+ * @param replace - If true, removes existing "a" tags for this address
27
+ */
28
+ export declare function addAddressPointerTag(address: string | AddressPointer | NostrEvent, relayHint?: string | ((pubkey: string) => Promise<string | undefined>), replace?: boolean): TagOperation;
14
29
  /** Removes all "a" tags for address pointer */
15
30
  export declare function removeAddressPointerTag(address: string | AddressPointer | NostrEvent): TagOperation;
16
31
  /** Adds a name / value tag */
@@ -2,13 +2,18 @@ import { createATagFromAddressPointer, createETagFromEventPointer, createPTagFro
2
2
  import { getAddressPointerForEvent, getEventPointerForEvent, getReplaceableAddressFromPointer, parseReplaceableAddress, } from "../../helpers/pointers.js";
3
3
  import { ensureNamedValueTag, ensureSingletonTag } from "../../helpers/tags.js";
4
4
  import { getReplaceableAddress, isEvent, skip } from "../../helpers/index.js";
5
- /** Adds a single "p" tag for a ProfilePointer */
6
- export function addProfilePointerTag(pubkey, replace = true) {
7
- return async (tags, { getPubkeyRelayHint }) => {
5
+ /**
6
+ * Adds a single "p" tag for a ProfilePointer
7
+ * @param pubkey - Pubkey string or ProfilePointer object
8
+ * @param relayHint - Optional relay hint (string) or function to get relay hint
9
+ * @param replace - If true, removes existing "p" tags for this pubkey
10
+ */
11
+ export function addProfilePointerTag(pubkey, relayHint, replace = true) {
12
+ return async (tags) => {
8
13
  const pointer = typeof pubkey === "string" ? { pubkey: pubkey } : { ...pubkey };
9
- // add relay hint
10
- if (getPubkeyRelayHint && pointer.relays?.[0] === undefined) {
11
- const hint = await getPubkeyRelayHint(pointer.pubkey);
14
+ // add relay hint (hybrid: string or function)
15
+ if (!pointer.relays?.[0] && relayHint) {
16
+ const hint = typeof relayHint === "string" ? relayHint : await relayHint(pointer.pubkey);
12
17
  if (hint)
13
18
  pointer.relays = [hint];
14
19
  }
@@ -24,13 +29,18 @@ export function removeProfilePointerTag(pubkey) {
24
29
  pubkey = typeof pubkey !== "string" ? pubkey.pubkey : pubkey;
25
30
  return (tags) => tags.filter((t) => !(t[0] === "p" && t[1] === pubkey));
26
31
  }
27
- /** Adds a single "e" tag for an EventPointer */
28
- export function addEventPointerTag(id, replace = true) {
29
- return async (tags, { getEventRelayHint }) => {
32
+ /**
33
+ * Adds a single "e" tag for an EventPointer
34
+ * @param id - Event ID string, EventPointer object, or NostrEvent
35
+ * @param relayHint - Optional relay hint (string) or function to get relay hint
36
+ * @param replace - If true, removes existing "e" tags for this event ID
37
+ */
38
+ export function addEventPointerTag(id, relayHint, replace = true) {
39
+ return async (tags) => {
30
40
  const pointer = typeof id === "string" ? { id } : isEvent(id) ? getEventPointerForEvent(id) : id;
31
- // add relay hint
32
- if (getEventRelayHint && pointer.relays?.[0] === undefined) {
33
- const hint = await getEventRelayHint(pointer.id);
41
+ // add relay hint (hybrid: string or function)
42
+ if (!pointer.relays?.[0] && relayHint) {
43
+ const hint = typeof relayHint === "string" ? relayHint : await relayHint(pointer.id);
34
44
  if (hint)
35
45
  pointer.relays = [hint];
36
46
  }
@@ -46,8 +56,13 @@ export function removeEventPointerTag(id) {
46
56
  id = typeof id === "string" ? id : id.id;
47
57
  return (tags) => tags.filter((t) => !(t[0] === "e" && t[1] === id));
48
58
  }
49
- /** Adds a single "a" tag based on an AddressPointer */
50
- export function addAddressPointerTag(address, replace = true) {
59
+ /**
60
+ * Adds a single "a" tag based on an AddressPointer
61
+ * @param address - Address string, AddressPointer object, or NostrEvent
62
+ * @param relayHint - Optional relay hint (string) or function to get relay hint
63
+ * @param replace - If true, removes existing "a" tags for this address
64
+ */
65
+ export function addAddressPointerTag(address, relayHint, replace = true) {
51
66
  // convert the string into an address pointer object
52
67
  const pointer = typeof address === "string"
53
68
  ? parseReplaceableAddress(address)
@@ -56,11 +71,11 @@ export function addAddressPointerTag(address, replace = true) {
56
71
  : address;
57
72
  if (!pointer)
58
73
  throw new Error("Unable to resolve address pointer");
59
- return async (tags, { getPubkeyRelayHint }) => {
74
+ return async (tags) => {
60
75
  const replaceableAddress = typeof address === "string" ? address : getReplaceableAddressFromPointer(pointer);
61
- // add relay hint if there isn't one
62
- if (getPubkeyRelayHint && pointer.relays?.[0] === undefined) {
63
- const hint = await getPubkeyRelayHint(pointer.pubkey);
76
+ // add relay hint if there isn't one (hybrid: string or function)
77
+ if (!pointer.relays?.[0] && relayHint) {
78
+ const hint = typeof relayHint === "string" ? relayHint : await relayHint(pointer.pubkey);
64
79
  if (hint)
65
80
  pointer.relays = [hint];
66
81
  }
@@ -1,4 +1,4 @@
1
- import { TagOperation } from "../../event-factory/types.js";
1
+ import type { TagOperation } from "../../factories/types.js";
2
2
  /** Adds a relay tag */
3
3
  export declare function addRelayTag(url: string | URL, tagName?: string, replace?: boolean): TagOperation;
4
4
  /** Removes all relay tags matching the relay */
@@ -1,4 +1,4 @@
1
- import { EventOperation, TagOperation } from "../event-factory/types.js";
1
+ import type { EventOperation, TagOperation } from "../factories/types.js";
2
2
  import { EventTemplate, NostrEvent, UnsignedEvent } from "../helpers/event.js";
3
3
  /** Includes only a single instance of tag in an events public tags */
4
4
  export declare function includeSingletonTag(tag: [string, ...string[]], replace?: boolean): EventOperation;
@@ -8,13 +8,19 @@ export declare function includeNameValueTag(tag: [string, string, ...string[]],
8
8
  export declare function modifyPublicTags<E extends EventTemplate | UnsignedEvent | NostrEvent>(...operations: (TagOperation | undefined)[]): EventOperation<E, E>;
9
9
  /**
10
10
  * Creates an event operation that modifies the hidden tags on an event with {@link TagOperation}s
11
+ * @param signer - EventSigner for encrypting/decrypting hidden tags
12
+ * @param operations - Tag operations to apply to hidden tags
11
13
  * @throws {Error} if no signer is provided
12
14
  * @throws {Error} if the event kind does not support hidden tags
13
15
  */
14
- export declare function modifyHiddenTags<E extends EventTemplate | UnsignedEvent | NostrEvent>(...operations: (TagOperation | undefined)[]): EventOperation<E, E>;
16
+ export declare function modifyHiddenTags<E extends EventTemplate | UnsignedEvent | NostrEvent>(signer: import("../factories/types.js").EventSigner | undefined, ...operations: (TagOperation | undefined)[]): EventOperation<E, E>;
15
17
  export type ModifyTagsOptions = TagOperation | TagOperation[] | {
16
18
  public?: TagOperation | TagOperation[];
17
19
  hidden?: TagOperation | TagOperation[];
18
20
  };
19
- /** A flexible method for creating an event operation that modifies the tags */
20
- export declare function modifyTags(tagOperations?: ModifyTagsOptions): EventOperation;
21
+ /**
22
+ * A flexible method for creating an event operation that modifies the tags
23
+ * @param tagOperations - Tag operations for public and/or hidden tags
24
+ * @param signer - Optional signer (required if modifying hidden tags)
25
+ */
26
+ export declare function modifyTags(tagOperations?: ModifyTagsOptions, signer?: import("../factories/types.js").EventSigner): EventOperation;
@@ -12,21 +12,23 @@ export function includeNameValueTag(tag, replace = true) {
12
12
  }
13
13
  /** An event operation that modifies the public tags with {@link TagOperation}s */
14
14
  export function modifyPublicTags(...operations) {
15
- return async (draft, ctx) => {
16
- return { ...draft, tags: await tagPipe(...operations)(Array.from(draft.tags), ctx) };
15
+ return async (draft) => {
16
+ return { ...draft, tags: await tagPipe(...operations)(Array.from(draft.tags)) };
17
17
  };
18
18
  }
19
19
  /**
20
20
  * Creates an event operation that modifies the hidden tags on an event with {@link TagOperation}s
21
+ * @param signer - EventSigner for encrypting/decrypting hidden tags
22
+ * @param operations - Tag operations to apply to hidden tags
21
23
  * @throws {Error} if no signer is provided
22
24
  * @throws {Error} if the event kind does not support hidden tags
23
25
  */
24
- export function modifyHiddenTags(...operations) {
26
+ export function modifyHiddenTags(signer, ...operations) {
25
27
  operations = operations.filter((o) => !!o);
26
28
  if (operations.length === 0)
27
29
  return skip();
28
- return async (draft, ctx) => {
29
- if (!ctx.signer)
30
+ return async (draft) => {
31
+ if (!signer)
30
32
  throw new Error("Missing signer for hidden tags");
31
33
  if (!canHaveHiddenTags(draft.kind))
32
34
  throw new Error("Event kind does not support hidden tags");
@@ -41,8 +43,8 @@ export function modifyHiddenTags(...operations) {
41
43
  if (hidden === undefined) {
42
44
  if (hasHiddenTags(draft)) {
43
45
  // draft is an existing event, attempt to unlock tags
44
- pubkey = await ctx.signer.getPublicKey();
45
- hidden = await unlockHiddenTags({ ...draft, pubkey }, ctx.signer);
46
+ pubkey = await signer.getPublicKey();
47
+ hidden = await unlockHiddenTags({ ...draft, pubkey }, signer);
46
48
  }
47
49
  // create a new array of hidden tags
48
50
  else
@@ -56,19 +58,23 @@ export function modifyHiddenTags(...operations) {
56
58
  if (hidden === undefined)
57
59
  throw new Error("Failed to find hidden tags");
58
60
  // Create the new hidden tags
59
- const tags = await tagPipe(...operations)(hidden, ctx);
61
+ const tags = await tagPipe(...operations)(hidden);
60
62
  // Encrypt new hidden tags
61
- const methods = getHiddenTagsEncryptionMethods(draft.kind, ctx.signer);
63
+ const methods = getHiddenTagsEncryptionMethods(draft.kind, signer);
62
64
  if (!pubkey)
63
- pubkey = await ctx.signer.getPublicKey();
65
+ pubkey = await signer.getPublicKey();
64
66
  const plaintext = JSON.stringify(tags);
65
67
  const content = await methods.encrypt(pubkey, plaintext);
66
68
  // add the plaintext content on the draft so it can be carried forward
67
69
  return { ...draft, content, [EncryptedContentSymbol]: plaintext };
68
70
  };
69
71
  }
70
- /** A flexible method for creating an event operation that modifies the tags */
71
- export function modifyTags(tagOperations) {
72
+ /**
73
+ * A flexible method for creating an event operation that modifies the tags
74
+ * @param tagOperations - Tag operations for public and/or hidden tags
75
+ * @param signer - Optional signer (required if modifying hidden tags)
76
+ */
77
+ export function modifyTags(tagOperations, signer) {
72
78
  let publicOperations = [];
73
79
  let hiddenOperations = [];
74
80
  // normalize tag operation arg
@@ -89,5 +95,5 @@ export function modifyTags(tagOperations) {
89
95
  hiddenOperations = tagOperations.hidden;
90
96
  }
91
97
  // return a new event operation that modifies the tags
92
- return eventPipe(publicOperations.length > 0 ? modifyPublicTags(...publicOperations) : undefined, hiddenOperations.length > 0 ? modifyHiddenTags(...hiddenOperations) : undefined);
98
+ return eventPipe(publicOperations.length > 0 ? modifyPublicTags(...publicOperations) : undefined, hiddenOperations.length > 0 ? modifyHiddenTags(signer, ...hiddenOperations) : undefined);
93
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-core",
3
- "version": "5.2.0",
3
+ "version": "6.0.2",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -60,10 +60,15 @@
60
60
  "require": "./dist/event-store/index.js",
61
61
  "types": "./dist/event-store/index.d.ts"
62
62
  },
63
- "./event-factory": {
64
- "import": "./dist/event-factory/index.js",
65
- "require": "./dist/event-factory/index.js",
66
- "types": "./dist/event-factory/index.d.ts"
63
+ "./factories": {
64
+ "import": "./dist/factories/index.js",
65
+ "require": "./dist/factories/index.js",
66
+ "types": "./dist/factories/index.d.ts"
67
+ },
68
+ "./factories/*": {
69
+ "import": "./dist/factories/*.js",
70
+ "require": "./dist/factories/*.js",
71
+ "types": "./dist/factories/*.d.ts"
67
72
  },
68
73
  "./operations": {
69
74
  "import": "./dist/operations/index.js",
@@ -74,6 +79,16 @@
74
79
  "import": "./dist/operations/*.js",
75
80
  "require": "./dist/operations/*.js",
76
81
  "types": "./dist/operations/*.d.ts"
82
+ },
83
+ "./casts": {
84
+ "import": "./dist/casts/index.js",
85
+ "require": "./dist/casts/index.js",
86
+ "types": "./dist/casts/index.d.ts"
87
+ },
88
+ "./casts/*": {
89
+ "import": "./dist/casts/*.js",
90
+ "require": "./dist/casts/*.js",
91
+ "types": "./dist/casts/*.d.ts"
77
92
  }
78
93
  },
79
94
  "dependencies": {
@@ -1,57 +0,0 @@
1
- import { EventTemplate, NostrEvent, UnsignedEvent } from "../helpers/event.js";
2
- import { ModifyTagsOptions } from "../operations/tags.js";
3
- import { EventFactoryTemplate } from "./methods.js";
4
- import { EventBlueprint, EventFactoryContext, EventOperation, IEventFactory } from "./types.js";
5
- /**
6
- * Base class that provides event creation functionality.
7
- * This class can be extended by other packages to add additional helpful event creation methods.
8
- *
9
- * @example
10
- * ```ts
11
- * // In another package (e.g., applesauce-common)
12
- * import { EventFactory } from "applesauce-core/event-factory";
13
- * import { NoteBlueprint, ReactionBlueprint } from "applesauce-common/blueprints";
14
- *
15
- * // Add methods to the prototype
16
- * EventFactory.prototype.note = function(content, options) {
17
- * return this.create(NoteBlueprint, content, options);
18
- * };
19
- *
20
- * EventFactory.prototype.reaction = function(event, emoji) {
21
- * return this.create(ReactionBlueprint, event, emoji);
22
- * };
23
- *
24
- * // Extend the type via module augmentation
25
- * declare module "applesauce-core/event-factory" {
26
- * interface EventFactory {
27
- * note(content: string, options?: NoteBlueprintOptions): Promise<EventTemplate>;
28
- * reaction(event: NostrEvent, emoji?: string): Promise<EventTemplate>;
29
- * }
30
- * }
31
- * ```
32
- */
33
- export declare class EventFactory implements IEventFactory {
34
- context: EventFactoryContext;
35
- constructor(context?: EventFactoryContext);
36
- /** Build an event template with operations */
37
- build(template: EventFactoryTemplate, ...operations: (EventOperation | undefined)[]): Promise<EventTemplate>;
38
- /** Create an event from a blueprint */
39
- create<T extends EventTemplate | UnsignedEvent | NostrEvent>(blueprint: EventBlueprint<T>): Promise<T>;
40
- create<T extends EventTemplate | UnsignedEvent | NostrEvent, Args extends Array<any>>(blueprint: (...args: Args) => EventBlueprint<T>, ...args: Args): Promise<T>;
41
- /** Modify an existing event with operations and updated the created_at */
42
- modify(draft: EventTemplate | UnsignedEvent | NostrEvent, ...operations: (EventOperation | undefined)[]): Promise<EventTemplate>;
43
- /** Modify a lists public and hidden tags and updated the created_at */
44
- modifyTags(event: EventTemplate | UnsignedEvent | NostrEvent, tagOperations?: ModifyTagsOptions, eventOperations?: EventOperation | (EventOperation | undefined)[]): Promise<EventTemplate>;
45
- /** Attaches the signers pubkey to an event template */
46
- stamp(draft: EventTemplate | UnsignedEvent): Promise<UnsignedEvent>;
47
- /** Signs a event template with the signer */
48
- sign(draft: EventTemplate | UnsignedEvent): Promise<NostrEvent>;
49
- /** Sets the signer in the context */
50
- setSigner(signer: EventFactoryContext["signer"]): void;
51
- /** clears the signer in the context */
52
- clearSigner(): void;
53
- /** sets the client in the context */
54
- setClient(client: EventFactoryContext["client"]): void;
55
- /** clears the client in the context */
56
- clearClient(): void;
57
- }