From 70b025b8da498f7e8499ca9a8ad0287686b5ab38 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 1 Jul 2023 22:42:00 -0500 Subject: [PATCH] nip19: use a DRY type --- nip19.ts | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/nip19.ts b/nip19.ts index 0970205..8f402f4 100644 --- a/nip19.ts +++ b/nip19.ts @@ -30,22 +30,26 @@ export type AddressPointer = { relays?: string[] } -export type DecodeResult = - | {type: 'nprofile'; data: ProfilePointer} - | {type: 'nrelay'; data: string} - | {type: 'nevent'; data: EventPointer} - | {type: 'naddr'; data: AddressPointer} - | {type: 'nsec'; data: string} - | {type: 'npub'; data: string} - | {type: 'note'; data: string} +type Prefixes = { + nprofile: ProfilePointer + nrelay: string + nevent: EventPointer + naddr: AddressPointer + nsec: string + npub: string + note: string +} -export function decode(nip19: `nprofile1${string}`): {type: 'nprofile'; data: ProfilePointer} -export function decode(nip19: `nrelay1${string}`): {type: 'nrelay'; data: string} -export function decode(nip19: `nevent1${string}`): {type: 'nevent'; data: EventPointer} -export function decode(nip19: `naddr1${string}`): {type: 'naddr'; data: AddressPointer} -export function decode(nip19: `nsec1${string}`): {type: 'nsec'; data: string} -export function decode(nip19: `npub1${string}`): {type: 'npub'; data: string} -export function decode(nip19: `note1${string}`): {type: 'note'; data: string} +type DecodeValue = { + type: Prefix + data: Prefixes[Prefix] +} + +export type DecodeResult = { + [P in keyof Prefixes]: DecodeValue

+}[keyof Prefixes] + +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)