diff --git a/src/constants.ts b/src/constants.ts index d8d0556..3c32857 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -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"; diff --git a/src/nostr/nip5.ts b/src/nostr/nip5.ts new file mode 100644 index 0000000..e40bb78 --- /dev/null +++ b/src/nostr/nip5.ts @@ -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`); + } +}; diff --git a/src/startup.ts b/src/startup.ts index 024c85d..e5d8d5c 100644 --- a/src/startup.ts +++ b/src/startup.ts @@ -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(); @@ -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"); diff --git a/src/utils.ts b/src/utils.ts index f80c434..fb98a7f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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, }); };