nostr-tools

2.20.02.21.0
lib/esm/index.js
~lib/esm/index.jsModified
+46−46
Index: package/lib/esm/index.js
===================================================================
--- package/lib/esm/index.js
+++ package/lib/esm/index.js
@@ -4,10 +4,10 @@
     __defProp(target, name, { get: all[name], enumerable: true });
 };
 
 // pure.ts
-import { schnorr } from "@noble/curves/secp256k1";
-import { bytesToHex as bytesToHex2 } from "@noble/hashes/utils";
+import { schnorr } from "@noble/curves/secp256k1.js";
+import { bytesToHex as bytesToHex2, hexToBytes as hexToBytes2 } from "@noble/hashes/utils.js";
 
 // core.ts
 var verifiedSymbol = Symbol("verified");
 var isRecord = (obj) => obj instanceof Object;
@@ -46,9 +46,9 @@
   });
 }
 
 // pure.ts
-import { sha256 } from "@noble/hashes/sha256";
+import { sha256 } from "@noble/hashes/sha2.js";
 
 // utils.ts
 var utils_exports = {};
 __export(utils_exports, {
@@ -62,9 +62,9 @@
   normalizeURL: () => normalizeURL,
   utf8Decoder: () => utf8Decoder,
   utf8Encoder: () => utf8Encoder
 });
