nip19: use a DRY type

This commit is contained in:
Alex Gleason 2023-07-01 22:42:00 -05:00
parent c9bc702d90
commit 70b025b8da
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 19 additions and 15 deletions

View File

@ -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<Prefix extends keyof Prefixes> = {
type: Prefix
data: Prefixes[Prefix]
}
export type DecodeResult = {
[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 {
let {prefix, words} = bech32.decode(nip19, Bech32MaxSize)