From 45b25c5bf528331ff42cc964259a78a47589c7a8 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 9 Sep 2024 14:20:12 -0300 Subject: [PATCH] nip19/nip49: remove nrelay and move bech32 string guard methods from core to nip19. --- core.ts | 19 ------------------- kinds.ts | 1 - nip19.test.ts | 10 ---------- nip19.ts | 39 ++++++++++++++++++++------------------- nip49.ts | 3 +-- package.json | 2 +- 6 files changed, 22 insertions(+), 52 deletions(-) diff --git a/core.ts b/core.ts index 82b26d72..5b5254b4 100644 --- a/core.ts +++ b/core.ts @@ -23,27 +23,8 @@ export type NostrEvent = Event export type EventTemplate = Pick export type UnsignedEvent = Pick -export type NProfile = `nprofile1${string}` -export type NRelay = `nrelay1${string}` -export type NEvent = `nevent1${string}` -export type NAddr = `naddr1${string}` -export type NSec = `nsec1${string}` -export type NPub = `npub1${string}` -export type Note = `note1${string}` -export type Ncryptsec = `ncryptsec1${string}` export type Nip05 = `${string}@${string}` -export const NostrTypeGuard = { - isNProfile: (value?: string | null): value is NProfile => /^nprofile1[a-z\d]+$/.test(value || ''), - isNRelay: (value?: string | null): value is NRelay => /^nrelay1[a-z\d]+$/.test(value || ''), - isNEvent: (value?: string | null): value is NEvent => /^nevent1[a-z\d]+$/.test(value || ''), - isNAddr: (value?: string | null): value is NAddr => /^naddr1[a-z\d]+$/.test(value || ''), - isNSec: (value?: string | null): value is NSec => /^nsec1[a-z\d]{58}$/.test(value || ''), - isNPub: (value?: string | null): value is NPub => /^npub1[a-z\d]{58}$/.test(value || ''), - isNote: (value?: string | null): value is Note => /^note1[a-z\d]+$/.test(value || ''), - isNcryptsec: (value?: string | null): value is Note => /^ncryptsec1[a-z\d]+$/.test(value || ''), -} - /** An event whose signature has been verified. */ export interface VerifiedEvent extends Event { [verifiedSymbol]: true diff --git a/kinds.ts b/kinds.ts index b6de6a82..2e545d67 100644 --- a/kinds.ts +++ b/kinds.ts @@ -35,7 +35,6 @@ export const ShortTextNote = 1 export const RecommendRelay = 2 export const Contacts = 3 export const EncryptedDirectMessage = 4 -export const EncryptedDirectMessages = 4 export const EventDeletion = 5 export const Repost = 6 export const Reaction = 7 diff --git a/nip19.test.ts b/nip19.test.ts index ab7da0d8..9594c31d 100644 --- a/nip19.test.ts +++ b/nip19.test.ts @@ -5,7 +5,6 @@ import { naddrEncode, nprofileEncode, npubEncode, - nrelayEncode, nsecEncode, neventEncode, type AddressPointer, @@ -152,12 +151,3 @@ test('decode naddr from go-nostr with different TLV ordering', () => { expect(pointer.kind).toEqual(30023) expect(pointer.identifier).toEqual('banana') }) - -test('encode and decode nrelay', () => { - let url = 'wss://relay.nostr.example' - let nrelay = nrelayEncode(url) - expect(nrelay).toMatch(/nrelay1\w+/) - let { type, data } = decode(nrelay) - expect(type).toEqual('nrelay') - expect(data).toEqual(url) -}) diff --git a/nip19.ts b/nip19.ts index ddcfef73..e4ad8989 100644 --- a/nip19.ts +++ b/nip19.ts @@ -2,7 +2,26 @@ import { bytesToHex, concatBytes, hexToBytes } from '@noble/hashes/utils' import { bech32 } from '@scure/base' import { utf8Decoder, utf8Encoder } from './utils.ts' -import { NAddr, NEvent, Note, NProfile, NPub, NRelay, NSec } from './core.ts' + +export type NProfile = `nprofile1${string}` +export type NRelay = `nrelay1${string}` +export type NEvent = `nevent1${string}` +export type NAddr = `naddr1${string}` +export type NSec = `nsec1${string}` +export type NPub = `npub1${string}` +export type Note = `note1${string}` +export type Ncryptsec = `ncryptsec1${string}` + +export const NostrTypeGuard = { + isNProfile: (value?: string | null): value is NProfile => /^nprofile1[a-z\d]+$/.test(value || ''), + isNRelay: (value?: string | null): value is NRelay => /^nrelay1[a-z\d]+$/.test(value || ''), + isNEvent: (value?: string | null): value is NEvent => /^nevent1[a-z\d]+$/.test(value || ''), + isNAddr: (value?: string | null): value is NAddr => /^naddr1[a-z\d]+$/.test(value || ''), + isNSec: (value?: string | null): value is NSec => /^nsec1[a-z\d]{58}$/.test(value || ''), + isNPub: (value?: string | null): value is NPub => /^npub1[a-z\d]{58}$/.test(value || ''), + isNote: (value?: string | null): value is Note => /^note1[a-z\d]+$/.test(value || ''), + isNcryptsec: (value?: string | null): value is Note => /^ncryptsec1[a-z\d]+$/.test(value || ''), +} export const Bech32MaxSize = 5000 @@ -46,7 +65,6 @@ export type AddressPointer = { type Prefixes = { nprofile: ProfilePointer - nrelay: string nevent: EventPointer naddr: AddressPointer nsec: Uint8Array @@ -120,16 +138,6 @@ export function decode(nip19: string): DecodeResult { } } - 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': return { type: prefix, data } @@ -217,13 +225,6 @@ export function naddrEncode(addr: AddressPointer): NAddr { return encodeBech32('naddr', data) } -export function nrelayEncode(url: string): NRelay { - let data = encodeTLV({ - 0: [utf8Encoder.encode(url)], - }) - return encodeBech32('nrelay', data) -} - function encodeTLV(tlv: TLV): Uint8Array { let entries: Uint8Array[] = [] diff --git a/nip49.ts b/nip49.ts index 203080b5..0021163b 100644 --- a/nip49.ts +++ b/nip49.ts @@ -1,9 +1,8 @@ import { scrypt } from '@noble/hashes/scrypt' import { xchacha20poly1305 } from '@noble/ciphers/chacha' import { concatBytes, randomBytes } from '@noble/hashes/utils' -import { Bech32MaxSize, encodeBytes } from './nip19.ts' +import { Bech32MaxSize, Ncryptsec, encodeBytes } from './nip19.ts' import { bech32 } from '@scure/base' -import { Ncryptsec } from './core.ts' export function encrypt( sec: Uint8Array, diff --git a/package.json b/package.json index 1f2519a8..2b865e7b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "nostr-tools", - "version": "2.7.2", + "version": "2.8.0", "description": "Tools for making a Nostr client.", "repository": { "type": "git",