nip46: fix bunker url parsing.

This commit is contained in:
fiatjaf
2024-02-12 15:32:50 -03:00
parent 566a2deea3
commit 9d78c90a79

View File

@@ -16,7 +16,7 @@ export function useFetchImplementation(fetchImplementation: any) {
_fetch = fetchImplementation _fetch = fetchImplementation
} }
export const BUNKER_REGEX = /^bunker:\/\/[0-9a-f]{64}\??[?\/\w:.=&%]*$/ export const BUNKER_REGEX = /^bunker:\/\/([0-9a-f]{64})\??([?\/\w:.=&%]*)$/
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/ const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
export type BunkerPointer = { export type BunkerPointer = {
@@ -31,11 +31,12 @@ export async function parseBunkerInput(input: string): Promise<BunkerPointer | n
let match = input.match(BUNKER_REGEX) let match = input.match(BUNKER_REGEX)
if (match) { if (match) {
try { try {
const bunkerURL = new URL(input) const pubkey = match[1]
const qs = new URLSearchParams(match[2])
return { return {
pubkey: bunkerURL.host, pubkey,
relays: bunkerURL.searchParams.getAll('relay'), relays: qs.getAll('relay'),
secret: bunkerURL.searchParams.get('secret'), secret: qs.get('secret'),
} }
} catch (_err) { } catch (_err) {
/* just move to the next case */ /* just move to the next case */