applesauce-core 5.1.0 → 5.2.0

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.
@@ -38,6 +38,8 @@ export type Emoji = {
38
38
  shortcode: string;
39
39
  /** The URL to the emoji image */
40
40
  url: string;
41
+ /** The NIP-01 "a" tag address of the emoji pack this emoji belongs to */
42
+ address?: AddressPointer;
41
43
  };
42
44
  export interface EmojiContext {
43
45
  /** An array of custom emojis that will be used for text notes */
@@ -4,42 +4,42 @@ export { decode as decodePointer, naddrEncode, neventEncode, noteEncode, nprofil
4
4
  export type { NAddr, NEvent, NProfile, NPub } from "nostr-tools/nip19";
5
5
  import { NostrEvent } from "./event.js";
6
6
  export type DecodeResult = DecodedResult;
7
- /** Decodes any nip-19 encoded entity to a ProfilePointer */
7
+ /** Decodes any NIP-19 encoded string into a ProfilePointer. Returns null if the string cannot be decoded. */
8
8
  export declare function decodeProfilePointer(str: string): ProfilePointer | null;
9
- /** Decodes an naddr encoded string to an AddressPointer */
9
+ /** Decodes an encoded NIP-19 naddr string to an AddressPointer. Returns null if the string cannot be decoded. */
10
10
  export declare function decodeAddressPointer(str: string): AddressPointer | null;
11
- /** Decodes a note1 or nevent encoded string to an EventPointer */
11
+ /** Decodes an encoded NIP-19 note1 or nevent string to an EventPointer. Returns null if the string cannot be decoded. */
12
12
  export declare function decodeEventPointer(str: string): EventPointer | null;
13
13
  export type AddressPointerWithoutD = Omit<AddressPointer, "identifier"> & {
14
14
  identifier?: string;
15
15
  };
16
- /** Parse the value of an "a" tag into an AddressPointer */
16
+ /** Parses the value of an "a" tag into an AddressPointer. Returns null if the address cannot be parsed. */
17
17
  export declare function parseReplaceableAddress(address: string, requireIdentifier?: boolean): AddressPointer | null;
18
- /** Extra a pubkey from the result of nip19.decode */
18
+ /** Extracts a pubkey from the result of nip19.decode Returns undefined if the decode result does not have a pubkey */
19
19
  export declare function getPubkeyFromDecodeResult(result?: DecodeResult): string | undefined;
20
- /** Gets the relays from a decode result */
20
+ /** Extracts the relays from a decode result */
21
21
  export declare function getRelaysFromDecodeResult(result?: DecodeResult): string[] | undefined;
22
- /** Encodes the result of nip19.decode */
22
+ /** Encodes the result of nip19.decode to a NIP-19 string. Returns an empty string if the result cannot be encoded. */
23
23
  export declare function encodeDecodeResult(result: DecodeResult): "" | `nevent1${string}` | `nprofile1${string}` | `naddr1${string}` | `nsec1${string}` | `npub1${string}` | `note1${string}`;
24
- /** Encodes a pointer to a NIP-19 string */
24
+ /** Encodes a pointer to a NIP-19 string. Returns an empty string if the pointer cannot be encoded. */
25
25
  export declare function encodePointer(pointer: AddressPointer | EventPointer | ProfilePointer): string;
26
- /** Gets an EventPointer form a common "e" tag */
26
+ /** Parses a common "e" tag into an EventPointer. Returns null if the tag cannot be parsed. */
27
27
  export declare function getEventPointerFromETag(tag: string[]): EventPointer | null;
28
- /** Gets an EventPointer form a common "q" tag */
28
+ /** Parses a common "q" tag into an EventPointer. Returns null if the tag cannot be parsed. */
29
29
  export declare function getEventPointerFromQTag(tag: string[]): EventPointer | null;
30
- /** Get an AddressPointer from a common "a" tag */
30
+ /** Parses a common "a" tag into an AddressPointer. Returns null if the tag cannot be parsed. */
31
31
  export declare function getAddressPointerFromATag(tag: string[]): AddressPointer | null;
32
- /** Gets a ProfilePointer from a common "p" tag */
32
+ /** Parses a common "p" tag into a ProfilePointer. Returns null if the tag cannot be parsed. */
33
33
  export declare function getProfilePointerFromPTag(tag: string[]): ProfilePointer | null;
34
- /** Checks if a pointer is an AddressPointer */
34
+ /** Checks if a pointer object is an AddressPointer */
35
35
  export declare function isAddressPointer(pointer: any): pointer is AddressPointer;
36
- /** Checks if a pointer is a ProfilePointer */
36
+ /** Checks if a pointer object is a ProfilePointer */
37
37
  export declare function isProfilePointer(pointer: any): pointer is ProfilePointer;
38
- /** Checks if a pointer is an EventPointer */
38
+ /** Checks if a pointer object is an EventPointer */
39
39
  export declare function isEventPointer(pointer: any): pointer is EventPointer;
40
- /** Returns the stringified address pointer */
40
+ /** Returns the stringified NIP-19 encoded naddr address pointer for an AddressPointer. */
41
41
  export declare function getReplaceableAddressFromPointer(pointer: AddressPointer): string;
42
- /** Returns an AddressPointer for a replaceable event */
42
+ /** Returns an AddressPointer for a replaceable event. Returns null if the event is not addressable or replaceable. */
43
43
  export declare function getAddressPointerForEvent(event: NostrEvent, relays?: string[]): AddressPointer | null;
44
44
  /** Returns an EventPointer for an event */
45
45
  export declare function getEventPointerForEvent(event: NostrEvent, relays?: string[]): EventPointer;
@@ -49,17 +49,17 @@ export declare function getPointerForEvent(event: NostrEvent, relays?: string[])
49
49
  export declare function addRelayHintsToPointer<T extends {
50
50
  relays?: string[];
51
51
  }>(pointer: T, relays?: Iterable<string>): T;
52
- /** Gets the hex pubkey from any nip-19 encoded string */
52
+ /** Parses any nip-19 encoded string into a hex pubkey. Returns null if the string is not a valid pubkey. */
53
53
  export declare function normalizeToPubkey(str: NPub): string;
54
54
  export declare function normalizeToPubkey(str: string): string | null;
55
- /** Gets a ProfilePointer from any nip-19 encoded string */
55
+ /** Parses any nip-19 encoded string into a ProfilePointer. Returns null if the string cannot be parsed. */
56
56
  export declare function normalizeToProfilePointer(str: NProfile): ProfilePointer;
57
57
  export declare function normalizeToProfilePointer(str: NPub): ProfilePointer;
58
58
  export declare function normalizeToProfilePointer(str: string): ProfilePointer | null;
59
- /** Gets an AddressPointer from any nip-19 encoded string */
59
+ /** Parses any nip-19 encoded string into an AddressPointer. Returns null if the string cannot be parsed. */
60
60
  export declare function normalizeToAddressPointer(str: NAddr): AddressPointer;
61
61
  export declare function normalizeToAddressPointer(str: string): AddressPointer | null;
62
- /** Gets an EventPointer from any nip-19 encoded string */
62
+ /** Parses any nip-19 encoded string into an EventPointer. Returns null if the string cannot be parsed. */
63
63
  export declare function normalizeToEventPointer(str: NEvent): EventPointer;
64
64
  export declare function normalizeToEventPointer(str: string): EventPointer | null;
65
65
  /** Returns all NIP-19 pointers in a content string */
@@ -79,5 +79,9 @@ export declare function mergeAddressPointers(a: AddressPointer, b: AddressPointe
79
79
  * @throws if the pubkeys are different
80
80
  */
81
81
  export declare function mergeProfilePointers(a: ProfilePointer, b: ProfilePointer): ProfilePointer;
82
+ /** Checks if two address pointers refer to the same addressable event. Ignores relays. */
83
+ export declare function isAddressPointerSame(a: AddressPointer, b: AddressPointer): boolean;
84
+ /** Checks if two event pointers refer to the same event. Ignores relays. */
85
+ export declare function isEventPointerSame(a: EventPointer, b: EventPointer): boolean;
82
86
  /** Checks if an event matches a pointer */
83
87
  export declare function eventMatchesPointer(event: NostrEvent, pointer: EventPointer | AddressPointer | AddressPointerWithoutD): boolean;
@@ -7,7 +7,7 @@ import { Tokens } from "./regexp.js";
7
7
  import { isSafeRelayURL, relaySet } from "./relays.js";
8
8
  import { isHexKey } from "./string.js";
9
9
  import { normalizeURL } from "./url.js";
10
- /** Decodes a string and handles errors */
10
+ /** Decodes a string and handles errors. Returns null if the string cannot be decoded. */
11
11
  function safeDecode(str) {
12
12
  try {
13
13
  return decode(str);
@@ -16,7 +16,7 @@ function safeDecode(str) {
16
16
  return null;
17
17
  }
18
18
  }
19
- /** Decodes any nip-19 encoded entity to a ProfilePointer */
19
+ /** Decodes any NIP-19 encoded string into a ProfilePointer. Returns null if the string cannot be decoded. */
20
20
  export function decodeProfilePointer(str) {
21
21
  const result = safeDecode(str);
22
22
  if (!result)
@@ -29,14 +29,14 @@ export function decodeProfilePointer(str) {
29
29
  relays: getRelaysFromDecodeResult(result),
30
30
  };
31
31
  }
32
- /** Decodes an naddr encoded string to an AddressPointer */
32
+ /** Decodes an encoded NIP-19 naddr string to an AddressPointer. Returns null if the string cannot be decoded. */
33
33
  export function decodeAddressPointer(str) {
34
34
  const result = safeDecode(str);
35
35
  if (!result)
36
36
  return null;
37
37
  return result.type === "naddr" ? result.data : null;
38
38
  }
39
- /** Decodes a note1 or nevent encoded string to an EventPointer */
39
+ /** Decodes an encoded NIP-19 note1 or nevent string to an EventPointer. Returns null if the string cannot be decoded. */
40
40
  export function decodeEventPointer(str) {
41
41
  const result = safeDecode(str);
42
42
  if (!result)
@@ -50,7 +50,7 @@ export function decodeEventPointer(str) {
50
50
  return null;
51
51
  }
52
52
  }
53
- /** Parse the value of an "a" tag into an AddressPointer */
53
+ /** Parses the value of an "a" tag into an AddressPointer. Returns null if the address cannot be parsed. */
54
54
  export function parseReplaceableAddress(address, requireIdentifier = false) {
55
55
  const parts = address.split(":");
56
56
  const kind = parts[0] ? parseInt(parts[0]) : undefined;
@@ -73,7 +73,7 @@ export function parseReplaceableAddress(address, requireIdentifier = false) {
73
73
  identifier,
74
74
  };
75
75
  }
76
- /** Extra a pubkey from the result of nip19.decode */
76
+ /** Extracts a pubkey from the result of nip19.decode Returns undefined if the decode result does not have a pubkey */
77
77
  export function getPubkeyFromDecodeResult(result) {
78
78
  if (!result)
79
79
  return;
@@ -89,7 +89,7 @@ export function getPubkeyFromDecodeResult(result) {
89
89
  return undefined;
90
90
  }
91
91
  }
92
- /** Gets the relays from a decode result */
92
+ /** Extracts the relays from a decode result */
93
93
  export function getRelaysFromDecodeResult(result) {
94
94
  if (!result)
95
95
  return;
@@ -103,7 +103,7 @@ export function getRelaysFromDecodeResult(result) {
103
103
  }
104
104
  return undefined;
105
105
  }
106
- /** Encodes the result of nip19.decode */
106
+ /** Encodes the result of nip19.decode to a NIP-19 string. Returns an empty string if the result cannot be encoded. */
107
107
  export function encodeDecodeResult(result) {
108
108
  switch (result.type) {
109
109
  case "naddr":
@@ -121,7 +121,7 @@ export function encodeDecodeResult(result) {
121
121
  }
122
122
  return "";
123
123
  }
124
- /** Encodes a pointer to a NIP-19 string */
124
+ /** Encodes a pointer to a NIP-19 string. Returns an empty string if the pointer cannot be encoded. */
125
125
  export function encodePointer(pointer) {
126
126
  if (isAddressPointer(pointer))
127
127
  return naddrEncode(pointer);
@@ -132,7 +132,7 @@ export function encodePointer(pointer) {
132
132
  else
133
133
  return "";
134
134
  }
135
- /** Gets an EventPointer form a common "e" tag */
135
+ /** Parses a common "e" tag into an EventPointer. Returns null if the tag cannot be parsed. */
136
136
  export function getEventPointerFromETag(tag) {
137
137
  const id = tag[1];
138
138
  if (!id || !isHexKey(id))
@@ -142,7 +142,7 @@ export function getEventPointerFromETag(tag) {
142
142
  pointer.relays = [normalizeURL(tag[2])];
143
143
  return pointer;
144
144
  }
145
- /** Gets an EventPointer form a common "q" tag */
145
+ /** Parses a common "q" tag into an EventPointer. Returns null if the tag cannot be parsed. */
146
146
  export function getEventPointerFromQTag(tag) {
147
147
  const id = tag[1];
148
148
  if (!id || !isHexKey(id))
@@ -154,7 +154,7 @@ export function getEventPointerFromQTag(tag) {
154
154
  pointer.author = tag[3];
155
155
  return pointer;
156
156
  }
157
- /** Get an AddressPointer from a common "a" tag */
157
+ /** Parses a common "a" tag into an AddressPointer. Returns null if the tag cannot be parsed. */
158
158
  export function getAddressPointerFromATag(tag) {
159
159
  if (!tag[1])
160
160
  return null;
@@ -165,7 +165,7 @@ export function getAddressPointerFromATag(tag) {
165
165
  pointer.relays = [normalizeURL(tag[2])];
166
166
  return pointer;
167
167
  }
168
- /** Gets a ProfilePointer from a common "p" tag */
168
+ /** Parses a common "p" tag into a ProfilePointer. Returns null if the tag cannot be parsed. */
169
169
  export function getProfilePointerFromPTag(tag) {
170
170
  const pubkey = tag[1];
171
171
  if (!pubkey || !isHexKey(pubkey))
@@ -175,7 +175,7 @@ export function getProfilePointerFromPTag(tag) {
175
175
  pointer.relays = [normalizeURL(tag[2])];
176
176
  return pointer;
177
177
  }
178
- /** Checks if a pointer is an AddressPointer */
178
+ /** Checks if a pointer object is an AddressPointer */
179
179
  export function isAddressPointer(pointer) {
180
180
  return (typeof pointer === "object" &&
181
181
  pointer !== null &&
@@ -186,7 +186,7 @@ export function isAddressPointer(pointer) {
186
186
  typeof pointer.pubkey === "string" &&
187
187
  typeof pointer.kind === "number");
188
188
  }
189
- /** Checks if a pointer is a ProfilePointer */
189
+ /** Checks if a pointer object is a ProfilePointer */
190
190
  export function isProfilePointer(pointer) {
191
191
  return (typeof pointer === "object" &&
192
192
  pointer !== null &&
@@ -196,15 +196,15 @@ export function isProfilePointer(pointer) {
196
196
  !isEventPointer(pointer) &&
197
197
  !isAddressPointer(pointer));
198
198
  }
199
- /** Checks if a pointer is an EventPointer */
199
+ /** Checks if a pointer object is an EventPointer */
200
200
  export function isEventPointer(pointer) {
201
201
  return typeof pointer === "object" && pointer !== null && "id" in pointer && typeof pointer.id === "string";
202
202
  }
203
- /** Returns the stringified address pointer */
203
+ /** Returns the stringified NIP-19 encoded naddr address pointer for an AddressPointer. */
204
204
  export function getReplaceableAddressFromPointer(pointer) {
205
205
  return pointer.kind + ":" + pointer.pubkey + ":" + pointer.identifier;
206
206
  }
207
- /** Returns an AddressPointer for a replaceable event */
207
+ /** Returns an AddressPointer for a replaceable event. Returns null if the event is not addressable or replaceable. */
208
208
  export function getAddressPointerForEvent(event, relays) {
209
209
  if (!isAddressableKind(event.kind) && !isReplaceableKind(event.kind))
210
210
  return null;
@@ -345,6 +345,21 @@ export function mergeProfilePointers(a, b) {
345
345
  const relays = relaySet(a.relays, b.relays);
346
346
  return { ...a, relays };
347
347
  }
348
+ /** Checks if two address pointers refer to the same addressable event. Ignores relays. */
349
+ export function isAddressPointerSame(a, b) {
350
+ return a.kind === b.kind && a.pubkey === b.pubkey && a.identifier === b.identifier;
351
+ }
352
+ /** Checks if two event pointers refer to the same event. Ignores relays. */
353
+ export function isEventPointerSame(a, b) {
354
+ return (
355
+ // Check if id is the same
356
+ a.id === b.id &&
357
+ // Because event id's are unique, we can skip extra checks if either pointer is missing some fields
358
+ // If any kind is undefined, ignore it
359
+ (a.kind !== undefined || b.kind !== undefined ? a.kind === b.kind : true) &&
360
+ // If any author is undefined, ignore it
361
+ (a.author !== undefined || b.author !== undefined ? a.author === b.author : true));
362
+ }
348
363
  /** Checks if an event matches a pointer */
349
364
  export function eventMatchesPointer(event, pointer) {
350
365
  if (isEventPointer(pointer)) {
@@ -1,7 +1,7 @@
1
1
  import { EncryptedContentSymbol } from "../helpers/encrypted-content.js";
2
2
  import { ensureProfilePointerTag, ensureQuoteEventPointerTag } from "../helpers/factory.js";
3
3
  import { eventPipe, skip } from "../helpers/pipeline.js";
4
- import { getContentPointers, getPubkeyFromDecodeResult } from "../helpers/pointers.js";
4
+ import { getContentPointers, getPubkeyFromDecodeResult, getReplaceableAddressFromPointer, } from "../helpers/pointers.js";
5
5
  import { Expressions } from "../helpers/regexp.js";
6
6
  import { ensureNamedValueTag } from "../helpers/tags.js";
7
7
  /** Override the event content */
@@ -86,7 +86,9 @@ export function includeEmojis(emojis) {
86
86
  for (const [_, name] of matches) {
87
87
  const emoji = all.find((e) => e.shortcode === name);
88
88
  if (emoji?.url) {
89
- tags.push(["emoji", emoji.shortcode, emoji.url]);
89
+ tags.push(emoji.address
90
+ ? ["emoji", emoji.shortcode, emoji.url, getReplaceableAddressFromPointer(emoji.address)]
91
+ : ["emoji", emoji.shortcode, emoji.url]);
90
92
  }
91
93
  }
92
94
  return { ...draft, tags };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-core",
3
- "version": "5.1.0",
3
+ "version": "5.2.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",