applesauce-core 5.0.0 → 5.0.3

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.
@@ -1,7 +1,8 @@
1
- import { Ncryptsec } from "nostr-tools/nip19";
1
+ import { Ncryptsec, NSec } from "nostr-tools/nip19";
2
2
  export { generateSecretKey, getPublicKey } from "nostr-tools/pure";
3
3
  /** Converts hex to nsec strings into Uint8 secret keys */
4
- export declare function normalizeToSecretKey(str: string | Uint8Array): Uint8Array;
4
+ export declare function normalizeToSecretKey(str: NSec): Uint8Array;
5
+ export declare function normalizeToSecretKey(str: string | Uint8Array): Uint8Array | null;
5
6
  /** Encrypt a secret key using NIP-49 */
6
7
  export declare function encryptSecretKey(key: Uint8Array, password: string): Ncryptsec;
7
8
  /** Decrypt a secret key using NIP-49 */
@@ -1,20 +1,28 @@
1
1
  import { decode } from "nostr-tools/nip19";
2
+ import { decrypt, encrypt } from "nostr-tools/nip49";
2
3
  import { hexToBytes } from "nostr-tools/utils";
3
4
  import { isHexKey } from "./string.js";
4
- import { encrypt, decrypt } from "nostr-tools/nip49";
5
5
  // Re-export types from nostr-tools
6
6
  export { generateSecretKey, getPublicKey } from "nostr-tools/pure";
7
- /** Converts hex to nsec strings into Uint8 secret keys */
8
7
  export function normalizeToSecretKey(str) {
9
- if (str instanceof Uint8Array)
8
+ if (str instanceof Uint8Array) {
9
+ // Ignore invalid lengths
10
+ if (str.length !== 32)
11
+ return null;
10
12
  return str;
13
+ }
11
14
  else if (isHexKey(str))
12
15
  return hexToBytes(str);
13
16
  else {
14
- const result = decode(str);
15
- if (result.type !== "nsec")
16
- throw new Error(`Cant get secret key from ${result.type}`);
17
- return result.data;
17
+ try {
18
+ const result = decode(str);
19
+ if (result.type !== "nsec")
20
+ return null;
21
+ return result.data;
22
+ }
23
+ catch {
24
+ return null;
25
+ }
18
26
  }
19
27
  }
20
28
  /** Encrypt a secret key using NIP-49 */
@@ -1,8 +1,9 @@
1
- import { AddressPointer, decode, EventPointer, ProfilePointer } from "nostr-tools/nip19";
1
+ import { AddressPointer, DecodedResult, EventPointer, NAddr, NEvent, NProfile, NPub, ProfilePointer } from "nostr-tools/nip19";
2
2
  export type { AddressPointer, EventPointer, ProfilePointer } from "nostr-tools/nip19";
3
3
  export { decode as decodePointer, naddrEncode, neventEncode, noteEncode, nprofileEncode, npubEncode, nsecEncode, } from "nostr-tools/nip19";
4
+ export type { NAddr, NEvent, NProfile, NPub } from "nostr-tools/nip19";
4
5
  import { NostrEvent } from "./event.js";
5
- export type DecodeResult = ReturnType<typeof decode>;
6
+ export type DecodeResult = DecodedResult;
6
7
  /** Decodes any nip-19 encoded entity to a ProfilePointer */
7
8
  export declare function decodeProfilePointer(str: string): ProfilePointer | null;
8
9
  /** Decodes an naddr encoded string to an AddressPointer */
@@ -49,9 +50,18 @@ export declare function addRelayHintsToPointer<T extends {
49
50
  relays?: string[];
50
51
  }>(pointer: T, relays?: Iterable<string>): T;
51
52
  /** Gets the hex pubkey from any nip-19 encoded string */
53
+ export declare function normalizeToPubkey(str: NPub): string;
52
54
  export declare function normalizeToPubkey(str: string): string | null;
53
55
  /** Gets a ProfilePointer from any nip-19 encoded string */
56
+ export declare function normalizeToProfilePointer(str: NProfile): ProfilePointer;
57
+ export declare function normalizeToProfilePointer(str: NPub): ProfilePointer;
54
58
  export declare function normalizeToProfilePointer(str: string): ProfilePointer | null;
59
+ /** Gets an AddressPointer from any nip-19 encoded string */
60
+ export declare function normalizeToAddressPointer(str: NAddr): AddressPointer;
61
+ export declare function normalizeToAddressPointer(str: string): AddressPointer | null;
62
+ /** Gets an EventPointer from any nip-19 encoded string */
63
+ export declare function normalizeToEventPointer(str: NEvent): EventPointer;
64
+ export declare function normalizeToEventPointer(str: string): EventPointer | null;
55
65
  /** Returns all NIP-19 pointers in a content string */
56
66
  export declare function getContentPointers(content: string): DecodeResult[];
