feat: nip46 oAuth login

This commit is contained in:
2024-02-15 11:28:09 +00:00
parent 512307f42d
commit 0d9d5a0a4c
5 changed files with 107 additions and 27 deletions

View File

@ -160,10 +160,13 @@ export function bech32ToText(str: string) {
return new TextDecoder().decode(Uint8Array.from(buf));
}
export interface NostrJson {
names: Record<string, string>;
relays?: Record<string, Array<string>>;
nip46?: Record<string, Array<string>>;
}
export async function fetchNip05Pubkey(name: string, domain: string, timeout = 2_000): Promise<string | undefined> {
interface NostrJson {
names: Record<string, string>;
}
if (!name || !domain) {
return undefined;
}
@ -182,6 +185,21 @@ export async function fetchNip05Pubkey(name: string, domain: string, timeout = 2
return undefined;
}
export async function fetchNostrAddress(name: string, domain: string, timeout = 2_000): Promise<NostrJson | undefined> {
if (!name || !domain) {
return undefined;
}
try {
const res = await fetch(`https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(name)}`, {
signal: AbortSignal.timeout(timeout),
});
return await res.json() as NostrJson;
} catch {
// ignored
}
return undefined;
}
export function removeUndefined<T>(v: Array<T | undefined>) {
return v.filter(a => a != undefined).map(a => unwrap(a));
}
@ -208,7 +226,7 @@ export function normalizeReaction(content: string) {
}
}
export class OfflineError extends Error {}
export class OfflineError extends Error { }
export function throwIfOffline() {
if (isOffline()) {

View File

@ -8,6 +8,7 @@
"module": "ESNext",
"strict": true,
"declaration": true,
"declarationMap": true,
"inlineSourceMap": true,
"outDir": "dist",
"skipLibCheck": true