From 7c3c061b88029f7643f471f2cf2279cc115e0719 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 5 Mar 2024 15:08:33 +0500 Subject: [PATCH] fix: update the logic for login with nsecbunker --- src/pages/login/index.tsx | 7 +++--- src/utils/nostr.ts | 47 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 3016dc9..741f661 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -1,5 +1,5 @@ import { Box, Button, TextField, Typography } from '@mui/material' -import { getPublicKey, nip05, nip19 } from 'nostr-tools' +import { getPublicKey, nip19 } from 'nostr-tools' import { useEffect, useState } from 'react' import { useDispatch } from 'react-redux' import { toast } from 'react-toastify' @@ -18,6 +18,7 @@ import { LoginMethods } from '../../store/auth/types' import { Dispatch } from '../../store/store' import styles from './style.module.scss' import { useNavigate } from 'react-router-dom' +import { queryNip05 } from '../../utils' export const Login = () => { const dispatch: Dispatch = useDispatch() @@ -116,7 +117,7 @@ export const Login = () => { } if (inputValue.includes('@')) { - const nip05Profile = await nip05.queryProfile(inputValue).catch((err) => { + const nip05Profile = await queryNip05(inputValue).catch((err) => { toast.error('An error occurred while querying nip05 profile: ' + err) return null }) @@ -144,7 +145,7 @@ export const Login = () => { return displayError('nip05 not present in metadata') } - const nip05Profile = await nip05.queryProfile(inputValue).catch((err) => { + const nip05Profile = await queryNip05(inputValue).catch((err) => { toast.error('An error occurred while querying nip05 profile: ' + err) return null }) diff --git a/src/utils/nostr.ts b/src/utils/nostr.ts index b81ccef..10e2b88 100644 --- a/src/utils/nostr.ts +++ b/src/utils/nostr.ts @@ -1,5 +1,6 @@ import { nip05, nip19, verifyEvent } from 'nostr-tools' import { SignedEvent } from '../types' +import axios from 'axios' /** * @param hexKey hex private or public key @@ -77,3 +78,49 @@ export const verifySignedEvent = (event: SignedEvent) => { ) } } + +export const queryNip05 = async ( + nip05: string +): Promise<{ + pubkey: string + relays: string[] +}> => { + const NIP05_REGEX = /^(?:([\w.+-]+)@)?([\w_-]+(\.[\w_-]+)+)$/ + const match = nip05.match(NIP05_REGEX) + if (!match) throw new Error('Invalid nip05') + + const [_, name = '_', domain] = match + const url = `https://${domain}/.well-known/nostr.json?name=${name}` + const res = await axios(url) + .then((res) => { + return res.data + }) + .catch((err) => { + console.log('err :>> ', err) + throw err + }) + + const pubkey = res.names[name] + const relays: string[] = [] + + if (pubkey) { + // check nip46 for user pubkey, if relays found for user, return those + const userRelays = res.nip46?.[pubkey] as string[] + if (userRelays && userRelays.length > 0) { + relays.push(...userRelays) + } else { + // otherwise check nip46 for root user pubkey, if relays found, return those + const root = res.names['_'] + if (root) { + const rootUserRelays = res.nip46?.[root] as string[] + if (rootUserRelays && rootUserRelays.length > 0) + relays.push(...rootUserRelays) + } + } + } + + return { + pubkey, + relays + } +}