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 EventTemplate = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at'>
|
||||||
export type UnsignedEvent = Pick<Event, 'kind' | 'tags' | 'content' | 'created_at' | 'pubkey'>
|
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 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. */
|
/** An event whose signature has been verified. */
|
||||||
export interface VerifiedEvent extends Event {
|
export interface VerifiedEvent extends Event {
|
||||||
[verifiedSymbol]: true
|
[verifiedSymbol]: true
|
||||||
|
|
1
kinds.ts
1
kinds.ts
|
@ -35,7 +35,6 @@ export const ShortTextNote = 1
|
||||||
export const RecommendRelay = 2
|
export const RecommendRelay = 2
|
||||||
export const Contacts = 3
|
export const Contacts = 3
|
||||||
export const EncryptedDirectMessage = 4
|
export const EncryptedDirectMessage = 4
|
||||||
export const EncryptedDirectMessages = 4
|
|
||||||
export const EventDeletion = 5
|
export const EventDeletion = 5
|
||||||
export const Repost = 6
|
export const Repost = 6
|
||||||
export const Reaction = 7
|
export const Reaction = 7
|
||||||
|
|
|
@ -5,7 +5,6 @@ import {
|
||||||
naddrEncode,
|
naddrEncode,
|
||||||
nprofileEncode,
|
nprofileEncode,
|
||||||
npubEncode,
|
npubEncode,
|
||||||
nrelayEncode,
|
|
||||||
nsecEncode,
|
nsecEncode,
|
||||||
neventEncode,
|
neventEncode,
|
||||||
type AddressPointer,
|
type AddressPointer,
|
||||||
|
@ -152,12 +151,3 @@ test('decode naddr from go-nostr with different TLV ordering', () => {
|
||||||
expect(pointer.kind).toEqual(30023)
|
expect(pointer.kind).toEqual(30023)
|
||||||
expect(pointer.identifier).toEqual('banana')
|
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 { bech32 } from '@scure/base'
|
||||||
|
|
||||||
import { utf8Decoder, utf8Encoder } from './utils.ts'
|
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
|
export const Bech32MaxSize = 5000
|
||||||
|
|
||||||
|
@ -46,7 +65,6 @@ export type AddressPointer = {
|
||||||
|
|
||||||
type Prefixes = {
|
type Prefixes = {
|
||||||
nprofile: ProfilePointer
|
nprofile: ProfilePointer
|
||||||
nrelay: string
|
|
||||||
nevent: EventPointer
|
nevent: EventPointer
|
||||||
naddr: AddressPointer
|
naddr: AddressPointer
|
||||||
nsec: Uint8Array
|
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':
|
case 'nsec':
|
||||||
return { type: prefix, data }
|
return { type: prefix, data }
|
||||||
|
|
||||||
|
@ -217,13 +225,6 @@ export function naddrEncode(addr: AddressPointer): NAddr {
|
||||||
return encodeBech32('naddr', data)
|
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 {
|
function encodeTLV(tlv: TLV): Uint8Array {
|
||||||
let entries: Uint8Array[] = []
|
let entries: Uint8Array[] = []
|
||||||
|
|
||||||
|
|
3
nip49.ts
3
nip49.ts
|
@ -1,9 +1,8 @@
|
||||||
import { scrypt } from '@noble/hashes/scrypt'
|
import { scrypt } from '@noble/hashes/scrypt'
|
||||||
import { xchacha20poly1305 } from '@noble/ciphers/chacha'
|
import { xchacha20poly1305 } from '@noble/ciphers/chacha'
|
||||||
import { concatBytes, randomBytes } from '@noble/hashes/utils'
|
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 { bech32 } from '@scure/base'
|
||||||
import { Ncryptsec } from './core.ts'
|
|
||||||
|
|
||||||
export function encrypt(
|
export function encrypt(
|
||||||
sec: Uint8Array,
|
sec: Uint8Array,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"name": "nostr-tools",
|
"name": "nostr-tools",
|
||||||
"version": "2.7.2",
|
"version": "2.8.0",
|
||||||
"description": "Tools for making a Nostr client.",
|
"description": "Tools for making a Nostr client.",
|
||||||
"repository": {
|
"repository": {
|
||||||
"type": "git",
|
"type": "git",
|
||||||
|
|
Loading…
Reference in New Issue