nostr-tools 2.23.3 → 2.23.4

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 (122) hide show
  1. package/lib/cjs/index.js +175 -15
  2. package/lib/cjs/index.js.map +4 -4
  3. package/lib/cjs/nip17.js +31 -9
  4. package/lib/cjs/nip17.js.map +2 -2
  5. package/lib/cjs/nip22.js +154 -0
  6. package/lib/cjs/nip22.js.map +7 -0
  7. package/lib/cjs/nip44.js +34 -10
  8. package/lib/cjs/nip44.js.map +2 -2
  9. package/lib/cjs/nip46.js +34 -10
  10. package/lib/cjs/nip46.js.map +2 -2
  11. package/lib/cjs/nip47.js +3 -3
  12. package/lib/cjs/nip47.js.map +2 -2
  13. package/lib/cjs/nip59.js +31 -9
  14. package/lib/cjs/nip59.js.map +2 -2
  15. package/lib/esm/index.js +175 -15
  16. package/lib/esm/index.js.map +4 -4
  17. package/lib/esm/nip17.js +31 -9
  18. package/lib/esm/nip17.js.map +2 -2
  19. package/lib/esm/nip22.js +133 -0
  20. package/lib/esm/nip22.js.map +7 -0
  21. package/lib/esm/nip44.js +34 -10
  22. package/lib/esm/nip44.js.map +2 -2
  23. package/lib/esm/nip46.js +34 -10
  24. package/lib/esm/nip46.js.map +2 -2
  25. package/lib/esm/nip47.js +3 -3
  26. package/lib/esm/nip47.js.map +2 -2
  27. package/lib/esm/nip59.js +31 -9
  28. package/lib/esm/nip59.js.map +2 -2
  29. package/lib/nostr.bundle.js +175 -15
  30. package/lib/nostr.bundle.js.map +4 -4
  31. package/package.json +8 -2
  32. package/lib/types/abstract-pool.d.ts +0 -60
  33. package/lib/types/abstract-relay.d.ts +0 -110
  34. package/lib/types/benchmarks.d.ts +0 -1
  35. package/lib/types/core.d.ts +0 -32
  36. package/lib/types/core.test.d.ts +0 -1
  37. package/lib/types/fakejson.d.ts +0 -6
  38. package/lib/types/fakejson.test.d.ts +0 -1
  39. package/lib/types/filter.d.ts +0 -19
  40. package/lib/types/filter.test.d.ts +0 -1
  41. package/lib/types/helpers.d.ts +0 -2
  42. package/lib/types/index.d.ts +0 -30
  43. package/lib/types/kinds.d.ts +0 -200
  44. package/lib/types/kinds.test.d.ts +0 -1
  45. package/lib/types/nip04.d.ts +0 -2
  46. package/lib/types/nip04.test.d.ts +0 -1
  47. package/lib/types/nip05.d.ts +0 -17
  48. package/lib/types/nip05.test.d.ts +0 -1
  49. package/lib/types/nip06.d.ts +0 -15
  50. package/lib/types/nip06.test.d.ts +0 -1
  51. package/lib/types/nip07.d.ts +0 -13
  52. package/lib/types/nip10.d.ts +0 -24
  53. package/lib/types/nip10.test.d.ts +0 -1
  54. package/lib/types/nip11.d.ts +0 -266
  55. package/lib/types/nip11.test.d.ts +0 -1
  56. package/lib/types/nip13.d.ts +0 -8
  57. package/lib/types/nip13.test.d.ts +0 -1
  58. package/lib/types/nip17.d.ts +0 -15
  59. package/lib/types/nip17.test.d.ts +0 -1
  60. package/lib/types/nip18.d.ts +0 -22
  61. package/lib/types/nip18.test.d.ts +0 -1
  62. package/lib/types/nip19.d.ts +0 -81
  63. package/lib/types/nip19.test.d.ts +0 -1
  64. package/lib/types/nip21.d.ts +0 -34
  65. package/lib/types/nip21.test.d.ts +0 -1
  66. package/lib/types/nip25.d.ts +0 -15
  67. package/lib/types/nip25.test.d.ts +0 -1
  68. package/lib/types/nip27.d.ts +0 -32
  69. package/lib/types/nip27.test.d.ts +0 -1
  70. package/lib/types/nip28.d.ts +0 -46
  71. package/lib/types/nip28.test.d.ts +0 -1
  72. package/lib/types/nip29.d.ts +0 -265
  73. package/lib/types/nip30.d.ts +0 -22
  74. package/lib/types/nip30.test.d.ts +0 -1
  75. package/lib/types/nip39.d.ts +0 -2
  76. package/lib/types/nip39.test.d.ts +0 -1
  77. package/lib/types/nip40.d.ts +0 -10
  78. package/lib/types/nip40.test.d.ts +0 -1
  79. package/lib/types/nip42.d.ts +0 -5
  80. package/lib/types/nip42.test.d.ts +0 -1
  81. package/lib/types/nip44.d.ts +0 -13
  82. package/lib/types/nip44.test.d.ts +0 -1
  83. package/lib/types/nip46.d.ts +0 -117
  84. package/lib/types/nip47.d.ts +0 -9
  85. package/lib/types/nip47.test.d.ts +0 -1
  86. package/lib/types/nip49.d.ts +0 -3
  87. package/lib/types/nip49.test.d.ts +0 -1
  88. package/lib/types/nip54.d.ts +0 -1
  89. package/lib/types/nip54.test.d.ts +0 -1
  90. package/lib/types/nip55.d.ts +0 -30
  91. package/lib/types/nip55.test.d.ts +0 -1
  92. package/lib/types/nip57.d.ts +0 -25
  93. package/lib/types/nip57.test.d.ts +0 -1
  94. package/lib/types/nip58.d.ts +0 -134
  95. package/lib/types/nip58.test.d.ts +0 -1
  96. package/lib/types/nip59.d.ts +0 -12
  97. package/lib/types/nip59.test.d.ts +0 -1
  98. package/lib/types/nip75.d.ts +0 -61
  99. package/lib/types/nip75.test.d.ts +0 -1
  100. package/lib/types/nip77.d.ts +0 -102
  101. package/lib/types/nip77.test.d.ts +0 -1
  102. package/lib/types/nip94.d.ts +0 -87
  103. package/lib/types/nip94.test.d.ts +0 -1
  104. package/lib/types/nip98.d.ts +0 -74
  105. package/lib/types/nip98.test.d.ts +0 -1
  106. package/lib/types/nip99.d.ts +0 -92
  107. package/lib/types/nip99.test.d.ts +0 -1
  108. package/lib/types/nipb7.d.ts +0 -23
  109. package/lib/types/nipb7.test.d.ts +0 -1
  110. package/lib/types/pool.d.ts +0 -6
  111. package/lib/types/pool.test.d.ts +0 -1
  112. package/lib/types/pure.d.ts +0 -8
  113. package/lib/types/pure.test.d.ts +0 -1
  114. package/lib/types/references.d.ts +0 -10
  115. package/lib/types/references.test.d.ts +0 -1
  116. package/lib/types/relay.d.ts +0 -11
  117. package/lib/types/relay.test.d.ts +0 -1
  118. package/lib/types/signer.d.ts +0 -11
  119. package/lib/types/test-helpers.d.ts +0 -14
  120. package/lib/types/utils.d.ts +0 -9
  121. package/lib/types/utils.test.d.ts +0 -1
  122. package/lib/types/wasm.d.ts +0 -8
