Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion nip17.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { decode } from './nip19.ts'
import { wrapEvent, wrapManyEvents, unwrapEvent } from './nip17.ts'
import { hexToBytes } from '@noble/hashes/utils'

const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data as Uint8Array
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data

const sk1 = hexToBytes('f09ac9b695d0a4c6daa418fe95b977eea20f54d9545592bc36a4f9e14f3eb840')
const sk2 = hexToBytes('5393a825e5892d8e18d4a5ea61ced105e8bb2a106f42876be3a40522e0b13747')
Expand Down
27 changes: 12 additions & 15 deletions nip19.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import { test, expect, describe } from 'bun:test'
import { generateSecretKey, getPublicKey } from './pure.ts'
import { describe, expect, test } from 'bun:test'
import {
decode,
naddrEncode,
nprofileEncode,
npubEncode,
nsecEncode,
neventEncode,
type AddressPointer,
type ProfilePointer,
EventPointer,
NostrTypeGuard,
nprofileEncode,
npubEncode,
nsecEncode
} from './nip19.ts'
import { generateSecretKey, getPublicKey } from './pure.ts'

test('encode and decode nsec', () => {
let sk = generateSecretKey()
Expand All @@ -38,7 +35,7 @@ test('encode and decode nprofile', () => {
expect(nprofile).toMatch(/nprofile1\w+/)
let { type, data } = decode(nprofile)
expect(type).toEqual('nprofile')
const pointer = data as ProfilePointer
const pointer = data
expect(pointer.pubkey).toEqual(pk)
expect(pointer.relays).toContain(relays[0])
expect(pointer.relays).toContain(relays[1])
Expand Down Expand Up @@ -67,7 +64,7 @@ test('encode and decode naddr', () => {
expect(naddr).toMatch(/naddr1\w+/)
let { type, data } = decode(naddr)
expect(type).toEqual('naddr')
const pointer = data as AddressPointer
const pointer = data
expect(pointer.pubkey).toEqual(pk)
expect(pointer.relays).toContain(relays[0])
expect(pointer.relays).toContain(relays[1])
Expand All @@ -86,7 +83,7 @@ test('encode and decode nevent', () => {
expect(nevent).toMatch(/nevent1\w+/)
let { type, data } = decode(nevent)
expect(type).toEqual('nevent')
const pointer = data as EventPointer
const pointer = data
expect(pointer.id).toEqual(pk)
expect(pointer.relays).toContain(relays[0])
expect(pointer.kind).toEqual(30023)
Expand All @@ -103,7 +100,7 @@ test('encode and decode nevent with kind 0', () => {
expect(nevent).toMatch(/nevent1\w+/)
let { type, data } = decode(nevent)
expect(type).toEqual('nevent')
const pointer = data as EventPointer
const pointer = data
expect(pointer.id).toEqual(pk)
expect(pointer.relays).toContain(relays[0])
expect(pointer.kind).toEqual(0)
Expand All @@ -121,7 +118,7 @@ test('encode and decode naddr with empty "d"', () => {
expect(naddr).toMatch(/naddr\w+/)
let { type, data } = decode(naddr)
expect(type).toEqual('naddr')
const pointer = data as AddressPointer
const pointer = data
expect(pointer.identifier).toEqual('')
expect(pointer.relays).toContain(relays[0])
expect(pointer.kind).toEqual(3)
Expand All @@ -133,7 +130,7 @@ test('decode naddr from habla.news', () => {
'naddr1qq98yetxv4ex2mnrv4esygrl54h466tz4v0re4pyuavvxqptsejl0vxcmnhfl60z3rth2xkpjspsgqqqw4rsf34vl5',
)
expect(type).toEqual('naddr')
const pointer = data as AddressPointer
const pointer = data
expect(pointer.pubkey).toEqual('7fa56f5d6962ab1e3cd424e758c3002b8665f7b0d8dcee9fe9e288d7751ac194')
expect(pointer.kind).toEqual(30023)
expect(pointer.identifier).toEqual('references')
Expand All @@ -145,7 +142,7 @@ test('decode naddr from go-nostr with different TLV ordering', () => {
)

expect(type).toEqual('naddr')
const pointer = data as AddressPointer
const pointer = data
expect(pointer.pubkey).toEqual('3bf0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d')
expect(pointer.relays).toContain('wss://relay.nostr.example.mydomain.example.com')
expect(pointer.relays).toContain('wss://nostr.banana.com')
Expand Down
65 changes: 40 additions & 25 deletions nip19.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,31 +70,46 @@ export function decodeNostrURI(nip19code: string): ReturnType<typeof decode> | {
}
}

export function decode(code: string):
| {
type: 'nevent'
data: EventPointer
}
| {
type: 'nprofile'
data: ProfilePointer
}
| {
type: 'naddr'
data: AddressPointer
}
| {
type: 'npub'
data: string
}
| {
type: 'nsec'
data: Uint8Array
}
| {
type: 'note'
data: string
} {
export type DecodedNevent = {
type: 'nevent'
data: EventPointer
}

export type DecodedNprofile = {
type: 'nprofile'
data: ProfilePointer
}

export type DecodedNaddr = {
type: 'naddr'
data: AddressPointer
}

export type DecodedNsec = {
type: 'nsec'
data: Uint8Array
}

export type DecodedNpub = {
type: 'npub'
data: string
}

export type DecodedNote = {
type: 'note'
data: string
}

export type DecodedResult = DecodedNevent | DecodedNprofile | DecodedNaddr | DecodedNpub | DecodedNsec | DecodedNote

export function decode(nip19: NEvent): DecodedNevent
export function decode(nip19: NProfile): DecodedNprofile
export function decode(nip19: NAddr): DecodedNaddr
export function decode(nip19: NSec): DecodedNsec
export function decode(nip19: NPub): DecodedNpub
export function decode(nip19: Note): DecodedNote
export function decode(code: string): DecodedResult
export function decode(code: string): DecodedResult {
let { prefix, words } = bech32.decode(code, Bech32MaxSize)
let data = new Uint8Array(bech32.fromWords(words))

Expand Down
6 changes: 3 additions & 3 deletions nip29.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AbstractSimplePool } from './abstract-pool.ts'
import { Subscription } from './abstract-relay.ts'
import type { Event, EventTemplate } from './core.ts'
import { fetchRelayInformation, RelayInformation } from './nip11.ts'
import { AddressPointer, decode } from './nip19.ts'
import { AddressPointer, decode, NostrTypeGuard } from './nip19.ts'
import { normalizeURL } from './utils.ts'

/**
Expand Down Expand Up @@ -518,11 +518,11 @@ export async function loadGroupFromCode(pool: AbstractSimplePool, code: string):
* @returns A GroupReference object if the code is valid, otherwise null.
*/
export function parseGroupCode(code: string): null | GroupReference {
if (code.startsWith('naddr1')) {
if (NostrTypeGuard.isNAddr(code)) {
try {
let { data } = decode(code)

let { relays, identifier } = data as AddressPointer
let { relays, identifier } = data
if (!relays || relays.length === 0) return null

let host = relays![0]
Expand Down
Loading