nostr-tools 2.3.0 → 2.3.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 (89) hide show
  1. package/README.md +5 -1
  2. package/lib/cjs/abstract-pool.js +9 -6
  3. package/lib/cjs/abstract-pool.js.map +2 -2
  4. package/lib/cjs/abstract-relay.js +9 -6
  5. package/lib/cjs/abstract-relay.js.map +2 -2
  6. package/lib/cjs/filter.js.map +2 -2
  7. package/lib/cjs/index.js +60 -53
  8. package/lib/cjs/index.js.map +3 -3
  9. package/lib/cjs/kinds.js.map +2 -2
  10. package/lib/cjs/nip04.js.map +2 -2
  11. package/lib/cjs/nip11.js.map +2 -2
  12. package/lib/cjs/nip13.js.map +2 -2
  13. package/lib/cjs/nip18.js.map +2 -2
  14. package/lib/cjs/nip19.js.map +2 -2
  15. package/lib/cjs/nip21.js.map +2 -2
  16. package/lib/cjs/nip25.js.map +2 -2
  17. package/lib/cjs/nip27.js.map +2 -2
  18. package/lib/cjs/nip28.js.map +2 -2
  19. package/lib/cjs/nip29.js.map +2 -2
  20. package/lib/cjs/nip30.js.map +2 -2
  21. package/lib/cjs/nip42.js.map +1 -1
  22. package/lib/cjs/nip44.js +51 -47
  23. package/lib/cjs/nip44.js.map +2 -2
  24. package/lib/cjs/nip46.js +20 -9
  25. package/lib/cjs/nip46.js.map +3 -3
  26. package/lib/cjs/nip47.js.map +2 -2
  27. package/lib/cjs/nip57.js.map +2 -2
  28. package/lib/cjs/nip75.js.map +1 -1
  29. package/lib/cjs/nip94.js.map +1 -1
  30. package/lib/cjs/nip96.js +3 -5
  31. package/lib/cjs/nip96.js.map +2 -2
  32. package/lib/cjs/nip98.js.map +2 -2
  33. package/lib/cjs/nip99.js.map +1 -1
  34. package/lib/cjs/pool.js +9 -6
  35. package/lib/cjs/pool.js.map +2 -2
  36. package/lib/cjs/pure.js.map +2 -2
  37. package/lib/cjs/references.js.map +2 -2
  38. package/lib/cjs/relay.js +9 -6
  39. package/lib/cjs/relay.js.map +2 -2
  40. package/lib/cjs/utils.js.map +2 -2
  41. package/lib/esm/abstract-pool.js +9 -6
  42. package/lib/esm/abstract-pool.js.map +2 -2
  43. package/lib/esm/abstract-relay.js +9 -6
  44. package/lib/esm/abstract-relay.js.map +2 -2
  45. package/lib/esm/filter.js.map +2 -2
  46. package/lib/esm/index.js +60 -53
  47. package/lib/esm/index.js.map +3 -3
  48. package/lib/esm/kinds.js.map +2 -2
  49. package/lib/esm/nip04.js.map +2 -2
  50. package/lib/esm/nip11.js.map +2 -2
  51. package/lib/esm/nip13.js.map +2 -2
  52. package/lib/esm/nip18.js.map +2 -2
  53. package/lib/esm/nip19.js.map +2 -2
  54. package/lib/esm/nip21.js.map +2 -2
  55. package/lib/esm/nip25.js.map +2 -2
  56. package/lib/esm/nip27.js.map +2 -2
  57. package/lib/esm/nip28.js.map +2 -2
  58. package/lib/esm/nip29.js.map +2 -2
  59. package/lib/esm/nip30.js.map +2 -2
  60. package/lib/esm/nip42.js.map +1 -1
  61. package/lib/esm/nip44.js +53 -47
  62. package/lib/esm/nip44.js.map +2 -2
  63. package/lib/esm/nip46.js +20 -9
  64. package/lib/esm/nip46.js.map +3 -3
  65. package/lib/esm/nip47.js.map +2 -2
  66. package/lib/esm/nip57.js.map +2 -2
  67. package/lib/esm/nip75.js.map +1 -1
  68. package/lib/esm/nip94.js.map +1 -1
  69. package/lib/esm/nip96.js +5 -5
  70. package/lib/esm/nip96.js.map +3 -3
  71. package/lib/esm/nip98.js.map +2 -2
  72. package/lib/esm/nip99.js.map +1 -1
  73. package/lib/esm/pool.js +9 -6
  74. package/lib/esm/pool.js.map +2 -2
  75. package/lib/esm/pure.js.map +2 -2
  76. package/lib/esm/references.js.map +2 -2
  77. package/lib/esm/relay.js +9 -6
  78. package/lib/esm/relay.js.map +2 -2
  79. package/lib/esm/utils.js.map +2 -2
  80. package/lib/nostr.bundle.js +60 -53
  81. package/lib/nostr.bundle.js.map +3 -3
  82. package/lib/types/abstract-relay.d.ts +1 -0
  83. package/lib/types/nip44.d.ts +27 -53
  84. package/lib/types/nip46.d.ts +2 -1
  85. package/lib/types/nip47.d.ts +6 -3
  86. package/lib/types/nip96.d.ts +2 -0
  87. package/lib/types/test-helpers.d.ts +0 -1
  88. package/lib/types/utils.d.ts +1 -0
  89. package/package.json +1 -1
