From 19ae9837a718068c61d81b4bf484617fb2cfbeec Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 2 Mar 2025 11:51:31 -0300 Subject: [PATCH] nip19: decodeNostrURI() function that doesn't throw. --- nip19.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/nip19.ts b/nip19.ts index cf04c5d..4746ffc 100644 --- a/nip19.ts +++ b/nip19.ts @@ -79,6 +79,15 @@ export type DecodeResult = { [P in keyof Prefixes]: DecodeValue

}[keyof Prefixes] +export function decodeNostrURI(nip19code: string): DecodeResult | { type: 'invalid'; data: null } { + try { + if (nip19code.startsWith('nostr:')) nip19code = nip19code.substring(6) + return decode(nip19code) + } catch (_err) { + return { type: 'invalid', data: null } + } +} + export function decode(nip19: `${Prefix}1${string}`): DecodeValue export function decode(nip19: string): DecodeResult export function decode(nip19: string): DecodeResult {