Add domain package
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
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
|
||||
@@ -0,0 +1,50 @@
|
||||
import {fromPairs, parseJson} from "@welshman/lib"
|
||||
import {getAddress, Tags} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Handler<E extends TrustedEvent> = {
|
||||
kind: number
|
||||
name: string
|
||||
about: string
|
||||
image: string
|
||||
identifier: string
|
||||
event: E
|
||||
website?: string
|
||||
lud16?: string
|
||||
nip05?: string
|
||||
}
|
||||
|
||||
export const readHandlers = <E extends TrustedEvent>(event: E) => {
|
||||
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<E>[]
|
||||
}
|
||||
|
||||
export const getHandlerKey = <E extends TrustedEvent>(handler: Handler<E>) => `${handler.kind}:${getAddress(handler.event)}`
|
||||
|
||||
export const displayHandler = <E extends TrustedEvent>(handler?: Handler<E>, fallback = "") => handler?.name || fallback
|
||||
|
||||
export const getHandlerAddress = <E extends TrustedEvent>(event: E) => {
|
||||
const tags = Tags.fromEvent(event).whereKey("a")
|
||||
const tag = tags.filter(t => t.last() === "web").first() || tags.first()
|
||||
|
||||
return tag?.value()
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from "./handle"
|
||||
export * from "./handler"
|
||||
export * from "./list"
|
||||
export * from "./profile"
|
||||
export * from "./relay"
|
||||
export * from "./util"
|
||||
@@ -0,0 +1,45 @@
|
||||
import {parseJson} from "@welshman/lib"
|
||||
import {Address, isShareableRelayUrl, TrustedEvent} from "@welshman/util"
|
||||
import {Encryptable, DecryptedEvent} from "./util"
|
||||
|
||||
export type ListParams = {
|
||||
kind: number
|
||||
}
|
||||
|
||||
export type List<E extends TrustedEvent> = ListParams & {
|
||||
publicTags: string[][]
|
||||
privateTags: string[][]
|
||||
event?: DecryptedEvent<E>
|
||||
}
|
||||
|
||||
export type PublishedList<E extends TrustedEvent> = Omit<List<E>, "event"> & {
|
||||
event: DecryptedEvent<E>
|
||||
}
|
||||
|
||||
export const makeList = <E extends TrustedEvent>(list: ListParams & Partial<List<E>>): List<E> =>
|
||||
({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 = <E extends TrustedEvent>(event: DecryptedEvent<E>): PublishedList<E> => {
|
||||
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 = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: List<E>) =>
|
||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||
|
||||
export const editList = <E extends TrustedEvent>({kind, publicTags = [], privateTags = []}: PublishedList<E>) =>
|
||||
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
|
||||
@@ -0,0 +1,71 @@
|
||||
import {nip19} from "nostr-tools"
|
||||
import {ellipsize, parseJson} from "@welshman/lib"
|
||||
import {PROFILE, TrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Profile<E extends TrustedEvent> = {
|
||||
name?: string
|
||||
nip05?: string
|
||||
lud06?: string
|
||||
lud16?: string
|
||||
about?: string
|
||||
banner?: string
|
||||
picture?: string
|
||||
website?: string
|
||||
display_name?: string
|
||||
event?: E
|
||||
}
|
||||
|
||||
export type PublishedProfile<E extends TrustedEvent> = Omit<Profile<E>, "event"> & {
|
||||
event: E
|
||||
}
|
||||
|
||||
export const isPublishedProfile = <E extends TrustedEvent>(profile: Profile<E>): profile is PublishedProfile<E> =>
|
||||
Boolean(profile.event)
|
||||
|
||||
export const makeProfile = <E extends TrustedEvent>(profile: Partial<Profile<E>> = {}): Profile<E> => ({
|
||||
name: "",
|
||||
nip05: "",
|
||||
lud06: "",
|
||||
lud16: "",
|
||||
about: "",
|
||||
banner: "",
|
||||
picture: "",
|
||||
website: "",
|
||||
display_name: "",
|
||||
...profile,
|
||||
})
|
||||
|
||||
export const readProfile = <E extends TrustedEvent>(event: E) => {
|
||||
const profile = parseJson(event.content) || {}
|
||||
|
||||
return {...profile, event} as PublishedProfile<E>
|
||||
}
|
||||
|
||||
export const createProfile = <E extends TrustedEvent>({event, ...profile}: Profile<E>) => ({
|
||||
kind: PROFILE,
|
||||
content: JSON.stringify(profile),
|
||||
})
|
||||
|
||||
export const editProfile = <E extends TrustedEvent>({event, ...profile}: PublishedProfile<E>) => ({
|
||||
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 = <E extends TrustedEvent>(profile?: Profile<E>, 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 = <E extends TrustedEvent>(profile?: Profile<E>) => Boolean(profile?.name || profile?.display_name)
|
||||
@@ -0,0 +1,63 @@
|
||||
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,
|
||||
})
|
||||
@@ -0,0 +1,48 @@
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
export type Encrypt = (x: string) => Promise<string>
|
||||
|
||||
export type EventContent = {
|
||||
content?: string
|
||||
tags?: string[][]
|
||||
}
|
||||
|
||||
export type DecryptedEvent<E extends TrustedEvent> = E & {
|
||||
plaintext: EventContent
|
||||
}
|
||||
|
||||
export const asDecryptedEvent = <E extends TrustedEvent>(event: E, plaintext: EventContent) =>
|
||||
({...event, plaintext}) as DecryptedEvent<E>
|
||||
|
||||
export class Encryptable<E extends TrustedEvent> {
|
||||
constructor(readonly event: Partial<E>, readonly updates: EventContent) {}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user