package/lib/esm/index.js CHANGED
@@ -1970,7 +1970,8 @@ import { sha256 as sha2563 } from "@noble/hashes/sha2.js";
1970
1970
  import { concatBytes as concatBytes2, hexToBytes as hexToBytes5, randomBytes as randomBytes2 } from "@noble/hashes/utils.js";
1971
1971
  import { base64 as base642 } from "@scure/base";
1972
1972
  var minPlaintextSize = 1;
1973
- var maxPlaintextSize = 65535;
1973
+ var maxPlaintextSize = 4294967295;
1974
+ var extendedPrefixThreshold = 65536;
1974
1975
  function getConversationKey(privkeyA, pubkeyB) {
1975
1976
  const sharedX = secp256k12.getSharedSecret(privkeyA, hexToBytes5("02" + pubkeyB)).subarray(1, 33);
1976
1977
  return hkdf_extract(sha2563, sharedX, utf8Encoder.encode("nip44-v2"));
@@ -1988,28 +1989,49 @@ function calcPaddedLen(len) {
1988
1989
  throw new Error("expected positive integer");
1989
1990
  if (len <= 32)
1990
1991
  return 32;
1991
- const nextPower = 1 << Math.floor(Math.log2(len - 1)) + 1;
1992
+ const nextPower = 2 ** (Math.floor(Math.log2(len - 1)) + 1);
1992
1993
  const chunk = nextPower <= 256 ? 32 : nextPower / 8;
1993
1994
  return chunk * (Math.floor((len - 1) / chunk) + 1);
1994
1995
  }
1995
1996
  function writeU16BE(num) {
1996
- if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > maxPlaintextSize)
1997
+ if (!Number.isSafeInteger(num) || num < minPlaintextSize || num > 65535)
1997
1998
  throw new Error("invalid plaintext size: must be between 1 and 65535 bytes");
1998
1999
  const arr = new Uint8Array(2);
1999
2000
  new DataView(arr.buffer).setUint16(0, num, false);
2000
2001
  return arr;
2001
2002
  }
