From 72b7f4b9790bfb29b0f57b6a2617595f9e16159d Mon Sep 17 00:00:00 2001 From: Daniel Cadenas Date: Tue, 16 Jul 2024 18:28:16 -0300 Subject: [PATCH] Fix underscore name for a root domain --- src/middlewares/extractValidatedName.js | 13 ++++++++++- src/nameRecord.js | 18 +++++++++------ test/app.test.js | 30 +++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/src/middlewares/extractValidatedName.js b/src/middlewares/extractValidatedName.js index 7e2311c..b95aafe 100644 --- a/src/middlewares/extractValidatedName.js +++ b/src/middlewares/extractValidatedName.js @@ -20,7 +20,18 @@ function extractName(req) { let name = nameFromQueryOrBody; if (nameFromQueryOrBody === "_") { - name = validateAndReturnSubdomain(nonRootSubdomains); + if (!nonRootSubdomains) { + throw new AppError( + 422, + "The _ format requires a corresponding subdomain as the NIP-05 name." + ); + } + + if (nonRootSubdomains === config.rootDomain) { + name = "_"; + } else { + name = nonRootSubdomains; + } } return validateName(name); diff --git a/src/nameRecord.js b/src/nameRecord.js index d5e38fa..0596305 100644 --- a/src/nameRecord.js +++ b/src/nameRecord.js @@ -29,13 +29,6 @@ export function validateName(name) { throw new AppError(422, "Name is required."); } - if (name.length < 3) { - throw new AppError( - 422, - `Name '${name}' should have more than 3 characters.` - ); - } - if (name.startsWith("-")) { throw new AppError(422, `Name '${name}' should not start with a hyphen -.`); } @@ -44,6 +37,17 @@ export function validateName(name) { throw new AppError(422, `Name '${name}' should not start with a hyphen -.`); } + if (name === "_") { + return name; + } + + if (name.length < 3) { + throw new AppError( + 422, + `Name '${name}' should have more than 3 characters.` + ); + } + if (name.includes("_")) { throw new AppError( 422, diff --git a/test/app.test.js b/test/app.test.js index 6b82545..2b1eb3f 100644 --- a/test/app.test.js +++ b/test/app.test.js @@ -302,6 +302,36 @@ describe("Nostr NIP 05 API tests", () => { }); }); + it.only("should store and retrieve Nostr NIP 05 data through an empty subdomain", async () => { + const userData = createUserPayload({ name: "_" }); + + await request(app) + .post("/api/names") + .set("Host", "nos.social") + .set("Authorization", `Nostr ${nip98PostAuthToken}`) + .send(userData) + .expect(200); + + const getResponse = await request(app) + .get("/.well-known/nostr.json") + .set("Host", "nos.social") + .query({ name: "_" }) + .expect(200); + + const jsonResponse = JSON.parse(getResponse.text); + + expect(jsonResponse).toEqual({ + names: { _: config.servicePubkey }, + relays: { + [config.servicePubkey]: [ + "wss://relay1.com", + "wss://relay2.com", + "wss://relay.nos.social", + ], + }, + }); + }); + it("should not use components of the root domain as a subdomain", async () => { const userData = createUserPayload({ name: "nos" });