nip05.queryProfile() and test.

This commit is contained in:
fiatjaf
2022-12-20 18:36:49 -03:00
parent 482c5affd4
commit 3f1025f551
4 changed files with 43 additions and 5 deletions

View File

@@ -1,3 +1,5 @@
import {ProfilePointer} from './nip19'
var _fetch = fetch
export function useFetchImplementation(fetchImplementation: any) {
@@ -19,13 +21,28 @@ export async function searchDomain(
}
}
export async function queryName(fullname: string): Promise<string> {
export async function queryProfile(
fullname: string
): Promise<ProfilePointer | null> {
let [name, domain] = fullname.split('@')
if (!domain) throw new Error('invalid identifier, must contain an @')
if (!domain) {
// if there is no @, it is because it is just a domain, so assume the name is "_"
domain = name
name = '_'
}
let res = await (
await _fetch(`https://${domain}/.well-known/nostr.json?name=${name}`)
).json()
return res.names && res.names[name]
if (!res?.names?.[name]) return null
let pubkey = res.names[name] as string
let relays = (res.relays?.[pubkey] || []) as string[] // nip35
return {
pubkey,
relays
}
}