nip19: use a DRY type

This commit is contained in:
Alex Gleason
2023-07-01 22:42:00 -05:00
parent c9bc702d90
commit 70b025b8da

View File

@@ -30,22 +30,26 @@ export type AddressPointer = {
relays?: string[] relays?: string[]
} }
export type DecodeResult = type Prefixes = {
| {type: 'nprofile'; data: ProfilePointer} nprofile: ProfilePointer
| {type: 'nrelay'; data: string} nrelay: string
| {type: 'nevent'; data: EventPointer} nevent: EventPointer
| {type: 'naddr'; data: AddressPointer} naddr: AddressPointer
| {type: 'nsec'; data: string} nsec: string
| {type: 'npub'; data: string} npub: string
| {type: 'note'; data: string} note: string
}
export function decode(nip19: `nprofile1${string}`): {type: 'nprofile'; data: ProfilePointer} type DecodeValue<Prefix extends keyof Prefixes> = {
export function decode(nip19: `nrelay1${string}`): {type: 'nrelay'; data: string} type: Prefix
export function decode(nip19: `nevent1${string}`): {type: 'nevent'; data: EventPointer} data: Prefixes[Prefix]
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 type DecodeResult = {
export function decode(nip19: `note1${string}`): {type: 'note'; data: string} [P in keyof Prefixes]: DecodeValue<P>
}[keyof Prefixes]
export function decode<Prefix extends keyof Prefixes>(nip19: `${Prefix}1${string}`): DecodeValue<Prefix>
export function decode(nip19: string): DecodeResult export function decode(nip19: string): DecodeResult
export function decode(nip19: string): DecodeResult { export function decode(nip19: string): DecodeResult {
let {prefix, words} = bech32.decode(nip19, Bech32MaxSize) let {prefix, words} = bech32.decode(nip19, Bech32MaxSize)