@nostr-dev-kit/ndk
Version:
NDK - Nostr Development Kit. Includes AI Guardrails to catch common mistakes during development.
37 lines (35 loc) • 1.02 kB
JavaScript
;
// src/workers/sig-verification.ts
var import_secp256k1 = require("@noble/curves/secp256k1");
var import_sha256 = require("@noble/hashes/sha256");
globalThis.onmessage = (msg) => {
const { serialized, id, sig, pubkey } = msg.data;
queueMicrotask(() => {
const eventHash = (0, import_sha256.sha256)(new TextEncoder().encode(serialized));
const idHash = hexToBytes(id);
if (!compareTypedArrays(eventHash, idHash)) {
postMessage([id, false]);
return;
}
const result = import_secp256k1.schnorr.verify(sig, idHash, pubkey);
postMessage([id, result]);
});
};
function hexToBytes(hex) {
const bytes = new Uint8Array(hex.length / 2);
for (let i = 0; i < bytes.length; i++) {
bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
}
return bytes;
}
function compareTypedArrays(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (let i = 0; i < arr1.length; i++) {
if (arr1[i] !== arr2[i]) {
return false;
}
}
return true;
}