-import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
+import { bytesToHex, hexToBytes } from "@noble/hashes/utils.js";
 var utf8Decoder = new TextDecoder("utf-8");
 var utf8Encoder = new TextEncoder();
 function normalizeURL(url) {
   try {
@@ -181,18 +181,18 @@
 
 // pure.ts
 var JS = class {
   generateSecretKey() {
-    return schnorr.utils.randomPrivateKey();
+    return schnorr.utils.randomSecretKey();
   }
   getPublicKey(secretKey) {
     return bytesToHex2(schnorr.getPublicKey(secretKey));
   }
   finalizeEvent(t, secretKey) {
     const event = t;
     event.pubkey = bytesToHex2(schnorr.getPublicKey(secretKey));
     event.id = getEventHash(event);
-    event.sig = bytesToHex2(schnorr.sign(getEventHash(event), secretKey));
+    event.sig = bytesToHex2(schnorr.sign(hexToBytes2(getEventHash(event)), secretKey));
     event[verifiedSymbol] = true;
     return event;
   }
   verifyEvent(event) {
@@ -203,9 +203,9 @@
       event[verifiedSymbol] = false;
       return false;
     }
     try {
-      const valid = schnorr.verify(event.sig, hash, event.pubkey);
+      const valid = schnorr.verify(hexToBytes2(event.sig), hexToBytes2(hash), hexToBytes2(event.pubkey));
       event[verifiedSymbol] = valid;
       return valid;
     } catch (err) {
       event[verifiedSymbol] = false;
@@ -1374,9 +1374,9 @@
   nprofileEncode: () => nprofileEncode,
   npubEncode: () => npubEncode,
   nsecEncode: () => nsecEncode
 });
-import { bytesToHex as bytesToHex3, concatBytes, hexToBytes as hexToBytes2 } from "@noble/hashes/utils";
+import { bytesToHex as bytesToHex3, concatBytes, hexToBytes as hexToBytes3 } from "@noble/hashes/utils.js";
 import { bech32 } from "@scure/base";
 var NostrTypeGuard = {
   isNProfile: (value) => /^nprofile1[a-z\d]+$/.test(value || ""),
   isNEvent: (value) => /^nevent1[a-z\d]+$/.test(value || ""),
@@ -1492,12 +1492,12 @@
 function nsecEncode(key) {
   return encodeBytes("nsec", key);
 }
 function npubEncode(hex) {
-  return encodeBytes("npub", hexToBytes2(hex));
+  return encodeBytes("npub", hexToBytes3(hex));
 }
 function noteEncode(hex) {
-  return encodeBytes("note", hexToBytes2(hex));
+  return encodeBytes("note", hexToBytes3(hex));
 }
 function encodeBech32(prefix, data) {
   let words = bech32.toWords(data);
   return bech32.encode(prefix, words, Bech32MaxSize);
@@ -1506,9 +1506,9 @@
   return encodeBech32(prefix, bytes);
 }
 function nprofileEncode(profile) {
   let data = encodeTLV({
-    0: [hexToBytes2(profile.pubkey)],
+    0: [hexToBytes3(profile.pubkey)],
     1: (profile.relays || []).map((url) => utf8Encoder.encode(url))
   });
   return encodeBech32("nprofile", data);
 }
@@ -1517,11 +1517,11 @@
   if (event.kind !== void 0) {
     kindArray = integerToUint8Array(event.kind);
   }
   let data = encodeTLV({
-    0: [hexToBytes2(event.id)],
+    0: [hexToBytes3(event.id)],
     1: (event.relays || []).map((url) => utf8Encoder.encode(url)),
-    2: event.author ? [hexToBytes2(event.author)] : [],
+    2: event.author ? [hexToBytes3(event.author)] : [],
     3: kindArray ? [new Uint8Array(kindArray)] : []
   });
   return encodeBech32("nevent", data);
 }
@@ -1530,9 +1530,9 @@
   new DataView(kind).setUint32(0, addr.kind, false);
   let data = encodeTLV({
     0: [utf8Encoder.encode(addr.identifier)],
     1: (addr.relays || []).map((url) => utf8Encoder.encode(url)),
-    2: [hexToBytes2(addr.pubkey)],
+    2: [hexToBytes3(addr.pubkey)],
     3: [new Uint8Array(kind)]
   });
   return encodeBech32("naddr", data);
 }
@@ -1644,15 +1644,15 @@
 __export(nip04_exports, {
   decrypt: () => decrypt,
   encrypt: () => encrypt
 });
-import { bytesToHex as bytesToHex4, randomBytes } from "@noble/hashes/utils";
-import { secp256k1 } from "@noble/curves/secp256k1";
-import { cbc } from "@noble/ciphers/aes";
+import { hexToBytes as hexToBytes4, randomBytes } from "@noble/hashes/utils.js";
+import { secp256k1 } from "@noble/curves/secp256k1.js";
+import { cbc } from "@noble/ciphers/aes.js";
 import { base64 } from "@scure/base";
 function encrypt(secretKey, pubkey, text) {
-  const privkey = secretKey instanceof Uint8Array ? bytesToHex4(secretKey) : secretKey;
-  const key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
+  const privkey = secretKey instanceof Uint8Array ? secretKey : hexToBytes4(secretKey);
+  const key = secp256k1.getSharedSecret(privkey, hexToBytes4("02" + pubkey));
   const normalizedKey = getNormalizedX(key);
   let iv = Uint8Array.from(randomBytes(16));
   let plaintext = utf8Encoder.encode(text);
   let ciphertext = cbc(normalizedKey, iv).encrypt(plaintext);
@@ -1660,11 +1660,11 @@
   let ivb64 = base64.encode(new Uint8Array(iv.buffer));
   return `${ctb64}?iv=${ivb64}`;
 }
 function decrypt(secretKey, pubkey, data) {
-  const privkey = secretKey instanceof Uint8Array ? bytesToHex4(secretKey) : secretKey;
+  const privkey = secretKey instanceof Uint8Array ? secretKey : hexToBytes4(secretKey);
   let [ctb64, ivb64] = data.split("?iv=");
-  let key = secp256k1.getSharedSecret(privkey, "02" + pubkey);
+  let key = secp256k1.getSharedSecret(privkey, hexToBytes4("02" + pubkey));
   let normalizedKey = getNormalizedX(key);
   let iv = base64.decode(ivb64);
   let ciphertext = base64.decode(ctb64);
   let plaintext = cbc(normalizedKey, iv).decrypt(ciphertext);
@@ -1862,10 +1862,10 @@
   fastEventHash: () => fastEventHash,
   getPow: () => getPow,
   minePow: () => minePow
 });
