Skip to content

Commit

Permalink
some beginnings of nip29 helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiatjaf committed Jan 19, 2024
1 parent 3b81e5e commit 9b08550
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
60 changes: 60 additions & 0 deletions nip29.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { Event } from './pure'

export type Group = {
id: string
name?: string
picture?: string
about?: string
relay?: string
public?: boolean
open?: boolean
}

export function parseGroup(event: Event): Group {
const chan: Partial<Group> = {}
for (let i = 0; i < event.tags.length; i++) {
const tag = event.tags[i]
switch (tag[0]) {
case 'd':
chan.id = tag[1] || ''
break
case 'name':
chan.name = tag[1] || ''
break
case 'about':
chan.about = tag[1] || ''
break
case 'picture':
chan.picture = tag[1] || ''
break
case 'open':
chan.open = true
break
case 'public':
chan.public = true
break
}
}
return chan as Group
}

export type Member = {
pubkey: string
label?: string
permissions: string[]
}

export function parseMembers(event: Event): Member[] {
const members = []
for (let i = 0; i < event.tags.length; i++) {
const tag = event.tags[i]
if (tag.length < 2) continue
if (tag[0] !== 'p') continue
if (!tag[1].match(/^[0-9a-f]{64}$/)) continue
const member: Member = { pubkey: tag[1], permissions: [] }
if (tag.length > 2) member.label = tag[2]
if (tag.length > 3) member.permissions = tag.slice(3)
members.push(member)
}
return members
}
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"type": "module",
"name": "nostr-tools",
"version": "2.1.3",
"version": "2.1.4",
"description": "Tools for making a Nostr client.",
"repository": {
"type": "git",
Expand Down Expand Up @@ -130,6 +130,11 @@
"require": "./lib/cjs/nip28.js",
"types": "./lib/types/nip28.d.ts"
},
"./nip29": {
"import": "./lib/esm/nip29.js",
"require": "./lib/cjs/nip29.js",
"types": "./lib/types/nip29.d.ts"
},
"./nip30": {
"import": "./lib/esm/nip30.js",
"require": "./lib/cjs/nip30.js",
Expand Down

0 comments on commit 9b08550

Please sign in to comment.