2024-02-22 17:08:09 +07:00
|
|
|
"use strict";
|
2024-05-16 18:34:07 +07:00
|
|
|
let { validateEvent, verifyEvent, nip19 } = require("nostr-tools");
|
|
|
|
let { authorized_keys, private_keys, noscraper } = require(process.env.BOSTR_CONFIG_PATH || "./config");
|
|
|
|
|
|
|
|
authorized_keys = authorized_keys?.map(i => i.startsWith("npub") ? nip19.decode(i).data : i);
|
|
|
|
|
|
|
|
for (const key in private_keys) {
|
|
|
|
if (!key.startsWith("npub")) continue;
|
|
|
|
private_keys[nip19.decode(key).data] = private_keys[key];
|
|
|
|
|
|
|
|
delete private_keys[key];
|
|
|
|
}
|
2023-11-16 21:53:58 +07:00
|
|
|
|
2023-11-20 21:21:22 +07:00
|
|
|
module.exports = (authKey, data, ws, req) => {
|
2024-05-10 17:23:23 +07:00
|
|
|
if (!authorized_keys?.length && !Object.keys(private_keys).length && !noscraper) return; // do nothing
|
2024-02-18 12:56:00 +07:00
|
|
|
if (!validateEvent(data) || !verifyEvent(data)) {
|
2023-11-16 21:53:58 +07:00
|
|
|
ws.send(JSON.stringify(["NOTICE", "error: invalid challenge response."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:54:23 +07:00
|
|
|
let pubkeyInConfig = authorized_keys?.includes(data.pubkey) || data.pubkey in private_keys;
|
|
|
|
|
|
|
|
if (authorized_keys?.length && !pubkeyInConfig) {
|
2023-11-16 21:53:58 +07:00
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "unauthorized."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.kind != 22242) {
|
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "not kind 22242."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-28 21:20:25 +07:00
|
|
|
const tags = Object.fromEntries(data.tags);
|
2024-02-17 19:35:33 +07:00
|
|
|
|
2024-04-28 21:20:25 +07:00
|
|
|
if (!tags.relay?.includes(req.headers.host)) {
|
2023-11-16 21:53:58 +07:00
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "unmatched relay url."]));
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
2024-04-28 21:20:25 +07:00
|
|
|
if (tags.challenge !== authKey) {
|
2023-11-16 21:53:58 +07:00
|
|
|
ws.send(JSON.stringify(["OK", data.id, false, "unmatched challenge string."]));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-11-20 21:21:22 +07:00
|
|
|
ws.send(JSON.stringify(["OK", data.id, true, `Hello ${data.pubkey}`]));
|
2023-11-16 21:53:58 +07:00
|
|
|
return true;
|
|
|
|
}
|