fix nip19: relays TLV items are optional.

This commit is contained in:
fiatjaf
2023-02-26 07:44:00 -03:00
parent 9530849f0a
commit 27749d91b8
3 changed files with 16 additions and 4 deletions

View File

@@ -56,3 +56,15 @@ test('encode and decode naddr', () => {
expect(data.kind).toEqual(30023) expect(data.kind).toEqual(30023)
expect(data.identifier).toEqual('banana') expect(data.identifier).toEqual('banana')
}) })
test('encode and decode naddr from habla.news', () => {
let {type, data} = nip19.decode(
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5'
)
expect(type).toEqual('naddr')
expect(data.pubkey).toEqual(
'7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194'
)
expect(data.kind).toEqual(30023)
expect(data.identifier).toEqual('references')
})

View File

@@ -39,7 +39,7 @@ export function decode(nip19: string): {
type: 'nprofile', type: 'nprofile',
data: { data: {
pubkey: secp256k1.utils.bytesToHex(tlv[0][0]), pubkey: secp256k1.utils.bytesToHex(tlv[0][0]),
relays: tlv[1].map(d => utf8Decoder.decode(d)) relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
} }
} }
} }
@@ -52,7 +52,7 @@ export function decode(nip19: string): {
type: 'nevent', type: 'nevent',
data: { data: {
id: secp256k1.utils.bytesToHex(tlv[0][0]), id: secp256k1.utils.bytesToHex(tlv[0][0]),
relays: tlv[1].map(d => utf8Decoder.decode(d)) relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
} }
} }
} }
@@ -71,7 +71,7 @@ export function decode(nip19: string): {
identifier: utf8Decoder.decode(tlv[0][0]), identifier: utf8Decoder.decode(tlv[0][0]),
pubkey: secp256k1.utils.bytesToHex(tlv[2][0]), pubkey: secp256k1.utils.bytesToHex(tlv[2][0]),
kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16), kind: parseInt(secp256k1.utils.bytesToHex(tlv[3][0]), 16),
relays: tlv[1].map(d => utf8Decoder.decode(d)) relays: tlv[1] ? tlv[1].map(d => utf8Decoder.decode(d)) : []
} }
} }
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "nostr-tools", "name": "nostr-tools",
"version": "1.6.0", "version": "1.6.1",
"description": "Tools for making a Nostr client.", "description": "Tools for making a Nostr client.",
"repository": { "repository": {
"type": "git", "type": "git",