fix: update the logic for login with nsecbunker
All checks were successful
Release / build_and_release (push) Successful in 49s

This commit is contained in:
Sabir Hassan 2024-03-05 15:08:33 +05:00
parent f670bd57f5
commit 7c3c061b88
2 changed files with 51 additions and 3 deletions

View File

@ -1,5 +1,5 @@
import { Box, Button, TextField, Typography } from '@mui/material' 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 { useEffect, useState } from 'react'
import { useDispatch } from 'react-redux' import { useDispatch } from 'react-redux'
import { toast } from 'react-toastify' import { toast } from 'react-toastify'
@ -18,6 +18,7 @@ import { LoginMethods } from '../../store/auth/types'
import { Dispatch } from '../../store/store' import { Dispatch } from '../../store/store'
import styles from './style.module.scss' import styles from './style.module.scss'
import { useNavigate } from 'react-router-dom' import { useNavigate } from 'react-router-dom'
import { queryNip05 } from '../../utils'
export const Login = () => { export const Login = () => {
const dispatch: Dispatch = useDispatch() const dispatch: Dispatch = useDispatch()
@ -116,7 +117,7 @@ export const Login = () => {
} }
if (inputValue.includes('@')) { 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) toast.error('An error occurred while querying nip05 profile: ' + err)
return null return null
}) })
@ -144,7 +145,7 @@ export const Login = () => {
return displayError('nip05 not present in metadata') 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) toast.error('An error occurred while querying nip05 profile: ' + err)
return null return null
}) })

View File

@ -1,5 +1,6 @@
import { nip05, nip19, verifyEvent } from 'nostr-tools' import { nip05, nip19, verifyEvent } from 'nostr-tools'
import { SignedEvent } from '../types' import { SignedEvent } from '../types'
import axios from 'axios'
/** /**
* @param hexKey hex private or public key * @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
}
}