package/lib/cjs/index.js CHANGED
@@ -3,6 +3,7 @@ var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
7
  var __export = (target, all) => {
7
8
  for (var name in all)
8
9
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -16,6 +17,10 @@ var __copyProps = (to, from, except, desc) => {
16
17
  return to;
17
18
  };
18
19
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+ var __publicField = (obj, key, value) => {
21
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
22
+ return value;
23
+ };
19
24
 
20
25
  // index.ts
21
26
  var nostr_tools_exports = {};
@@ -679,16 +684,19 @@ var AbstractRelay = class {
679
684
  this.ws.onerror = (ev) => {
680
685
  reject(ev.message);
681
686
  if (this._connected) {
687
+ this._connected = false;
688
+ this.connectionPromise = void 0;
682
689
  this.onclose?.();
683
690
  this.closeAllSubscriptions("relay connection errored");
684
- this._connected = false;
685
691
  }
686
692
  };
687
693
  this.ws.onclose = async () => {
688
- this.connectionPromise = void 0;
689
- this.onclose?.();
690
- this.closeAllSubscriptions("relay connection closed");
691
- this._connected = false;
694
+ if (this._connected) {
695
+ this._connected = false;
696
+ this.connectionPromise = void 0;
697
+ this.onclose?.();
698
+ this.closeAllSubscriptions("relay connection closed");
699
+ }
692
700
  };
693
701
  this.ws.onmessage = this._onmessage.bind(this);
694
702
  });
@@ -881,7 +889,7 @@ var Subscription = class {
881
889
  this.oneose?.();
882
890
  }
