Skip to content

Commit

Permalink
Fix underscore name for a root domain
Browse files Browse the repository at this point in the history
  • Loading branch information
dcadenas committed Jul 16, 2024
1 parent 9339e86 commit 72b7f4b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 8 deletions.
13 changes: 12 additions & 1 deletion src/middlewares/extractValidatedName.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions src/nameRecord.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 -.`);
}
Expand All @@ -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,
Expand Down
30 changes: 30 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down

0 comments on commit 72b7f4b

Please sign in to comment.