diff --git a/auth.js b/auth.js index 13a90f7..f17cdba 100644 --- a/auth.js +++ b/auth.js @@ -1,17 +1,12 @@ -const { validateEvent, verifySignature } = require("nostr-tools"); +const { verifyEvent } = require("nostr-tools"); const { authorized_keys, private_keys } = require("./config"); module.exports = (authKey, data, ws, req) => { - if (!validateEvent(data)) { + if (!verifyEvent(data)) { ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."])); return false; } - if (!verifySignature(data)) { - ws.send(JSON.stringify(["OK", data.id, false, "signature verification failed."])); - return false; - } - if (!authorized_keys?.includes(data.pubkey) && !(private_keys && private_keys[data.pubkey])) { ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."])); return false; @@ -23,6 +18,7 @@ module.exports = (authKey, data, ws, req) => { } const tags = new Map(data.tags); + if (!tags.get("relay").includes(req.headers.host)) { ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."])); return false; diff --git a/bouncer.js b/bouncer.js index 7d35d58..2a3faf2 100644 --- a/bouncer.js +++ b/bouncer.js @@ -1,7 +1,7 @@ "use strict"; const { version } = require("./package.json"); const WebSocket = require("ws"); -const { verifySignature, validateEvent, nip19, matchFilters, mergeFilters, getFilterLimit } = require("nostr-tools"); +const { verifyEvent, nip19, matchFilters, mergeFilters, getFilterLimit } = require("nostr-tools"); const auth = require("./auth.js"); const nip42 = require("./nip42.js"); @@ -59,7 +59,7 @@ module.exports = (ws, req, onClose) => { switch (data[0]) { case "EVENT": if (!authorized) return; - if (!validateEvent(data[1]) || !verifySignature(data[1])) return ws.send(JSON.stringify(["NOTICE", "error: invalid event"])); + if (!verifyEvent(data[1])) return ws.send(JSON.stringify(["NOTICE", "error: invalid event"])); if (data[1].kind == 22242) return ws.send(JSON.stringify(["OK", data[1]?.id, false, "rejected: kind 22242"])); if ( diff --git a/nip42.js b/nip42.js index 608246f..8377756 100644 --- a/nip42.js +++ b/nip42.js @@ -1,11 +1,10 @@ -const { getEventHash, getSignature, nip19 } = require("nostr-tools"); +const { finalizeEvent, nip19 } = require("nostr-tools"); module.exports = (relay, pubkey, privkey, challenge) => { if (!privkey) return; if (privkey.startsWith("nsec")) privkey = nip19.decode(privkey).data; - let signed_challenge = { - pubkey, + let signed_challenge = finalizeEvent({ created_at: Math.floor(Date.now() / 1000), kind: 22242, tags: [ @@ -13,9 +12,7 @@ module.exports = (relay, pubkey, privkey, challenge) => { ["challenge", challenge] ], content: "" - } + }, privkey); - signed_challenge.id = getEventHash(signed_challenge); - signed_challenge.sig = getSignature(signed_challenge, privkey); relay.send(JSON.stringify(["AUTH", signed_challenge])); }