From 52079f6e755d9434287b8a9c0f023b3991c4e746 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sat, 26 Apr 2025 09:00:23 -0300 Subject: [PATCH] saner nip19 types. @staab should be happy now. --- nip19.ts | 50 +++++++++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/nip19.ts b/nip19.ts index 4746ffc..28aa7ea 100644 --- a/nip19.ts +++ b/nip19.ts @@ -61,25 +61,7 @@ export type AddressPointer = { relays?: string[] } -type Prefixes = { - nprofile: ProfilePointer - nevent: EventPointer - naddr: AddressPointer - nsec: Uint8Array - npub: string - note: string -} - -type DecodeValue = { - type: Prefix - data: Prefixes[Prefix] -} - -export type DecodeResult = { - [P in keyof Prefixes]: DecodeValue

-}[keyof Prefixes] - -export function decodeNostrURI(nip19code: string): DecodeResult | { type: 'invalid'; data: null } { +export function decodeNostrURI(nip19code: string): ReturnType | { type: 'invalid'; data: null } { try { if (nip19code.startsWith('nostr:')) nip19code = nip19code.substring(6) return decode(nip19code) @@ -88,10 +70,32 @@ export function decodeNostrURI(nip19code: string): DecodeResult | { type: 'inval } } -export function decode(nip19: `${Prefix}1${string}`): DecodeValue -export function decode(nip19: string): DecodeResult -export function decode(nip19: string): DecodeResult { - let { prefix, words } = bech32.decode(nip19, Bech32MaxSize) +export function decode(code: string): + | { + type: 'nevent' + data: EventPointer + } + | { + type: 'nprofile' + data: ProfilePointer + } + | { + type: 'naddr' + data: AddressPointer + } + | { + type: 'npub' + data: string + } + | { + type: 'nsec' + data: Uint8Array + } + | { + type: 'note' + data: string + } { + let { prefix, words } = bech32.decode(code, Bech32MaxSize) let data = new Uint8Array(bech32.fromWords(words)) switch (prefix) {