Skip to content

Commit

Permalink
nip19/nip49: remove nrelay and move bech32 string guard methods from …
Browse files Browse the repository at this point in the history
…core to nip19.
  • Loading branch information
fiatjaf committed Sep 9, 2024
1 parent ee76d69 commit 67185ae
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 51 deletions.
19 changes: 0 additions & 19 deletions core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,8 @@ export type NostrEvent = Event
export type EventTemplate = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at'>
export type UnsignedEvent = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at' | 'pubkey'>

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
Expand Down
1 change: 0 additions & 1 deletion kinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions nip19.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
naddrEncode,
nprofileEncode,
npubEncode,
nrelayEncode,
nsecEncode,
neventEncode,
type AddressPointer,
Expand Down Expand Up @@ -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)
})
39 changes: 20 additions & 19 deletions nip19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -46,7 +65,6 @@ export type AddressPointer = {

type Prefixes = {
nprofile: ProfilePointer
nrelay: string
nevent: EventPointer
naddr: AddressPointer
nsec: Uint8Array
Expand Down Expand Up @@ -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 }

Expand Down Expand Up @@ -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[] = []

Expand Down
3 changes: 1 addition & 2 deletions nip49.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down

0 comments on commit 67185ae

Please sign in to comment.