57
67
  /**
@@ -7,9 +7,20 @@ 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 */
11
+ function safeDecode(str) {
12
+ try {
13
+ return decode(str);
14
+ }
15
+ catch {
16
+ return null;
17
+ }
18
+ }
10
19
  /** Decodes any nip-19 encoded entity to a ProfilePointer */
11
20
  export function decodeProfilePointer(str) {
12
- const result = decode(str);
21
+ const result = safeDecode(str);
22
+ if (!result)
23
+ return null;
13
24
  const pubkey = getPubkeyFromDecodeResult(result);
14
25
  if (!pubkey)
15
26
  return null;
@@ -20,12 +31,16 @@ export function decodeProfilePointer(str) {
20
31
  }
21
32
  /** Decodes an naddr encoded string to an AddressPointer */
22
33
  export function decodeAddressPointer(str) {
23
- const result = decode(str);
34
+ const result = safeDecode(str);
35
+ if (!result)
36
+ return null;
24
37
  return result.type === "naddr" ? result.data : null;
25
38
  }
26
39
  /** Decodes a note1 or nevent encoded string to an EventPointer */
27
40
  export function decodeEventPointer(str) {
28
- const result = decode(str);
41
+ const result = safeDecode(str);
42
+ if (!result)
43
+ return null;
29
44
  switch (result.type) {
30
45
  case "note":
31
46
  return { id: result.data };
@@ -227,24 +242,26 @@ export function addRelayHintsToPointer(pointer, relays) {
227
242
  else
228
243
  return { ...pointer, relays: relaySet(relays, pointer.relays) };
229
244
  }
230
- /** Gets the hex pubkey from any nip-19 encoded string */
231
245
  export function normalizeToPubkey(str) {
232
246
  if (isHexKey(str))
233
247
  return str.toLowerCase();
234
248
  else {
235
- const result = decode(str);
249
+ const result = safeDecode(str);
250
+ if (!result)
251
+ return null;
236
252
  const pubkey = getPubkeyFromDecodeResult(result);
237
253
  if (!pubkey)
238
254
  return null;
239
255
  return pubkey;
240
256
  }
241
257
  }
242
- /** Gets a ProfilePointer from any nip-19 encoded string */
243
258
  export function normalizeToProfilePointer(str) {
244
259
  if (isHexKey(str))
245
260
  return { pubkey: str.toLowerCase() };
246
261
  else {
247
- const result = decode(str);
262
+ const result = safeDecode(str);
263
+ if (!result)
264
+ return null;
248
265
  // Return it if it's a profile pointer
249
266
  if (result.type === "nprofile")
250
267
  return result.data;
@@ -256,13 +273,42 @@ export function normalizeToProfilePointer(str) {
256
273
  return { pubkey, relays };
257
274
  }
258
275
  }
276
+ export function normalizeToAddressPointer(str) {
277
+ // Handle <kind>:<pubkey>:<identifier> format
278
+ if (str.includes(":"))
279
+ return parseReplaceableAddress(str);
280
+ // Decode nip-19 encoded string
281
+ const result = safeDecode(str);
282
+ if (!result)
283
+ return null;
284
+ return result.type === "naddr" ? result.data : null;
285
+ }
286
+ export function normalizeToEventPointer(str) {
287
+ // Handle <event-id> format
288
+ if (isHexKey(str))
289
+ return { id: str.toLowerCase() };
290
+ // Decode nip-19 encoded string
291
+ const result = safeDecode(str);
292
+ if (!result)
293
+ return null;
294
+ switch (result.type) {
295
+ case "nevent":
296
+ return result.data;
297
+ case "note":
298
+ return { id: result.data };
299
+ default:
300
+ return null;
301
+ }
302
+ }
259
303
  /** Returns all NIP-19 pointers in a content string */
260
304
  export function getContentPointers(content) {
261
305
  const mentions = content.matchAll(Tokens.nostrLink);
262
306
  const pointers = [];
263
307
  for (const [_, $1] of mentions) {
264
308
  try {
265
- const result = decode($1);
309
+ const result = safeDecode($1);
310
+ if (!result)
311
+ continue;
266
312
  pointers.push(result);
267
313
  }
268
314
  catch (error) { }
@@ -61,7 +61,7 @@ export function ensureWebSocketURL(url) {
61
61
  p.protocol = "ws:";
62
62
  else if (p.protocol === "https:")
63
63
  p.protocol = "wss:";
64
- else
64
+ else if (p.protocol !== "ws:")
65
65
  p.protocol = "wss:";
66
66
  // return a string if a string was passed in
67
67
  // @ts-expect-error
@@ -74,7 +74,7 @@ export function ensureHttpURL(url) {
74
74
  p.protocol = "http:";
75
75
  else if (p.protocol === "wss:")
76
76
  p.protocol = "https:";
77
- else
77
+ else if (p.protocol !== "http:")
78
78
  p.protocol = "https:";
79
79
  // return a string if a string was passed in
80
80
  // @ts-expect-error
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-core",
3
- "version": "5.0.0",
3
+ "version": "5.0.3",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",