mirror of
https://github.com/Yonle/bostr.git
synced 2025-03-18 05:42:03 +01:00
code: renaming & reworking as per nostr-tools
Signed-off-by: Yonle <yonle@lecturify.net>
This commit is contained in:
parent
8ba52b945e
commit
7fae06bc8c
10
auth.js
10
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;
|
||||
|
@ -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 (
|
||||
|
9
nip42.js
9
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]));
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user