-import { bytesToHex as bytesToHex5 } from "@noble/hashes/utils";
-import { sha256 as sha2562 } from "@noble/hashes/sha256";
+import { bytesToHex as bytesToHex4 } from "@noble/hashes/utils.js";
+import { sha256 as sha2562 } from "@noble/hashes/sha2.js";
 function getPow(hex) {
   let count = 0;
   for (let i2 = 0; i2 < 64; i2 += 8) {
     const nibble = parseInt(hex.substring(i2, i2 + 8), 16);
@@ -1897,9 +1897,9 @@
   }
   return event;
 }
 function fastEventHash(evt) {
-  return bytesToHex5(
+  return bytesToHex4(
     sha2562(utf8Encoder.encode(JSON.stringify([0, evt.pubkey, evt.created_at, evt.kind, evt.tags, evt.content])))
   );
 }
 
@@ -1931,21 +1931,21 @@
   encrypt: () => encrypt2,
   getConversationKey: () => getConversationKey,
   v2: () => v2
 });
-import { chacha20 } from "@noble/ciphers/chacha";
-import { equalBytes } from "@noble/ciphers/utils";
-import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1";
-import { extract as hkdf_extract, expand as hkdf_expand } from "@noble/hashes/hkdf";
-import { hmac } from "@noble/hashes/hmac";
-import { sha256 as sha2563 } from "@noble/hashes/sha256";
-import { concatBytes as concatBytes2, randomBytes as randomBytes2 } from "@noble/hashes/utils";
+import { chacha20 } from "@noble/ciphers/chacha.js";
+import { equalBytes } from "@noble/ciphers/utils.js";
+import { secp256k1 as secp256k12 } from "@noble/curves/secp256k1.js";
+import { extract as hkdf_extract, expand as hkdf_expand } from "@noble/hashes/hkdf.js";
+import { hmac } from "@noble/hashes/hmac.js";
+import { sha256 as sha2563 } from "@noble/hashes/sha2.js";
+import { concatBytes as concatBytes2, hexToBytes as hexToBytes5, randomBytes as randomBytes2 } from "@noble/hashes/utils.js";
 import { base64 as base642 } from "@scure/base";
 var minPlaintextSize = 1;
 var maxPlaintextSize = 65535;
 function getConversationKey(privkeyA, pubkeyB) {
-  const sharedX = secp256k12.getSharedSecret(privkeyA, "02" + pubkeyB).subarray(1, 33);
-  return hkdf_extract(sha2563, sharedX, "nip44-v2");
+  const sharedX = secp256k12.getSharedSecret(privkeyA, hexToBytes5("02" + pubkeyB)).subarray(1, 33);
+  return hkdf_extract(sha2563, sharedX, utf8Encoder.encode("nip44-v2"));
 }
 function getMessageKeys(conversationKey, nonce) {
   const keys = hkdf_expand(sha2563, conversationKey, nonce, 76);
   return {
@@ -2803,10 +2803,10 @@
   Negentropy: () => Negentropy,
   NegentropyStorageVector: () => NegentropyStorageVector,
   NegentropySync: () => NegentropySync
 });
-import { bytesToHex as bytesToHex6, hexToBytes as hexToBytes3 } from "@noble/ciphers/utils";
-import { sha256 as sha2564 } from "@noble/hashes/sha256";
+import { bytesToHex as bytesToHex5, hexToBytes as hexToBytes6 } from "@noble/hashes/utils.js";
+import { sha256 as sha2564 } from "@noble/hashes/sha2.js";
 var PROTOCOL_VERSION = 97;
 var ID_SIZE = 32;
 var FINGERPRINT_SIZE = 16;
 var Mode = {
@@ -2949,9 +2949,9 @@
   }
   insert(timestamp, id) {
     if (this.sealed)
       throw Error("already sealed");
-    const idb = hexToBytes3(id);
+    const idb = hexToBytes6(id);
     if (idb.byteLength !== ID_SIZE)
       throw Error("bad id size for added item");
     this.items.push({ timestamp, id: idb });
   }
