code: renaming & reworking as per nostr-tools

Signed-off-by: Yonle <yonle@lecturify.net>
This commit is contained in:
Yonle
2024-02-17 19:35:33 +07:00
parent 8ba52b945e
commit 7fae06bc8c
3 changed files with 8 additions and 15 deletions

10
auth.js
View File

@@ -1,17 +1,12 @@
const { validateEvent, verifySignature } = require("nostr-tools"); const { verifyEvent } = require("nostr-tools");
const { authorized_keys, private_keys } = require("./config"); const { authorized_keys, private_keys } = require("./config");
module.exports = (authKey, data, ws, req) => { module.exports = (authKey, data, ws, req) => {
if (!validateEvent(data)) { if (!verifyEvent(data)) {
ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."])); ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."]));
return false; 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])) { if (!authorized_keys?.includes(data.pubkey) && !(private_keys && private_keys[data.pubkey])) {
ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."])); ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."]));
return false; return false;
@@ -23,6 +18,7 @@ module.exports = (authKey, data, ws, req) => {
} }
const tags = new Map(data.tags); const tags = new Map(data.tags);
if (!tags.get("relay").includes(req.headers.host)) { if (!tags.get("relay").includes(req.headers.host)) {
ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."])); ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."]));
return false; return false;

View File

@@ -1,7 +1,7 @@
"use strict"; "use strict";
const { version } = require("./package.json"); const { version } = require("./package.json");
const WebSocket = require("ws"); 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 auth = require("./auth.js");
const nip42 = require("./nip42.js"); const nip42 = require("./nip42.js");
@@ -59,7 +59,7 @@ module.exports = (ws, req, onClose) => {
switch (data[0]) { switch (data[0]) {
case "EVENT": case "EVENT":
if (!authorized) return; 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 (data[1].kind == 22242) return ws.send(JSON.stringify(["OK", data[1]?.id, false, "rejected: kind 22242"]));
if ( if (

View File

@@ -1,11 +1,10 @@
const { getEventHash, getSignature, nip19 } = require("nostr-tools"); const { finalizeEvent, nip19 } = require("nostr-tools");
module.exports = (relay, pubkey, privkey, challenge) => { module.exports = (relay, pubkey, privkey, challenge) => {
if (!privkey) return; if (!privkey) return;
if (privkey.startsWith("nsec")) privkey = nip19.decode(privkey).data; if (privkey.startsWith("nsec")) privkey = nip19.decode(privkey).data;
let signed_challenge = { let signed_challenge = finalizeEvent({
pubkey,
created_at: Math.floor(Date.now() / 1000), created_at: Math.floor(Date.now() / 1000),
kind: 22242, kind: 22242,
tags: [ tags: [
@@ -13,9 +12,7 @@ module.exports = (relay, pubkey, privkey, challenge) => {
["challenge", challenge] ["challenge", challenge]
], ],
content: "" content: ""
} }, privkey);
signed_challenge.id = getEventHash(signed_challenge);
signed_challenge.sig = getSignature(signed_challenge, privkey);
relay.send(JSON.stringify(["AUTH", signed_challenge])); relay.send(JSON.stringify(["AUTH", signed_challenge]));
} }