From 27749d91b890ce45214bcf6771897c613e73f856 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Sun, 26 Feb 2023 07:44:00 -0300 Subject: [PATCH] fix nip19: relays TLV items are optional. --- nip19.test.js | 12 ++++++++++++ nip19.ts | 6 +++--- package.json | 2 +- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/nip19.test.js b/nip19.test.js index 1e8f341..8d25172 100644 --- a/nip19.test.js +++ b/nip19.test.js @@ -56,3 +56,15 @@ test('encode and decode naddr', () => { expect(data.kind).toEqual(30023) 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') +}) diff --git a/nip19.ts b/nip19.ts index 7821991..66a2ca6 100644 --- a/nip19.ts +++ b/nip19.ts @@ -39,7 +39,7 @@ export function decode(nip19: string): { type: 'nprofile', data: { 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', data: { 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]), pubkey: secp256k1.utils.bytesToHex(tlv[2][0]), 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)) : [] } } } diff --git a/package.json b/package.json index 6d1677b..12d34c2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "nostr-tools", - "version": "1.6.0", + "version": "1.6.1", "description": "Tools for making a Nostr client.", "repository": { "type": "git",