Get rid of domain module, allow app to override default event type
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
build
|
||||
normalize-url
|
||||
@@ -1,3 +0,0 @@
|
||||
# @welshman/domain [](https://npmjs.com/package/@welshman/domain)
|
||||
|
||||
A collection of utilities for mapping nostr events to useful data structures.
|
||||
@@ -1,38 +0,0 @@
|
||||
{
|
||||
"name": "@welshman/domain",
|
||||
"version": "0.0.1",
|
||||
"author": "hodlbod",
|
||||
"license": "MIT",
|
||||
"description": "A collection of utilities for mapping nostr events to useful data structures.",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"type": "module",
|
||||
"files": [
|
||||
"build"
|
||||
],
|
||||
"types": "./build/src/index.d.ts",
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./build/src/index.d.ts",
|
||||
"import": "./build/src/index.mjs",
|
||||
"require": "./build/src/index.cjs"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"pub": "npm run lint && npm run build && npm publish",
|
||||
"build": "gts clean && tsc-multi",
|
||||
"lint": "gts lint",
|
||||
"fix": "gts fix"
|
||||
},
|
||||
"devDependencies": {
|
||||
"gts": "^5.0.1",
|
||||
"tsc-multi": "^1.1.0",
|
||||
"typescript": "~5.1.6"
|
||||
},
|
||||
"dependencies": {
|
||||
"nostr-tools": "^2.7.2",
|
||||
"@welshman/lib": "0.0.14",
|
||||
"@welshman/util": "0.0.25"
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
import {last} from '@welshman/lib'
|
||||
|
||||
export type Handle = {
|
||||
pubkey: string
|
||||
nip05: string
|
||||
nip46: string[]
|
||||
relays: string[]
|
||||
}
|
||||
|
||||
export const displayHandle = (handle: Handle) =>
|
||||
handle.nip05.startsWith("_@") ? last(handle.nip05.split("@")) : handle.nip05
|
||||
@@ -1,50 +0,0 @@
|
||||
import {fromPairs, parseJson} from "@welshman/lib"
|
||||
import {getAddress, Tags} from "@welshman/util"
|
||||
import type {ExtensibleTrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Handler = {
|
||||
kind: number
|
||||
name: string
|
||||
about: string
|
||||
image: string
|
||||
identifier: string
|
||||
event: ExtensibleTrustedEvent
|
||||
website?: string
|
||||
lud16?: string
|
||||
nip05?: string
|
||||
}
|
||||
|
||||
export const readHandlers = (event: ExtensibleTrustedEvent) => {
|
||||
const {d: identifier} = fromPairs(event.tags)
|
||||
const meta = parseJson(event.content)
|
||||
const normalizedMeta = {
|
||||
name: meta?.name || meta?.display_name || "",
|
||||
image: meta?.image || meta?.picture || "",
|
||||
about: meta?.about || "",
|
||||
website: meta?.website || "",
|
||||
lud16: meta?.lud16 || "",
|
||||
nip05: meta?.nip05 || "",
|
||||
}
|
||||
|
||||
// If our meta is missing important stuff, don't bother showing it
|
||||
if (!normalizedMeta.name || !normalizedMeta.image) {
|
||||
return []
|
||||
}
|
||||
|
||||
return Tags.fromEvent(event)
|
||||
.whereKey("k")
|
||||
.values()
|
||||
.valueOf()
|
||||
.map(kind => ({...normalizedMeta, kind: parseInt(kind), identifier, event})) as Handler[]
|
||||
}
|
||||
|
||||
export const getHandlerKey = (handler: Handler) => `${handler.kind}:${getAddress(handler.event)}`
|
||||
|
||||
export const displayHandler = (handler?: Handler, fallback = "") => handler?.name || fallback
|
||||
|
||||
export const getHandlerAddress = (event: ExtensibleTrustedEvent) => {
|
||||
const tags = Tags.fromEvent(event).whereKey("a")
|
||||
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
|
||||
|
||||
return tag?.value()
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
export * from "./handle"
|
||||
export * from "./handler"
|
||||
export * from "./list"
|
||||
export * from "./profile"
|
||||
export * from "./relay"
|
||||
export * from "./util"
|
||||
@@ -1,45 +0,0 @@
|
||||
import {parseJson} from "@welshman/lib"
|
||||
import {Address, isShareableRelayUrl} from "@welshman/util"
|
||||
import {Encryptable, DecryptedEvent} from "./util"
|
||||
|
||||
export type ListParams = {
|
||||
kind: number
|
||||
}
|
||||
|
||||
export type List = ListParams & {
|
||||
publicTags: string[][]
|
||||
privateTags: string[][]
|
||||
event?: DecryptedEvent
|
||||
}
|
||||
|
||||
export type PublishedList = Omit<List, "event"> & {
|
||||
event: DecryptedEvent
|
||||
}
|
||||
|
||||
export const makeList = (list: ListParams & Partial<List>): List =>
|
||||
({publicTags: [], privateTags: [], ...list})
|
||||
|
||||
const isValidTag = (tag: string[]) => {
|
||||
if (tag[0] === "p") return tag[1]?.length === 64
|
||||
if (tag[0] === "e") return tag[1]?.length === 64
|
||||
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
|
||||
if (tag[0] === "t") return tag[1]?.length > 0
|
||||
if (tag[0] === "r") return isShareableRelayUrl(tag[1])
|
||||
if (tag[0] === "relay") return isShareableRelayUrl(tag[1])
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export const readList = (event: DecryptedEvent): PublishedList => {
|
||||
const getTags = (tags: string[][]) => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
|
||||
const privateTags = getTags(parseJson(event.plaintext?.content))
|
||||
const publicTags = getTags(event.tags)
|
||||
|
||||
return {event, kind: event.kind, publicTags, privateTags}
|
||||
}
|
||||
|
||||
export const createList = ({kind, publicTags = [], privateTags = []}: List) =>
|
||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||
|
||||
export const editList = ({kind, publicTags = [], privateTags = []}: PublishedList) =>
|
||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||
@@ -1,71 +0,0 @@
|
||||
import {nip19} from "nostr-tools"
|
||||
import {ellipsize, parseJson} from "@welshman/lib"
|
||||
import {PROFILE, ExtensibleTrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Profile = {
|
||||
name?: string
|
||||
nip05?: string
|
||||
lud06?: string
|
||||
lud16?: string
|
||||
about?: string
|
||||
banner?: string
|
||||
picture?: string
|
||||
website?: string
|
||||
display_name?: string
|
||||
event?: ExtensibleTrustedEvent
|
||||
}
|
||||
|
||||
export type PublishedProfile = Omit<Profile, "event"> & {
|
||||
event: ExtensibleTrustedEvent
|
||||
}
|
||||
|
||||
export const isPublishedProfile = (profile: Profile): profile is PublishedProfile =>
|
||||
Boolean(profile.event)
|
||||
|
||||
export const makeProfile = (profile: Partial<Profile> = {}): Profile => ({
|
||||
name: "",
|
||||
nip05: "",
|
||||
lud06: "",
|
||||
lud16: "",
|
||||
about: "",
|
||||
banner: "",
|
||||
picture: "",
|
||||
website: "",
|
||||
display_name: "",
|
||||
...profile,
|
||||
})
|
||||
|
||||
export const readProfile = (event: ExtensibleTrustedEvent) => {
|
||||
const profile = parseJson(event.content) || {}
|
||||
|
||||
return {...profile, event} as PublishedProfile
|
||||
}
|
||||
|
||||
export const createProfile = ({event, ...profile}: Profile) => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
})
|
||||
|
||||
export const editProfile = ({event, ...profile}: PublishedProfile) => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
tags: event.tags,
|
||||
})
|
||||
|
||||
export const displayPubkey = (pubkey: string) => {
|
||||
const d = nip19.npubEncode(pubkey)
|
||||
|
||||
return d.slice(0, 8) + "…" + d.slice(-5)
|
||||
}
|
||||
|
||||
export const displayProfile = (profile?: Profile, fallback = "") => {
|
||||
const {display_name, name, event} = profile || {}
|
||||
|
||||
if (name) return ellipsize(name, 60)
|
||||
if (display_name) return ellipsize(display_name, 60)
|
||||
if (event) return displayPubkey(event.pubkey)
|
||||
|
||||
return fallback
|
||||
}
|
||||
|
||||
export const profileHasName = (profile?: Profile) => Boolean(profile?.name || profile?.display_name)
|
||||
@@ -1,63 +0,0 @@
|
||||
import {last} from "@welshman/lib"
|
||||
import {LOCAL_RELAY_URL, normalizeRelayUrl as _normalizeRelayUrl} from "@welshman/util"
|
||||
|
||||
// Utils related to bare urls
|
||||
|
||||
export function normalizeRelayUrl(url: string, opts = {}) {
|
||||
if (url === LOCAL_RELAY_URL) {
|
||||
return url
|
||||
}
|
||||
|
||||
try {
|
||||
return _normalizeRelayUrl(url, opts)
|
||||
} catch (e) {
|
||||
return url
|
||||
}
|
||||
}
|
||||
|
||||
export const displayRelayUrl = (url: string) => last(url.split("://")).replace(/\/$/, "")
|
||||
|
||||
// Relay profiles
|
||||
|
||||
export type RelayProfile = {
|
||||
url: string
|
||||
name?: string
|
||||
contact?: string
|
||||
description?: string
|
||||
supported_nips?: number[]
|
||||
limitation?: {
|
||||
payment_required?: boolean
|
||||
auth_required?: boolean
|
||||
}
|
||||
}
|
||||
|
||||
export const makeRelayProfile = (relayProfile: RelayProfile) => relayProfile
|
||||
|
||||
export const filterRelaysByNip = (nip: number, relays: RelayProfile[]) =>
|
||||
relays.filter(r => r.supported_nips?.includes(nip))
|
||||
|
||||
// Relay policies
|
||||
|
||||
export enum RelayMode {
|
||||
Read = "read",
|
||||
Write = "write",
|
||||
Inbox = "inbox",
|
||||
}
|
||||
|
||||
export type RelayPolicy = {
|
||||
url: string
|
||||
read: boolean
|
||||
write: boolean
|
||||
inbox: boolean
|
||||
}
|
||||
|
||||
export const makeRelayPolicy = ({
|
||||
url,
|
||||
...relayPolicy
|
||||
}: Partial<RelayPolicy> & {url: string}): RelayPolicy => ({
|
||||
url: normalizeRelayUrl(url),
|
||||
read: false,
|
||||
write: false,
|
||||
inbox: false,
|
||||
...relayPolicy,
|
||||
})
|
||||
@@ -1,43 +0,0 @@
|
||||
import type {EventContent, ExtensibleTrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Encrypt = (x: string) => Promise<string>
|
||||
|
||||
export type DecryptedEvent = ExtensibleTrustedEvent & {
|
||||
plaintext: Partial<EventContent>
|
||||
}
|
||||
|
||||
export const asDecryptedEvent = (event: ExtensibleTrustedEvent, plaintext: Partial<EventContent>) =>
|
||||
({...event, plaintext}) as DecryptedEvent
|
||||
|
||||
export class Encryptable<E extends Partial<EventContent>> {
|
||||
constructor(readonly event: E, readonly updates: E) {}
|
||||
|
||||
async reconcile(encrypt: Encrypt) {
|
||||
const encryptContent = () => {
|
||||
if (!this.updates.content) return null
|
||||
|
||||
return encrypt(this.updates.content)
|
||||
}
|
||||
|
||||
const encryptTags = () => {
|
||||
if (!this.updates.tags) return null
|
||||
|
||||
return Promise.all(
|
||||
this.updates.tags.map(async tag => {
|
||||
tag[1] = await encrypt(tag[1])
|
||||
|
||||
return tag
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
const [content, tags] = await Promise.all([encryptContent(), encryptTags()])
|
||||
|
||||
// Updates are optional. If not provided, fall back to the event's content and tags.
|
||||
return {
|
||||
...this.event,
|
||||
tags: tags || this.event.tags || [],
|
||||
content: content || this.event.content || "",
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"targets": [
|
||||
{"extname": ".cjs", "module": "commonjs"},
|
||||
{"extname": ".mjs", "module": "esnext", "moduleResolution": "node"}
|
||||
],
|
||||
"projects": ["tsconfig.json"]
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
{
|
||||
"extends": "../../node_modules/gts/tsconfig-google.json",
|
||||
"compilerOptions": {
|
||||
"rootDir": ".",
|
||||
"outDir": "build",
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"lib": ["esnext", "dom", "dom.iterable"]
|
||||
},
|
||||
"include": ["**/*.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user