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
@@ -42,6 +42,7 @@ var NostrTools = (() => {
42
42
  nip18: () => nip18_exports,
43
43
  nip19: () => nip19_exports,
44
44
  nip21: () => nip21_exports,
45
+ nip22: () => nip22_exports,
45
46
  nip25: () => nip25_exports,
46
47
  nip27: () => nip27_exports,
47
48
  nip28: () => nip28_exports,
@@ -5704,7 +5705,8 @@ var NostrTools = (() => {
5704
5705
 
5705
5706
  // nip44.ts
5706
5707
  var minPlaintextSize = 1;
5707
- var maxPlaintextSize = 65535;
5708
+ var maxPlaintextSize = 4294967295;
5709
+ var extendedPrefixThreshold = 65536;
5708
5710
  function getConversationKey(privkeyA, pubkeyB) {
5709
5711
  const sharedX = secp256k1.getSharedSecret(privkeyA, hexToBytes("02" + pubkeyB)).subarray(1, 33);
5710
5712
  return extract(sha256, sharedX, utf8Encoder.encode("nip44-v2"));
@@ -5722,28 +5724,49 @@ var NostrTools = (() => {
5722
5724
  throw new Error("expected positive integer");
5723
5725
  if (len <= 32)
5724
5726
  return 32;
5725
- const nextPower = 1 << Math.floor(Math.log2(len - 1)) + 1;
5727
+ const nextPower = 2 ** (Math.floor(Math.log2(len - 1)) + 1);
5726
5728
  const chunk = nextPower <= 256 ? 32 : nextPower / 8;
5727
5729
  return chunk * (Math.floor((len - 1) / chunk) + 1);
5728
5730
  }
5729
5731
  function writeU16BE(num2) {
5730
- if (!Number.isSafeInteger(num2) || num2 < minPlaintextSize || num2 > maxPlaintextSize)
5732
+ if (!Number.isSafeInteger(num2) || num2 < minPlaintextSize || num2 > 65535)
5731
5733
  throw new Error("invalid plaintext size: must be between 1 and 65535 bytes");
5732
5734
  const arr = new Uint8Array(2);
5733
5735
  new DataView(arr.buffer).setUint16(0, num2, false);
5734
5736
  return arr;
5735
5737
  }
5738
+ function writeU32BE(num2) {
5739
+ if (!Number.isSafeInteger(num2) || num2 < extendedPrefixThreshold || num2 > maxPlaintextSize)
5740
+ throw new Error("invalid plaintext size: must be between 65536 and 4294967295 bytes");
5741
+ const arr = new Uint8Array(4);
5742
+ new DataView(arr.buffer).setUint32(0, num2, false);
5743
+ return arr;
5744
+ }
5736
5745
  function pad(plaintext) {
5737
5746
  const unpadded = utf8Encoder.encode(plaintext);
5738
5747
  const unpaddedLen = unpadded.length;
5739
- const prefix = writeU16BE(unpaddedLen);
5748
+ if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize)
5749
+ throw new Error("invalid plaintext size: must be between 1 and 4294967295 bytes");
5750
+ const prefix = unpaddedLen >= extendedPrefixThreshold ? concatBytes(new Uint8Array([0, 0]), writeU32BE(unpaddedLen)) : writeU16BE(unpaddedLen);
5740
5751
  const suffix = new Uint8Array(calcPaddedLen(unpaddedLen) - unpaddedLen);
5741
5752
  return concatBytes(prefix, unpadded, suffix);
5742
5753
  }
5743
5754
  function unpad(padded) {
5744
- const unpaddedLen = new DataView(padded.buffer).getUint16(0);
5745
- const unpadded = padded.subarray(2, 2 + unpaddedLen);
5746
- if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== 2 + calcPaddedLen(unpaddedLen))
5755
+ const dv = new DataView(padded.buffer, padded.byteOffset, padded.byteLength);
5756
+ const firstTwo = dv.getUint16(0);
5757
+ let unpaddedLen;
5758
+ let prefixLen;
5759
+ if (firstTwo === 0) {
5760
+ unpaddedLen = dv.getUint32(2);
5761
+ if (unpaddedLen < extendedPrefixThreshold)
5762
+ throw new Error("invalid padding");
5763
+ prefixLen = 6;
5764
+ } else {
5765
+ unpaddedLen = firstTwo;
5766
+ prefixLen = 2;
5767
+ }
5768
+ const unpadded = padded.subarray(prefixLen, prefixLen + unpaddedLen);
5769
+ if (unpaddedLen < minPlaintextSize || unpaddedLen > maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== prefixLen + calcPaddedLen(unpaddedLen))
5747
5770
  throw new Error("invalid padding");
5748
5771
  return utf8Decoder.decode(unpadded);
5749
5772
  }
@@ -5757,7 +5780,7 @@ var NostrTools = (() => {
5757
5780
  if (typeof payload !== "string")
5758
5781
  throw new Error("payload must be a valid string");
5759
5782
  const plen = payload.length;
5760
- if (plen < 132 || plen > 87472)
5783
+ if (plen < 132)
5761
5784
  throw new Error("invalid payload length: " + plen);
5762
5785
  if (payload[0] === "#")
5763
5786
  throw new Error("unknown encryption version");
@@ -5768,7 +5791,7 @@ var NostrTools = (() => {
5768
5791
  throw new Error("invalid base64: " + error.message);
5769
5792
  }
5770
5793
  const dlen = data.length;
5771
- if (dlen < 99 || dlen > 65603)
5794
+ if (dlen < 99)
5772
5795
  throw new Error("invalid data length: " + dlen);
5773
5796
  const vers = data[0];
5774
5797
  if (vers !== 2)
@@ -5798,7 +5821,9 @@ var NostrTools = (() => {
5798
5821
  var v2 = {
5799
5822
  utils: {
5800
5823
  getConversationKey,
5801
- calcPaddedLen
5824
+ calcPaddedLen,
5825
+ pad,
5826
+ unpad
5802
5827
  },
5803
5828
  encrypt: encrypt3,
5804
5829
  decrypt: decrypt3
@@ -6003,6 +6028,141 @@ var NostrTools = (() => {
6003
6028
  };
6004
6029
  }
6005
6030
 
6031
+ // nip22.ts
6032
+ var nip22_exports = {};
6033
+ __export(nip22_exports, {
6034
+ parse: () => parse3
6035
+ });
6036
+ function parseKind(kind) {
6037
+ if (!kind)
6038
+ return void 0;
6039
+ return /^\d+$/.test(kind) ? parseInt(kind, 10) : kind;
6040
+ }
6041
+ function parseAddressPointer(value, relayUrl) {
6042
+ const idx = value.indexOf(":");
6043
+ const idx2 = value.indexOf(":", idx + 1);
6044
+ if (idx === -1 || idx2 === -1)
6045
+ return void 0;
6046
+ const kind = parseInt(value.slice(0, idx), 10);
6047
+ if (Number.isNaN(kind))
6048
+ return void 0;
6049
+ return {
6050
+ kind,
6051
+ pubkey: value.slice(idx + 1, idx2),
6052
+ identifier: value.slice(idx2 + 1),
6053
+ relays: relayUrl ? [relayUrl] : []
6054
+ };
6055
+ }
6056
+ function parsePointer(tag) {
6057
+ switch (tag[0]) {
6058
+ case "E":
6059
+ case "e":
6060
+ if (!tag[1])
6061
+ return void 0;
6062
+ return {
6063
+ id: tag[1],
6064
+ relays: tag[2] ? [tag[2]] : [],
6065
+ author: tag[3]
6066
+ };
6067
+ case "A":
6068
+ case "a":
6069
+ if (!tag[1])
6070
+ return void 0;
6071
+ return parseAddressPointer(tag[1], tag[2]);
6072
+ case "I":
6073
+ case "i":
6074
+ if (!tag[1])
6075
+ return void 0;
6076
+ return {
6077
+ value: tag[1],
6078
+ hint: tag[2]
6079
+ };
6080
+ }
6081
+ }
6082
+ function parseQuote(tag) {
6083
+ if (!tag[1])
6084
+ return void 0;
6085
+ if (tag[1].includes(":")) {
6086
+ return parseAddressPointer(tag[1], tag[2]);
6087
+ }
6088
+ return {
6089
+ id: tag[1],
6090
+ relays: tag[2] ? [tag[2]] : [],
6091
+ author: tag[3]
6092
+ };
6093
+ }
6094
+ function choosePointer(candidates) {
6095
+ 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;
6096
+ }
6097
+ function inheritRelayHints(pointer, profiles) {
6098
+ if (!pointer || !("id" in pointer) || !pointer.author)
6099
+ return;
6100
+ const author = profiles.find((profile) => profile.pubkey === pointer.author);
6101
+ if (!author || !author.relays)
6102
+ return;
6103
+ if (!pointer.relays) {
6104
+ pointer.relays = [];
6105
+ }
6106
+ author.relays.forEach((url) => {
6107
+ if (pointer.relays.indexOf(url) === -1)
6108
+ pointer.relays.push(url);
6109
+ });
6110
+ author.relays = pointer.relays;
6111
+ }
6112
+ function parse3(event) {
6113
+ const result = {
6114
+ root: void 0,
6115
+ rootKind: void 0,
6116
+ reply: void 0,
6117
+ replyKind: void 0,
6118
+ mentions: [],
6119
+ quotes: [],
6120
+ profiles: []
6121
+ };
6122
+ const rootCandidates = [];
6123
+ const replyCandidates = [];
6124
+ for (const tag of event.tags) {
6125
+ if ((tag[0] === "E" || tag[0] === "A" || tag[0] === "I") && tag[1]) {
6126
+ const pointer = parsePointer(tag);
6127
+ if (pointer)
6128
+ rootCandidates.push({ tagName: tag[0], pointer });
6129
+ continue;
6130
+ }
6131
+ if ((tag[0] === "e" || tag[0] === "a" || tag[0] === "i") && tag[1]) {
6132
+ const pointer = parsePointer(tag);
6133
+ if (pointer)
6134
+ replyCandidates.push({ tagName: tag[0], pointer });
6135
+ continue;
6136
+ }
6137
+ if (tag[0] === "K") {
6138
+ result.rootKind = parseKind(tag[1]);
6139
+ continue;
6140
+ }
6141
+ if (tag[0] === "k") {
6142
+ result.replyKind = parseKind(tag[1]);
6143
+ continue;
6144
+ }
6145
+ if (tag[0] === "q") {
6146
+ const pointer = parseQuote(tag);
6147
+ if (pointer)
6148
+ result.quotes.push(pointer);
6149
+ continue;
6150
+ }
6151
+ if ((tag[0] === "P" || tag[0] === "p") && tag[1]) {
6152
+ result.profiles.push({
6153
+ pubkey: tag[1],
6154
+ relays: tag[2] ? [tag[2]] : []
6155
+ });
6156
+ }
6157
+ }
6158
+ result.root = choosePointer(rootCandidates);
6159
+ result.reply = choosePointer(replyCandidates);
6160
+ inheritRelayHints(result.root, result.profiles);
6161
+ inheritRelayHints(result.reply, result.profiles);
6162
+ result.quotes.forEach((pointer) => inheritRelayHints(pointer, result.profiles));
6163
+ return result;
6164
+ }
6165
+
6006
6166
  // nip25.ts
6007
6167
  var nip25_exports = {};
6008
6168
  __export(nip25_exports, {
@@ -6050,12 +6210,12 @@ var NostrTools = (() => {
6050
6210
  // nip27.ts
6051
6211
  var nip27_exports = {};
6052
6212
  __export(nip27_exports, {
6053
- parse: () => parse3
6213
+ parse: () => parse4
6054
6214
  });
6055
6215
  var noCharacter = /\W/m;
6056
6216
  var noURLCharacter = /[^\w\/] |[^\w\/]$|$|,| /m;
6057
6217
  var MAX_HASHTAG_LENGTH = 42;
6058
- function* parse3(content) {
6218
+ function* parse4(content) {
6059
6219
  let emojis = [];
6060
6220
  if (typeof content !== "string") {
6061
6221
  for (let i2 = 0; i2 < content.tags.length; i2++) {
@@ -6366,12 +6526,12 @@ var NostrTools = (() => {
6366
6526
  function parseConnectionString(connectionString) {
6367
6527
  const { host, pathname, searchParams } = new URL(connectionString);
6368
6528
  const pubkey = pathname || host;
6369
- const relay = searchParams.get("relay");
6529
+ const relays = searchParams.getAll("relay");
6370
6530
  const secret = searchParams.get("secret");
6371
- if (!pubkey || !relay || !secret) {
6531
+ if (!pubkey || relays.length === 0 || !secret) {
6372
6532
  throw new Error("invalid connection string");
6373
6533
  }
6374
- return { pubkey, relay, secret };
6534
+ return { pubkey, relay: relays[0], relays, secret };
6375
6535
  }
6376
6536
  async function makeNwcRequestEvent(pubkey, secretKey, invoice) {
6377
6537
  const content = {