2003
+ function writeU32BE(num) {
2004
+ if (!Number.isSafeInteger(num) || num < extendedPrefixThreshold || num > maxPlaintextSize)
2005
+ throw new Error("invalid plaintext size: must be between 65536 and 4294967295 bytes");
2006
+ const arr = new Uint8Array(4);
2007
+ new DataView(arr.buffer).setUint32(0, num, false);
2008
+ return arr;
2009
+ }
2002
2010
  function pad(plaintext) {
2003
2011
  const unpadded = utf8Encoder.encode(plaintext);
2004
2012
  const unpaddedLen = unpadded.length;
2005
- const prefix = writeU16BE(unpaddedLen);
2013
+ if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize)
2014
+ throw new Error("invalid plaintext size: must be between 1 and 4294967295 bytes");
2015
+ const prefix = unpaddedLen >= extendedPrefixThreshold ? concatBytes2(new Uint8Array([0, 0]), writeU32BE(unpaddedLen)) : writeU16BE(unpaddedLen);
2006
2016
  const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen);
2007
2017
  return concatBytes2(prefix, unpadded, suffix);
2008
2018
  }
2009
2019
  function unpad(padded) {
2010
- const unpaddedLen = new DataView(padded.buffer).getUint16(0);
2011
- const unpadded = padded.subarray(2, 2 + unpaddedLen);
2012
- if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== 2 + calcPaddedLen(unpaddedLen))
2020
+ const dv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
2021
+ const firstTwo = dv.getUint16(0);
2022
+ let unpaddedLen;
2023
+ let prefixLen;
2024
+ if (firstTwo === 0) {
2025
+ unpaddedLen = dv.getUint32(2);
2026
+ if (unpaddedLen < extendedPrefixThreshold)
2027
+ throw new Error("invalid padding");
2028
+ prefixLen = 6;
2029
+ } else {
2030
+ unpaddedLen = firstTwo;
2031
+ prefixLen = 2;
2032
+ }
2033
+ const unpadded = padded.subarray(prefixLen, prefixLen + unpaddedLen);
2034
+ if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== prefixLen + calcPaddedLen(unpaddedLen))
2013
2035
  throw new Error("invalid padding");
2014
2036
  return utf8Decoder.decode(unpadded);
2015
2037
  }
