some beginnings of nip29 helpers.

This commit is contained in:
fiatjaf
2024-01-19 16:13:00 -03:00
parent 3b81e5e762
commit 9b08550885
2 changed files with 66 additions and 1 deletions

60
nip29.ts Normal file
View File

@@ -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
}

View File

@@ -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",
@@ -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",