@@ -3043,12 +3043,12 @@
   initiate() {
     let output = new WrappedBuffer();
     output.extend(new Uint8Array([PROTOCOL_VERSION]));
     this.splitRange(0, this.storage.size(), this._bound(Number.MAX_VALUE), output);
-    return bytesToHex6(output.unwrap());
+    return bytesToHex5(output.unwrap());
   }
   reconcile(queryMsg, onhave, onneed) {
-    const query = new WrappedBuffer(hexToBytes3(queryMsg));
+    const query = new WrappedBuffer(hexToBytes6(queryMsg));
     this.lastTimestampIn = this.lastTimestampOut = 0;
     let fullOutput = new WrappedBuffer();
     fullOutput.extend(new Uint8Array([PROTOCOL_VERSION]));
     let protocolVersion = getByte(query);
@@ -3089,24 +3089,24 @@
         let numIds = decodeVarInt(query);
         let theirElems = {};
         for (let i2 = 0; i2 < numIds; i2++) {
           let e = getBytes(query, ID_SIZE);
-          theirElems[bytesToHex6(e)] = e;
+          theirElems[bytesToHex5(e)] = e;
         }
         skip = true;
         this.storage.iterate(lower, upper, (item) => {
           let k = item.id;
-          const id = bytesToHex6(k);
+          const id = bytesToHex5(k);
           if (!theirElems[id]) {
             onhave?.(id);
           } else {
-            delete theirElems[bytesToHex6(k)];
+            delete theirElems[bytesToHex5(k)];
           }
           return true;
         });
         if (onneed) {
           for (let v of Object.values(theirElems)) {
-            onneed(bytesToHex6(v));
+            onneed(bytesToHex5(v));
           }
         }
       } else {
         throw Error("unexpected mode");
@@ -3122,9 +3122,9 @@
       }
       prevIndex = upper;
       prevBound = currBound;
     }
-    return fullOutput.length === 1 ? null : bytesToHex6(fullOutput.unwrap());
+    return fullOutput.length === 1 ? null : bytesToHex5(fullOutput.unwrap());
   }
   splitRange(lower, upper, upperBound, o) {
     let numElems = upper - lower;
     let buckets = 16;
@@ -3310,10 +3310,10 @@
   validateEventTimestamp: () => validateEventTimestamp,
   validateEventUrlTag: () => validateEventUrlTag,
   validateToken: () => validateToken
 });
-import { sha256 as sha2565 } from "@noble/hashes/sha256";
-import { bytesToHex as bytesToHex7 } from "@noble/hashes/utils";
+import { sha256 as sha2565 } from "@noble/hashes/sha2.js";
+import { bytesToHex as bytesToHex6 } from "@noble/hashes/utils.js";
 import { base64 as base643 } from "@scure/base";
 var _authorizationScheme = "Nostr ";
 async function getToken(loginUrl, httpMethod, sign, includeAuthorizationScheme = false, payload) {
   const event = {
@@ -3377,9 +3377,9 @@
   return methodTag.length > 0 && methodTag[1].toLowerCase() === method.toLowerCase();
 }
 function hashPayload(payload) {
   const hash = sha2565(utf8Encoder.encode(JSON.stringify(payload)));
-  return bytesToHex7(hash);
+  return bytesToHex6(hash);
 }
 function validateEventPayloadTag(event, payload) {
   const payloadTag = event.tags.find((t) => t[0] === "payload");
   if (!payloadTag) {