Skip to content

Commit

Permalink
Check nip5 when you're logged in.
Browse files Browse the repository at this point in the history
  • Loading branch information
chmac committed Aug 11, 2024
1 parent 3ce87b2 commit 2540a57
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ export const HITCHMAPS_AUTHOR_PUBLIC_KEY =
"53055ee011e96a00a705b38253b9cbc6614ccbd37df4dad42ec69bbe608c4209" as const;

export const TRUSTROOTS_NPUB_PUT_URL = "https://www.trustroots.org/api/users";
export const TRUSTROOTS_NIP5_URL =
"https://www.trustroots.org/.well-known/nostr.json";
32 changes: 32 additions & 0 deletions src/nostr/nip5.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { TRUSTROOTS_NIP5_URL } from "../constants";
import { getTrustrootsUsernameFromLocation } from "../router";
import { alert } from "../utils";
import { getPublicKey } from "./keys";

export const validateNip5 = async () => {
try {
const username = getTrustrootsUsernameFromLocation();
if (username.length < 3) {
alert(
`Sorry, you need to click to this page from trustroots.org. Without doing that, this site won't work properly. Please go to www.trustroots.org and click the Notes link to come back here. #wBjsEe`,
`You need to click from trustroots`
);
return;
}

const result = await fetch(`${TRUSTROOTS_NIP5_URL}?name=${username}`);
const nip5 = (await result.json()) as {
names: { [username: string]: string };
};
const nip5PublicKey = nip5.names?.[username];
const localPublicKey = await getPublicKey();
if (nip5PublicKey !== localPublicKey) {
alert(
`Your key doesn't match trustroots. Posting to the map will not work. Please sign out and sign in again with the nsec key that matches your trustroots npub key. #H9bEe2`,
"Fatal error"
);
}
} catch (error) {
alert(error, `Unexpected error`);
}
};
3 changes: 3 additions & 0 deletions src/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
import { getDefaultRelays } from "./nostr/relays";
import { startUserOnboarding } from "./onboarding";
import { startWelcomeSequence } from "./welcome";
import { validateNip5 } from "./nostr/nip5";

export const startup = async () => {
const isLoggedIn = await hasPrivateKey();
Expand All @@ -31,6 +32,8 @@ export const startup = async () => {
const loggedOut = L.DomUtil.get("loggedOut")!;

if (isLoggedIn) {
validateNip5();

L.DomUtil.addClass(loggedIn, "show");
L.DomUtil.addClass(loggedOut, "hide");

Expand Down
3 changes: 2 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export const confirmYesNo = async (text: string) => {
return false;
};

export const alert = async (text: string) => {
export const alert = async (text: string, title?: string) => {
await Swal.fire({
title,
text,
});
};
Expand Down

0 comments on commit 2540a57

Please sign in to comment.