mirror of
https://github.com/nbd-wtf/nostr-tools.git
synced 2025-12-13 10:28:51 +00:00
NIP-19: Add nrelay encoding and decoding
This commit is contained in:
committed by
fiatjaf_
parent
f6f5ee8223
commit
f17ab41d72
18
nip19.ts
18
nip19.ts
@@ -82,6 +82,16 @@ export function decode(nip19: string): {
|
||||
}
|
||||
}
|
||||
|
||||
case 'nrelay': {
|
||||
let tlv = parseTLV(data)
|
||||
if (!tlv[0]?.[0]) throw new Error('missing TLV 0 for nrelay')
|
||||
|
||||
return {
|
||||
type: 'nrelay',
|
||||
data: utf8Decoder.decode(tlv[0][0])
|
||||
}
|
||||
}
|
||||
|
||||
case 'nsec':
|
||||
case 'npub':
|
||||
case 'note':
|
||||
@@ -160,6 +170,14 @@ export function naddrEncode(addr: AddressPointer): string {
|
||||
return bech32.encode('naddr', words, Bech32MaxSize)
|
||||
}
|
||||
|
||||
export function nrelayEncode(url: string): string {
|
||||
let data = encodeTLV({
|
||||
0: [utf8Encoder.encode(url)]
|
||||
})
|
||||
let words = bech32.toWords(data)
|
||||
return bech32.encode('nrelay', words, Bech32MaxSize)
|
||||
}
|
||||
|
||||
function encodeTLV(tlv: TLV): Uint8Array {
|
||||
let entries: Uint8Array[] = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user