Merge pull request #241 from alexgleason/nip19-cool-type

nip19: use template literal types
This commit is contained in:
Alex Gleason
2023-07-02 12:22:04 -05:00
committed by GitHub

View File

@@ -30,15 +30,27 @@ 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
}
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 { export function decode(nip19: string): DecodeResult {
let {prefix, words} = bech32.decode(nip19, Bech32MaxSize) let {prefix, words} = bech32.decode(nip19, Bech32MaxSize)
let data = new Uint8Array(bech32.fromWords(words)) let data = new Uint8Array(bech32.fromWords(words))
@@ -131,44 +143,46 @@ function parseTLV(data: Uint8Array): TLV {
return result return result
} }
export function nsecEncode(hex: string): string { export function nsecEncode(hex: string): `nsec1${string}` {
return encodeBytes('nsec', hex) return encodeBytes('nsec', hex)
} }
export function npubEncode(hex: string): string { export function npubEncode(hex: string): `npub1${string}` {
return encodeBytes('npub', hex) return encodeBytes('npub', hex)
} }
export function noteEncode(hex: string): string { export function noteEncode(hex: string): `note1${string}` {
return encodeBytes('note', hex) return encodeBytes('note', hex)
} }
function encodeBytes(prefix: string, hex: string): string { function encodeBech32<Prefix extends string>(prefix: Prefix, data: Uint8Array): `${Prefix}1${string}` {
let data = hexToBytes(hex)
let words = bech32.toWords(data) let words = bech32.toWords(data)
return bech32.encode(prefix, words, Bech32MaxSize) return bech32.encode(prefix, words, Bech32MaxSize) as `${Prefix}1${string}`
} }
export function nprofileEncode(profile: ProfilePointer): string { function encodeBytes<Prefix extends string>(prefix: Prefix, hex: string): `${Prefix}1${string}` {
let data = hexToBytes(hex)
return encodeBech32(prefix, data)
}
export function nprofileEncode(profile: ProfilePointer): `nprofile1${string}` {
let data = encodeTLV({ let data = encodeTLV({
0: [hexToBytes(profile.pubkey)], 0: [hexToBytes(profile.pubkey)],
1: (profile.relays || []).map(url => utf8Encoder.encode(url)) 1: (profile.relays || []).map(url => utf8Encoder.encode(url))
}) })
let words = bech32.toWords(data) return encodeBech32('nprofile', data)
return bech32.encode('nprofile', words, Bech32MaxSize)
} }
export function neventEncode(event: EventPointer): string { export function neventEncode(event: EventPointer): `nevent1${string}` {
let data = encodeTLV({ let data = encodeTLV({
0: [hexToBytes(event.id)], 0: [hexToBytes(event.id)],
1: (event.relays || []).map(url => utf8Encoder.encode(url)), 1: (event.relays || []).map(url => utf8Encoder.encode(url)),
2: event.author ? [hexToBytes(event.author)] : [] 2: event.author ? [hexToBytes(event.author)] : []
}) })
let words = bech32.toWords(data) return encodeBech32('nevent', data)
return bech32.encode('nevent', words, Bech32MaxSize)
} }
export function naddrEncode(addr: AddressPointer): string { export function naddrEncode(addr: AddressPointer): `naddr1${string}` {
let kind = new ArrayBuffer(4) let kind = new ArrayBuffer(4)
new DataView(kind).setUint32(0, addr.kind, false) new DataView(kind).setUint32(0, addr.kind, false)
@@ -178,16 +192,14 @@ export function naddrEncode(addr: AddressPointer): string {
2: [hexToBytes(addr.pubkey)], 2: [hexToBytes(addr.pubkey)],
3: [new Uint8Array(kind)] 3: [new Uint8Array(kind)]
}) })
let words = bech32.toWords(data) return encodeBech32('naddr', data)
return bech32.encode('naddr', words, Bech32MaxSize)
} }
export function nrelayEncode(url: string): string { export function nrelayEncode(url: string): `nrelay1${string}` {
let data = encodeTLV({ let data = encodeTLV({
0: [utf8Encoder.encode(url)] 0: [utf8Encoder.encode(url)]
}) })
let words = bech32.toWords(data) return encodeBech32('nrelay', data)
return bech32.encode('nrelay', words, Bech32MaxSize)
} }
function encodeTLV(tlv: TLV): Uint8Array { function encodeTLV(tlv: TLV): Uint8Array {