@@ -2023,7 +2045,7 @@ function decodePayload(payload) {
2023
2045
  if (typeof payload !== "string")
2024
2046
  throw new Error("payload must be a valid string");
2025
2047
  const plen = payload.length;
2026
- if (plen < 132 || plen > 87472)
2048
+ if (plen < 132)
2027
2049
  throw new Error("invalid payload length: " + plen);
2028
2050
  if (payload[0] === "#")
2029
2051
  throw new Error("unknown encryption version");
@@ -2034,7 +2056,7 @@ function decodePayload(payload) {
2034
2056
  throw new Error("invalid base64: " + error.message);
2035
2057
  }
2036
2058
  const dlen = data.length;
2037
- if (dlen < 99 || dlen > 65603)
2059
+ if (dlen < 99)
2038
2060
  throw new Error("invalid data length: " + dlen);
2039
2061
  const vers = data[0];
2040
2062
  if (vers !== 2)
@@ -2064,7 +2086,9 @@ function decrypt2(payload, conversationKey) {
2064
2086
  var v2 = {
2065
2087
  utils: {
2066
2088
  getConversationKey,
2067
- calcPaddedLen
2089
+ calcPaddedLen,
2090
+ pad,
2091
+ unpad
2068
2092
  },
2069
2093
  encrypt: encrypt2,
2070
2094
  decrypt: decrypt2
@@ -2269,6 +2293,141 @@ function parse2(uri) {
2269
2293
  };
2270
2294
  }
2271
2295
 
2296
+ // nip22.ts
2297
+ var nip22_exports = {};
2298
+ __export(nip22_exports, {
2299
+ parse: () => parse3
2300
+ });
2301
+ function parseKind(kind) {
2302
+ if (!kind)
2303
+ return void 0;
2304
+ return /^\d+$/.test(kind) ? parseInt(kind, 10) : kind;
2305
+ }
2306
+ function parseAddressPointer(value, relayUrl) {
2307
+ const idx = value.indexOf(":");
2308
+ const idx2 = value.indexOf(":", idx + 1);
2309
+ if (idx === -1 || idx2 === -1)
2310
+ return void 0;
2311
+ const kind = parseInt(value.slice(0, idx), 10);
2312
+ if (Number.isNaN(kind))
2313
+ return void 0;
2314
+ return {
2315
+ kind,
2316
+ pubkey: value.slice(idx + 1, idx2),
2317
+ identifier: value.slice(idx2 + 1),
2318
+ relays: relayUrl ? [relayUrl] : []
2319
+ };
2320
+ }
2321
+ function parsePointer(tag) {
2322
+ switch (tag[0]) {
2323
+ case "E":
2324
+ case "e":
2325
+ if (!tag[1])
2326
+ return void 0;
2327
+ return {
2328
+ id: tag[1],
2329
+ relays: tag[2] ? [tag[2]] : [],
2330
+ author: tag[3]
2331
+ };
2332
+ case "A":
2333
+ case "a":
2334
+ if (!tag[1])
2335
+ return void 0;
2336
+ return parseAddressPointer(tag[1], tag[2]);
2337
+ case "I":
2338
+ case "i":
2339
+ if (!tag[1])
2340
+ return void 0;
2341
+ return {
2342
+ value: tag[1],
2343
+ hint: tag[2]
2344
+ };
2345
+ }
2346
+ }
2347
+ function parseQuote(tag) {
2348
+ if (!tag[1])
2349
+ return void 0;
2350
+ if (tag[1].includes(":")) {
2351
+ return parseAddressPointer(tag[1], tag[2]);
2352
+ }
2353
+ return {
2354
+ id: tag[1],
2355
+ relays: tag[2] ? [tag[2]] : [],
2356
+ author: tag[3]
2357
+ };
2358
+ }
2359
+ function choosePointer(candidates) {
2360
+ return candidates.findLast((candidate) => candidate.tagName === "A" || candidate.tagName === "a")?.pointer || candidates.findLast((candidate) => candidate.tagName === "I" || candidate.tagName === "i")?.pointer || candidates.findLast((candidate) => candidate.tagName === "E" || candidate.tagName === "e")?.pointer;
2361
+ }
2362
+ function inheritRelayHints(pointer, profiles) {
2363
+ if (!pointer || !("id" in pointer) || !pointer.author)
2364
+ return;
2365
+ const author = profiles.find((profile) => profile.pubkey === pointer.author);
2366
+ if (!author || !author.relays)
2367
+ return;
2368
+ if (!pointer.relays) {
2369
+ pointer.relays = [];
2370
+ }
2371
+ author.relays.forEach((url) => {
2372
+ if (pointer.relays.indexOf(url) === -1)
2373
+ pointer.relays.push(url);
2374
+ });
2375
+ author.relays = pointer.relays;
2376
+ }
2377
+ function parse3(event) {
2378
+ const result = {
2379
+ root: void 0,
2380
+ rootKind: void 0,
2381
+ reply: void 0,
2382
+ replyKind: void 0,
2383
+ mentions: [],
2384
+ quotes: [],
2385
+ profiles: []
2386
+ };
2387
+ const rootCandidates = [];
2388
+ const replyCandidates = [];
2389
+ for (const tag of event.tags) {
2390
+ if ((tag[0] === "E" || tag[0] === "A" || tag[0] === "I") && tag[1]) {
2391
+ const pointer = parsePointer(tag);
2392
+ if (pointer)
2393
+ rootCandidates.push({ tagName: tag[0], pointer });
2394
+ continue;
2395
+ }
2396
+ if ((tag[0] === "e" || tag[0] === "a" || tag[0] === "i") && tag[1]) {
2397
+ const pointer = parsePointer(tag);
2398
+ if (pointer)
2399
+ replyCandidates.push({ tagName: tag[0], pointer });
2400
+ continue;
2401
+ }
2402
+ if (tag[0] === "K") {
2403
+ result.rootKind = parseKind(tag[1]);
2404
+ continue;
2405
+ }
2406
+ if (tag[0] === "k") {
2407
+ result.replyKind = parseKind(tag[1]);
2408
+ continue;
2409
+ }
2410
+ if (tag[0] === "q") {
2411
+ const pointer = parseQuote(tag);
2412
+ if (pointer)
2413
+ result.quotes.push(pointer);
2414
+ continue;
2415
+ }
2416
+ if ((tag[0] === "P" || tag[0] === "p") && tag[1]) {
2417
+ result.profiles.push({
2418
+ pubkey: tag[1],
2419
+ relays: tag[2] ? [tag[2]] : []
2420
+ });
2421
+ }
2422
+ }
2423
+ result.root = choosePointer(rootCandidates);
2424
+ result.reply = choosePointer(replyCandidates);
2425
+ inheritRelayHints(result.root, result.profiles);
2426
+ inheritRelayHints(result.reply, result.profiles);
2427
+ result.quotes.forEach((pointer) => inheritRelayHints(pointer, result.profiles));
2428
+ return result;
2429
+ }
2430
+
2272
2431
  // nip25.ts
2273
2432
  var nip25_exports = {};
2274
2433
  __export(nip25_exports, {
@@ -2316,12 +2475,12 @@ function getReactedEventPointer(event) {
2316
2475
  // nip27.ts
2317
2476
  var nip27_exports = {};
2318
2477
  __export(nip27_exports, {
2319
- parse: () => parse3
2478
+ parse: () => parse4
2320
2479
  });
2321
2480
  var noCharacter = /\W/m;
2322
2481
  var noURLCharacter = /[^\w\/] |[^\w\/]$|$|,| /m;
2323
2482
  var MAX_HASHTAG_LENGTH = 42;
2324
- function* parse3(content) {
2483
+ function* parse4(content) {
2325
2484
  let emojis = [];
2326
2485
  if (typeof content !== "string") {
2327
2486
  for (let i2 = 0; i2 < content.tags.length; i2++) {
@@ -2632,12 +2791,12 @@ __export(nip47_exports, {
2632
2791
  function parseConnectionString(connectionString) {
2633
2792
  const { host, pathname, searchParams } = new URL(connectionString);
2634
2793
  const pubkey = pathname || host;
2635
- const relay = searchParams.get("relay");
2794
+ const relays = searchParams.getAll("relay");
2636
2795
  const secret = searchParams.get("secret");
2637
- if (!pubkey || !relay || !secret) {
2796
+ if (!pubkey || relays.length === 0 || !secret) {
2638
2797
  throw new Error("invalid connection string");
2639
2798
  }
2640
- return { pubkey, relay, secret };
2799
+ return { pubkey, relay: relays[0], relays, secret };
2641
2800
  }
2642
2801
  async function makeNwcRequestEvent(pubkey, secretKey, invoice) {
2643
2802
  const content = {
@@ -3462,6 +3621,7 @@ export {
3462
3621
  nip18_exports as nip18,
3463
3622
  nip19_exports as nip19,
3464
3623
  nip21_exports as nip21,
3624
+ nip22_exports as nip22,
3465
3625
  nip25_exports as nip25,
3466
3626
  nip27_exports as nip27,
3467
3627
  nip28_exports as nip28,