883
891
  close(reason = "closed by caller") {
884
- if (!this.closed) {
892
+ if (!this.closed && this.relay.connected) {
885
893
  this.relay.send('["CLOSE",' + JSON.stringify(this.id) + "]");
886
894
  this.closed = true;
887
895
  }
@@ -1866,26 +1874,23 @@ var import_sha2562 = require("@noble/hashes/sha256");
1866
1874
  var import_utils10 = require("@noble/hashes/utils");
1867
1875
  var import_base3 = require("@scure/base");
1868
1876
  var decoder = new TextDecoder();
1869
- var u = {
1870
- minPlaintextSize: 1,
1871
- maxPlaintextSize: 65535,
1872
- utf8Encode: import_utils10.utf8ToBytes,
1873
- utf8Decode(bytes) {
1877
+ var _u = class {
1878
+ static utf8Decode(bytes) {
1874
1879
  return decoder.decode(bytes);
1875
- },
1876
- getConversationKey(privkeyA, pubkeyB) {
1880
+ }
1881
+ static getConversationKey(privkeyA, pubkeyB) {
1877
1882
  const sharedX = import_secp256k13.secp256k1.getSharedSecret(privkeyA, "02" + pubkeyB).subarray(1, 33);
1878
1883
  return (0, import_hkdf.extract)(import_sha2562.sha256, sharedX, "nip44-v2");
1879
- },
1880
- getMessageKeys(conversationKey, nonce) {
1884
+ }
1885
+ static getMessageKeys(conversationKey, nonce) {
1881
1886
  const keys = (0, import_hkdf.expand)(import_sha2562.sha256, conversationKey, nonce, 76);
1882
1887
  return {
1883
1888
  chacha_key: keys.subarray(0, 32),
1884
1889
  chacha_nonce: keys.subarray(32, 44),
1885
1890
  hmac_key: keys.subarray(44, 76)
1886
1891
  };
1887
- },
1888
- calcPaddedLen(len) {
1892
+ }
1893
+ static calcPaddedLen(len) {
1889
1894
  if (!Number.isSafeInteger(len) || len < 1)
1890
1895
  throw new Error("expected positive integer");
1891
1896
  if (len <= 32)
@@ -1893,35 +1898,35 @@ var u = {
1893
1898
  const nextPower = 1 << Math.floor(Math.log2(len - 1)) + 1;
1894
1899
  const chunk = nextPower <= 256 ? 32 : nextPower / 8;
1895
1900
  return chunk * (Math.floor((len - 1) / chunk) + 1);
1896
- },
1897
- writeU16BE(num) {
1898
- if (!Number.isSafeInteger(num) || num < u.minPlaintextSize || num > u.maxPlaintextSize)
1901
+ }
1902
+ static writeU16BE(num) {
1903
+ if (!Number.isSafeInteger(num) || num < _u.minPlaintextSize || num > _u.maxPlaintextSize)
1899
1904
  throw new Error("invalid plaintext size: must be between 1 and 65535 bytes");
1900
1905
  const arr = new Uint8Array(2);
1901
1906
  new DataView(arr.buffer).setUint16(0, num, false);
1902
1907
  return arr;
1903
- },
1904
- pad(plaintext) {
1905
- const unpadded = u.utf8Encode(plaintext);
1908
+ }
1909
+ static pad(plaintext) {
1910
+ const unpadded = _u.utf8Encode(plaintext);
1906
1911
  const unpaddedLen = unpadded.length;
1907
- const prefix = u.writeU16BE(unpaddedLen);
1908
- const suffix = new Uint8Array(u.calcPaddedLen(unpaddedLen) - unpaddedLen);
1912
+ const prefix = _u.writeU16BE(unpaddedLen);
1913
+ const suffix = new Uint8Array(_u.calcPaddedLen(unpaddedLen) - unpaddedLen);
1909
1914
  return (0, import_utils10.concatBytes)(prefix, unpadded, suffix);
1910
- },
1911
- unpad(padded) {
1915
+ }
1916
+ static unpad(padded) {
1912
1917
  const unpaddedLen = new DataView(padded.buffer).getUint16(0);
1913
1918
  const unpadded = padded.subarray(2, 2 + unpaddedLen);
1914
- if (unpaddedLen < u.minPlaintextSize || unpaddedLen > u.maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== 2 + u.calcPaddedLen(unpaddedLen))
1919
+ if (unpaddedLen < _u.minPlaintextSize || unpaddedLen > _u.maxPlaintextSize || unpadded.length !== unpaddedLen || padded.length !== 2 + _u.calcPaddedLen(unpaddedLen))
1915
1920
  throw new Error("invalid padding");
1916
- return u.utf8Decode(unpadded);
1917
- },
1918
- hmacAad(key, message, aad) {
1921
+ return _u.utf8Decode(unpadded);
1922
+ }
1923
+ static hmacAad(key, message, aad) {
1919
1924
  if (aad.length !== 32)
1920
1925
  throw new Error("AAD associated data must be 32 bytes");
1921
1926
  const combined = (0, import_utils10.concatBytes)(aad, message);
1922
1927
  return (0, import_hmac.hmac)(import_sha2562.sha256, key, combined);
1923
- },
1924
- decodePayload(payload) {
1928
+ }
1929
+ static decodePayload(payload) {
1925
1930
  if (typeof payload !== "string")
1926
1931
  throw new Error("payload must be a valid string");
1927
1932
  const plen = payload.length;
@@ -1948,27 +1953,29 @@ var u = {
1948
1953
  };
1949
1954
  }
1950
1955
  };
1951
- function encrypt2(plaintext, conversationKey, nonce = (0, import_utils10.randomBytes)(32)) {
1952
- const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce);
1953
- const padded = u.pad(plaintext);
1954
- const ciphertext = (0, import_chacha.chacha20)(chacha_key, chacha_nonce, padded);
1955
- const mac = u.hmacAad(hmac_key, ciphertext, nonce);
1956
- return import_base3.base64.encode((0, import_utils10.concatBytes)(new Uint8Array([2]), nonce, ciphertext, mac));
1957
- }
1958
- function decrypt2(payload, conversationKey) {
1959
- const { nonce, ciphertext, mac } = u.decodePayload(payload);
1960
- const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce);
1961
- const calculatedMac = u.hmacAad(hmac_key, ciphertext, nonce);
1962
- if (!(0, import_utils9.equalBytes)(calculatedMac, mac))
1963
- throw new Error("invalid MAC");
1964
- const padded = (0, import_chacha.chacha20)(chacha_key, chacha_nonce, ciphertext);
1965
- return u.unpad(padded);
1966
- }
1967
- var v2 = {
1968
- utils: u,
1969
- encrypt: encrypt2,
1970
- decrypt: decrypt2
1956
+ var u = _u;
1957
+ __publicField(u, "minPlaintextSize", 1);
1958
+ __publicField(u, "maxPlaintextSize", 65535);
1959
+ __publicField(u, "utf8Encode", import_utils10.utf8ToBytes);
1960
+ var v2 = class {
1961
+ static encrypt(plaintext, conversationKey, nonce = (0, import_utils10.randomBytes)(32)) {
1962
+ const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce);
1963
+ const padded = u.pad(plaintext);
1964
+ const ciphertext = (0, import_chacha.chacha20)(chacha_key, chacha_nonce, padded);
1965
+ const mac = u.hmacAad(hmac_key, ciphertext, nonce);
1966
+ return import_base3.base64.encode((0, import_utils10.concatBytes)(new Uint8Array([2]), nonce, ciphertext, mac));
1967
+ }
1968
+ static decrypt(payload, conversationKey) {
1969
+ const { nonce, ciphertext, mac } = u.decodePayload(payload);
1970
+ const { chacha_key, chacha_nonce, hmac_key } = u.getMessageKeys(conversationKey, nonce);
1971
+ const calculatedMac = u.hmacAad(hmac_key, ciphertext, nonce);
1972
+ if (!(0, import_utils9.equalBytes)(calculatedMac, mac))
1973
+ throw new Error("invalid MAC");
1974
+ const padded = (0, import_chacha.chacha20)(chacha_key, chacha_nonce, ciphertext);
1975
+ return u.unpad(padded);
1976
+ }
1971
1977
  };
1978
+ __publicField(v2, "utils", u);
1972
1979
  var nip44_default = { v2 };
1973
1980
 
1974
1981
  // nip47.ts