nip19/nip49: remove nrelay and move bech32 string guard methods from core to nip19.
This commit is contained in:
parent
ee76d69b4b
commit
45b25c5bf5
19
core.ts
19
core.ts
|
@ -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
|
||||
|
|
1
kinds.ts
1
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
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
|
|
39
nip19.ts
39
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[] = []
|
||||
|
||||
|
|
3
nip49.ts
3
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,
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue