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
commit 75fc836cf6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 38 additions and 26 deletions

View File

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